5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 2001 Pekka Riikonen
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; version 2 of the License.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 Code is mostly ripped from Gabber, thus code also copyrighted by:
19 Copyright (C) 1999-2001 Dave Smith & Julian Missig
23 #include "silcerchatview.hh"
24 #include <libgnome/gnome-url.h>
25 #include <gtk--/text.h>
26 #include <gtk/gtkbox.h>
34 {0, 0, 0, 0}, /* 0 black */
35 {0, 0xcccc, 0xcccc, 0xcccc}, /* 1 white */
36 {0, 0, 0, 0xcccc}, /* 2 blue */
37 {0, 0, 0xcccc, 0}, /* 3 green */
38 {0, 0xcccc, 0, 0}, /* 4 red */
39 {0, 0xbbbb, 0xbbbb, 0}, /* 5 yellow/brown */
40 {0, 0xbbbb, 0, 0xbbbb}, /* 6 purple */
41 {0, 0xffff, 0xaaaa, 0}, /* 7 orange */
42 {0, 0xffff, 0xffff, 0}, /* 8 yellow */
43 {0, 0, 0xffff, 0}, /* 9 green */
44 {0, 0, 0xcccc, 0xcccc}, /* 10 aqua */
45 {0, 0, 0xffff, 0xffff}, /* 11 light aqua */
46 {0, 0, 0, 0xffff}, /* 12 blue */
47 {0, 0xffff, 0, 0xffff}, /* 13 pink */
48 {0, 0x7777, 0x7777, 0x7777}, /* 14 grey */
49 {0, 0x9999, 0x9999, 0x9999}, /* 15 light grey */
50 {0, 0, 0, 0xcccc}, /* 16 blue markBack */
51 {0, 0xeeee, 0xeeee, 0xeeee}, /* 17 white markFore */
52 {0, 0xcccc, 0xcccc, 0xcccc}, /* 18 foreground (white) */
53 {0, 0, 0, 0}, /* 19 background (black) */
56 typedef const int CONSTANT;
57 const int WORD_URL = 1;
58 const int WORD_HOST = 2;
60 void palette_load(GtkWidget* w)
64 if (!colors[0].pixel) /* don't do it again */
65 for (i = 0; i < 20; i++) {
66 colors[i].pixel = (gulong) ((colors[i].red & 0xff00) * 256 +
67 (colors[i].green & 0xff00) +
68 (colors[i].blue & 0xff00) / 256);
69 if (!gdk_colormap_alloc_color (gtk_widget_get_colormap (w),
71 cerr << "Error allocating color " << i << endl;
75 int word_check(GtkXText* t, char* word)
79 int len = strlen (word);
81 if (!strncasecmp (word, "irc://", 6))
84 if (!strncasecmp (word, "irc.", 4))
87 if (!strncasecmp (word, "ftp.", 4))
90 if (!strncasecmp (word, "ftp:", 4))
93 if (!strncasecmp (word, "www.", 4))
96 if (!strncasecmp (word, "http:", 5))
99 if (!strncasecmp (word, "https:", 6))
102 if (!strncasecmp (word, "silc:", 7))
105 if (!strncasecmp (word, "sftp:", 7))
108 if (!strncasecmp (word + len - 4, ".org", 4))
111 if (!strncasecmp (word + len - 4, ".net", 4))
114 if (!strncasecmp (word + len - 4, ".com", 4))
117 if (!strncasecmp (word + len - 4, ".edu", 4))
123 SilcerChatView::SilcerChatView(Gtk::Widget *owner, Gtk::Container *parent,
126 // Create a Gtk::Text so we can grab the colors, then destroy it
127 Gtk::Text textwidget;
128 parent->add(textwidget);
129 textwidget.realize();
131 // Copy the style of the Gtk::Text widget
132 GtkStyle* gs = gtk_widget_get_style(GTK_WIDGET(textwidget.gtkobj()));
133 colors[16] = gs->bg[GTK_STATE_SELECTED];
134 colors[17] = gs->fg[GTK_STATE_SELECTED];
135 colors[18] = gs->fg[GTK_STATE_NORMAL];
136 colors[19] = gs->base[GTK_STATE_NORMAL];
137 parent->remove(textwidget);
138 textwidget.destroy();
140 // Initialize the palette
141 palette_load(owner->gtkobj());
143 // Create the GtkXText object
144 _xtext = GTK_XTEXT(gtk_xtext_new(75, 0));
147 _xtext->max_lines = 1024;
148 _xtext->urlcheck_function = word_check;
149 _xtext->auto_indent = !indent;
150 _xtext->wordwrap = true;
152 // Display the widget
153 gtk_xtext_set_palette(_xtext, colors);
154 gtk_xtext_set_font(_xtext, gs->font, "");
156 // Create a frame around it
157 _frmChat = GTK_FRAME(gtk_frame_new(NULL));
158 gtk_frame_set_shadow_type(GTK_FRAME(_frmChat), GTK_SHADOW_IN);
160 // Add the widget to a container, which happens to be a frame
161 gtk_container_add(parent->gtkobj(), GTK_WIDGET(_frmChat));
162 gtk_container_add(GTK_CONTAINER(_frmChat), GTK_WIDGET(_xtext));
163 gtk_widget_show(GTK_WIDGET(_frmChat));
164 gtk_widget_show(GTK_WIDGET(_xtext));
166 // Create a scrollbar
167 _vsChat = GTK_VSCROLLBAR(gtk_vscrollbar_new(_xtext->adj));
168 gtk_box_pack_start (GTK_BOX (parent->gtkobj()), GTK_WIDGET(_vsChat),
170 GTK_WIDGET_UNSET_FLAGS (_vsChat, GTK_CAN_FOCUS);
171 gtk_widget_show (GTK_WIDGET(_vsChat));
173 // Hookup stub callback
174 gtk_signal_connect(GTK_OBJECT(_xtext), "word_click",
175 GTK_SIGNAL_FUNC(&SilcerChatView::_on_word_clicked_stub),
179 SilcerChatView::~SilcerChatView()
181 gtk_widget_destroy(GTK_WIDGET(_xtext));
182 gtk_widget_destroy(GTK_WIDGET(_frmChat));
183 gtk_widget_destroy(GTK_WIDGET(_vsChat));
186 void SilcerChatView::render(const string &message, const string &username,
187 const string ×tamp, COLOR_T &c)
190 print(timestamp + c + "***\017", message);
192 print(timestamp + c + "<\017" + username + c + ">\017" , message);
195 void SilcerChatView::render_error(const string &message, const string &error,
196 const string ×tamp, COLOR_T &c)
198 // Process error messages
199 print(timestamp + c + error + "\017:", message);
202 void SilcerChatView::clearbuffer()
205 gtk_xtext_remove_lines(_xtext, -1, true);
208 string SilcerChatView::get_chars()
210 return string(gtk_xtext_get_chars(_xtext));
213 void SilcerChatView::on_word_clicked(char *word, GdkEventButton *evt)
215 string s_word = word;
217 if (evt->button == 3) {
218 switch(word_check(_xtext, word))
221 gnome_url_show(word);
224 s_word = "http://" + s_word;
225 gnome_url_show(s_word.c_str());
231 void SilcerChatView::_on_word_clicked_stub(GtkXText *xtext, char *word,
233 SilcerChatView *_this)
235 _this->on_word_clicked(word, evt);
238 inline void SilcerChatView::print(const string &s)
240 char* astr = (char*)s.c_str();
241 char* anewline = strchr(astr, '\n');
245 gtk_xtext_append(_xtext, astr,
246 (unsigned long)anewline - (unsigned long)astr);
250 anewline = strchr(astr, '\n');
252 gtk_xtext_append(_xtext, astr, -1);
257 gtk_xtext_append(_xtext, astr, s.length());
261 inline void SilcerChatView::print(const string& left, const string& right)
263 char* s = (char*)right.c_str();
264 char* newline = strchr(s, '\n');
267 // Display the first line w/ the user who said it
268 gtk_xtext_append_indent(_xtext, (char*)left.c_str(), left.length(),
269 s, (unsigned long)newline - (unsigned long)s);
270 // Now loop through the rest of the string, displaying it
275 newline = strchr(s, '\n');
276 // If another newline is found, append it to the display and loop again
278 gtk_xtext_append_indent(_xtext, "", 0, s,
279 (unsigned long)newline - (unsigned long)s);
281 // Otherwise append the rest of the string and break out of the loop
282 gtk_xtext_append_indent(_xtext, "", 0, s, -1);
287 gtk_xtext_append_indent(_xtext, (char*)left.c_str(), left.length(),
288 (char*)right.c_str(), right.length());