I'm really going to try to release Tuesday night. I'm going to try to
[pidgin-git.git] / pidgin / gtkimhtmltoolbar.c
blobe6d6f7652bc4ba0e666feca23dca5462cbd39e55
1 /*
2 * @file gtkimhtmltoolbar.c GTK+ IMHtml Toolbar
3 * @ingroup pidgin
4 */
6 /* pidgin
8 * Pidgin is the legal property of its developers, whose names are too numerous
9 * to list here. Please refer to the COPYRIGHT file distributed with this
10 * source distribution.
12 * This program is free software; you can redistribute it and/or modify
13 * under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
27 #include "internal.h"
28 #include "pidgin.h"
30 #include "imgstore.h"
31 #include "notify.h"
32 #include "prefs.h"
33 #include "request.h"
34 #include "pidginstock.h"
35 #include "util.h"
36 #include "debug.h"
38 #include "gtkdialogs.h"
39 #include "gtkimhtmltoolbar.h"
40 #include "gtksmiley.h"
41 #include "gtkthemes.h"
42 #include "gtkutils.h"
44 #include <gdk/gdkkeysyms.h>
46 static GtkHBoxClass *parent_class = NULL;
48 static void toggle_button_set_active_block(GtkToggleButton *button,
49 gboolean is_active,
50 GtkIMHtmlToolbar *toolbar);
52 static gboolean
53 gtk_imhtmltoolbar_popup_menu(GtkWidget *widget,
54 GdkEventButton *event, GtkIMHtmlToolbar *toolbar);
56 static void do_bold(GtkWidget *bold, GtkIMHtmlToolbar *toolbar)
58 g_return_if_fail(toolbar != NULL);
59 gtk_imhtml_toggle_bold(GTK_IMHTML(toolbar->imhtml));
60 gtk_widget_grab_focus(toolbar->imhtml);
63 static void
64 do_italic(GtkWidget *italic, GtkIMHtmlToolbar *toolbar)
66 g_return_if_fail(toolbar != NULL);
67 gtk_imhtml_toggle_italic(GTK_IMHTML(toolbar->imhtml));
68 gtk_widget_grab_focus(toolbar->imhtml);
71 static void
72 do_underline(GtkWidget *underline, GtkIMHtmlToolbar *toolbar)
74 g_return_if_fail(toolbar != NULL);
75 gtk_imhtml_toggle_underline(GTK_IMHTML(toolbar->imhtml));
76 gtk_widget_grab_focus(toolbar->imhtml);
79 static void
80 do_strikethrough(GtkWidget *strikethrough, GtkIMHtmlToolbar *toolbar)
82 g_return_if_fail(toolbar != NULL);
83 gtk_imhtml_toggle_strike(GTK_IMHTML(toolbar->imhtml));
84 gtk_widget_grab_focus(toolbar->imhtml);
87 static void
88 do_small(GtkWidget *smalltb, GtkIMHtmlToolbar *toolbar)
90 g_return_if_fail(toolbar != NULL);
91 /* Only shrink the font on activation, not deactivation as well */
92 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(smalltb)))
93 gtk_imhtml_font_shrink(GTK_IMHTML(toolbar->imhtml));
94 gtk_widget_grab_focus(toolbar->imhtml);
97 static void
98 do_big(GtkWidget *large, GtkIMHtmlToolbar *toolbar)
100 g_return_if_fail(toolbar);
101 /* Only grow the font on activation, not deactivation as well */
102 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(large)))
103 gtk_imhtml_font_grow(GTK_IMHTML(toolbar->imhtml));
104 gtk_widget_grab_focus(toolbar->imhtml);
107 static gboolean
108 destroy_toolbar_font(GtkWidget *widget, GdkEvent *event,
109 GtkIMHtmlToolbar *toolbar)
111 if (widget != NULL)
112 gtk_imhtml_toggle_fontface(GTK_IMHTML(toolbar->imhtml), "");
114 if (toolbar->font_dialog != NULL)
116 gtk_widget_destroy(toolbar->font_dialog);
117 toolbar->font_dialog = NULL;
119 return FALSE;
122 static void
123 realize_toolbar_font(GtkWidget *widget, GtkIMHtmlToolbar *toolbar)
125 GtkFontSelection *sel;
127 sel = GTK_FONT_SELECTION(GTK_FONT_SELECTION_DIALOG(toolbar->font_dialog)->fontsel);
128 gtk_widget_hide_all(gtk_widget_get_parent(sel->size_entry));
129 gtk_widget_show_all(sel->family_list);
130 gtk_widget_show(gtk_widget_get_parent(sel->family_list));
131 gtk_widget_show(gtk_widget_get_parent(gtk_widget_get_parent(sel->family_list)));
134 static void
135 cancel_toolbar_font(GtkWidget *widget, GtkIMHtmlToolbar *toolbar)
137 destroy_toolbar_font(widget, NULL, toolbar);
140 static void
141 apply_font(GtkWidget *widget, GtkFontSelectionDialog *fontsel)
143 /* this could be expanded to include font size, weight, etc.
144 but for now only works with font face */
145 gchar *fontname = gtk_font_selection_dialog_get_font_name(fontsel);
146 GtkIMHtmlToolbar *toolbar = g_object_get_data(G_OBJECT(fontsel),
147 "purple_toolbar");
149 if (fontname) {
150 const gchar *family_name = NULL;
151 PangoFontDescription *desc = NULL;
153 desc = pango_font_description_from_string(fontname);
154 family_name = pango_font_description_get_family(desc);
156 if (family_name) {
157 gtk_imhtml_toggle_fontface(GTK_IMHTML(toolbar->imhtml),
158 family_name);
161 pango_font_description_free(desc);
162 g_free(fontname);
165 cancel_toolbar_font(NULL, toolbar);
168 static void
169 toggle_font(GtkWidget *font, GtkIMHtmlToolbar *toolbar)
171 g_return_if_fail(toolbar);
173 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(font))) {
174 char *fontname = gtk_imhtml_get_current_fontface(GTK_IMHTML(toolbar->imhtml));
176 if (!toolbar->font_dialog) {
177 toolbar->font_dialog = gtk_font_selection_dialog_new(_("Select Font"));
179 g_object_set_data(G_OBJECT(toolbar->font_dialog), "purple_toolbar", toolbar);
181 if(fontname) {
182 char *fonttif = g_strdup_printf("%s 12", fontname);
183 g_free(fontname);
184 gtk_font_selection_dialog_set_font_name(GTK_FONT_SELECTION_DIALOG(toolbar->font_dialog),
185 fonttif);
186 g_free(fonttif);
187 } else {
188 gtk_font_selection_dialog_set_font_name(GTK_FONT_SELECTION_DIALOG(toolbar->font_dialog),
189 DEFAULT_FONT_FACE);
192 g_signal_connect(G_OBJECT(toolbar->font_dialog), "delete_event",
193 G_CALLBACK(destroy_toolbar_font), toolbar);
194 g_signal_connect(G_OBJECT(GTK_FONT_SELECTION_DIALOG(toolbar->font_dialog)->ok_button), "clicked",
195 G_CALLBACK(apply_font), toolbar->font_dialog);
196 g_signal_connect(G_OBJECT(GTK_FONT_SELECTION_DIALOG(toolbar->font_dialog)->cancel_button), "clicked",
197 G_CALLBACK(cancel_toolbar_font), toolbar);
198 g_signal_connect_after(G_OBJECT(toolbar->font_dialog), "realize",
199 G_CALLBACK(realize_toolbar_font), toolbar);
201 gtk_window_present(GTK_WINDOW(toolbar->font_dialog));
202 } else {
203 cancel_toolbar_font(font, toolbar);
205 gtk_widget_grab_focus(toolbar->imhtml);
208 static gboolean
209 destroy_toolbar_fgcolor(GtkWidget *widget, GdkEvent *event,
210 GtkIMHtmlToolbar *toolbar)
212 if (widget != NULL)
213 gtk_imhtml_toggle_forecolor(GTK_IMHTML(toolbar->imhtml), "");
215 if (toolbar->fgcolor_dialog != NULL)
217 gtk_widget_destroy(toolbar->fgcolor_dialog);
218 toolbar->fgcolor_dialog = NULL;
220 return FALSE;
223 static void cancel_toolbar_fgcolor(GtkWidget *widget,
224 GtkIMHtmlToolbar *toolbar)
226 destroy_toolbar_fgcolor(widget, NULL, toolbar);
229 static void do_fgcolor(GtkWidget *widget, GtkColorSelection *colorsel)
231 GdkColor text_color;
232 GtkIMHtmlToolbar *toolbar = g_object_get_data(G_OBJECT(colorsel), "purple_toolbar");
233 char *open_tag;
235 open_tag = g_malloc(30);
236 gtk_color_selection_get_current_color(colorsel, &text_color);
237 g_snprintf(open_tag, 23, "#%02X%02X%02X",
238 text_color.red / 256,
239 text_color.green / 256,
240 text_color.blue / 256);
241 gtk_imhtml_toggle_forecolor(GTK_IMHTML(toolbar->imhtml), open_tag);
242 g_free(open_tag);
244 cancel_toolbar_fgcolor(NULL, toolbar);
247 static void
248 toggle_fg_color(GtkWidget *color, GtkIMHtmlToolbar *toolbar)
250 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(color))) {
251 GtkWidget *colorsel;
252 GdkColor fgcolor;
253 char *color = gtk_imhtml_get_current_forecolor(GTK_IMHTML(toolbar->imhtml));
255 if (!toolbar->fgcolor_dialog) {
257 toolbar->fgcolor_dialog = gtk_color_selection_dialog_new(_("Select Text Color"));
258 colorsel = GTK_COLOR_SELECTION_DIALOG(toolbar->fgcolor_dialog)->colorsel;
259 if (color) {
260 gdk_color_parse(color, &fgcolor);
261 gtk_color_selection_set_current_color(GTK_COLOR_SELECTION(colorsel), &fgcolor);
262 g_free(color);
265 g_object_set_data(G_OBJECT(colorsel), "purple_toolbar", toolbar);
267 g_signal_connect(G_OBJECT(toolbar->fgcolor_dialog), "delete_event",
268 G_CALLBACK(destroy_toolbar_fgcolor), toolbar);
269 g_signal_connect(G_OBJECT(GTK_COLOR_SELECTION_DIALOG(toolbar->fgcolor_dialog)->ok_button), "clicked",
270 G_CALLBACK(do_fgcolor), colorsel);
271 g_signal_connect(G_OBJECT (GTK_COLOR_SELECTION_DIALOG(toolbar->fgcolor_dialog)->cancel_button), "clicked",
272 G_CALLBACK(cancel_toolbar_fgcolor), toolbar);
274 gtk_window_present(GTK_WINDOW(toolbar->fgcolor_dialog));
275 } else {
276 cancel_toolbar_fgcolor(color, toolbar);
278 gtk_widget_grab_focus(toolbar->imhtml);
281 static gboolean
282 destroy_toolbar_bgcolor(GtkWidget *widget, GdkEvent *event,
283 GtkIMHtmlToolbar *toolbar)
285 if (widget != NULL) {
286 if (gtk_text_buffer_get_selection_bounds(GTK_IMHTML(toolbar->imhtml)->text_buffer, NULL, NULL))
287 gtk_imhtml_toggle_backcolor(GTK_IMHTML(toolbar->imhtml), "");
288 else
289 gtk_imhtml_toggle_background(GTK_IMHTML(toolbar->imhtml), "");
292 if (toolbar->bgcolor_dialog != NULL)
294 gtk_widget_destroy(toolbar->bgcolor_dialog);
295 toolbar->bgcolor_dialog = NULL;
297 return FALSE;
300 static void
301 cancel_toolbar_bgcolor(GtkWidget *widget, GtkIMHtmlToolbar *toolbar)
303 destroy_toolbar_bgcolor(widget, NULL, toolbar);
306 static void do_bgcolor(GtkWidget *widget, GtkColorSelection *colorsel)
308 GdkColor text_color;
309 GtkIMHtmlToolbar *toolbar = g_object_get_data(G_OBJECT(colorsel), "purple_toolbar");
310 char *open_tag;
312 open_tag = g_malloc(30);
313 gtk_color_selection_get_current_color(colorsel, &text_color);
314 g_snprintf(open_tag, 23, "#%02X%02X%02X",
315 text_color.red / 256,
316 text_color.green / 256,
317 text_color.blue / 256);
318 if (gtk_text_buffer_get_selection_bounds(GTK_IMHTML(toolbar->imhtml)->text_buffer, NULL, NULL))
319 gtk_imhtml_toggle_backcolor(GTK_IMHTML(toolbar->imhtml), open_tag);
320 else
321 gtk_imhtml_toggle_background(GTK_IMHTML(toolbar->imhtml), open_tag);
322 g_free(open_tag);
324 cancel_toolbar_bgcolor(NULL, toolbar);
327 static void
328 toggle_bg_color(GtkWidget *color, GtkIMHtmlToolbar *toolbar)
330 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(color))) {
331 GtkWidget *colorsel;
332 GdkColor bgcolor;
333 char *color = gtk_imhtml_get_current_backcolor(GTK_IMHTML(toolbar->imhtml));
335 if (!toolbar->bgcolor_dialog) {
337 toolbar->bgcolor_dialog = gtk_color_selection_dialog_new(_("Select Background Color"));
338 colorsel = GTK_COLOR_SELECTION_DIALOG(toolbar->bgcolor_dialog)->colorsel;
339 if (color) {
340 gdk_color_parse(color, &bgcolor);
341 gtk_color_selection_set_current_color(GTK_COLOR_SELECTION(colorsel), &bgcolor);
342 g_free(color);
345 g_object_set_data(G_OBJECT(colorsel), "purple_toolbar", toolbar);
347 g_signal_connect(G_OBJECT(toolbar->bgcolor_dialog), "delete_event",
348 G_CALLBACK(destroy_toolbar_bgcolor), toolbar);
349 g_signal_connect(G_OBJECT(GTK_COLOR_SELECTION_DIALOG(toolbar->bgcolor_dialog)->ok_button), "clicked",
350 G_CALLBACK(do_bgcolor), colorsel);
351 g_signal_connect(G_OBJECT(GTK_COLOR_SELECTION_DIALOG(toolbar->bgcolor_dialog)->cancel_button), "clicked",
352 G_CALLBACK(cancel_toolbar_bgcolor), toolbar);
355 gtk_window_present(GTK_WINDOW(toolbar->bgcolor_dialog));
356 } else {
357 cancel_toolbar_bgcolor(color, toolbar);
359 gtk_widget_grab_focus(toolbar->imhtml);
362 static void
363 clear_formatting_cb(GtkWidget *clear, GtkIMHtmlToolbar *toolbar)
365 toggle_button_set_active_block(GTK_TOGGLE_BUTTON(toolbar->clear), FALSE, toolbar);
366 gtk_imhtml_clear_formatting(GTK_IMHTML(toolbar->imhtml));
369 static void
370 cancel_link_cb(GtkIMHtmlToolbar *toolbar, PurpleRequestFields *fields)
372 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(toolbar->link), FALSE);
374 toolbar->link_dialog = NULL;
377 static void
378 close_link_dialog(GtkIMHtmlToolbar *toolbar)
380 if (toolbar->link_dialog != NULL)
382 purple_request_close(PURPLE_REQUEST_FIELDS, toolbar->link_dialog);
383 toolbar->link_dialog = NULL;
387 static void
388 do_insert_link_cb(GtkIMHtmlToolbar *toolbar, PurpleRequestFields *fields)
390 const char *url, *description;
392 url = purple_request_fields_get_string(fields, "url");
393 if (GTK_IMHTML(toolbar->imhtml)->format_functions & GTK_IMHTML_LINKDESC)
394 description = purple_request_fields_get_string(fields, "description");
395 else
396 description = NULL;
398 if (description == NULL)
399 description = url;
401 gtk_imhtml_insert_link(GTK_IMHTML(toolbar->imhtml),
402 gtk_text_buffer_get_insert(GTK_IMHTML(toolbar->imhtml)->text_buffer),
403 url, description);
405 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(toolbar->link), FALSE);
407 toolbar->link_dialog = NULL;
410 static void
411 insert_link_cb(GtkWidget *w, GtkIMHtmlToolbar *toolbar)
413 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(toolbar->link))) {
414 PurpleRequestFields *fields;
415 PurpleRequestFieldGroup *group;
416 PurpleRequestField *field;
417 GtkTextIter start, end;
418 char *msg;
419 char *desc = NULL;
421 fields = purple_request_fields_new();
423 group = purple_request_field_group_new(NULL);
424 purple_request_fields_add_group(fields, group);
426 field = purple_request_field_string_new("url", _("_URL"), NULL, FALSE);
427 purple_request_field_set_required(field, TRUE);
428 purple_request_field_group_add_field(group, field);
430 if(GTK_IMHTML(toolbar->imhtml)->format_functions & GTK_IMHTML_LINKDESC) {
431 if (gtk_text_buffer_get_selection_bounds(GTK_IMHTML(toolbar->imhtml)->text_buffer, &start, &end)) {
432 desc = gtk_imhtml_get_text(GTK_IMHTML(toolbar->imhtml), &start, &end);
434 field = purple_request_field_string_new("description", _("_Description"),
435 desc, FALSE);
436 purple_request_field_group_add_field(group, field);
437 msg = g_strdup(_("Please enter the URL and description of the "
438 "link that you want to insert. The description "
439 "is optional."));
440 } else {
441 msg = g_strdup(_("Please enter the URL of the "
442 "link that you want to insert."));
445 toolbar->link_dialog =
446 purple_request_fields(toolbar, _("Insert Link"),
447 NULL,
448 msg,
449 fields,
450 _("_Insert"), G_CALLBACK(do_insert_link_cb),
451 _("Cancel"), G_CALLBACK(cancel_link_cb),
452 NULL, NULL, NULL,
453 toolbar);
454 g_free(msg);
455 g_free(desc);
456 } else {
457 close_link_dialog(toolbar);
459 gtk_widget_grab_focus(toolbar->imhtml);
462 static void insert_hr_cb(GtkWidget *widget, GtkIMHtmlToolbar *toolbar)
464 GtkTextIter iter;
465 GtkTextMark *ins;
466 GtkIMHtmlScalable *hr;
468 ins = gtk_text_buffer_get_insert(gtk_text_view_get_buffer(GTK_TEXT_VIEW(toolbar->imhtml)));
469 gtk_text_buffer_get_iter_at_mark(gtk_text_view_get_buffer(GTK_TEXT_VIEW(toolbar->imhtml)), &iter, ins);
470 hr = gtk_imhtml_hr_new();
471 gtk_imhtml_hr_add_to(hr, GTK_IMHTML(toolbar->imhtml), &iter);
474 static void
475 do_insert_image_cb(GtkWidget *widget, int response, GtkIMHtmlToolbar *toolbar)
477 gchar *filename, *name, *buf;
478 char *filedata;
479 size_t size;
480 GError *error = NULL;
481 int id;
482 GtkTextIter iter;
483 GtkTextMark *ins;
485 if (response != GTK_RESPONSE_ACCEPT)
487 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(toolbar->image), FALSE);
488 return;
491 filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(widget));
493 if (filename == NULL) {
494 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(toolbar->image), FALSE);
495 return;
498 /* The following triggers a callback that closes the widget */
499 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(toolbar->image), FALSE);
501 if (!g_file_get_contents(filename, &filedata, &size, &error)) {
502 purple_notify_error(NULL, NULL, error->message, NULL);
504 g_error_free(error);
505 g_free(filename);
507 return;
510 name = strrchr(filename, G_DIR_SEPARATOR) + 1;
512 id = purple_imgstore_add_with_id(filedata, size, name);
514 if (id == 0) {
515 buf = g_strdup_printf(_("Failed to store image: %s\n"), filename);
516 purple_notify_error(NULL, NULL, buf, NULL);
518 g_free(buf);
519 g_free(filename);
521 return;
524 g_free(filename);
526 ins = gtk_text_buffer_get_insert(gtk_text_view_get_buffer(GTK_TEXT_VIEW(toolbar->imhtml)));
527 gtk_text_buffer_get_iter_at_mark(gtk_text_view_get_buffer(GTK_TEXT_VIEW(toolbar->imhtml)),
528 &iter, ins);
529 gtk_imhtml_insert_image_at_iter(GTK_IMHTML(toolbar->imhtml), id, &iter);
530 purple_imgstore_unref_by_id(id);
534 static void
535 insert_image_cb(GtkWidget *save, GtkIMHtmlToolbar *toolbar)
537 GtkWidget *window;
539 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(toolbar->image))) {
540 window = gtk_file_chooser_dialog_new(_("Insert Image"),
541 NULL,
542 GTK_FILE_CHOOSER_ACTION_OPEN,
543 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
544 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
545 NULL);
546 gtk_dialog_set_default_response(GTK_DIALOG(window), GTK_RESPONSE_ACCEPT);
547 g_signal_connect(G_OBJECT(GTK_FILE_CHOOSER(window)),
548 "response", G_CALLBACK(do_insert_image_cb), toolbar);
550 gtk_widget_show(window);
551 toolbar->image_dialog = window;
552 } else {
553 gtk_widget_destroy(toolbar->image_dialog);
554 toolbar->image_dialog = NULL;
557 gtk_widget_grab_focus(toolbar->imhtml);
561 static void
562 destroy_smiley_dialog(GtkIMHtmlToolbar *toolbar)
564 if (toolbar->smiley_dialog != NULL)
566 gtk_widget_destroy(toolbar->smiley_dialog);
567 toolbar->smiley_dialog = NULL;
571 static gboolean
572 close_smiley_dialog(GtkIMHtmlToolbar *toolbar)
574 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(toolbar->smiley), FALSE);
575 return FALSE;
579 static void
580 insert_smiley_text(GtkWidget *widget, GtkIMHtmlToolbar *toolbar)
582 char *smiley_text, *escaped_smiley;
584 smiley_text = g_object_get_data(G_OBJECT(widget), "smiley_text");
585 escaped_smiley = g_markup_escape_text(smiley_text, -1);
587 gtk_imhtml_insert_smiley(GTK_IMHTML(toolbar->imhtml),
588 GTK_IMHTML(toolbar->imhtml)->protocol_name,
589 escaped_smiley);
591 g_free(escaped_smiley);
593 close_smiley_dialog(toolbar);
596 /* smiley buttons list */
597 struct smiley_button_list {
598 int width, height;
599 GtkWidget *button;
600 const GtkIMHtmlSmiley *smiley;
601 struct smiley_button_list *next;
604 static struct smiley_button_list *
605 sort_smileys(struct smiley_button_list *ls, GtkIMHtmlToolbar *toolbar,
606 int *width, const GtkIMHtmlSmiley *smiley)
608 GtkWidget *image;
609 GtkWidget *button;
610 GtkRequisition size;
611 struct smiley_button_list *cur;
612 struct smiley_button_list *it, *it_last;
613 const gchar *filename = smiley->file;
614 gchar *face = smiley->smile;
615 PurpleSmiley *psmiley = NULL;
616 gboolean supports_custom = (gtk_imhtml_get_format_functions(GTK_IMHTML(toolbar->imhtml)) & GTK_IMHTML_CUSTOM_SMILEY);
618 cur = g_new0(struct smiley_button_list, 1);
619 it = ls;
620 it_last = ls; /* list iterators*/
621 image = gtk_image_new_from_file(filename);
623 gtk_widget_size_request(image, &size);
625 if (size.width > 24 &&
626 smiley->flags & GTK_IMHTML_SMILEY_CUSTOM) { /* This is a custom smiley, let's scale it */
627 GdkPixbuf *pixbuf = NULL;
628 GtkImageType type;
630 type = gtk_image_get_storage_type(GTK_IMAGE(image));
632 if (type == GTK_IMAGE_PIXBUF) {
633 pixbuf = gtk_image_get_pixbuf(GTK_IMAGE(image));
634 } else if (type == GTK_IMAGE_ANIMATION) {
635 GdkPixbufAnimation *animation;
637 animation = gtk_image_get_animation(GTK_IMAGE(image));
639 pixbuf = gdk_pixbuf_animation_get_static_image(animation);
642 if (pixbuf != NULL) {
643 GdkPixbuf *resized;
644 resized = gdk_pixbuf_scale_simple(pixbuf, 24, 24,
645 GDK_INTERP_HYPER);
647 gtk_image_set_from_pixbuf(GTK_IMAGE(image), resized); /* This unrefs pixbuf */
648 gtk_widget_size_request(image, &size);
649 g_object_unref(G_OBJECT(resized));
653 (*width) += size.width;
655 button = gtk_button_new();
656 gtk_container_add(GTK_CONTAINER(button), image);
658 g_object_set_data(G_OBJECT(button), "smiley_text", face);
659 g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(insert_smiley_text), toolbar);
661 gtk_tooltips_set_tip(toolbar->tooltips, button, face, NULL);
663 /* these look really weird with borders */
664 gtk_button_set_relief(GTK_BUTTON(button), GTK_RELIEF_NONE);
666 psmiley = purple_smileys_find_by_shortcut(smiley->smile);
667 /* If this is a "non-custom" smiley, check to see if its shortcut is
668 "shadowed" by any custom smiley. This can only happen if the connection
669 is custom smiley-enabled */
670 if (supports_custom && psmiley && !(smiley->flags & GTK_IMHTML_SMILEY_CUSTOM)) {
671 gchar tip[128];
672 g_snprintf(tip, sizeof(tip),
673 _("This smiley is disabled because a custom smiley exists for this shortcut:\n %s"),
674 face);
675 gtk_tooltips_set_tip(toolbar->tooltips, button, tip, NULL);
676 gtk_widget_set_sensitive(button, FALSE);
677 } else if (psmiley) {
678 /* Remove the button if the smiley is destroyed */
679 g_signal_connect_object(G_OBJECT(psmiley), "destroy", G_CALLBACK(gtk_widget_destroy),
680 button, G_CONNECT_SWAPPED);
683 /* set current element to add */
684 cur->height = size.height;
685 cur->width = size.width;
686 cur->button = button;
687 cur->smiley = smiley;
688 cur->next = ls;
690 /* check where to insert by height */
691 if (ls == NULL)
692 return cur;
693 while (it != NULL) {
694 it_last = it;
695 it = it->next;
697 cur->next = it;
698 it_last->next = cur;
699 return ls;
702 static gboolean
703 smiley_is_unique(GSList *list, GtkIMHtmlSmiley *smiley)
705 while (list) {
706 GtkIMHtmlSmiley *cur = (GtkIMHtmlSmiley *) list->data;
707 if (!strcmp(cur->file, smiley->file))
708 return FALSE;
709 list = list->next;
711 return TRUE;
714 static gboolean
715 smiley_dialog_input_cb(GtkWidget *dialog, GdkEvent *event, GtkIMHtmlToolbar *toolbar)
717 if ((event->type == GDK_KEY_PRESS && event->key.keyval == GDK_Escape) ||
718 (event->type == GDK_BUTTON_PRESS && event->button.button == 1))
720 close_smiley_dialog(toolbar);
721 return TRUE;
724 return FALSE;
727 static void
728 add_smiley_list(GtkWidget *container, struct smiley_button_list *list,
729 int max_width, gboolean custom)
731 GtkWidget *line;
732 int line_width = 0;
734 if (!list)
735 return;
737 line = gtk_hbox_new(FALSE, 0);
738 gtk_box_pack_start(GTK_BOX(container), line, FALSE, FALSE, 0);
739 for (; list; list = list->next) {
740 if (custom != !!(list->smiley->flags & GTK_IMHTML_SMILEY_CUSTOM))
741 continue;
742 gtk_box_pack_start(GTK_BOX(line), list->button, FALSE, FALSE, 0);
743 gtk_widget_show(list->button);
744 line_width += list->width;
745 if (line_width >= max_width) {
746 if (list->next) {
747 line = gtk_hbox_new(FALSE, 0);
748 gtk_box_pack_start(GTK_BOX(container), line, FALSE, FALSE, 0);
750 line_width = 0;
755 static void
756 insert_smiley_cb(GtkWidget *smiley, GtkIMHtmlToolbar *toolbar)
758 GtkWidget *dialog, *vbox;
759 GtkWidget *smiley_table = NULL;
760 GSList *smileys, *unique_smileys = NULL;
761 const GSList *custom_smileys = NULL;
762 gboolean supports_custom = FALSE;
763 GtkRequisition req;
764 GtkWidget *scrolled, *viewport;
766 if (!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(smiley))) {
767 destroy_smiley_dialog(toolbar);
768 gtk_widget_grab_focus(toolbar->imhtml);
769 return;
772 if (toolbar->sml)
773 smileys = pidgin_themes_get_proto_smileys(toolbar->sml);
774 else
775 smileys = pidgin_themes_get_proto_smileys(NULL);
777 /* Note: prepend smileys to list to avoid O(n^2) overhead when there is
778 a large number of smileys... need to revers the list after for the dialog
779 work... */
780 while(smileys) {
781 GtkIMHtmlSmiley *smiley = (GtkIMHtmlSmiley *) smileys->data;
782 if(!smiley->hidden) {
783 if(smiley_is_unique(unique_smileys, smiley)) {
784 unique_smileys = g_slist_prepend(unique_smileys, smiley);
787 smileys = smileys->next;
789 supports_custom = (gtk_imhtml_get_format_functions(GTK_IMHTML(toolbar->imhtml)) & GTK_IMHTML_CUSTOM_SMILEY);
790 if (toolbar->imhtml && supports_custom) {
791 const GSList *iterator = NULL;
792 custom_smileys = pidgin_smileys_get_all();
794 for (iterator = custom_smileys ; iterator ;
795 iterator = g_slist_next(iterator)) {
796 GtkIMHtmlSmiley *smiley = (GtkIMHtmlSmiley *) iterator->data;
797 unique_smileys = g_slist_prepend(unique_smileys, smiley);
801 /* we need to reverse the list to get the smileys in the correct order */
802 unique_smileys = g_slist_reverse(unique_smileys);
804 dialog = pidgin_create_dialog(_("Smile!"), 0, "smiley_dialog", FALSE);
805 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_MOUSE);
806 vbox = pidgin_dialog_get_vbox_with_properties(GTK_DIALOG(dialog), FALSE, 0);
808 if (unique_smileys != NULL) {
809 struct smiley_button_list *ls;
810 int max_line_width, num_lines, button_width = 0;
812 /* We use hboxes packed in a vbox */
813 ls = NULL;
814 max_line_width = 0;
815 num_lines = floor(sqrt(g_slist_length(unique_smileys)));
816 smiley_table = gtk_vbox_new(FALSE, 0);
818 if (supports_custom) {
819 GtkWidget *manage = gtk_button_new_with_mnemonic(_("_Manage custom smileys"));
820 GtkRequisition req;
821 g_signal_connect(G_OBJECT(manage), "clicked",
822 G_CALLBACK(pidgin_smiley_manager_show), NULL);
823 g_signal_connect_swapped(G_OBJECT(manage), "clicked",
824 G_CALLBACK(gtk_widget_destroy), dialog);
825 gtk_box_pack_end(GTK_BOX(vbox), manage, FALSE, TRUE, 0);
826 gtk_widget_size_request(manage, &req);
827 button_width = req.width;
830 /* create list of smileys sorted by height */
831 while (unique_smileys) {
832 GtkIMHtmlSmiley *smiley = (GtkIMHtmlSmiley *) unique_smileys->data;
833 if (!smiley->hidden) {
834 ls = sort_smileys(ls, toolbar, &max_line_width, smiley);
836 unique_smileys = g_slist_delete_link(unique_smileys, unique_smileys);
838 /* The window will be at least as wide as the 'Manage ..' button */
839 max_line_width = MAX(button_width, max_line_width / num_lines);
841 /* pack buttons of the list */
842 add_smiley_list(smiley_table, ls, max_line_width, FALSE);
843 if (supports_custom) {
844 gtk_box_pack_start(GTK_BOX(smiley_table), gtk_hseparator_new(), TRUE, FALSE, 0);
845 add_smiley_list(smiley_table, ls, max_line_width, TRUE);
847 while (ls) {
848 struct smiley_button_list *tmp = ls->next;
849 g_free(ls);
850 ls = tmp;
853 gtk_widget_add_events(dialog, GDK_KEY_PRESS_MASK);
855 else {
856 smiley_table = gtk_label_new(_("This theme has no available smileys."));
857 gtk_widget_add_events(dialog, GDK_KEY_PRESS_MASK | GDK_BUTTON_PRESS_MASK);
858 g_signal_connect(G_OBJECT(dialog), "button-press-event", (GCallback)smiley_dialog_input_cb, toolbar);
862 scrolled = pidgin_make_scrollable(smiley_table, GTK_POLICY_NEVER, GTK_POLICY_NEVER, GTK_SHADOW_NONE, -1, -1);
863 gtk_box_pack_start(GTK_BOX(vbox), scrolled, TRUE, TRUE, 0);
864 gtk_widget_show(smiley_table);
866 viewport = gtk_widget_get_parent(smiley_table);
867 gtk_viewport_set_shadow_type(GTK_VIEWPORT(viewport), GTK_SHADOW_NONE);
869 /* connect signals */
870 g_signal_connect_swapped(G_OBJECT(dialog), "destroy", G_CALLBACK(close_smiley_dialog), toolbar);
871 g_signal_connect(G_OBJECT(dialog), "key-press-event", G_CALLBACK(smiley_dialog_input_cb), toolbar);
873 gtk_window_set_transient_for(GTK_WINDOW(dialog),
874 GTK_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(toolbar))));
876 /* show everything */
877 gtk_widget_show_all(dialog);
879 gtk_widget_size_request(viewport, &req);
880 gtk_widget_set_size_request(scrolled, MIN(300, req.width), MIN(290, req.height));
882 /* The window has to be made resizable, and the scrollbars in the scrolled window
883 * enabled only after setting the desired size of the window. If we do either of
884 * these tasks before now, GTK+ miscalculates the required size, and erronously
885 * makes one or both scrollbars visible (sometimes).
886 * I too think this hack is gross. But I couldn't find a better way -- sadrul */
887 gtk_window_set_resizable(GTK_WINDOW(dialog), TRUE);
888 g_object_set(G_OBJECT(scrolled),
889 "hscrollbar-policy", GTK_POLICY_AUTOMATIC,
890 "vscrollbar-policy", GTK_POLICY_AUTOMATIC,
891 NULL);
893 #ifdef _WIN32
894 winpidgin_ensure_onscreen(dialog);
895 #endif
897 toolbar->smiley_dialog = dialog;
899 gtk_widget_grab_focus(toolbar->imhtml);
902 static void send_attention_cb(GtkWidget *attention, GtkIMHtmlToolbar *toolbar)
904 PurpleConversation *conv =
905 g_object_get_data(G_OBJECT(toolbar), "active_conv");
906 const gchar *who = purple_conversation_get_name(conv);
907 PurpleConnection *gc = purple_conversation_get_gc(conv);
909 toggle_button_set_active_block(GTK_TOGGLE_BUTTON(attention), FALSE, toolbar);
910 purple_prpl_send_attention(gc, who, 0);
911 gtk_widget_grab_focus(toolbar->imhtml);
914 static void update_buttons_cb(GtkIMHtml *imhtml, GtkIMHtmlButtons buttons, GtkIMHtmlToolbar *toolbar)
916 gtk_widget_set_sensitive(GTK_WIDGET(toolbar->bold), buttons & GTK_IMHTML_BOLD);
917 gtk_widget_set_sensitive(GTK_WIDGET(toolbar->italic), buttons & GTK_IMHTML_ITALIC);
918 gtk_widget_set_sensitive(GTK_WIDGET(toolbar->underline), buttons & GTK_IMHTML_UNDERLINE);
919 gtk_widget_set_sensitive(GTK_WIDGET(toolbar->strikethrough), buttons & GTK_IMHTML_STRIKE);
921 gtk_widget_set_sensitive(GTK_WIDGET(toolbar->larger_size), buttons & GTK_IMHTML_GROW);
922 gtk_widget_set_sensitive(GTK_WIDGET(toolbar->smaller_size), buttons & GTK_IMHTML_SHRINK);
924 gtk_widget_set_sensitive(GTK_WIDGET(toolbar->font), buttons & GTK_IMHTML_FACE);
925 gtk_widget_set_sensitive(GTK_WIDGET(toolbar->fgcolor), buttons & GTK_IMHTML_FORECOLOR);
926 gtk_widget_set_sensitive(GTK_WIDGET(toolbar->bgcolor), buttons & GTK_IMHTML_BACKCOLOR);
928 gtk_widget_set_sensitive(GTK_WIDGET(toolbar->clear),
929 (buttons & GTK_IMHTML_BOLD ||
930 buttons & GTK_IMHTML_ITALIC ||
931 buttons & GTK_IMHTML_UNDERLINE ||
932 buttons & GTK_IMHTML_STRIKE ||
933 buttons & GTK_IMHTML_GROW ||
934 buttons & GTK_IMHTML_SHRINK ||
935 buttons & GTK_IMHTML_FACE ||
936 buttons & GTK_IMHTML_FORECOLOR ||
937 buttons & GTK_IMHTML_BACKCOLOR));
939 gtk_widget_set_sensitive(GTK_WIDGET(toolbar->image), buttons & GTK_IMHTML_IMAGE);
940 gtk_widget_set_sensitive(GTK_WIDGET(toolbar->link), buttons & GTK_IMHTML_LINK);
941 gtk_widget_set_sensitive(GTK_WIDGET(toolbar->smiley), buttons & GTK_IMHTML_SMILEY);
944 /* we call this when we want to _set_active the toggle button, it'll
945 * block the callback thats connected to the button so we don't have to
946 * do the double toggling hack
948 static void toggle_button_set_active_block(GtkToggleButton *button,
949 gboolean is_active,
950 GtkIMHtmlToolbar *toolbar)
952 GObject *object;
953 g_return_if_fail(toolbar);
955 object = g_object_ref(button);
956 g_signal_handlers_block_matched(object, G_SIGNAL_MATCH_DATA,
957 0, 0, NULL, NULL, toolbar);
958 gtk_toggle_button_set_active(button, is_active);
959 g_signal_handlers_unblock_matched(object, G_SIGNAL_MATCH_DATA,
960 0, 0, NULL, NULL, toolbar);
961 g_object_unref(object);
964 static void update_buttons(GtkIMHtmlToolbar *toolbar)
966 gboolean bold, italic, underline, strike;
967 char *tmp;
968 char *tmp2;
969 GtkLabel *label = g_object_get_data(G_OBJECT(toolbar), "font_label");
971 gtk_label_set_label(label, _("_Font"));
973 gtk_imhtml_get_current_format(GTK_IMHTML(toolbar->imhtml),
974 &bold, &italic, &underline);
975 strike = GTK_IMHTML(toolbar->imhtml)->edit.strike;
977 if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(toolbar->bold)) != bold)
978 toggle_button_set_active_block(GTK_TOGGLE_BUTTON(toolbar->bold), bold,
979 toolbar);
980 if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(toolbar->italic)) != italic)
981 toggle_button_set_active_block(GTK_TOGGLE_BUTTON(toolbar->italic), italic,
982 toolbar);
983 if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(toolbar->underline)) != underline)
984 toggle_button_set_active_block(GTK_TOGGLE_BUTTON(toolbar->underline),
985 underline, toolbar);
986 if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(toolbar->strikethrough)) != strike)
987 toggle_button_set_active_block(GTK_TOGGLE_BUTTON(toolbar->strikethrough),
988 strike, toolbar);
990 /* These buttons aren't ever "active". */
991 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(toolbar->smaller_size), FALSE);
992 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(toolbar->larger_size), FALSE);
994 if (bold) {
995 gchar *markup = g_strdup_printf("<b>%s</b>",
996 gtk_label_get_label(label));
997 gtk_label_set_markup_with_mnemonic(label, markup);
998 g_free(markup);
1000 if (italic) {
1001 gchar *markup = g_strdup_printf("<i>%s</i>",
1002 gtk_label_get_label(label));
1003 gtk_label_set_markup_with_mnemonic(label, markup);
1004 g_free(markup);
1006 if (underline) {
1007 gchar *markup = g_strdup_printf("<u>%s</u>",
1008 gtk_label_get_label(label));
1009 gtk_label_set_markup_with_mnemonic(label, markup);
1010 g_free(markup);
1012 if (strike) {
1013 gchar *markup = g_strdup_printf("<s>%s</s>",
1014 gtk_label_get_label(label));
1015 gtk_label_set_markup_with_mnemonic(label, markup);
1016 g_free(markup);
1019 tmp = gtk_imhtml_get_current_fontface(GTK_IMHTML(toolbar->imhtml));
1020 toggle_button_set_active_block(GTK_TOGGLE_BUTTON(toolbar->font),
1021 (tmp != NULL), toolbar);
1022 if (tmp != NULL) {
1023 gchar *markup = g_strdup_printf("<span font_desc=\"%s\">%s</span>",
1024 tmp, gtk_label_get_label(label));
1025 gtk_label_set_markup_with_mnemonic(label, markup);
1026 g_free(markup);
1028 g_free(tmp);
1030 tmp = gtk_imhtml_get_current_forecolor(GTK_IMHTML(toolbar->imhtml));
1031 toggle_button_set_active_block(GTK_TOGGLE_BUTTON(toolbar->fgcolor),
1032 (tmp != NULL), toolbar);
1033 if (tmp != NULL) {
1034 gchar *markup = g_strdup_printf("<span foreground=\"%s\">%s</span>",
1035 tmp, gtk_label_get_label(label));
1036 gtk_label_set_markup_with_mnemonic(label, markup);
1037 g_free(markup);
1039 g_free(tmp);
1041 tmp = gtk_imhtml_get_current_backcolor(GTK_IMHTML(toolbar->imhtml));
1042 tmp2 = gtk_imhtml_get_current_background(GTK_IMHTML(toolbar->imhtml));
1043 toggle_button_set_active_block(GTK_TOGGLE_BUTTON(toolbar->bgcolor),
1044 (tmp != NULL || tmp2 != NULL), toolbar);
1045 if (tmp != NULL) {
1046 gchar *markup = g_strdup_printf("<span background=\"%s\">%s</span>",
1047 tmp, gtk_label_get_label(label));
1048 gtk_label_set_markup_with_mnemonic(label, markup);
1049 g_free(markup);
1051 g_free(tmp);
1052 g_free(tmp2);
1055 static void toggle_button_cb(GtkIMHtml *imhtml, GtkIMHtmlButtons buttons, GtkIMHtmlToolbar *toolbar)
1057 update_buttons(toolbar);
1060 static void update_format_cb(GtkIMHtml *imhtml, GtkIMHtmlToolbar *toolbar) {
1061 update_buttons(toolbar);
1064 static void mark_set_cb(GtkTextBuffer *buffer, GtkTextIter *location,
1065 GtkTextMark *mark, GtkIMHtmlToolbar *toolbar)
1067 if(mark != gtk_text_buffer_get_insert(buffer))
1068 return;
1070 update_buttons(toolbar);
1074 /* This comes from gtkmenutoolbutton.c from gtk+
1075 * Copyright (C) 2003 Ricardo Fernandez Pascual
1076 * Copyright (C) 2004 Paolo Borelli
1078 static void
1079 menu_position_func (GtkMenu *menu,
1080 int *x,
1081 int *y,
1082 gboolean *push_in,
1083 gpointer data)
1085 GtkWidget *widget = GTK_WIDGET(data);
1086 GtkRequisition menu_req;
1087 gint ythickness = widget->style->ythickness;
1088 int savy;
1090 gtk_widget_size_request(GTK_WIDGET (menu), &menu_req);
1091 gdk_window_get_origin(widget->window, x, y);
1092 *x += widget->allocation.x;
1093 *y += widget->allocation.y + widget->allocation.height;
1094 savy = *y;
1096 pidgin_menu_position_func_helper(menu, x, y, push_in, data);
1098 if (savy > *y + ythickness + 1)
1099 *y -= widget->allocation.height;
1102 static gboolean
1103 button_activate_on_click(GtkWidget *button, GdkEventButton *event, GtkIMHtmlToolbar *toolbar)
1105 if (event->button == 1 && GTK_IS_TOGGLE_BUTTON(button))
1106 gtk_widget_activate(button);
1107 else if (event->button == 3)
1108 return gtk_imhtmltoolbar_popup_menu(button, event, toolbar);
1109 return FALSE;
1112 static void pidgin_menu_clicked(GtkWidget *button, GtkMenu *menu)
1114 gtk_widget_show_all(GTK_WIDGET(menu));
1115 gtk_menu_popup(menu, NULL, NULL, menu_position_func, button, 0, gtk_get_current_event_time());
1118 static void pidgin_menu_deactivate(GtkWidget *menu, GtkToggleButton *button)
1120 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), FALSE);
1123 enum {
1124 LAST_SIGNAL
1126 /* static guint signals [LAST_SIGNAL] = { 0 }; */
1128 static void
1129 gtk_imhtmltoolbar_finalize (GObject *object)
1131 GtkIMHtmlToolbar *toolbar = GTK_IMHTMLTOOLBAR(object);
1132 GtkWidget *menu;
1134 if (toolbar->image_dialog != NULL)
1136 gtk_widget_destroy(toolbar->image_dialog);
1137 toolbar->image_dialog = NULL;
1140 destroy_toolbar_font(NULL, NULL, toolbar);
1141 if (toolbar->smiley_dialog != NULL) {
1142 g_signal_handlers_disconnect_by_func(G_OBJECT(toolbar->smiley_dialog), close_smiley_dialog, toolbar);
1143 destroy_smiley_dialog(toolbar);
1145 destroy_toolbar_bgcolor(NULL, NULL, toolbar);
1146 destroy_toolbar_fgcolor(NULL, NULL, toolbar);
1147 close_link_dialog(toolbar);
1148 if (toolbar->imhtml) {
1149 g_signal_handlers_disconnect_matched(toolbar->imhtml,
1150 G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL,
1151 toolbar);
1152 g_signal_handlers_disconnect_matched(GTK_IMHTML(toolbar->imhtml)->text_buffer,
1153 G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL,
1154 toolbar);
1157 g_free(toolbar->sml);
1158 gtk_object_sink(GTK_OBJECT(toolbar->tooltips));
1160 menu = g_object_get_data(object, "font_menu");
1161 if (menu)
1162 gtk_widget_destroy(menu);
1163 menu = g_object_get_data(object, "insert_menu");
1164 if (menu)
1165 gtk_widget_destroy(menu);
1167 purple_prefs_disconnect_by_handle(object);
1169 G_OBJECT_CLASS(parent_class)->finalize (object);
1172 static void
1173 switch_toolbar_view(GtkWidget *item, GtkIMHtmlToolbar *toolbar)
1175 purple_prefs_set_bool(PIDGIN_PREFS_ROOT "/conversations/toolbar/wide",
1176 !purple_prefs_get_bool(PIDGIN_PREFS_ROOT "/conversations/toolbar/wide"));
1179 static gboolean
1180 gtk_imhtmltoolbar_popup_menu(GtkWidget *widget, GdkEventButton *event, GtkIMHtmlToolbar *toolbar)
1182 GtkWidget *menu;
1183 GtkWidget *item;
1184 gboolean wide;
1186 if (event->button != 3)
1187 return FALSE;
1189 wide = GTK_WIDGET_VISIBLE(toolbar->bold);
1191 menu = gtk_menu_new();
1192 item = gtk_menu_item_new_with_mnemonic(wide ? _("Group Items") : _("Ungroup Items"));
1193 g_signal_connect(G_OBJECT(item), "activate", G_CALLBACK(switch_toolbar_view), toolbar);
1194 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
1195 gtk_widget_show(item);
1197 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, pidgin_menu_position_func_helper,
1198 widget, event->button, event->time);
1199 return TRUE;
1202 /* Boring GTK+ stuff */
1203 static void gtk_imhtmltoolbar_class_init (GtkIMHtmlToolbarClass *class)
1205 GObjectClass *gobject_class;
1206 gobject_class = (GObjectClass*) class;
1207 parent_class = g_type_class_ref(GTK_TYPE_HBOX);
1208 gobject_class->finalize = gtk_imhtmltoolbar_finalize;
1210 purple_prefs_add_none(PIDGIN_PREFS_ROOT "/conversations/toolbar");
1211 purple_prefs_add_bool(PIDGIN_PREFS_ROOT "/conversations/toolbar/wide", FALSE);
1214 static void gtk_imhtmltoolbar_create_old_buttons(GtkIMHtmlToolbar *toolbar)
1216 GtkWidget *hbox;
1217 GtkWidget *button;
1218 struct {
1219 char *stock;
1220 gpointer callback;
1221 GtkWidget **button;
1222 const char *tooltip;
1223 } buttons[] = {
1224 {GTK_STOCK_BOLD, G_CALLBACK(do_bold), &toolbar->bold, _("Bold")},
1225 {GTK_STOCK_ITALIC, do_italic, &toolbar->italic, _("Italic")},
1226 {GTK_STOCK_UNDERLINE, do_underline, &toolbar->underline, _("Underline")},
1227 {GTK_STOCK_STRIKETHROUGH, do_strikethrough, &toolbar->strikethrough, _("Strikethrough")},
1228 {"", NULL, NULL, NULL},
1229 {PIDGIN_STOCK_TOOLBAR_TEXT_LARGER, do_big, &toolbar->larger_size, _("Increase Font Size")},
1230 {PIDGIN_STOCK_TOOLBAR_TEXT_SMALLER, do_small, &toolbar->smaller_size, _("Decrease Font Size")},
1231 {"", NULL, NULL, NULL},
1232 {PIDGIN_STOCK_TOOLBAR_FONT_FACE, toggle_font, &toolbar->font, _("Font Face")},
1233 {PIDGIN_STOCK_TOOLBAR_FGCOLOR, toggle_fg_color, &toolbar->fgcolor, _("Foreground Color")},
1234 {PIDGIN_STOCK_TOOLBAR_BGCOLOR, toggle_bg_color, &toolbar->bgcolor, _("Background Color")},
1235 {"", NULL, NULL, NULL},
1236 {PIDGIN_STOCK_CLEAR, clear_formatting_cb, &toolbar->clear, _("Reset Formatting")},
1237 {"", NULL, NULL, NULL},
1238 {PIDGIN_STOCK_TOOLBAR_INSERT_IMAGE, insert_image_cb, &toolbar->image, _("Insert IM Image")},
1239 {PIDGIN_STOCK_TOOLBAR_INSERT_LINK, insert_link_cb, &toolbar->link, _("Insert Link")},
1240 {"", NULL, NULL, NULL},
1241 {PIDGIN_STOCK_TOOLBAR_SMILEY, insert_smiley_cb, &toolbar->smiley, _("Insert Smiley")},
1242 {NULL, NULL, NULL, NULL}
1244 int iter;
1246 hbox = gtk_hbox_new(FALSE, 0);
1248 for (iter = 0; buttons[iter].stock; iter++) {
1249 if (buttons[iter].stock[0]) {
1250 button = pidgin_pixbuf_toolbar_button_from_stock(buttons[iter].stock);
1251 g_signal_connect(G_OBJECT(button), "button-press-event", G_CALLBACK(gtk_imhtmltoolbar_popup_menu), toolbar);
1252 g_signal_connect(G_OBJECT(button), "clicked",
1253 G_CALLBACK(buttons[iter].callback), toolbar);
1254 *(buttons[iter].button) = button;
1255 gtk_tooltips_set_tip(toolbar->tooltips, button, buttons[iter].tooltip, NULL);
1256 } else
1257 button = gtk_vseparator_new();
1258 gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);
1260 /* create the attention button (this is a bit hacky to not break ABI) */
1261 button = pidgin_pixbuf_toolbar_button_from_stock(PIDGIN_STOCK_TOOLBAR_SEND_ATTENTION);
1262 g_signal_connect(G_OBJECT(button), "button-press-event", G_CALLBACK(gtk_imhtmltoolbar_popup_menu), toolbar);
1263 g_signal_connect(G_OBJECT(button), "clicked",
1264 G_CALLBACK(send_attention_cb), toolbar);
1265 g_object_set_data(G_OBJECT(toolbar), "attention", button);
1266 gtk_tooltips_set_tip(toolbar->tooltips, button, _("Send Attention"), NULL);
1267 gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);
1269 gtk_box_pack_start(GTK_BOX(toolbar), hbox, FALSE, FALSE, 0);
1270 g_object_set_data(G_OBJECT(toolbar), "wide-view", hbox);
1273 static void
1274 button_visibility_changed(GtkWidget *button, gpointer dontcare, GtkWidget *item)
1276 if (GTK_WIDGET_VISIBLE(button))
1277 gtk_widget_hide(item);
1278 else
1279 gtk_widget_show(item);
1282 static void
1283 button_sensitiveness_changed(GtkWidget *button, gpointer dontcare, GtkWidget *item)
1285 gtk_widget_set_sensitive(item, GTK_WIDGET_IS_SENSITIVE(button));
1288 static void
1289 update_menuitem(GtkToggleButton *button, GtkCheckMenuItem *item)
1291 g_signal_handlers_block_by_func(G_OBJECT(item), G_CALLBACK(gtk_button_clicked), button);
1292 gtk_check_menu_item_set_active(item, gtk_toggle_button_get_active(button));
1293 g_signal_handlers_unblock_by_func(G_OBJECT(item), G_CALLBACK(gtk_button_clicked), button);
1296 static void
1297 enable_markup(GtkWidget *widget, gpointer null)
1299 if (GTK_IS_LABEL(widget))
1300 g_object_set(G_OBJECT(widget), "use-markup", TRUE, NULL);
1303 static void
1304 imhtmltoolbar_view_pref_changed(const char *name, PurplePrefType type,
1305 gconstpointer value, gpointer toolbar)
1307 if (value) {
1308 gtk_widget_hide_all(g_object_get_data(G_OBJECT(toolbar), "lean-view"));
1309 gtk_widget_show_all(g_object_get_data(G_OBJECT(toolbar), "wide-view"));
1310 } else {
1311 gtk_widget_hide_all(g_object_get_data(G_OBJECT(toolbar), "wide-view"));
1312 gtk_widget_show_all(g_object_get_data(G_OBJECT(toolbar), "lean-view"));
1316 static void gtk_imhtmltoolbar_init (GtkIMHtmlToolbar *toolbar)
1318 GtkWidget *hbox = GTK_WIDGET(toolbar), *event = gtk_event_box_new();
1319 GtkWidget *bbox, *box = gtk_hbox_new(FALSE, 0);
1320 GtkWidget *image;
1321 GtkWidget *label;
1322 GtkWidget *insert_button;
1323 GtkWidget *font_button;
1324 GtkWidget *smiley_button;
1325 GtkWidget *attention_button;
1326 GtkWidget *font_menu;
1327 GtkWidget *insert_menu;
1328 GtkWidget *menuitem;
1329 GtkWidget *sep;
1330 GObject *wide_attention_button;
1331 int i;
1332 struct {
1333 const char *label;
1334 GtkWidget **button;
1335 gboolean check;
1336 } buttons[] = {
1337 {_("<b>_Bold</b>"), &toolbar->bold, TRUE},
1338 {_("<i>_Italic</i>"), &toolbar->italic, TRUE},
1339 {_("<u>_Underline</u>"), &toolbar->underline, TRUE},
1340 {_("<span strikethrough='true'>Strikethrough</span>"), &toolbar->strikethrough, TRUE},
1341 {_("<span size='larger'>_Larger</span>"), &toolbar->larger_size, TRUE},
1342 #if 0
1343 {_("_Normal"), &toolbar->normal_size, TRUE},
1344 #endif
1345 {_("<span size='smaller'>_Smaller</span>"), &toolbar->smaller_size, TRUE},
1346 /* If we want to show the formatting for the following items, we would
1347 * need to update them when formatting changes. The above items don't need
1348 * no updating nor nothin' */
1349 {_("_Font face"), &toolbar->font, TRUE},
1350 {_("Foreground _color"), &toolbar->fgcolor, TRUE},
1351 {_("Bac_kground color"), &toolbar->bgcolor, TRUE},
1352 {_("_Reset formatting"), &toolbar->clear, FALSE},
1353 {NULL, NULL, FALSE}
1356 toolbar->imhtml = NULL;
1357 toolbar->font_dialog = NULL;
1358 toolbar->fgcolor_dialog = NULL;
1359 toolbar->bgcolor_dialog = NULL;
1360 toolbar->link_dialog = NULL;
1361 toolbar->smiley_dialog = NULL;
1362 toolbar->image_dialog = NULL;
1364 toolbar->tooltips = gtk_tooltips_new();
1366 gtk_box_set_spacing(GTK_BOX(toolbar), 3);
1368 gtk_imhtmltoolbar_create_old_buttons(toolbar);
1370 /* Fonts */
1371 font_button = gtk_toggle_button_new();
1372 gtk_button_set_relief(GTK_BUTTON(font_button), GTK_RELIEF_NONE);
1373 bbox = gtk_hbox_new(FALSE, 3);
1374 gtk_container_add(GTK_CONTAINER(font_button), bbox);
1375 image = gtk_image_new_from_stock(GTK_STOCK_BOLD, gtk_icon_size_from_name(PIDGIN_ICON_SIZE_TANGO_EXTRA_SMALL));
1376 gtk_box_pack_start(GTK_BOX(bbox), image, FALSE, FALSE, 0);
1377 label = gtk_label_new_with_mnemonic(_("_Font"));
1378 gtk_label_set_use_markup(GTK_LABEL(label), TRUE);
1379 g_object_set_data(G_OBJECT(hbox), "font_label", label);
1380 gtk_box_pack_start(GTK_BOX(bbox), label, FALSE, FALSE, 0);
1381 gtk_box_pack_start(GTK_BOX(box), font_button, FALSE, FALSE, 0);
1382 gtk_widget_show_all(font_button);
1384 font_menu = gtk_menu_new();
1385 g_object_set_data(G_OBJECT(toolbar), "font_menu", font_menu);
1387 for (i = 0; buttons[i].label; i++) {
1388 GtkWidget *old = *buttons[i].button;
1389 if (buttons[i].check) {
1390 menuitem = gtk_check_menu_item_new_with_mnemonic(buttons[i].label);
1391 g_signal_connect_after(G_OBJECT(old), "toggled",
1392 G_CALLBACK(update_menuitem), menuitem);
1393 } else {
1394 menuitem = gtk_menu_item_new_with_mnemonic(buttons[i].label);
1396 g_signal_connect_swapped(G_OBJECT(menuitem), "activate",
1397 G_CALLBACK(gtk_button_clicked), old);
1398 gtk_menu_shell_append(GTK_MENU_SHELL(font_menu), menuitem);
1399 g_signal_connect(G_OBJECT(old), "notify::sensitive",
1400 G_CALLBACK(button_sensitiveness_changed), menuitem);
1401 g_signal_connect(G_OBJECT(old), "notify::visible",
1402 G_CALLBACK(button_visibility_changed), menuitem);
1403 gtk_container_foreach(GTK_CONTAINER(menuitem), (GtkCallback)enable_markup, NULL);
1406 g_signal_connect(G_OBJECT(font_button), "button-press-event", G_CALLBACK(button_activate_on_click), toolbar);
1407 g_signal_connect(G_OBJECT(font_button), "activate", G_CALLBACK(pidgin_menu_clicked), font_menu);
1408 g_signal_connect(G_OBJECT(font_menu), "deactivate", G_CALLBACK(pidgin_menu_deactivate), font_button);
1410 /* Sep */
1411 sep = gtk_vseparator_new();
1412 gtk_box_pack_start(GTK_BOX(box), sep, FALSE, FALSE, 0);
1413 gtk_widget_show_all(sep);
1415 /* Insert */
1416 insert_button = gtk_toggle_button_new();
1417 gtk_button_set_relief(GTK_BUTTON(insert_button), GTK_RELIEF_NONE);
1418 bbox = gtk_hbox_new(FALSE, 3);
1419 gtk_container_add(GTK_CONTAINER(insert_button), bbox);
1420 image = gtk_image_new_from_stock(PIDGIN_STOCK_TOOLBAR_INSERT, gtk_icon_size_from_name(PIDGIN_ICON_SIZE_TANGO_EXTRA_SMALL));
1421 gtk_box_pack_start(GTK_BOX(bbox), image, FALSE, FALSE, 0);
1422 label = gtk_label_new_with_mnemonic(_("_Insert"));
1423 gtk_box_pack_start(GTK_BOX(bbox), label, FALSE, FALSE, 0);
1424 gtk_box_pack_start(GTK_BOX(box), insert_button, FALSE, FALSE, 0);
1425 gtk_widget_show_all(insert_button);
1427 insert_menu = gtk_menu_new();
1428 g_object_set_data(G_OBJECT(toolbar), "insert_menu", insert_menu);
1430 menuitem = gtk_menu_item_new_with_mnemonic(_("_Image"));
1431 g_signal_connect_swapped(G_OBJECT(menuitem), "activate", G_CALLBACK(gtk_button_clicked), toolbar->image);
1432 gtk_menu_shell_append(GTK_MENU_SHELL(insert_menu), menuitem);
1433 g_signal_connect(G_OBJECT(toolbar->image), "notify::sensitive",
1434 G_CALLBACK(button_sensitiveness_changed), menuitem);
1435 g_signal_connect(G_OBJECT(toolbar->image), "notify::visible",
1436 G_CALLBACK(button_visibility_changed), menuitem);
1438 menuitem = gtk_menu_item_new_with_mnemonic(_("_Link"));
1439 g_signal_connect_swapped(G_OBJECT(menuitem), "activate", G_CALLBACK(gtk_button_clicked), toolbar->link);
1440 gtk_menu_shell_append(GTK_MENU_SHELL(insert_menu), menuitem);
1441 g_signal_connect(G_OBJECT(toolbar->link), "notify::sensitive",
1442 G_CALLBACK(button_sensitiveness_changed), menuitem);
1443 g_signal_connect(G_OBJECT(toolbar->link), "notify::visible",
1444 G_CALLBACK(button_visibility_changed), menuitem);
1446 menuitem = gtk_menu_item_new_with_mnemonic(_("_Horizontal rule"));
1447 g_signal_connect(G_OBJECT(menuitem), "activate" , G_CALLBACK(insert_hr_cb), toolbar);
1448 gtk_menu_shell_append(GTK_MENU_SHELL(insert_menu), menuitem);
1449 toolbar->insert_hr = menuitem;
1451 g_signal_connect(G_OBJECT(insert_button), "button-press-event", G_CALLBACK(button_activate_on_click), toolbar);
1452 g_signal_connect(G_OBJECT(insert_button), "activate", G_CALLBACK(pidgin_menu_clicked), insert_menu);
1453 g_signal_connect(G_OBJECT(insert_menu), "deactivate", G_CALLBACK(pidgin_menu_deactivate), insert_button);
1454 toolbar->sml = NULL;
1456 /* Sep */
1457 sep = gtk_vseparator_new();
1458 gtk_box_pack_start(GTK_BOX(box), sep, FALSE, FALSE, 0);
1459 gtk_widget_show_all(sep);
1461 /* Smiley */
1462 smiley_button = gtk_button_new();
1463 gtk_button_set_relief(GTK_BUTTON(smiley_button), GTK_RELIEF_NONE);
1464 bbox = gtk_hbox_new(FALSE, 3);
1465 gtk_container_add(GTK_CONTAINER(smiley_button), bbox);
1466 image = gtk_image_new_from_stock(PIDGIN_STOCK_TOOLBAR_SMILEY, gtk_icon_size_from_name(PIDGIN_ICON_SIZE_TANGO_EXTRA_SMALL));
1467 gtk_box_pack_start(GTK_BOX(bbox), image, FALSE, FALSE, 0);
1468 label = gtk_label_new_with_mnemonic(_("_Smile!"));
1469 gtk_box_pack_start(GTK_BOX(bbox), label, FALSE, FALSE, 0);
1470 gtk_box_pack_start(GTK_BOX(box), smiley_button, FALSE, FALSE, 0);
1471 g_signal_connect(G_OBJECT(smiley_button), "button-press-event", G_CALLBACK(gtk_imhtmltoolbar_popup_menu), toolbar);
1472 g_signal_connect_swapped(G_OBJECT(smiley_button), "clicked", G_CALLBACK(gtk_button_clicked), toolbar->smiley);
1473 gtk_widget_show_all(smiley_button);
1475 /* Sep */
1476 sep = gtk_vseparator_new();
1477 gtk_box_pack_start(GTK_BOX(box), sep, FALSE, FALSE, 0);
1478 gtk_widget_show_all(sep);
1480 /* Attention */
1481 wide_attention_button = g_object_get_data(G_OBJECT(toolbar), "attention");
1483 attention_button = gtk_button_new();
1484 gtk_button_set_relief(GTK_BUTTON(attention_button), GTK_RELIEF_NONE);
1485 bbox = gtk_hbox_new(FALSE, 3);
1486 gtk_container_add(GTK_CONTAINER(attention_button), bbox);
1487 image = gtk_image_new_from_stock(PIDGIN_STOCK_TOOLBAR_SEND_ATTENTION,
1488 gtk_icon_size_from_name(PIDGIN_ICON_SIZE_TANGO_EXTRA_SMALL));
1489 gtk_box_pack_start(GTK_BOX(bbox), image, FALSE, FALSE, 0);
1490 label = gtk_label_new_with_mnemonic(_("_Attention!"));
1491 gtk_box_pack_start(GTK_BOX(bbox), label, FALSE, FALSE, 0);
1492 gtk_box_pack_start(GTK_BOX(box), attention_button, FALSE, FALSE, 0);
1493 g_signal_connect_swapped(G_OBJECT(attention_button), "clicked",
1494 G_CALLBACK(gtk_button_clicked), wide_attention_button);
1495 gtk_widget_show_all(attention_button);
1497 g_signal_connect(wide_attention_button, "notify::sensitive",
1498 G_CALLBACK(button_sensitiveness_changed), attention_button);
1499 g_signal_connect(wide_attention_button, "notify::visible",
1500 G_CALLBACK(button_visibility_changed), attention_button);
1502 /* set attention button to be greyed out until we get a conversation */
1503 gtk_widget_set_sensitive(GTK_WIDGET(wide_attention_button), FALSE);
1505 gtk_box_pack_start(GTK_BOX(hbox), box, FALSE, FALSE, 0);
1506 g_object_set_data(G_OBJECT(hbox), "lean-view", box);
1507 gtk_widget_show(box);
1509 purple_prefs_connect_callback(toolbar, PIDGIN_PREFS_ROOT "/conversations/toolbar/wide",
1510 imhtmltoolbar_view_pref_changed, toolbar);
1511 g_signal_connect_data(G_OBJECT(toolbar), "realize",
1512 G_CALLBACK(purple_prefs_trigger_callback), PIDGIN_PREFS_ROOT "/conversations/toolbar/wide",
1513 NULL, G_CONNECT_AFTER | G_CONNECT_SWAPPED);
1515 gtk_event_box_set_visible_window(GTK_EVENT_BOX(event), FALSE);
1517 gtk_widget_add_events(event, GDK_BUTTON_PRESS_MASK);
1518 gtk_box_pack_start(GTK_BOX(hbox), event, TRUE, TRUE, 0);
1519 g_signal_connect(G_OBJECT(event), "button-press-event", G_CALLBACK(gtk_imhtmltoolbar_popup_menu), toolbar);
1520 gtk_widget_show(event);
1523 GtkWidget *gtk_imhtmltoolbar_new()
1525 return GTK_WIDGET(g_object_new(gtk_imhtmltoolbar_get_type(), NULL));
1528 GType gtk_imhtmltoolbar_get_type()
1530 static GType imhtmltoolbar_type = 0;
1532 if (!imhtmltoolbar_type) {
1533 static const GTypeInfo imhtmltoolbar_info = {
1534 sizeof(GtkIMHtmlToolbarClass),
1535 NULL,
1536 NULL,
1537 (GClassInitFunc) gtk_imhtmltoolbar_class_init,
1538 NULL,
1539 NULL,
1540 sizeof (GtkIMHtmlToolbar),
1542 (GInstanceInitFunc) gtk_imhtmltoolbar_init,
1543 NULL
1546 imhtmltoolbar_type = g_type_register_static(GTK_TYPE_HBOX,
1547 "GtkIMHtmlToolbar", &imhtmltoolbar_info, 0);
1550 return imhtmltoolbar_type;
1554 void gtk_imhtmltoolbar_attach(GtkIMHtmlToolbar *toolbar, GtkWidget *imhtml)
1556 GtkIMHtmlButtons buttons;
1558 g_return_if_fail(toolbar != NULL);
1559 g_return_if_fail(GTK_IS_IMHTMLTOOLBAR(toolbar));
1560 g_return_if_fail(imhtml != NULL);
1561 g_return_if_fail(GTK_IS_IMHTML(imhtml));
1563 toolbar->imhtml = imhtml;
1564 g_signal_connect(G_OBJECT(imhtml), "format_buttons_update", G_CALLBACK(update_buttons_cb), toolbar);
1565 g_signal_connect_after(G_OBJECT(imhtml), "format_function_toggle", G_CALLBACK(toggle_button_cb), toolbar);
1566 g_signal_connect_after(G_OBJECT(imhtml), "format_function_clear", G_CALLBACK(update_format_cb), toolbar);
1567 g_signal_connect(G_OBJECT(imhtml), "format_function_update", G_CALLBACK(update_format_cb), toolbar);
1568 g_signal_connect_after(G_OBJECT(GTK_IMHTML(imhtml)->text_buffer), "mark-set", G_CALLBACK(mark_set_cb), toolbar);
1570 buttons = gtk_imhtml_get_format_functions(GTK_IMHTML(imhtml));
1571 update_buttons_cb(GTK_IMHTML(imhtml), buttons, toolbar);
1572 update_buttons(toolbar);
1575 void gtk_imhtmltoolbar_associate_smileys(GtkIMHtmlToolbar *toolbar, const char *proto_id)
1577 g_free(toolbar->sml);
1578 toolbar->sml = g_strdup(proto_id);
1581 void gtk_imhtmltoolbar_switch_active_conversation(GtkIMHtmlToolbar *toolbar,
1582 PurpleConversation *conv)
1584 PurpleConnection *gc = purple_conversation_get_gc(conv);
1585 PurplePlugin *prpl = purple_connection_get_prpl(gc);
1586 GtkWidget *attention =
1587 g_object_get_data(G_OBJECT(toolbar), "attention");
1589 g_object_set_data(G_OBJECT(toolbar), "active_conv", conv);
1591 /* gray out attention button on protocols that don't support it
1592 for the time being it is always disabled for chats */
1593 gtk_widget_set_sensitive(attention,
1594 conv && prpl && purple_conversation_get_type(conv) == PURPLE_CONV_TYPE_IM &&
1595 PURPLE_PLUGIN_PROTOCOL_INFO(prpl)->send_attention != NULL);