I just noticed I didn't differentiate bn_IN from bn when I added this translation.
[pidgin-git.git] / pidgin / gtkwhiteboard.c
blob938835f270cd04d238d2f2022d862225477c59a4
1 /*
2 * pidgin
4 * Pidgin is the legal property of its developers, whose names are too numerous
5 * to list here. Please refer to the COPYRIGHT file distributed with this
6 * source distribution.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
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 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
24 #include "internal.h"
25 #include "blist.h"
26 #include "debug.h"
28 #include "gtkwhiteboard.h"
29 #include "gtkutils.h"
31 /******************************************************************************
32 * Prototypes
33 *****************************************************************************/
34 static void pidgin_whiteboard_create(PurpleWhiteboard *wb);
36 static void pidgin_whiteboard_destroy(PurpleWhiteboard *wb);
37 static gboolean whiteboard_close_cb(GtkWidget *widget, GdkEvent *event, PidginWhiteboard *gtkwb);
39 /*static void pidginwhiteboard_button_start_press(GtkButton *button, gpointer data); */
41 static gboolean pidgin_whiteboard_configure_event(GtkWidget *widget, GdkEventConfigure *event, gpointer data);
42 static gboolean pidgin_whiteboard_expose_event(GtkWidget *widget, GdkEventExpose *event, gpointer data);
44 static gboolean pidgin_whiteboard_brush_down(GtkWidget *widget, GdkEventButton *event, gpointer data);
45 static gboolean pidgin_whiteboard_brush_motion(GtkWidget *widget, GdkEventMotion *event, gpointer data);
46 static gboolean pidgin_whiteboard_brush_up(GtkWidget *widget, GdkEventButton *event, gpointer data);
48 static void pidgin_whiteboard_draw_brush_point(PurpleWhiteboard *wb,
49 int x, int y, int color, int size);
50 static void pidgin_whiteboard_draw_brush_line(PurpleWhiteboard *wb, int x0, int y0,
51 int x1, int y1, int color, int size);
53 static void pidgin_whiteboard_set_dimensions(PurpleWhiteboard *wb, int width, int height);
54 static void pidgin_whiteboard_set_brush(PurpleWhiteboard *wb, int size, int color);
55 static void pidgin_whiteboard_clear(PurpleWhiteboard *wb);
57 static void pidgin_whiteboard_button_clear_press(GtkWidget *widget, gpointer data);
58 static void pidgin_whiteboard_button_save_press(GtkWidget *widget, gpointer data);
60 static void pidgin_whiteboard_set_canvas_as_icon(PidginWhiteboard *gtkwb);
62 static void pidgin_whiteboard_rgb24_to_rgb48(int color_rgb, GdkColor *color);
64 static void color_select_dialog(GtkWidget *widget, PidginWhiteboard *gtkwb);
66 /******************************************************************************
67 * Globals
68 *****************************************************************************/
70 GList *buttonList = NULL;
71 GdkColor DefaultColor[PALETTE_NUM_COLORS];
74 static int LastX; /* Tracks last position of the mouse when drawing */
75 static int LastY;
76 static int MotionCount; /* Tracks how many brush motions made */
77 static int BrushState = BRUSH_STATE_UP;
79 static PurpleWhiteboardUiOps ui_ops =
81 pidgin_whiteboard_create,
82 pidgin_whiteboard_destroy,
83 pidgin_whiteboard_set_dimensions,
84 pidgin_whiteboard_set_brush,
85 pidgin_whiteboard_draw_brush_point,
86 pidgin_whiteboard_draw_brush_line,
87 pidgin_whiteboard_clear,
88 NULL,
89 NULL,
90 NULL,
91 NULL
94 /******************************************************************************
95 * API
96 *****************************************************************************/
97 PurpleWhiteboardUiOps *pidgin_whiteboard_get_ui_ops(void)
99 return &ui_ops;
102 static void pidgin_whiteboard_create(PurpleWhiteboard *wb)
104 PurpleBuddy *buddy;
105 GtkWidget *window;
106 GtkWidget *drawing_area;
107 GtkWidget *vbox_controls;
108 GtkWidget *hbox_canvas_and_controls;
111 --------------------------
112 |[][][][palette[][][][][]|
113 |------------------------|
114 | canvas | con |
115 | | trol|
116 | | s |
117 | | |
118 | | |
119 --------------------------
121 GtkWidget *clear_button;
122 GtkWidget *save_button;
123 GtkWidget *color_button;
125 PidginWhiteboard *gtkwb = g_new0(PidginWhiteboard, 1);
127 gtkwb->wb = wb;
128 wb->ui_data = gtkwb;
130 /* Get dimensions (default?) for the whiteboard canvas */
131 if (!purple_whiteboard_get_dimensions(wb, &gtkwb->width, &gtkwb->height))
133 /* Give some initial board-size */
134 gtkwb->width = 300;
135 gtkwb->height = 250;
138 if (!purple_whiteboard_get_brush(wb, &gtkwb->brush_size, &gtkwb->brush_color))
140 /* Give some initial brush-info */
141 gtkwb->brush_size = 2;
142 gtkwb->brush_color = 0xff0000;
145 /* Try and set window title as the name of the buddy, else just use their
146 * username
148 buddy = purple_find_buddy(wb->account, wb->who);
150 window = pidgin_create_window(buddy != NULL ? purple_buddy_get_contact_alias(buddy) : wb->who, 0, NULL, FALSE);
151 gtkwb->window = window;
152 gtk_widget_set_name(window, wb->who);
154 g_signal_connect(G_OBJECT(window), "delete_event",
155 G_CALLBACK(whiteboard_close_cb), gtkwb);
157 #if 0
158 int i;
160 GtkWidget *hbox_palette;
161 GtkWidget *vbox_palette_above_canvas_and_controls;
162 GtkWidget *palette_color_box[PALETTE_NUM_COLORS];
164 /* Create vertical box to place palette above the canvas and controls */
165 vbox_palette_above_canvas_and_controls = gtk_vbox_new(FALSE, 0);
166 gtk_container_add(GTK_CONTAINER(window), vbox_palette_above_canvas_and_controls);
167 gtk_widget_show(vbox_palette_above_canvas_and_controls);
169 /* Create horizontal box for the palette and all its entries */
170 hbox_palette = gtk_hbox_new(FALSE, 0);
171 gtk_box_pack_start(GTK_BOX(vbox_palette_above_canvas_and_controls),
172 hbox_palette, FALSE, FALSE, PIDGIN_HIG_BORDER);
173 gtk_widget_show(hbox_palette);
175 /* Create horizontal box to seperate the canvas from the controls */
176 hbox_canvas_and_controls = gtk_hbox_new(FALSE, 0);
177 gtk_box_pack_start(GTK_BOX(vbox_palette_above_canvas_and_controls),
178 hbox_canvas_and_controls, FALSE, FALSE, PIDGIN_HIG_BORDER);
179 gtk_widget_show(hbox_canvas_and_controls);
181 for(i = 0; i < PALETTE_NUM_COLORS; i++)
183 palette_color_box[i] = gtk_image_new_from_pixbuf(NULL);
184 gtk_widget_set_size_request(palette_color_box[i], gtkwb->width / PALETTE_NUM_COLORS ,32);
185 gtk_container_add(GTK_CONTAINER(hbox_palette), palette_color_box[i]);
187 gtk_widget_show(palette_color_box[i]);
189 #endif
191 hbox_canvas_and_controls = gtk_hbox_new(FALSE, 0);
192 gtk_widget_show(hbox_canvas_and_controls);
194 gtk_container_add(GTK_CONTAINER(window), hbox_canvas_and_controls);
195 gtk_container_set_border_width(GTK_CONTAINER(window), PIDGIN_HIG_BORDER);
197 /* Create the drawing area */
198 drawing_area = gtk_drawing_area_new();
199 gtkwb->drawing_area = drawing_area;
200 gtk_widget_set_size_request(GTK_WIDGET(drawing_area), gtkwb->width, gtkwb->height);
201 gtk_box_pack_start(GTK_BOX(hbox_canvas_and_controls), drawing_area, TRUE, TRUE, PIDGIN_HIG_BOX_SPACE);
203 gtk_widget_show(drawing_area);
205 /* Signals used to handle backing pixmap */
206 g_signal_connect(G_OBJECT(drawing_area), "expose_event",
207 G_CALLBACK(pidgin_whiteboard_expose_event), gtkwb);
209 g_signal_connect(G_OBJECT(drawing_area), "configure_event",
210 G_CALLBACK(pidgin_whiteboard_configure_event), gtkwb);
212 /* Event signals */
213 g_signal_connect(G_OBJECT(drawing_area), "button_press_event",
214 G_CALLBACK(pidgin_whiteboard_brush_down), gtkwb);
216 g_signal_connect(G_OBJECT(drawing_area), "motion_notify_event",
217 G_CALLBACK(pidgin_whiteboard_brush_motion), gtkwb);
219 g_signal_connect(G_OBJECT(drawing_area), "button_release_event",
220 G_CALLBACK(pidgin_whiteboard_brush_up), gtkwb);
222 gtk_widget_set_events(drawing_area,
223 GDK_EXPOSURE_MASK |
224 GDK_LEAVE_NOTIFY_MASK |
225 GDK_BUTTON_PRESS_MASK |
226 GDK_POINTER_MOTION_MASK |
227 GDK_BUTTON_RELEASE_MASK |
228 GDK_POINTER_MOTION_HINT_MASK);
230 /* Create vertical box to contain the controls */
231 vbox_controls = gtk_vbox_new(FALSE, 0);
232 gtk_box_pack_start(GTK_BOX(hbox_canvas_and_controls),
233 vbox_controls, FALSE, FALSE, PIDGIN_HIG_BOX_SPACE);
234 gtk_widget_show(vbox_controls);
236 /* Add a clear button */
237 clear_button = gtk_button_new_from_stock(GTK_STOCK_CLEAR);
238 gtk_box_pack_start(GTK_BOX(vbox_controls), clear_button, FALSE, FALSE, PIDGIN_HIG_BOX_SPACE);
239 gtk_widget_show(clear_button);
240 g_signal_connect(G_OBJECT(clear_button), "clicked",
241 G_CALLBACK(pidgin_whiteboard_button_clear_press), gtkwb);
243 /* Add a save button */
244 save_button = gtk_button_new_from_stock(GTK_STOCK_SAVE);
245 gtk_box_pack_start(GTK_BOX(vbox_controls), save_button, FALSE, FALSE, PIDGIN_HIG_BOX_SPACE);
246 gtk_widget_show(save_button);
248 g_signal_connect(G_OBJECT(save_button), "clicked",
249 G_CALLBACK(pidgin_whiteboard_button_save_press), gtkwb);
251 /* Add a color selector */
252 color_button = gtk_button_new_from_stock(GTK_STOCK_SELECT_COLOR);
253 gtk_box_pack_start(GTK_BOX(vbox_controls), color_button, FALSE, FALSE, PIDGIN_HIG_BOX_SPACE);
254 gtk_widget_show(color_button);
255 g_signal_connect(G_OBJECT(color_button), "clicked",
256 G_CALLBACK(color_select_dialog), gtkwb);
258 /* Make all this (window) visible */
259 gtk_widget_show(window);
261 pidgin_whiteboard_set_canvas_as_icon(gtkwb);
263 /* TODO Specific protocol/whiteboard assignment here? Needs a UI Op? */
264 /* Set default brush size and color */
266 ds->brush_size = DOODLE_BRUSH_MEDIUM;
267 ds->brush_color = 0;
271 static void pidgin_whiteboard_destroy(PurpleWhiteboard *wb)
273 PidginWhiteboard *gtkwb;
274 GtkWidget *colour_dialog;
276 g_return_if_fail(wb != NULL);
277 gtkwb = wb->ui_data;
278 g_return_if_fail(gtkwb != NULL);
280 /* TODO Ask if user wants to save picture before the session is closed */
282 /* Clear graphical memory */
283 if(gtkwb->pixmap)
285 g_object_unref(gtkwb->pixmap);
286 gtkwb->pixmap = NULL;
289 colour_dialog = g_object_get_data(G_OBJECT(gtkwb->window), "colour-dialog");
290 if (colour_dialog) {
291 gtk_widget_destroy(colour_dialog);
292 g_object_set_data(G_OBJECT(gtkwb->window), "colour-dialog", NULL);
295 if(gtkwb->window)
297 gtk_widget_destroy(gtkwb->window);
298 gtkwb->window = NULL;
300 g_free(gtkwb);
301 wb->ui_data = NULL;
304 static gboolean whiteboard_close_cb(GtkWidget *widget, GdkEvent *event, PidginWhiteboard *gtkwb)
306 PurpleWhiteboard *wb;
308 g_return_val_if_fail(gtkwb != NULL, FALSE);
309 wb = gtkwb->wb;
310 g_return_val_if_fail(wb != NULL, FALSE);
312 purple_whiteboard_destroy(wb);
314 return FALSE;
318 * Whiteboard start button on conversation window (move this code to gtkconv?
319 * and use new prpl_info member?)
321 #if 0
322 static void pidginwhiteboard_button_start_press(GtkButton *button, gpointer data)
324 PurpleConversation *conv = data;
325 PurpleAccount *account = purple_conversation_get_account(conv);
326 PurpleConnection *gc = purple_account_get_connection(account);
327 char *to = (char*)(purple_conversation_get_name(conv));
329 /* Only handle this if local client requested Doodle session (else local
330 * client would have sent one)
332 PurpleWhiteboard *wb = purple_whiteboard_get(account, to);
334 /* Write a local message to this conversation showing that a request for a
335 * Doodle session has been made
337 /* XXXX because otherwise gettext will see this string, even though it's
338 * in an #if 0 block. Remove the XXXX if you want to use this code.
339 * But, it really shouldn't be a Yahoo-specific string. ;) */
340 purple_conv_im_write(PURPLE_CONV_IM(conv), "", XXXX_("Sent Doodle request."),
341 PURPLE_MESSAGE_NICK | PURPLE_MESSAGE_RECV, time(NULL));
343 yahoo_doodle_command_send_request(gc, to);
344 yahoo_doodle_command_send_ready(gc, to);
346 /* Insert this 'session' in the list. At this point, it's only a requested
347 * session.
349 wb = purple_whiteboard_create(account, to, DOODLE_STATE_REQUESTING);
351 #endif
353 static gboolean pidgin_whiteboard_configure_event(GtkWidget *widget, GdkEventConfigure *event, gpointer data)
355 PidginWhiteboard *gtkwb = (PidginWhiteboard*)data;
357 GdkPixmap *pixmap = gtkwb->pixmap;
359 if(pixmap)
360 g_object_unref(pixmap);
362 pixmap = gdk_pixmap_new(widget->window,
363 widget->allocation.width,
364 widget->allocation.height,
365 -1);
367 gtkwb->pixmap = pixmap;
369 gdk_draw_rectangle(pixmap,
370 widget->style->white_gc,
371 TRUE,
372 0, 0,
373 widget->allocation.width,
374 widget->allocation.height);
376 return TRUE;
379 static gboolean pidgin_whiteboard_expose_event(GtkWidget *widget, GdkEventExpose *event, gpointer data)
381 PidginWhiteboard *gtkwb = (PidginWhiteboard*)(data);
382 GdkPixmap *pixmap = gtkwb->pixmap;
384 gdk_draw_drawable(widget->window,
385 widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
386 pixmap,
387 event->area.x, event->area.y,
388 event->area.x, event->area.y,
389 event->area.width, event->area.height);
391 return FALSE;
394 static gboolean pidgin_whiteboard_brush_down(GtkWidget *widget, GdkEventButton *event, gpointer data)
396 PidginWhiteboard *gtkwb = (PidginWhiteboard*)data;
397 GdkPixmap *pixmap = gtkwb->pixmap;
399 PurpleWhiteboard *wb = gtkwb->wb;
400 GList *draw_list = wb->draw_list;
402 if(BrushState != BRUSH_STATE_UP)
404 /* Potential double-click DOWN to DOWN? */
405 BrushState = BRUSH_STATE_DOWN;
407 /* return FALSE; */
410 BrushState = BRUSH_STATE_DOWN;
412 if(event->button == 1 && pixmap != NULL)
414 /* Check if draw_list has contents; if so, clear it */
415 if(draw_list)
417 purple_whiteboard_draw_list_destroy(draw_list);
418 draw_list = NULL;
421 /* Set tracking variables */
422 LastX = event->x;
423 LastY = event->y;
425 MotionCount = 0;
427 draw_list = g_list_append(draw_list, GINT_TO_POINTER(LastX));
428 draw_list = g_list_append(draw_list, GINT_TO_POINTER(LastY));
430 pidgin_whiteboard_draw_brush_point(gtkwb->wb,
431 event->x, event->y,
432 gtkwb->brush_color, gtkwb->brush_size);
435 wb->draw_list = draw_list;
437 return TRUE;
440 static gboolean pidgin_whiteboard_brush_motion(GtkWidget *widget, GdkEventMotion *event, gpointer data)
442 int x;
443 int y;
444 int dx;
445 int dy;
447 GdkModifierType state;
449 PidginWhiteboard *gtkwb = (PidginWhiteboard*)data;
450 GdkPixmap *pixmap = gtkwb->pixmap;
452 PurpleWhiteboard *wb = gtkwb->wb;
453 GList *draw_list = wb->draw_list;
455 if(event->is_hint)
456 gdk_window_get_pointer(event->window, &x, &y, &state);
457 else
459 x = event->x;
460 y = event->y;
461 state = event->state;
464 if(state & GDK_BUTTON1_MASK && pixmap != NULL)
466 if((BrushState != BRUSH_STATE_DOWN) && (BrushState != BRUSH_STATE_MOTION))
468 purple_debug_error("gtkwhiteboard", "***Bad brush state transition %d to MOTION\n", BrushState);
470 BrushState = BRUSH_STATE_MOTION;
472 return FALSE;
474 BrushState = BRUSH_STATE_MOTION;
476 dx = x - LastX;
477 dy = y - LastY;
479 MotionCount++;
481 /* NOTE 100 is a temporary constant for how many deltas/motions in a
482 * stroke (needs UI Ops?)
484 if(MotionCount == 100)
486 draw_list = g_list_append(draw_list, GINT_TO_POINTER(dx));
487 draw_list = g_list_append(draw_list, GINT_TO_POINTER(dy));
489 /* Send draw list to the draw_list handler */
490 purple_whiteboard_send_draw_list(gtkwb->wb, draw_list);
492 /* The brush stroke is finished, clear the list for another one */
493 if(draw_list)
495 purple_whiteboard_draw_list_destroy(draw_list);
496 draw_list = NULL;
499 /* Reset motion tracking */
500 MotionCount = 0;
502 draw_list = g_list_append(draw_list, GINT_TO_POINTER(LastX));
503 draw_list = g_list_append(draw_list, GINT_TO_POINTER(LastY));
505 dx = x - LastX;
506 dy = y - LastY;
509 draw_list = g_list_append(draw_list, GINT_TO_POINTER(dx));
510 draw_list = g_list_append(draw_list, GINT_TO_POINTER(dy));
512 pidgin_whiteboard_draw_brush_line(gtkwb->wb,
513 LastX, LastY,
514 x, y,
515 gtkwb->brush_color, gtkwb->brush_size);
517 /* Set tracking variables */
518 LastX = x;
519 LastY = y;
522 wb->draw_list = draw_list;
524 return TRUE;
527 static gboolean pidgin_whiteboard_brush_up(GtkWidget *widget, GdkEventButton *event, gpointer data)
529 PidginWhiteboard *gtkwb = (PidginWhiteboard*)data;
530 GdkPixmap *pixmap = gtkwb->pixmap;
532 PurpleWhiteboard *wb = gtkwb->wb;
533 GList *draw_list = wb->draw_list;
535 if((BrushState != BRUSH_STATE_DOWN) && (BrushState != BRUSH_STATE_MOTION))
537 purple_debug_error("gtkwhiteboard", "***Bad brush state transition %d to UP\n", BrushState);
539 BrushState = BRUSH_STATE_UP;
541 return FALSE;
543 BrushState = BRUSH_STATE_UP;
545 if(event->button == 1 && pixmap != NULL)
547 /* If the brush was never moved, express two sets of two deltas That's a
548 * 'point,' but not for Yahoo!
550 /* if((event->x == LastX) && (event->y == LastY)) */
551 if(MotionCount == 0)
553 int index;
555 /* For Yahoo!, a (0 0) indicates the end of drawing */
556 /* FIXME: Yahoo Doodle specific! */
557 for(index = 0; index < 2; index++)
559 draw_list = g_list_append(draw_list, 0);
560 draw_list = g_list_append(draw_list, 0);
564 else
565 MotionCount = 0;
568 /* Send draw list to prpl draw_list handler */
569 purple_whiteboard_send_draw_list(gtkwb->wb, draw_list);
571 pidgin_whiteboard_set_canvas_as_icon(gtkwb);
573 /* The brush stroke is finished, clear the list for another one */
574 if(draw_list)
575 purple_whiteboard_draw_list_destroy(draw_list);
577 wb->draw_list = NULL;
580 return TRUE;
583 static void pidgin_whiteboard_draw_brush_point(PurpleWhiteboard *wb, int x, int y, int color, int size)
585 PidginWhiteboard *gtkwb = wb->ui_data;
586 GtkWidget *widget = gtkwb->drawing_area;
587 GdkPixmap *pixmap = gtkwb->pixmap;
589 GdkRectangle update_rect;
591 GdkGC *gfx_con = gdk_gc_new(pixmap);
592 GdkColor col;
594 update_rect.x = x - size / 2;
595 update_rect.y = y - size / 2;
596 update_rect.width = size;
597 update_rect.height = size;
599 /* Interpret and convert color */
600 pidgin_whiteboard_rgb24_to_rgb48(color, &col);
602 gdk_gc_set_rgb_fg_color(gfx_con, &col);
603 /* gdk_gc_set_rgb_bg_color(gfx_con, &col); */
605 /* NOTE 5 is a size constant for now... this is because of how poorly the
606 * gdk_draw_arc draws small circles
608 if(size < 5)
610 /* Draw a rectangle/square */
611 gdk_draw_rectangle(pixmap,
612 gfx_con,
613 TRUE,
614 update_rect.x, update_rect.y,
615 update_rect.width, update_rect.height);
617 else
619 /* Draw a circle */
620 gdk_draw_arc(pixmap,
621 gfx_con,
622 TRUE,
623 update_rect.x, update_rect.y,
624 update_rect.width, update_rect.height,
625 0, FULL_CIRCLE_DEGREES);
628 gtk_widget_queue_draw_area(widget,
629 update_rect.x, update_rect.y,
630 update_rect.width, update_rect.height);
632 g_object_unref(G_OBJECT(gfx_con));
635 /* Uses Bresenham's algorithm (as provided by Wikipedia) */
636 static void pidgin_whiteboard_draw_brush_line(PurpleWhiteboard *wb, int x0, int y0, int x1, int y1, int color, int size)
638 int temp;
640 int xstep;
641 int ystep;
643 int dx;
644 int dy;
646 int error;
647 int derror;
649 int x;
650 int y;
652 gboolean steep = abs(y1 - y0) > abs(x1 - x0);
654 if(steep)
656 temp = x0; x0 = y0; y0 = temp;
657 temp = x1; x1 = y1; y1 = temp;
660 dx = abs(x1 - x0);
661 dy = abs(y1 - y0);
663 error = 0;
664 derror = dy;
666 x = x0;
667 y = y0;
669 if(x0 < x1)
670 xstep = 1;
671 else
672 xstep = -1;
674 if(y0 < y1)
675 ystep = 1;
676 else
677 ystep = -1;
679 if(steep)
680 pidgin_whiteboard_draw_brush_point(wb, y, x, color, size);
681 else
682 pidgin_whiteboard_draw_brush_point(wb, x, y, color, size);
684 while(x != x1)
686 x += xstep;
687 error += derror;
689 if((error * 2) >= dx)
691 y += ystep;
692 error -= dx;
695 if(steep)
696 pidgin_whiteboard_draw_brush_point(wb, y, x, color, size);
697 else
698 pidgin_whiteboard_draw_brush_point(wb, x, y, color, size);
702 static void pidgin_whiteboard_set_dimensions(PurpleWhiteboard *wb, int width, int height)
704 PidginWhiteboard *gtkwb = wb->ui_data;
706 gtkwb->width = width;
707 gtkwb->height = height;
710 static void pidgin_whiteboard_set_brush(PurpleWhiteboard *wb, int size, int color)
712 PidginWhiteboard *gtkwb = wb->ui_data;
714 gtkwb->brush_size = size;
715 gtkwb->brush_color = color;
718 static void pidgin_whiteboard_clear(PurpleWhiteboard *wb)
720 PidginWhiteboard *gtkwb = wb->ui_data;
721 GdkPixmap *pixmap = gtkwb->pixmap;
722 GtkWidget *drawing_area = gtkwb->drawing_area;
724 gdk_draw_rectangle(pixmap,
725 drawing_area->style->white_gc,
726 TRUE,
727 0, 0,
728 drawing_area->allocation.width,
729 drawing_area->allocation.height);
731 gtk_widget_queue_draw_area(drawing_area,
732 0, 0,
733 drawing_area->allocation.width,
734 drawing_area->allocation.height);
737 static void pidgin_whiteboard_button_clear_press(GtkWidget *widget, gpointer data)
739 PidginWhiteboard *gtkwb = (PidginWhiteboard*)(data);
741 /* Confirm whether the user really wants to clear */
742 GtkWidget *dialog = gtk_message_dialog_new(GTK_WINDOW(gtkwb->window),
743 GTK_DIALOG_DESTROY_WITH_PARENT,
744 GTK_MESSAGE_QUESTION,
745 GTK_BUTTONS_YES_NO,
746 _("Do you really want to clear?"));
747 gint response = gtk_dialog_run(GTK_DIALOG(dialog));
748 gtk_widget_destroy(dialog);
750 if (response == GTK_RESPONSE_YES)
752 pidgin_whiteboard_clear(gtkwb->wb);
754 pidgin_whiteboard_set_canvas_as_icon(gtkwb);
756 /* Do protocol specific clearing procedures */
757 purple_whiteboard_send_clear(gtkwb->wb);
761 static void pidgin_whiteboard_button_save_press(GtkWidget *widget, gpointer data)
763 PidginWhiteboard *gtkwb = (PidginWhiteboard*)(data);
764 GdkPixbuf *pixbuf;
766 GtkWidget *dialog;
768 int result;
770 dialog = gtk_file_chooser_dialog_new (_("Save File"),
771 GTK_WINDOW(gtkwb->window),
772 GTK_FILE_CHOOSER_ACTION_SAVE,
773 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
774 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
775 NULL);
777 /* gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(dialog), (gboolean)(TRUE)); */
779 /* if(user_edited_a_new_document) */
781 /* gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), default_folder_for_saving); */
782 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), "whiteboard.jpg");
785 else
786 gtk_file_chooser_set_filename (GTK_FILE_CHOOSER (dialog), filename_for_existing_document);
789 result = gtk_dialog_run(GTK_DIALOG(dialog));
791 if(result == GTK_RESPONSE_ACCEPT)
793 char *filename;
795 filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
797 gtk_widget_destroy(dialog);
799 /* Makes an icon from the whiteboard's canvas 'image' */
800 pixbuf = gdk_pixbuf_get_from_drawable(NULL,
801 (GdkDrawable*)(gtkwb->pixmap),
802 gdk_drawable_get_colormap(gtkwb->pixmap),
803 0, 0,
804 0, 0,
805 gtkwb->width, gtkwb->height);
807 if(gdk_pixbuf_save(pixbuf, filename, "jpeg", NULL, "quality", "100", NULL))
808 purple_debug_info("gtkwhiteboard", "File Saved...\n");
809 else
810 purple_debug_info("gtkwhiteboard", "File not Saved... Error\n");
811 g_free(filename);
813 else if(result == GTK_RESPONSE_CANCEL)
815 gtk_widget_destroy(dialog);
817 purple_debug_info("gtkwhiteboard", "File not Saved... Cancelled\n");
821 static void pidgin_whiteboard_set_canvas_as_icon(PidginWhiteboard *gtkwb)
823 GdkPixbuf *pixbuf;
825 /* Makes an icon from the whiteboard's canvas 'image' */
826 pixbuf = gdk_pixbuf_get_from_drawable(NULL,
827 (GdkDrawable*)(gtkwb->pixmap),
828 gdk_drawable_get_colormap(gtkwb->pixmap),
829 0, 0,
830 0, 0,
831 gtkwb->width, gtkwb->height);
833 gtk_window_set_icon((GtkWindow*)(gtkwb->window), pixbuf);
836 static void pidgin_whiteboard_rgb24_to_rgb48(int color_rgb, GdkColor *color)
838 color->red = (color_rgb >> 8) | 0xFF;
839 color->green = (color_rgb & 0xFF00) | 0xFF;
840 color->blue = ((color_rgb & 0xFF) << 8) | 0xFF;
843 static void
844 change_color_cb(GtkColorSelection *selection, PidginWhiteboard *gtkwb)
846 GdkColor color;
847 int old_size = 5;
848 int old_color = 0;
849 int new_color;
850 PurpleWhiteboard *wb = gtkwb->wb;
852 gtk_color_selection_get_current_color(selection, &color);
853 new_color = (color.red & 0xFF00) << 8;
854 new_color |= (color.green & 0xFF00);
855 new_color |= (color.blue & 0xFF00) >> 8;
857 purple_whiteboard_get_brush(wb, &old_size, &old_color);
858 purple_whiteboard_send_brush(wb, old_size, new_color);
861 static void color_selection_dialog_destroy(GtkWidget *w, PidginWhiteboard *gtkwb)
863 GtkWidget *dialog = g_object_get_data(G_OBJECT(gtkwb->window), "colour-dialog");
864 gtk_widget_destroy(dialog);
865 g_object_set_data(G_OBJECT(gtkwb->window), "colour-dialog", NULL);
868 static void color_select_dialog(GtkWidget *widget, PidginWhiteboard *gtkwb)
870 GdkColor color;
871 GtkColorSelectionDialog *dialog;
873 dialog = (GtkColorSelectionDialog *)gtk_color_selection_dialog_new(_("Select color"));
874 g_object_set_data(G_OBJECT(gtkwb->window), "colour-dialog", dialog);
876 g_signal_connect(G_OBJECT(dialog->colorsel), "color-changed",
877 G_CALLBACK(change_color_cb), gtkwb);
879 gtk_widget_destroy(dialog->cancel_button);
880 gtk_widget_destroy(dialog->help_button);
882 g_signal_connect(G_OBJECT(dialog->ok_button), "clicked",
883 G_CALLBACK(color_selection_dialog_destroy), gtkwb);
885 gtk_color_selection_set_has_palette(GTK_COLOR_SELECTION(dialog->colorsel), TRUE);
887 pidgin_whiteboard_rgb24_to_rgb48(gtkwb->brush_color, &color);
888 gtk_color_selection_set_current_color(GTK_COLOR_SELECTION(dialog->colorsel), &color);
890 gtk_widget_show_all(GTK_WIDGET(dialog));