src/gtkwhiteboard.c

branch
gaim
changeset 20470
77693555855f
parent 13071
b98e72d4089a
parent 20469
b2836a24d81e
child 20471
1966704b3e42
equal deleted inserted replaced
13071:b98e72d4089a 20470:77693555855f
1 /*
2 * gaim
3 *
4 * Gaim 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.
7 *
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.
12 *
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.
17 *
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 */
23
24 #include <stdlib.h>
25
26 #include "blist.h"
27 #include "debug.h"
28
29 #include "gtkwhiteboard.h"
30
31 /******************************************************************************
32 * Prototypes
33 *****************************************************************************/
34 static void gaim_gtk_whiteboard_create(GaimWhiteboard *wb);
35
36 static void gaim_gtk_whiteboard_destroy(GaimWhiteboard *wb);
37 static void gaim_gtk_whiteboard_exit(GtkWidget *widget, gpointer data);
38
39 /*static void gaim_gtkwhiteboard_button_start_press(GtkButton *button, gpointer data); */
40
41 static gboolean gaim_gtk_whiteboard_configure_event(GtkWidget *widget, GdkEventConfigure *event, gpointer data);
42 static gboolean gaim_gtk_whiteboard_expose_event(GtkWidget *widget, GdkEventExpose *event, gpointer data);
43
44 static gboolean gaim_gtk_whiteboard_brush_down(GtkWidget *widget, GdkEventButton *event, gpointer data);
45 static gboolean gaim_gtk_whiteboard_brush_motion(GtkWidget *widget, GdkEventMotion *event, gpointer data);
46 static gboolean gaim_gtk_whiteboard_brush_up(GtkWidget *widget, GdkEventButton *event, gpointer data);
47
48 static void gaim_gtk_whiteboard_draw_brush_point(GaimWhiteboard *wb,
49 int x, int y, int color, int size);
50 static void gaim_gtk_whiteboard_draw_brush_line(GaimWhiteboard *wb, int x0, int y0,
51 int x1, int y1, int color, int size);
52
53 static void gaim_gtk_whiteboard_set_dimensions(GaimWhiteboard *wb, int width, int height);
54 static void gaim_gtk_whiteboard_set_brush(GaimWhiteboard *wb, int size, int color);
55 static void gaim_gtk_whiteboard_clear(GaimWhiteboard *wb);
56
57 static void gaim_gtk_whiteboard_button_clear_press(GtkWidget *widget, gpointer data);
58 static void gaim_gtk_whiteboard_button_save_press(GtkWidget *widget, gpointer data);
59
60 static void gaim_gtk_whiteboard_set_canvas_as_icon(GaimGtkWhiteboard *gtkwb);
61
62 static void gaim_gtk_whiteboard_rgb24_to_rgb48(int color_rgb, GdkColor *color);
63
64 static void color_select_dialog(GtkWidget *widget, GaimGtkWhiteboard *gtkwb);
65
66 /******************************************************************************
67 * Globals
68 *****************************************************************************/
69 /*
70 GList *buttonList = NULL;
71 GdkColor DefaultColor[PALETTE_NUM_COLORS];
72 */
73
74 static gboolean LocalShutdownRequest;
75
76 static int LastX; /* Tracks last position of the mouse when drawing */
77 static int LastY;
78 static int MotionCount; /* Tracks how many brush motions made */
79 static int BrushState = BRUSH_STATE_UP;
80
81 static GaimWhiteboardUiOps ui_ops =
82 {
83 gaim_gtk_whiteboard_create,
84 gaim_gtk_whiteboard_destroy,
85 gaim_gtk_whiteboard_set_dimensions,
86 gaim_gtk_whiteboard_set_brush,
87 gaim_gtk_whiteboard_draw_brush_point,
88 gaim_gtk_whiteboard_draw_brush_line,
89 gaim_gtk_whiteboard_clear
90 };
91
92 /******************************************************************************
93 * API
94 *****************************************************************************/
95 GaimWhiteboardUiOps *gaim_gtk_whiteboard_get_ui_ops(void)
96 {
97 return &ui_ops;
98 }
99
100 void gaim_gtk_whiteboard_create(GaimWhiteboard *wb)
101 {
102 GtkWidget *window;
103 GtkWidget *drawing_area;
104 GtkWidget *vbox_controls;
105 GtkWidget *hbox_canvas_and_controls;
106
107 /*
108 --------------------------
109 |[][][][palette[][][][][]|
110 |------------------------|
111 | canvas | con |
112 | | trol|
113 | | s |
114 | | |
115 | | |
116 --------------------------
117 */
118 GtkWidget *clear_button;
119 GtkWidget *save_button;
120 GtkWidget *color_button;
121
122 GaimGtkWhiteboard *gtkwb = g_new0(GaimGtkWhiteboard, 1);
123
124 const char *window_title;
125
126 gtkwb->wb = wb;
127 wb->ui_data = gtkwb;
128
129 /* Get dimensions (default?) for the whiteboard canvas */
130 if (!gaim_whiteboard_get_dimensions(wb, &gtkwb->width, &gtkwb->height))
131 {
132 /* Give some initial board-size */
133 gtkwb->width = 300;
134 gtkwb->height = 250;
135 }
136
137 if (!gaim_whiteboard_get_brush(wb, &gtkwb->brush_size, &gtkwb->brush_color))
138 {
139 /* Give some initial brush-info */
140 gtkwb->brush_size = 2;
141 gtkwb->brush_color = 0xff0000;
142 }
143
144 window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
145 gtkwb->window = window;
146 gtk_widget_set_name(window, wb->who);
147
148 /* Try and set window title as the name of the buddy, else just use their
149 * username
150 */
151 window_title = gaim_contact_get_alias(gaim_buddy_get_contact(gaim_find_buddy(wb->account, wb->who)));
152 if(window_title)
153 gtk_window_set_title((GtkWindow*)(window), window_title);
154 else
155 gtk_window_set_title((GtkWindow*)(window), wb->who);
156
157 gtk_window_set_resizable((GtkWindow*)(window), FALSE);
158
159 g_signal_connect(G_OBJECT(window), "destroy",
160 G_CALLBACK(gaim_gtk_whiteboard_exit), gtkwb);
161
162 #if 0
163 int i;
164
165 GtkWidget *hbox_palette;
166 GtkWidget *vbox_palette_above_canvas_and_controls;
167 GtkWidget *palette_color_box[PALETTE_NUM_COLORS];
168
169 /* Create vertical box to place palette above the canvas and controls */
170 vbox_palette_above_canvas_and_controls = gtk_vbox_new(FALSE, 0);
171 gtk_container_add(GTK_CONTAINER(window), vbox_palette_above_canvas_and_controls);
172 gtk_widget_show(vbox_palette_above_canvas_and_controls);
173
174 /* Create horizontal box for the palette and all its entries */
175 hbox_palette = gtk_hbox_new(FALSE, 0);
176 gtk_box_pack_start(GTK_BOX(vbox_palette_above_canvas_and_controls),
177 hbox_palette, FALSE, FALSE, GAIM_HIG_BORDER);
178 gtk_widget_show(hbox_palette);
179
180 /* Create horizontal box to seperate the canvas from the controls */
181 hbox_canvas_and_controls = gtk_hbox_new(FALSE, 0);
182 gtk_box_pack_start(GTK_BOX(vbox_palette_above_canvas_and_controls),
183 hbox_canvas_and_controls, FALSE, FALSE, GAIM_HIG_BORDER);
184 gtk_widget_show(hbox_canvas_and_controls);
185
186 for(i = 0; i < PALETTE_NUM_COLORS; i++)
187 {
188 palette_color_box[i] = gtk_image_new_from_pixbuf(NULL);
189 gtk_widget_set_size_request(palette_color_box[i], gtkwb->width / PALETTE_NUM_COLORS ,32);
190 gtk_container_add(GTK_CONTAINER(hbox_palette), palette_color_box[i]);
191
192 gtk_widget_show(palette_color_box[i]);
193 }
194 #endif
195
196 hbox_canvas_and_controls = gtk_hbox_new(FALSE, 0);
197 gtk_widget_show(hbox_canvas_and_controls);
198
199 gtk_container_add(GTK_CONTAINER(window), hbox_canvas_and_controls);
200 gtk_container_set_border_width(GTK_CONTAINER(window), GAIM_HIG_BORDER);
201
202 /* Create the drawing area */
203 drawing_area = gtk_drawing_area_new();
204 gtkwb->drawing_area = drawing_area;
205 gtk_widget_set_size_request(GTK_WIDGET(drawing_area), gtkwb->width, gtkwb->height);
206 gtk_box_pack_start(GTK_BOX(hbox_canvas_and_controls), drawing_area, TRUE, TRUE, GAIM_HIG_BOX_SPACE);
207
208 gtk_widget_show(drawing_area);
209
210 /* Signals used to handle backing pixmap */
211 g_signal_connect(G_OBJECT(drawing_area), "expose_event",
212 G_CALLBACK(gaim_gtk_whiteboard_expose_event), gtkwb);
213
214 g_signal_connect(G_OBJECT(drawing_area), "configure_event",
215 G_CALLBACK(gaim_gtk_whiteboard_configure_event), gtkwb);
216
217 /* Event signals */
218 g_signal_connect(G_OBJECT(drawing_area), "button_press_event",
219 G_CALLBACK(gaim_gtk_whiteboard_brush_down), gtkwb);
220
221 g_signal_connect(G_OBJECT(drawing_area), "motion_notify_event",
222 G_CALLBACK(gaim_gtk_whiteboard_brush_motion), gtkwb);
223
224 g_signal_connect(G_OBJECT(drawing_area), "button_release_event",
225 G_CALLBACK(gaim_gtk_whiteboard_brush_up), gtkwb);
226
227 gtk_widget_set_events(drawing_area,
228 GDK_EXPOSURE_MASK |
229 GDK_LEAVE_NOTIFY_MASK |
230 GDK_BUTTON_PRESS_MASK |
231 GDK_POINTER_MOTION_MASK |
232 GDK_BUTTON_RELEASE_MASK |
233 GDK_POINTER_MOTION_HINT_MASK);
234
235 /* Create vertical box to contain the controls */
236 vbox_controls = gtk_vbox_new(FALSE, 0);
237 gtk_box_pack_start(GTK_BOX(hbox_canvas_and_controls),
238 vbox_controls, FALSE, FALSE, GAIM_HIG_BOX_SPACE);
239 gtk_widget_show(vbox_controls);
240
241 /* Add a clear button */
242 clear_button = gtk_button_new_from_stock(GTK_STOCK_CLEAR);
243 gtk_box_pack_start(GTK_BOX(vbox_controls), clear_button, FALSE, FALSE, GAIM_HIG_BOX_SPACE);
244 gtk_widget_show(clear_button);
245 g_signal_connect(G_OBJECT(clear_button), "clicked",
246 G_CALLBACK(gaim_gtk_whiteboard_button_clear_press), gtkwb);
247
248 /* Add a save button */
249 save_button = gtk_button_new_from_stock(GTK_STOCK_SAVE);
250 gtk_box_pack_start(GTK_BOX(vbox_controls), save_button, FALSE, FALSE, GAIM_HIG_BOX_SPACE);
251 gtk_widget_show(save_button);
252
253 g_signal_connect(G_OBJECT(save_button), "clicked",
254 G_CALLBACK(gaim_gtk_whiteboard_button_save_press), gtkwb);
255
256 /* Add a color selector */
257 color_button = gtk_button_new_from_stock(GTK_STOCK_SELECT_COLOR);
258 gtk_box_pack_start(GTK_BOX(vbox_controls), color_button, FALSE, FALSE, GAIM_HIG_BOX_SPACE);
259 gtk_widget_show(color_button);
260 g_signal_connect(G_OBJECT(color_button), "clicked",
261 G_CALLBACK(color_select_dialog), gtkwb);
262
263 /* Make all this (window) visible */
264 gtk_widget_show(window);
265
266 gaim_gtk_whiteboard_set_canvas_as_icon(gtkwb);
267
268 /* TODO Specific protocol/whiteboard assignment here? Needs a UI Op? */
269 /* Set default brush size and color */
270 /*
271 ds->brush_size = DOODLE_BRUSH_MEDIUM;
272 ds->brush_color = 0;
273 */
274 }
275
276 void gaim_gtk_whiteboard_destroy(GaimWhiteboard *wb)
277 {
278 GaimGtkWhiteboard *gtkwb = wb->ui_data;
279
280 /* TODO Ask if user wants to save picture before the session is closed */
281
282 /* Clear graphical memory */
283 if(gtkwb->pixmap)
284 {
285 g_object_unref(gtkwb->pixmap);
286 gtkwb->pixmap = NULL;
287 }
288
289 if(gtkwb->window)
290 {
291 gtk_widget_destroy(gtkwb->window);
292 gtkwb->window = NULL;
293 }
294 }
295
296 void gaim_gtk_whiteboard_exit(GtkWidget *widget, gpointer data)
297 {
298 GaimGtkWhiteboard *gtkwb = (GaimGtkWhiteboard*)data;
299 GaimWhiteboard *wb = gtkwb->wb;
300
301 if(gtkwb->window && gtkwb->pixmap)
302 {
303 LocalShutdownRequest = TRUE;
304
305 gaim_gtk_whiteboard_destroy(wb);
306 }
307 else
308 LocalShutdownRequest = FALSE;
309
310 if(gtkwb)
311 {
312 g_free(gtkwb);
313
314 wb->ui_data = NULL;
315 }
316
317 /* Destroy whiteboard core, if the local user exited the whiteboard window */
318 if(wb && LocalShutdownRequest)
319 {
320 gaim_whiteboard_destroy(wb);
321 }
322 }
323
324 /*
325 * Whiteboard start button on conversation window (move this code to gtkconv?
326 * and use new prpl_info member?)
327 */
328 #if 0
329 void gaim_gtkwhiteboard_button_start_press(GtkButton *button, gpointer data)
330 {
331 GaimConversation *conv = data;
332 GaimAccount *account = gaim_conversation_get_account(conv);
333 GaimConnection *gc = gaim_account_get_connection(account);
334 char *to = (char*)(gaim_conversation_get_name(conv));
335
336 /* Only handle this if local client requested Doodle session (else local
337 * client would have sent one)
338 */
339 GaimWhiteboard *wb = gaim_whiteboard_get(account, to);
340
341 /* Write a local message to this conversation showing that a request for a
342 * Doodle session has been made
343 */
344 gaim_conv_im_write(GAIM_CONV_IM(conv), "", _("Sent Doodle request."),
345 GAIM_MESSAGE_NICK | GAIM_MESSAGE_RECV, time(NULL));
346
347 yahoo_doodle_command_send_request(gc, to);
348 yahoo_doodle_command_send_ready(gc, to);
349
350 /* Insert this 'session' in the list. At this point, it's only a requested
351 * session.
352 */
353 wb = gaim_whiteboard_create(account, to, DOODLE_STATE_REQUESTING);
354 }
355 #endif
356
357 gboolean gaim_gtk_whiteboard_configure_event(GtkWidget *widget, GdkEventConfigure *event, gpointer data)
358 {
359 GaimGtkWhiteboard *gtkwb = (GaimGtkWhiteboard*)data;
360
361 GdkPixmap *pixmap = gtkwb->pixmap;
362
363 if(pixmap)
364 g_object_unref(pixmap);
365
366 pixmap = gdk_pixmap_new(widget->window,
367 widget->allocation.width,
368 widget->allocation.height,
369 -1);
370
371 gtkwb->pixmap = pixmap;
372
373 gdk_draw_rectangle(pixmap,
374 widget->style->white_gc,
375 TRUE,
376 0, 0,
377 widget->allocation.width,
378 widget->allocation.height);
379
380 return TRUE;
381 }
382
383 gboolean gaim_gtk_whiteboard_expose_event(GtkWidget *widget, GdkEventExpose *event, gpointer data)
384 {
385 GaimGtkWhiteboard *gtkwb = (GaimGtkWhiteboard*)(data);
386 GdkPixmap *pixmap = gtkwb->pixmap;
387
388 gdk_draw_drawable(widget->window,
389 widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
390 pixmap,
391 event->area.x, event->area.y,
392 event->area.x, event->area.y,
393 event->area.width, event->area.height);
394
395 return FALSE;
396 }
397
398 gboolean gaim_gtk_whiteboard_brush_down(GtkWidget *widget, GdkEventButton *event, gpointer data)
399 {
400 GaimGtkWhiteboard *gtkwb = (GaimGtkWhiteboard*)data;
401 GdkPixmap *pixmap = gtkwb->pixmap;
402
403 GaimWhiteboard *wb = gtkwb->wb;
404 GList *draw_list = wb->draw_list;
405
406 if(BrushState != BRUSH_STATE_UP)
407 {
408 /* Potential double-click DOWN to DOWN? */
409 BrushState = BRUSH_STATE_DOWN;
410
411 /* return FALSE; */
412 }
413
414 BrushState = BRUSH_STATE_DOWN;
415
416 if(event->button == 1 && pixmap != NULL)
417 {
418 /* Check if draw_list has contents; if so, clear it */
419 if(draw_list)
420 {
421 gaim_whiteboard_draw_list_destroy(draw_list);
422 draw_list = NULL;
423 }
424
425 /* Set tracking variables */
426 LastX = event->x;
427 LastY = event->y;
428
429 MotionCount = 0;
430
431 draw_list = g_list_append(draw_list, GINT_TO_POINTER(LastX));
432 draw_list = g_list_append(draw_list, GINT_TO_POINTER(LastY));
433
434 gaim_gtk_whiteboard_draw_brush_point(gtkwb->wb,
435 event->x, event->y,
436 gtkwb->brush_color, gtkwb->brush_size);
437 }
438
439 wb->draw_list = draw_list;
440
441 return TRUE;
442 }
443
444 gboolean gaim_gtk_whiteboard_brush_motion(GtkWidget *widget, GdkEventMotion *event, gpointer data)
445 {
446 int x;
447 int y;
448 int dx;
449 int dy;
450
451 GdkModifierType state;
452
453 GaimGtkWhiteboard *gtkwb = (GaimGtkWhiteboard*)data;
454 GdkPixmap *pixmap = gtkwb->pixmap;
455
456 GaimWhiteboard *wb = gtkwb->wb;
457 GList *draw_list = wb->draw_list;
458
459 if(event->is_hint)
460 gdk_window_get_pointer(event->window, &x, &y, &state);
461 else
462 {
463 x = event->x;
464 y = event->y;
465 state = event->state;
466 }
467
468 if(state & GDK_BUTTON1_MASK && pixmap != NULL)
469 {
470 if((BrushState != BRUSH_STATE_DOWN) && (BrushState != BRUSH_STATE_MOTION))
471 {
472 gaim_debug_error("gtkwhiteboard", "***Bad brush state transition %d to MOTION\n", BrushState);
473
474 BrushState = BRUSH_STATE_MOTION;
475
476 return FALSE;
477 }
478 BrushState = BRUSH_STATE_MOTION;
479
480 dx = x - LastX;
481 dy = y - LastY;
482
483 MotionCount++;
484
485 /* NOTE 100 is a temporary constant for how many deltas/motions in a
486 * stroke (needs UI Ops?)
487 */
488 if(MotionCount == 100)
489 {
490 draw_list = g_list_append(draw_list, GINT_TO_POINTER(dx));
491 draw_list = g_list_append(draw_list, GINT_TO_POINTER(dy));
492
493 /* Send draw list to the draw_list handler */
494 gaim_whiteboard_send_draw_list(gtkwb->wb, draw_list);
495
496 /* The brush stroke is finished, clear the list for another one */
497 if(draw_list)
498 {
499 gaim_whiteboard_draw_list_destroy(draw_list);
500 draw_list = NULL;
501 }
502
503 /* Reset motion tracking */
504 MotionCount = 0;
505
506 draw_list = g_list_append(draw_list, GINT_TO_POINTER(LastX));
507 draw_list = g_list_append(draw_list, GINT_TO_POINTER(LastY));
508
509 dx = x - LastX;
510 dy = y - LastY;
511 }
512
513 draw_list = g_list_append(draw_list, GINT_TO_POINTER(dx));
514 draw_list = g_list_append(draw_list, GINT_TO_POINTER(dy));
515
516 gaim_gtk_whiteboard_draw_brush_line(gtkwb->wb,
517 LastX, LastY,
518 x, y,
519 gtkwb->brush_color, gtkwb->brush_size);
520
521 /* Set tracking variables */
522 LastX = x;
523 LastY = y;
524 }
525
526 wb->draw_list = draw_list;
527
528 return TRUE;
529 }
530
531 gboolean gaim_gtk_whiteboard_brush_up(GtkWidget *widget, GdkEventButton *event, gpointer data)
532 {
533 GaimGtkWhiteboard *gtkwb = (GaimGtkWhiteboard*)data;
534 GdkPixmap *pixmap = gtkwb->pixmap;
535
536 GaimWhiteboard *wb = gtkwb->wb;
537 GList *draw_list = wb->draw_list;
538
539 if((BrushState != BRUSH_STATE_DOWN) && (BrushState != BRUSH_STATE_MOTION))
540 {
541 gaim_debug_error("gtkwhiteboard", "***Bad brush state transition %d to UP\n", BrushState);
542
543 BrushState = BRUSH_STATE_UP;
544
545 return FALSE;
546 }
547 BrushState = BRUSH_STATE_UP;
548
549 if(event->button == 1 && pixmap != NULL)
550 {
551 /* If the brush was never moved, express two sets of two deltas That's a
552 * 'point,' but not for Yahoo!
553 */
554 /* if((event->x == LastX) && (event->y == LastY)) */
555 if(MotionCount == 0)
556 {
557 int index;
558
559 /* For Yahoo!, a (0 0) indicates the end of drawing */
560 /* FIXME: Yahoo Doodle specific! */
561 for(index = 0; index < 2; index++)
562 {
563 draw_list = g_list_append(draw_list, 0);
564 draw_list = g_list_append(draw_list, 0);
565 }
566 }
567 /*
568 else
569 MotionCount = 0;
570 */
571
572 /* Send draw list to prpl draw_list handler */
573 gaim_whiteboard_send_draw_list(gtkwb->wb, draw_list);
574
575 gaim_gtk_whiteboard_set_canvas_as_icon(gtkwb);
576
577 /* The brush stroke is finished, clear the list for another one */
578 if(draw_list)
579 gaim_whiteboard_draw_list_destroy(draw_list);
580
581 wb->draw_list = NULL;
582 }
583
584 return TRUE;
585 }
586
587 void gaim_gtk_whiteboard_draw_brush_point(GaimWhiteboard *wb, int x, int y, int color, int size)
588 {
589 GaimGtkWhiteboard *gtkwb = wb->ui_data;
590 GtkWidget *widget = gtkwb->drawing_area;
591 GdkPixmap *pixmap = gtkwb->pixmap;
592
593 GdkRectangle update_rect;
594
595 GdkGC *gfx_con = gdk_gc_new(pixmap);
596 GdkColor col;
597
598 update_rect.x = x - size / 2;
599 update_rect.y = y - size / 2;
600 update_rect.width = size;
601 update_rect.height = size;
602
603 /* Interpret and convert color */
604 gaim_gtk_whiteboard_rgb24_to_rgb48(color, &col);
605
606 gdk_gc_set_rgb_fg_color(gfx_con, &col);
607 /* gdk_gc_set_rgb_bg_color(gfx_con, &col); */
608
609 /* NOTE 5 is a size constant for now... this is because of how poorly the
610 * gdk_draw_arc draws small circles
611 */
612 if(size < 5)
613 {
614 /* Draw a rectangle/square */
615 gdk_draw_rectangle(pixmap,
616 gfx_con,
617 TRUE,
618 update_rect.x, update_rect.y,
619 update_rect.width, update_rect.height);
620 }
621 else
622 {
623 /* Draw a circle */
624 gdk_draw_arc(pixmap,
625 gfx_con,
626 TRUE,
627 update_rect.x, update_rect.y,
628 update_rect.width, update_rect.height,
629 0, FULL_CIRCLE_DEGREES);
630 }
631
632 gtk_widget_queue_draw_area(widget,
633 update_rect.x, update_rect.y,
634 update_rect.width, update_rect.height);
635
636 gdk_gc_unref(gfx_con);
637 }
638
639 /* Uses Bresenham's algorithm (as provided by Wikipedia) */
640 void gaim_gtk_whiteboard_draw_brush_line(GaimWhiteboard *wb, int x0, int y0, int x1, int y1, int color, int size)
641 {
642 int temp;
643
644 int xstep;
645 int ystep;
646
647 int dx;
648 int dy;
649
650 int error;
651 int derror;
652
653 int x;
654 int y;
655
656 gboolean steep = abs(y1 - y0) > abs(x1 - x0);
657
658 if(steep)
659 {
660 temp = x0; x0 = y0; y0 = temp;
661 temp = x1; x1 = y1; y1 = temp;
662 }
663
664 dx = abs(x1 - x0);
665 dy = abs(y1 - y0);
666
667 error = 0;
668 derror = dy;
669
670 x = x0;
671 y = y0;
672
673 if(x0 < x1)
674 xstep = 1;
675 else
676 xstep = -1;
677
678 if(y0 < y1)
679 ystep = 1;
680 else
681 ystep = -1;
682
683 if(steep)
684 gaim_gtk_whiteboard_draw_brush_point(wb, y, x, color, size);
685 else
686 gaim_gtk_whiteboard_draw_brush_point(wb, x, y, color, size);
687
688 while(x != x1)
689 {
690 x += xstep;
691 error += derror;
692
693 if((error * 2) >= dx)
694 {
695 y += ystep;
696 error -= dx;
697 }
698
699 if(steep)
700 gaim_gtk_whiteboard_draw_brush_point(wb, y, x, color, size);
701 else
702 gaim_gtk_whiteboard_draw_brush_point(wb, x, y, color, size);
703 }
704 }
705
706 void gaim_gtk_whiteboard_set_dimensions(GaimWhiteboard *wb, int width, int height)
707 {
708 GaimGtkWhiteboard *gtkwb = wb->ui_data;
709
710 gtkwb->width = width;
711 gtkwb->height = height;
712 }
713
714 void gaim_gtk_whiteboard_set_brush(GaimWhiteboard *wb, int size, int color)
715 {
716 GaimGtkWhiteboard *gtkwb = wb->ui_data;
717
718 gtkwb->brush_size = size;
719 gtkwb->brush_color = color;
720 }
721
722 void gaim_gtk_whiteboard_clear(GaimWhiteboard *wb)
723 {
724 GaimGtkWhiteboard *gtkwb = wb->ui_data;
725 GdkPixmap *pixmap = gtkwb->pixmap;
726 GtkWidget *drawing_area = gtkwb->drawing_area;
727
728 gdk_draw_rectangle(pixmap,
729 drawing_area->style->white_gc,
730 TRUE,
731 0, 0,
732 drawing_area->allocation.width,
733 drawing_area->allocation.height);
734
735 gtk_widget_queue_draw_area(drawing_area,
736 0, 0,
737 drawing_area->allocation.width,
738 drawing_area->allocation.height);
739 }
740
741 void gaim_gtk_whiteboard_button_clear_press(GtkWidget *widget, gpointer data)
742 {
743 GaimGtkWhiteboard *gtkwb = (GaimGtkWhiteboard*)(data);
744
745 gaim_gtk_whiteboard_clear(gtkwb->wb);
746
747 gaim_gtk_whiteboard_set_canvas_as_icon(gtkwb);
748
749 /* Do protocol specific clearing procedures */
750 gaim_whiteboard_send_clear(gtkwb->wb);
751 }
752
753 void gaim_gtk_whiteboard_button_save_press(GtkWidget *widget, gpointer data)
754 {
755 GaimGtkWhiteboard *gtkwb = (GaimGtkWhiteboard*)(data);
756 GdkPixbuf *pixbuf;
757
758 GtkWidget *dialog;
759
760 int result;
761
762 #if GTK_CHECK_VERSION(2,4,0) /* FILECHOOSER */
763 dialog = gtk_file_chooser_dialog_new ("Save File",
764 GTK_WINDOW(gtkwb->window),
765 GTK_FILE_CHOOSER_ACTION_SAVE,
766 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
767 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
768 NULL);
769
770 /* gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(dialog), (gboolean)(TRUE)); */
771
772 /* if(user_edited_a_new_document) */
773 {
774 /* gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), default_folder_for_saving); */
775 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), "whiteboard.jpg");
776 }
777 /*
778 else
779 gtk_file_chooser_set_filename (GTK_FILE_CHOOSER (dialog), filename_for_existing_document);
780 */
781 #else
782 dialog = gtk_file_selection_new("Save File");
783 gtk_file_selection_set_filename(GTK_FILE_SELECTION(dialog), "whiteboard.jpg");
784 #endif
785 result = gtk_dialog_run(GTK_DIALOG(dialog));
786
787 if(result == GTK_RESPONSE_ACCEPT)
788 {
789 char *filename;
790
791 #if GTK_CHECK_VERSION(2,4,0) /* FILECHOOSER */
792 filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
793 #else
794 filename = g_strdup(gtk_file_selection_get_filename(GTK_FILE_SELECTION(dialog)));
795 #endif
796 gtk_widget_destroy(dialog);
797
798 /* Makes an icon from the whiteboard's canvas 'image' */
799 pixbuf = gdk_pixbuf_get_from_drawable(NULL,
800 (GdkDrawable*)(gtkwb->pixmap),
801 gdk_drawable_get_colormap(gtkwb->pixmap),
802 0, 0,
803 0, 0,
804 gtkwb->width, gtkwb->height);
805
806 if(gdk_pixbuf_save(pixbuf, filename, "jpeg", NULL, "quality", "100", NULL))
807 gaim_debug_info("gtkwhiteboard", "File Saved...\n");
808 else
809 gaim_debug_info("gtkwhiteboard", "File not Saved... Error\n");
810 g_free(filename);
811 }
812 else if(result == GTK_RESPONSE_CANCEL)
813 {
814 gtk_widget_destroy(dialog);
815
816 gaim_debug_info("gtkwhiteboard", "File not Saved... Canceled\n");
817 }
818 }
819
820 void gaim_gtk_whiteboard_set_canvas_as_icon(GaimGtkWhiteboard *gtkwb)
821 {
822 GdkPixbuf *pixbuf;
823
824 /* Makes an icon from the whiteboard's canvas 'image' */
825 pixbuf = gdk_pixbuf_get_from_drawable(NULL,
826 (GdkDrawable*)(gtkwb->pixmap),
827 gdk_drawable_get_colormap(gtkwb->pixmap),
828 0, 0,
829 0, 0,
830 gtkwb->width, gtkwb->height);
831
832 gtk_window_set_icon((GtkWindow*)(gtkwb->window), pixbuf);
833 }
834
835 void gaim_gtk_whiteboard_rgb24_to_rgb48(int color_rgb, GdkColor *color)
836 {
837 color->red = (color_rgb >> 8) | 0xFF;
838 color->green = (color_rgb & 0xFF00) | 0xFF;
839 color->blue = ((color_rgb & 0xFF) << 8) | 0xFF;
840 }
841
842 static void
843 change_color_cb(GtkColorSelection *selection, GaimGtkWhiteboard *gtkwb)
844 {
845 GdkColor color;
846 int old_size = 5;
847 int old_color = 0;
848 int new_color;
849 GaimWhiteboard *wb = gtkwb->wb;
850
851 gtk_color_selection_get_current_color(selection, &color);
852 new_color = (color.red & 0xFF00) << 8;
853 new_color |= (color.green & 0xFF00);
854 new_color |= (color.blue & 0xFF00) >> 8;
855
856 gaim_whiteboard_get_brush(wb, &old_size, &old_color);
857 gaim_whiteboard_send_brush(wb, old_size, new_color);
858 }
859
860 static void color_selection_dialog_destroy(GtkWidget *w, GtkWidget *destroy)
861 {
862 gtk_widget_destroy(destroy);
863 }
864
865 static void color_select_dialog(GtkWidget *widget, GaimGtkWhiteboard *gtkwb)
866 {
867 GdkColor color;
868 GtkColorSelectionDialog *dialog;
869
870 dialog = (GtkColorSelectionDialog *)gtk_color_selection_dialog_new("Select color");
871
872 g_signal_connect(G_OBJECT(dialog->colorsel), "color-changed",
873 G_CALLBACK(change_color_cb), gtkwb);
874
875 gtk_widget_destroy(dialog->cancel_button);
876 gtk_widget_destroy(dialog->help_button);
877
878 g_signal_connect(G_OBJECT(dialog->ok_button), "clicked",
879 G_CALLBACK(color_selection_dialog_destroy), dialog);
880
881 gtk_color_selection_set_has_palette(GTK_COLOR_SELECTION(dialog->colorsel), TRUE);
882
883 gaim_gtk_whiteboard_rgb24_to_rgb48(gtkwb->brush_color, &color);
884 gtk_color_selection_set_current_color(GTK_COLOR_SELECTION(dialog->colorsel), &color);
885
886 gtk_widget_show_all(GTK_WIDGET(dialog));
887 }
888

mercurial