comparison src/gtkstatusbox.c @ 10643:868aafbe859b

[gaim-migrate @ 12153] GtkGaimStatusBox, a new status selection widget. It currently sucks. Pretty much just the UI code with enough copy/paste code hacked in from gtkstatusselector to minimally work. Minimally work provided you're only using protocol which have already been updated to handle offline/online as statuses. Which I think is limited to AIM and Yahoo! right now. Anyway, it's cool. committer: Tailor Script <tailor@pidgin.im>
author Sean Egan <seanegan@gmail.com>
date Sun, 27 Feb 2005 08:35:11 +0000
parents
children 3353656c119e
comparison
equal deleted inserted replaced
10642:0376d4c84b9f 10643:868aafbe859b
1 /*
2 * @file gtkstatusbox.c GTK+ Status Selection Widget
3 * @ingroup gtkui
4 *
5 * gaim
6 *
7 * Gaim is the legal property of its developers, whose names are too numerous
8 * to list here. Please refer to the COPYRIGHT file distributed with this
9 * source distribution.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 */
25
26 #include "internal.h"
27 #include "account.h"
28 #include "status.h"
29 #include "gtkgaim.h"
30 #include "gtkstock.h"
31 #include "gtkstatusbox.h"
32
33 static void imhtml_changed_cb(GtkTextBuffer *buffer, void *data);
34
35 static void gtk_gaim_status_box_changed(GtkComboBox *box);
36 static void gtk_gaim_status_box_size_request (GtkWidget *widget, GtkRequisition *requisition);
37 static void gtk_gaim_status_box_size_allocate (GtkWidget *widget, GtkAllocation *allocation);
38 static gboolean gtk_gaim_status_box_expose_event (GtkWidget *widget, GdkEventExpose *event);
39 static void gtk_gaim_status_box_forall (GtkContainer *container, gboolean include_internals, GtkCallback callback, gpointer callback_data);
40
41 static void (*combo_box_size_request)(GtkWidget *widget, GtkRequisition *requisition);
42 static void (*combo_box_size_allocate)(GtkWidget *widget, GtkAllocation *allocation);
43 static gboolean (*combo_box_expose_event)(GtkWidget *widget, GdkEventExpose *event);
44 static void (*combo_box_forall) (GtkContainer *container, gboolean include_internals, GtkCallback callback, gpointer callback_data);
45 enum {
46 ICON_COLUMN,
47 TEXT_COLUMN,
48 TITLE_COLUMN,
49 DESC_COLUMN,
50 TYPE_COLUMN,
51 NUM_COLUMNS
52 };
53
54 static void gtk_gaim_status_box_class_init (GtkGaimStatusBoxClass *klass);
55 static void gtk_gaim_status_box_init (GtkGaimStatusBox *status_box);
56
57 GType
58 gtk_gaim_status_box_get_type (void)
59 {
60 static GType status_box_type = 0;
61
62 if (!status_box_type)
63 {
64 static const GTypeInfo status_box_info =
65 {
66 sizeof (GtkGaimStatusBoxClass),
67 NULL, /* base_init */
68 NULL, /* base_finalize */
69 (GClassInitFunc) gtk_gaim_status_box_class_init,
70 NULL, /* class_finalize */
71 NULL, /* class_data */
72 sizeof (GtkGaimStatusBox),
73 0,
74 (GInstanceInitFunc) gtk_gaim_status_box_init
75 };
76
77 status_box_type = g_type_register_static (GTK_TYPE_COMBO_BOX,
78 "GtkGaimStatusBox",
79 &status_box_info,
80 0);
81 }
82
83 return status_box_type;
84 }
85
86 static void
87 gtk_gaim_status_box_class_init (GtkGaimStatusBoxClass *klass)
88 {
89 GObjectClass *object_class;
90 GtkWidgetClass *widget_class;
91 GtkComboBoxClass *parent_class = (GtkComboBoxClass*)klass;
92 GtkContainerClass *container_class = (GtkContainerClass*)klass;
93
94 parent_class->changed = gtk_gaim_status_box_changed;
95 widget_class = (GtkWidgetClass*)klass;
96 combo_box_size_request = widget_class->size_request;
97 widget_class->size_request = gtk_gaim_status_box_size_request;
98 combo_box_size_allocate = widget_class->size_allocate;
99 widget_class->size_allocate = gtk_gaim_status_box_size_allocate;
100 combo_box_expose_event = widget_class->expose_event;
101 widget_class->expose_event = gtk_gaim_status_box_expose_event;
102
103 combo_box_forall = container_class->forall;
104 container_class->forall = gtk_gaim_status_box_forall;
105
106 object_class = (GObjectClass *)klass;
107 }
108
109 static void
110 gtk_gaim_status_box_refresh(GtkGaimStatusBox *status_box)
111 {
112 char *text;
113 char aa_color[8];
114 GdkPixbuf *pixbuf;
115
116 GtkStyle *style = gtk_widget_get_style(GTK_WIDGET(status_box));
117 snprintf(aa_color, sizeof(aa_color), "#%02x%02x%02x",
118 style->text_aa[GTK_STATE_NORMAL].red >> 8,
119 style->text_aa[GTK_STATE_NORMAL].green >> 8,
120 style->text_aa[GTK_STATE_NORMAL].blue >> 8);
121
122 if (status_box->error) {
123 text = g_strdup_printf("%s\n<span size=\"smaller\" weight=\"bold\" color=\"red\">%s</span>",
124 status_box->title, status_box->error);
125 } else if (status_box->typing) {
126 text = text = g_strdup_printf("%s\n<span size=\"smaller\" color=\"%s\">%s</span>",
127 status_box->title,
128 aa_color,
129 _("Typing"));
130 } else if (status_box->connecting) {
131 text = g_strdup_printf("%s\n<span size=\"smaller\" color=\"%s\">%s</span>",
132 status_box->title,
133 aa_color,
134 _("Connecting"));
135 } else if (status_box->desc) {
136 text = g_strdup_printf("%s\n<span size=\"smaller\" color=\"%s\">%s</span>",
137 status_box->title, aa_color, status_box->desc);
138 } else {
139 text = g_strdup_printf("%s", status_box->title);
140 }
141
142 if (status_box->error)
143 pixbuf = status_box->error_pixbuf;
144 else if (status_box->typing)
145 pixbuf = status_box->typing_pixbufs[status_box->typing_index];
146 else if (status_box->connecting)
147 pixbuf = status_box->connecting_pixbufs[status_box->connecting_index];
148 else
149 pixbuf = status_box->pixbuf;
150
151 gtk_list_store_set(status_box->store, &(status_box->iter),
152 ICON_COLUMN, pixbuf,
153 TEXT_COLUMN, text,
154 TITLE_COLUMN, status_box->title,
155 DESC_COLUMN, status_box->desc,
156 TYPE_COLUMN, NULL, -1);
157 gtk_cell_view_set_displayed_row(GTK_CELL_VIEW(status_box->cell_view), gtk_tree_path_new_from_string("0"));
158
159 g_free(text);
160 }
161
162 static void
163 gtk_gaim_status_box_init (GtkGaimStatusBox *status_box)
164 {
165 GtkCellRenderer *text_rend = gtk_cell_renderer_text_new();
166 GtkCellRenderer *icon_rend = gtk_cell_renderer_pixbuf_new();
167 GtkTextBuffer *buffer;
168 GdkPixbuf *pixbuf, *pixbuf2, *pixbuf3, *pixbuf4;
169 GtkIconSize icon_size = gtk_icon_size_from_name(GAIM_ICON_SIZE_STATUS);
170
171 status_box->imhtml_visible = FALSE;
172 status_box->error_pixbuf = gtk_widget_render_icon (GTK_WIDGET(status_box), GAIM_STOCK_STATUS_OFFLINE,
173 icon_size, "GtkGaimStatusBox");
174 status_box->connecting_index = 0;
175 status_box->connecting_pixbufs[0] = gtk_widget_render_icon (GTK_WIDGET(status_box), GAIM_STOCK_STATUS_CONNECT0,
176 icon_size, "GtkGaimStatusBox");
177 status_box->connecting_pixbufs[1] = gtk_widget_render_icon (GTK_WIDGET(status_box), GAIM_STOCK_STATUS_CONNECT1,
178 icon_size, "GtkGaimStatusBox");
179 status_box->connecting_pixbufs[2] = gtk_widget_render_icon (GTK_WIDGET(status_box), GAIM_STOCK_STATUS_CONNECT2,
180 icon_size, "GtkGaimStatusBox");
181 status_box->connecting_pixbufs[3] = gtk_widget_render_icon (GTK_WIDGET(status_box), GAIM_STOCK_STATUS_CONNECT3,
182 icon_size, "GtkGaimStatusBox");
183
184 status_box->typing_index = 0;
185 status_box->typing_pixbufs[0] = gtk_widget_render_icon (GTK_WIDGET(status_box), GAIM_STOCK_STATUS_TYPING0,
186 icon_size, "GtkGaimStatusBox");
187 status_box->typing_pixbufs[1] = gtk_widget_render_icon (GTK_WIDGET(status_box), GAIM_STOCK_STATUS_TYPING1,
188 icon_size, "GtkGaimStatusBox");
189 status_box->typing_pixbufs[2] = gtk_widget_render_icon (GTK_WIDGET(status_box), GAIM_STOCK_STATUS_TYPING2,
190 icon_size, "GtkGaimStatusBox");
191 status_box->typing_pixbufs[3] = gtk_widget_render_icon (GTK_WIDGET(status_box), GAIM_STOCK_STATUS_TYPING3,
192 icon_size, "GtkGaimStatusBox");
193 status_box->connecting = FALSE;
194 status_box->typing = FALSE;
195 status_box->title = NULL;
196 status_box->pixbuf = NULL;
197 status_box->cell_view = gtk_cell_view_new();
198 gtk_widget_show (status_box->cell_view);
199
200 status_box->store = gtk_list_store_new(NUM_COLUMNS, GDK_TYPE_PIXBUF, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
201 status_box->dropdown_store = gtk_list_store_new(NUM_COLUMNS, GDK_TYPE_PIXBUF, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
202 gtk_combo_box_set_model(GTK_COMBO_BOX(status_box), GTK_TREE_MODEL(status_box->dropdown_store));
203 gtk_cell_view_set_model(GTK_CELL_VIEW(status_box->cell_view), GTK_TREE_MODEL(status_box->store));
204 gtk_combo_box_set_wrap_width(GTK_COMBO_BOX(status_box), 0);
205 gtk_list_store_append(status_box->store, &(status_box->iter));
206 gtk_gaim_status_box_refresh(status_box);
207 gtk_cell_view_set_displayed_row(GTK_CELL_VIEW(status_box->cell_view), gtk_tree_path_new_from_string("0"));
208 gtk_container_add(GTK_CONTAINER(status_box), status_box->cell_view);
209
210 status_box->icon_rend = gtk_cell_renderer_pixbuf_new();
211 status_box->text_rend = gtk_cell_renderer_text_new();
212
213 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(status_box), icon_rend, FALSE);
214 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(status_box), text_rend, TRUE);
215 gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(status_box), icon_rend, "pixbuf", ICON_COLUMN, NULL);
216 gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(status_box), text_rend, "markup", TEXT_COLUMN, NULL);
217
218 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(status_box->cell_view), status_box->icon_rend, FALSE);
219 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(status_box->cell_view), status_box->text_rend, TRUE);
220 gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(status_box->cell_view), status_box->icon_rend, "pixbuf", ICON_COLUMN, NULL);
221 gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(status_box->cell_view), status_box->text_rend, "markup", TEXT_COLUMN, NULL);
222
223 status_box->vbox = gtk_vbox_new(0, FALSE);
224 status_box->imhtml = gtk_imhtml_new(NULL, NULL);
225 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(status_box->imhtml));
226 g_signal_connect(G_OBJECT(buffer), "changed", G_CALLBACK(imhtml_changed_cb), status_box);
227 gtk_imhtml_set_editable(GTK_IMHTML(status_box->imhtml), TRUE);
228 gtk_widget_set_parent(status_box->vbox, GTK_WIDGET(status_box));
229 status_box->sw = gtk_scrolled_window_new(NULL, NULL);
230 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(status_box->sw), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
231 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(status_box->sw), GTK_SHADOW_IN);
232 gtk_container_add(GTK_CONTAINER(status_box->sw), status_box->imhtml);
233 gtk_box_pack_start(GTK_BOX(status_box->vbox), status_box->sw, TRUE, TRUE, 0);
234 pixbuf = gtk_widget_render_icon (GTK_WIDGET(status_box), GAIM_STOCK_STATUS_ONLINE,
235 icon_size, "GtkGaimStatusBox");
236 pixbuf2 = gtk_widget_render_icon (GTK_WIDGET(status_box), GAIM_STOCK_STATUS_AWAY,
237 icon_size, "GtkGaimStatusBox");
238 pixbuf3 = gtk_widget_render_icon (GTK_WIDGET(status_box), GAIM_STOCK_STATUS_OFFLINE,
239 icon_size, "GtkGaimStatusBox");
240 pixbuf4 = gtk_widget_render_icon (GTK_WIDGET(status_box), GAIM_STOCK_STATUS_INVISIBLE,
241 icon_size, "GtkGaimStatusBox");
242 /* hacks */
243 gtk_gaim_status_box_add(GTK_GAIM_STATUS_BOX(status_box), pixbuf, _("Available"), NULL, "available");
244 gtk_gaim_status_box_add(GTK_GAIM_STATUS_BOX(status_box), pixbuf2, _("Away"), NULL, "away");
245 gtk_gaim_status_box_add(GTK_GAIM_STATUS_BOX(status_box), pixbuf4, _("Invisible"), NULL, "invisible");
246 gtk_gaim_status_box_add(GTK_GAIM_STATUS_BOX(status_box), pixbuf3, _("Offline"), NULL, "offline");
247 gtk_combo_box_set_active(GTK_COMBO_BOX(status_box), 0);
248
249 }
250
251
252 static void
253 gtk_gaim_status_box_size_request (GtkWidget *widget,
254 GtkRequisition *requisition)
255 {
256 GtkRequisition box_req;
257 combo_box_size_request(widget, requisition);
258
259 gtk_widget_size_request(GTK_GAIM_STATUS_BOX(widget)->vbox, &box_req);
260 if (box_req.height > 1)
261 requisition->height = requisition->height + box_req.height + 6;
262
263 requisition->width = 1;
264
265 }
266
267 static void
268 gtk_gaim_status_box_size_allocate (GtkWidget *widget,
269 GtkAllocation *allocation)
270 {
271 GtkRequisition req = {0,0};
272 GtkAllocation parent_alc = *allocation, box_alc = *allocation ;
273 combo_box_size_request(widget, &req);
274
275 /* EVIL XXX */
276 box_alc.height = 80;//MAX(1,box_alc.height - req.height - 6);
277
278 box_alc.y = box_alc.y + req.height + 6;
279 gtk_widget_size_allocate((GTK_GAIM_STATUS_BOX(widget))->vbox, &box_alc);
280
281 parent_alc.height = MAX(1,req.height);
282 combo_box_size_allocate(widget, &parent_alc);
283 widget->allocation = *allocation;
284 }
285
286
287 static gboolean
288 gtk_gaim_status_box_expose_event (GtkWidget *widget,
289 GdkEventExpose *event)
290 {
291
292 GtkGaimStatusBox *status_box = GTK_GAIM_STATUS_BOX (widget);
293 combo_box_expose_event(widget, event);
294
295 gtk_container_propagate_expose (GTK_CONTAINER (widget),
296 status_box->vbox, event);
297 return FALSE;
298 }
299
300 static void
301 gtk_gaim_status_box_forall (GtkContainer *container,
302 gboolean include_internals,
303 GtkCallback callback,
304 gpointer callback_data)
305 {
306 GtkGaimStatusBox *status_box = GTK_GAIM_STATUS_BOX (container);
307
308 if (include_internals)
309 {
310 (* callback) (status_box->vbox, callback_data);
311 }
312
313 combo_box_forall(container, include_internals, callback, callback_data);
314 }
315
316 GtkWidget *
317 gtk_gaim_status_box_new()
318 {
319 return g_object_new(GTK_GAIM_TYPE_STATUS_BOX, NULL);
320 }
321
322
323 void
324 gtk_gaim_status_box_add(GtkGaimStatusBox *status_box, GdkPixbuf *pixbuf, const char *text, const char *sec_text, char *edit)
325 {
326 GtkTreeIter iter;
327 char *t;
328
329 if (sec_text) {
330 char aa_color[8];
331 GtkStyle *style = gtk_widget_get_style(GTK_WIDGET(status_box));
332 snprintf(aa_color, sizeof(aa_color), "#%02x%02x%02x",
333 style->text_aa[GTK_STATE_NORMAL].red >> 8,
334 style->text_aa[GTK_STATE_NORMAL].green >> 8,
335 style->text_aa[GTK_STATE_NORMAL].blue >> 8);
336 t = g_strdup_printf("%s\n<span color=\"%s\">%s</span>", text, aa_color, sec_text);
337 } else {
338 t = g_strdup(text);
339 }
340
341 gtk_list_store_append(status_box->dropdown_store, &iter);
342 gtk_list_store_set(status_box->dropdown_store, &iter,
343 ICON_COLUMN, pixbuf,
344 TEXT_COLUMN, t,
345 TITLE_COLUMN, text,
346 DESC_COLUMN, sec_text,
347 TYPE_COLUMN, edit, -1);
348 }
349
350 void
351 gtk_gaim_status_box_set_error(GtkGaimStatusBox *status_box, const gchar *error)
352 {
353 status_box->error = g_strdup(error);
354 gtk_gaim_status_box_refresh(status_box);
355 }
356
357 void
358 gtk_gaim_status_box_set_connecting(GtkGaimStatusBox *status_box, gboolean connecting)
359 {
360 if (!status_box)
361 return;
362 status_box->connecting = connecting;
363 gtk_gaim_status_box_refresh(status_box);
364 }
365
366 void
367 gtk_gaim_status_box_pulse_connecting(GtkGaimStatusBox *status_box)
368 {
369 if (!status_box)
370 return;
371 if (status_box->connecting_index == 3)
372 status_box->connecting_index = 0;
373 else
374 status_box->connecting_index++;
375 gtk_gaim_status_box_refresh(status_box);
376 }
377
378 void
379 gtk_gaim_status_box_pulse_typing(GtkGaimStatusBox *status_box)
380 {
381 if (status_box->typing_index == 3)
382 status_box->typing_index = 0;
383 else
384 status_box->typing_index++;
385 gtk_gaim_status_box_refresh(status_box);
386 }
387
388 static void remove_typing_cb(GtkGaimStatusBox *box)
389 {
390 gchar *status_type_id;
391 GList *l;
392 GtkTreeIter iter;
393
394 gtk_combo_box_get_active_iter(GTK_COMBO_BOX(box), &iter);
395 gtk_tree_model_get(GTK_TREE_MODEL(box->dropdown_store), &iter, TYPE_COLUMN, &status_type_id, -1);
396 for (l = gaim_accounts_get_all(); l != NULL; l = l->next) {
397 GaimAccount *account = (GaimAccount*)l->data;
398 GaimStatusType *status_type;
399
400 if (!gaim_account_get_enabled(account, GAIM_GTK_UI))
401 continue;
402
403 status_type = gaim_account_get_status_type(account, status_type_id);
404
405 if (status_type == NULL)
406 continue;
407 gaim_account_set_status(account, status_type_id, TRUE,
408 "message",gtk_imhtml_get_markup(GTK_IMHTML(box->imhtml)), NULL);
409 }
410 g_source_remove(box->typing);
411 box->typing = 0;
412 gtk_gaim_status_box_refresh(box);
413 }
414
415 static void gtk_gaim_status_box_changed(GtkComboBox *box)
416 {
417 GtkGaimStatusBox *status_box = GTK_GAIM_STATUS_BOX(box);
418 GtkTreeIter iter;
419 char *text, *sec_text;
420 GdkPixbuf *pixbuf;
421 gchar *status_type_id;
422 GList *l;
423
424 gtk_combo_box_get_active_iter(GTK_COMBO_BOX(status_box), &iter);
425 gtk_tree_model_get(GTK_TREE_MODEL(status_box->dropdown_store), &iter, TITLE_COLUMN, &text,
426 DESC_COLUMN, &sec_text, ICON_COLUMN, &pixbuf,
427 TYPE_COLUMN, &status_type_id, -1);
428 if (status_box->title)
429 g_free(status_box->title);
430 status_box->title = g_strdup(text);
431 if (status_box->desc && sec_text)
432 g_free(status_box->desc);
433 status_box->desc = g_strdup(sec_text);
434 if (status_box->pixbuf)
435 g_object_unref(status_box->pixbuf);
436 status_box->pixbuf = pixbuf;
437
438 if (!strcmp(status_type_id, "away")) {
439 gtk_widget_show_all(status_box->vbox);
440 status_box->typing = g_timeout_add(3000, (GSourceFunc)remove_typing_cb, status_box);
441 gtk_imhtml_clear(GTK_IMHTML(status_box->imhtml));
442 gtk_widget_grab_focus(status_box->imhtml);
443 } else {
444 if (status_box->typing) {
445 g_source_remove(status_box->typing);
446 status_box->typing = 0;
447 }
448 gtk_widget_hide_all(status_box->vbox);
449 for (l = gaim_accounts_get_all(); l != NULL; l = l->next) {
450 GaimAccount *account = (GaimAccount*)l->data;
451 GaimStatusType *status_type;
452
453 if (!gaim_account_get_enabled(account, GAIM_GTK_UI))
454 continue;
455
456 status_type = gaim_account_get_status_type(account, status_type_id);
457
458 if (status_type == NULL)
459 continue;
460 gaim_account_set_status(account, status_type_id, TRUE, NULL);
461 }
462 }
463 gtk_gaim_status_box_refresh(status_box);
464 }
465
466 static void imhtml_changed_cb(GtkTextBuffer *buffer, void *data)
467 {
468 GtkGaimStatusBox *box = (GtkGaimStatusBox*)data;
469 if (box->typing) {
470 gtk_gaim_status_box_pulse_typing(box);
471 g_source_remove(box->typing);
472 }
473 box->typing = g_timeout_add(3000, (GSourceFunc)remove_typing_cb, box);
474 gtk_gaim_status_box_refresh(box);
475 }