1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
|
/*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*
*
* Authors:
* Chris Lahey <clahey@ximian.com>
*
* Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
*
*/
#include "e-canvas.h"
#include <config.h>
#include <gtk/gtk.h>
#include <libedataserver/libedataserver.h>
#define d(x)
enum {
REFLOW,
LAST_SIGNAL
};
static guint signals[LAST_SIGNAL];
G_DEFINE_TYPE (
ECanvas,
e_canvas,
GNOME_TYPE_CANVAS)
/* Emits an event for an item in the canvas, be it the current
* item, grabbed item, or focused item, as appropriate. */
static gint
canvas_emit_event (GnomeCanvas *canvas,
GdkEvent *event)
{
GdkEvent *ev;
gint finished;
GnomeCanvasItem *item;
GnomeCanvasItem *parent;
guint mask;
/* Choose where we send the event */
item = canvas->current_item;
if (canvas->focused_item &&
((event->type == GDK_KEY_PRESS) ||
(event->type == GDK_KEY_RELEASE) ||
(event->type == GDK_FOCUS_CHANGE)))
item = canvas->focused_item;
if (canvas->grabbed_item)
item = canvas->grabbed_item;
/* Perform checks for grabbed items */
if (canvas->grabbed_item) {
switch (event->type) {
case GDK_ENTER_NOTIFY:
mask = GDK_ENTER_NOTIFY_MASK;
break;
case GDK_LEAVE_NOTIFY:
mask = GDK_LEAVE_NOTIFY_MASK;
break;
case GDK_MOTION_NOTIFY:
mask = GDK_POINTER_MOTION_MASK;
break;
case GDK_BUTTON_PRESS:
case GDK_2BUTTON_PRESS:
case GDK_3BUTTON_PRESS:
mask = GDK_BUTTON_PRESS_MASK;
break;
case GDK_BUTTON_RELEASE:
mask = GDK_BUTTON_RELEASE_MASK;
break;
case GDK_KEY_PRESS:
mask = GDK_KEY_PRESS_MASK;
break;
case GDK_KEY_RELEASE:
mask = GDK_KEY_RELEASE_MASK;
break;
default:
mask = 0;
break;
}
if (!(mask & canvas->grabbed_event_mask))
return FALSE;
}
/* Convert to world coordinates -- we have two cases because of
* different offsets of the fields in the event structures. */
ev = gdk_event_copy (event);
switch (ev->type) {
case GDK_ENTER_NOTIFY:
case GDK_LEAVE_NOTIFY:
gnome_canvas_window_to_world (
canvas,
ev->crossing.x, ev->crossing.y,
&ev->crossing.x, &ev->crossing.y);
break;
case GDK_MOTION_NOTIFY:
case GDK_BUTTON_PRESS:
case GDK_2BUTTON_PRESS:
case GDK_3BUTTON_PRESS:
case GDK_BUTTON_RELEASE:
gnome_canvas_window_to_world (
canvas,
ev->motion.x, ev->motion.y,
&ev->motion.x, &ev->motion.y);
break;
default:
break;
}
/* The event is propagated up the hierarchy (for if someone connected
* to a group instead of a leaf event), and emission is stopped if a
* handler returns TRUE, just like for GtkWidget events. */
finished = FALSE;
while (item && !finished) {
g_object_ref (item);
g_signal_emit_by_name (item, "event", ev, &finished);
parent = item->parent;
g_object_unref (item);
item = parent;
}
gdk_event_free (ev);
return finished;
}
/* This routine invokes the point method of the item. The argument x, y
* should be in the parent's item-relative coordinate system. This routine
* applies the inverse of the item's transform, maintaining the affine
* invariant. */
static GnomeCanvasItem *
gnome_canvas_item_invoke_point (GnomeCanvasItem *item,
gdouble x,
gdouble y,
gint cx,
gint cy)
{
cairo_matrix_t inverse;
/* Calculate x & y in item local coordinates */
inverse = item->matrix;
if (cairo_matrix_invert (&inverse) != CAIRO_STATUS_SUCCESS)
return NULL;
cairo_matrix_transform_point (&inverse, &x, &y);
if (GNOME_CANVAS_ITEM_GET_CLASS (item)->point)
return GNOME_CANVAS_ITEM_GET_CLASS (item)->point (item, x, y, cx, cy);
return NULL;
}
/* Re-picks the current item in the canvas, based on the event's coordinates.
* Also emits enter/leave events for items as appropriate.
*/
#define DISPLAY_X1(canvas) (GNOME_CANVAS (canvas)->layout.xoffset)
#define DISPLAY_Y1(canvas) (GNOME_CANVAS (canvas)->layout.yoffset)
static gint
pick_current_item (GnomeCanvas *canvas,
GdkEvent *event)
{
gint button_down;
gdouble x, y;
gint cx, cy;
gint retval;
retval = FALSE;
/* If a button is down, we'll perform enter and leave events on the
* current item, but not enter on any other item. This is more or less
* like X pointer grabbing for canvas items.
*/
button_down = canvas->state & (GDK_BUTTON1_MASK
| GDK_BUTTON2_MASK
| GDK_BUTTON3_MASK
| GDK_BUTTON4_MASK
| GDK_BUTTON5_MASK);
if (!button_down)
canvas->left_grabbed_item = FALSE;
/* Save the event in the canvas. This is used to synthesize enter and
* leave events in case the current item changes. It is also used to
* re-pick the current item if the current one gets deleted. Also,
* synthesize an enter event.
*/
if (event != &canvas->pick_event) {
if ((event->type == GDK_MOTION_NOTIFY) ||
(event->type == GDK_BUTTON_RELEASE)) {
/* these fields have the same offsets in both types of events */
canvas->pick_event.crossing.type = GDK_ENTER_NOTIFY;
canvas->pick_event.crossing.window = event->motion.window;
canvas->pick_event.crossing.send_event = event->motion.send_event;
canvas->pick_event.crossing.subwindow = NULL;
canvas->pick_event.crossing.x = event->motion.x;
canvas->pick_event.crossing.y = event->motion.y;
canvas->pick_event.crossing.mode = GDK_CROSSING_NORMAL;
canvas->pick_event.crossing.detail = GDK_NOTIFY_NONLINEAR;
canvas->pick_event.crossing.focus = FALSE;
canvas->pick_event.crossing.state = event->motion.state;
/* these fields don't have the same offsets in both types of events */
if (event->type == GDK_MOTION_NOTIFY) {
canvas->pick_event.crossing.x_root = event->motion.x_root;
canvas->pick_event.crossing.y_root = event->motion.y_root;
} else {
canvas->pick_event.crossing.x_root = event->button.x_root;
canvas->pick_event.crossing.y_root = event->button.y_root;
}
} else
canvas->pick_event = *event;
}
/* Don't do anything else if this is a recursive call */
if (canvas->in_repick)
return retval;
/* LeaveNotify means that there is no current item, so we don't look for one */
if (canvas->pick_event.type != GDK_LEAVE_NOTIFY) {
/* these fields don't have the same offsets in both types of events */
if (canvas->pick_event.type == GDK_ENTER_NOTIFY) {
x = canvas->pick_event.crossing.x +
canvas->scroll_x1 - canvas->zoom_xofs;
y = canvas->pick_event.crossing.y +
canvas->scroll_y1 - canvas->zoom_yofs;
} else {
x = canvas->pick_event.motion.x +
canvas->scroll_x1 - canvas->zoom_xofs;
y = canvas->pick_event.motion.y +
canvas->scroll_y1 - canvas->zoom_yofs;
}
/* canvas pixel coords */
cx = (gint) (x + 0.5);
cy = (gint) (y + 0.5);
/* world coords */
x = canvas->scroll_x1 + x;
y = canvas->scroll_y1 + y;
/* find the closest item */
if (canvas->root->flags & GNOME_CANVAS_ITEM_VISIBLE)
canvas->new_current_item =
gnome_canvas_item_invoke_point (
canvas->root, x, y, cx, cy);
else
canvas->new_current_item = NULL;
} else
canvas->new_current_item = NULL;
if ((canvas->new_current_item == canvas->current_item) &&
!canvas->left_grabbed_item)
return retval; /* current item did not change */
/* Synthesize events for old and new current items */
if ((canvas->new_current_item != canvas->current_item)
&& (canvas->current_item != NULL)
&& !canvas->left_grabbed_item) {
GdkEvent new_event = { 0 };
new_event = canvas->pick_event;
new_event.type = GDK_LEAVE_NOTIFY;
new_event.crossing.detail = GDK_NOTIFY_ANCESTOR;
new_event.crossing.subwindow = NULL;
canvas->in_repick = TRUE;
retval = canvas_emit_event (canvas, &new_event);
canvas->in_repick = FALSE;
}
/* new_current_item may have been set to NULL during
* the call to canvas_emit_event() above. */
if ((canvas->new_current_item != canvas->current_item) && button_down) {
canvas->left_grabbed_item = TRUE;
return retval;
}
/* Handle the rest of cases */
canvas->left_grabbed_item = FALSE;
canvas->current_item = canvas->new_current_item;
if (canvas->current_item != NULL) {
GdkEvent new_event = { 0 };
new_event = canvas->pick_event;
new_event.type = GDK_ENTER_NOTIFY;
new_event.crossing.detail = GDK_NOTIFY_ANCESTOR;
new_event.crossing.subwindow = NULL;
retval = canvas_emit_event (canvas, &new_event);
}
return retval;
}
static void
canvas_style_set_recursive (GnomeCanvasItem *item,
GtkStyle *previous_style)
{
guint signal_id = g_signal_lookup ("style_set", G_OBJECT_TYPE (item));
if (signal_id >= 1) {
GSignalQuery query;
g_signal_query (signal_id, &query);
if (query.return_type == G_TYPE_NONE &&
query.n_params == 1 &&
query.param_types[0] == GTK_TYPE_STYLE) {
g_signal_emit (item, signal_id, 0, previous_style);
}
}
if (GNOME_IS_CANVAS_GROUP (item)) {
GList *items = GNOME_CANVAS_GROUP (item)->item_list;
for (; items; items = items->next)
canvas_style_set_recursive (
items->data, previous_style);
}
}
static void
canvas_dispose (GObject *object)
{
ECanvas *canvas = E_CANVAS (object);
if (canvas->idle_id)
g_source_remove (canvas->idle_id);
canvas->idle_id = 0;
if (canvas->grab_cancelled_check_id)
g_source_remove (canvas->grab_cancelled_check_id);
canvas->grab_cancelled_check_id = 0;
if (canvas->toplevel) {
if (canvas->visibility_notify_id)
g_signal_handler_disconnect (
canvas->toplevel,
canvas->visibility_notify_id);
canvas->visibility_notify_id = 0;
g_object_unref (canvas->toplevel);
canvas->toplevel = NULL;
}
if (canvas->im_context) {
g_object_unref (canvas->im_context);
canvas->im_context = NULL;
}
/* Chain up to parent's dispose() method. */
G_OBJECT_CLASS (e_canvas_parent_class)->dispose (object);
}
static void
canvas_realize (GtkWidget *widget)
{
ECanvas *ecanvas = E_CANVAS (widget);
GdkWindow *window;
/* Chain up to parent's realize() method. */
GTK_WIDGET_CLASS (e_canvas_parent_class)->realize (widget);
window = gtk_layout_get_bin_window (GTK_LAYOUT (widget));
gdk_window_set_background_pattern (window, NULL);
window = gtk_widget_get_window (widget);
gtk_im_context_set_client_window (ecanvas->im_context, window);
}
static void
canvas_unrealize (GtkWidget *widget)
{
ECanvas * ecanvas = E_CANVAS (widget);
if (ecanvas->idle_id) {
g_source_remove (ecanvas->idle_id);
ecanvas->idle_id = 0;
}
gtk_im_context_set_client_window (ecanvas->im_context, NULL);
/* Chain up to parent's unrealize() method. */
GTK_WIDGET_CLASS (e_canvas_parent_class)->unrealize (widget);
}
static void
canvas_style_set (GtkWidget *widget,
GtkStyle *previous_style)
{
canvas_style_set_recursive (
GNOME_CANVAS_ITEM (gnome_canvas_root (
GNOME_CANVAS (widget))), previous_style);
}
static gint
canvas_button_event (GtkWidget *widget,
GdkEventButton *event)
{
GnomeCanvas *canvas;
GdkWindow *bin_window;
gint mask;
gint retval;
g_return_val_if_fail (GNOME_IS_CANVAS (widget), FALSE);
g_return_val_if_fail (event != NULL, FALSE);
retval = FALSE;
canvas = GNOME_CANVAS (widget);
bin_window = gtk_layout_get_bin_window (GTK_LAYOUT (canvas));
d (
g_print ("button %d, event type %d, grabbed=%p, current=%p\n",
event->button,
event->type,
canvas->grabbed_item,
canvas->current_item));
/* dispatch normally regardless of the event's window if an item has
has a pointer grab in effect */
if (!canvas->grabbed_item && event->window != bin_window)
return retval;
switch (event->button) {
case 1:
mask = GDK_BUTTON1_MASK;
break;
case 2:
mask = GDK_BUTTON2_MASK;
break;
case 3:
mask = GDK_BUTTON3_MASK;
break;
case 4:
mask = GDK_BUTTON4_MASK;
break;
case 5:
mask = GDK_BUTTON5_MASK;
break;
default:
mask = 0;
}
switch (event->type) {
case GDK_BUTTON_PRESS:
case GDK_2BUTTON_PRESS:
case GDK_3BUTTON_PRESS:
/* Pick the current item as if the button were not
* pressed, and then process the event. */
canvas->state = event->state;
pick_current_item (canvas, (GdkEvent *) event);
canvas->state ^= mask;
retval = canvas_emit_event (canvas, (GdkEvent *) event);
break;
case GDK_BUTTON_RELEASE:
/* Process the event as if the button were pressed,
* then repick after the button has been released. */
canvas->state = event->state;
retval = canvas_emit_event (canvas, (GdkEvent *) event);
event->state ^= mask;
canvas->state = event->state;
pick_current_item (canvas, (GdkEvent *) event);
event->state ^= mask;
break;
default:
g_return_val_if_reached (0);
}
return retval;
}
static gint
canvas_key_event (GtkWidget *widget,
GdkEventKey *event)
{
GnomeCanvas *canvas;
GdkEvent full_event = { 0 };
g_return_val_if_fail (GNOME_IS_CANVAS (widget), FALSE);
g_return_val_if_fail (event != NULL, FALSE);
canvas = GNOME_CANVAS (widget);
full_event.type = event->type;
full_event.key = *event;
return canvas_emit_event (canvas, &full_event);
}
static gint
canvas_focus_in_event (GtkWidget *widget,
GdkEventFocus *event)
{
GnomeCanvas *canvas;
ECanvas *ecanvas;
GdkEvent full_event = { 0 };
canvas = GNOME_CANVAS (widget);
ecanvas = E_CANVAS (widget);
/* XXX Can't access flags directly anymore, but is it really needed?
* If so, could we call gtk_widget_send_focus_change() instead? */
#if 0
GTK_WIDGET_SET_FLAGS (widget, GTK_HAS_FOCUS);
#endif
gtk_im_context_focus_in (ecanvas->im_context);
if (canvas->focused_item) {
full_event.type = event->type;
full_event.focus_change = *event;
return canvas_emit_event (canvas, &full_event);
} else {
return FALSE;
}
}
static gint
canvas_focus_out_event (GtkWidget *widget,
GdkEventFocus *event)
{
GnomeCanvas *canvas;
ECanvas *ecanvas;
GdkEvent full_event = { 0 };
canvas = GNOME_CANVAS (widget);
ecanvas = E_CANVAS (widget);
/* XXX Can't access flags directly anymore, but is it really needed?
* If so, could we call gtk_widget_send_focus_change() instead? */
#if 0
GTK_WIDGET_UNSET_FLAGS (widget, GTK_HAS_FOCUS);
#endif
gtk_im_context_focus_out (ecanvas->im_context);
if (canvas->focused_item) {
full_event.type = event->type;
full_event.focus_change = *event;
return canvas_emit_event (canvas, &full_event);
} else {
return FALSE;
}
}
static void
canvas_reflow (ECanvas *canvas)
{
/* Placeholder so subclasses can safely chain up. */
}
static void
e_canvas_class_init (ECanvasClass *class)
{
GObjectClass *object_class;
GtkWidgetClass *widget_class;
object_class = G_OBJECT_CLASS (class);
object_class->dispose = canvas_dispose;
widget_class = GTK_WIDGET_CLASS (class);
widget_class->realize = canvas_realize;
widget_class->unrealize = canvas_unrealize;
widget_class->style_set = canvas_style_set;
widget_class->button_press_event = canvas_button_event;
widget_class->button_release_event = canvas_button_event;
widget_class->key_press_event = canvas_key_event;
widget_class->key_release_event = canvas_key_event;
widget_class->focus_in_event = canvas_focus_in_event;
widget_class->focus_out_event = canvas_focus_out_event;
class->reflow = canvas_reflow;
signals[REFLOW] = g_signal_new (
"reflow",
G_OBJECT_CLASS_TYPE (object_class),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (ECanvasClass, reflow),
NULL, NULL,
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE, 0);
}
static void
e_canvas_init (ECanvas *canvas)
{
canvas->im_context = gtk_im_multicontext_new ();
}
GtkWidget *
e_canvas_new (void)
{
return g_object_new (E_TYPE_CANVAS, NULL);
}
/**
* e_canvas_item_grab_focus:
* @item: A canvas item.
* @widget_too: Whether or not to grab the widget-level focus too
*
* Makes the specified item take the keyboard focus, so all keyboard
* events will be sent to it. If the canvas widget itself did not have
* the focus and @widget_too is %TRUE, it grabs that focus as well.
**/
void
e_canvas_item_grab_focus (GnomeCanvasItem *item,
gboolean widget_too)
{
GnomeCanvasItem *focused_item;
GdkWindow *bin_window;
GdkEvent ev = { 0 };
g_return_if_fail (GNOME_IS_CANVAS_ITEM (item));
g_return_if_fail (gtk_widget_get_can_focus (GTK_WIDGET (item->canvas)));
bin_window = gtk_layout_get_bin_window (GTK_LAYOUT (item->canvas));
focused_item = item->canvas->focused_item;
if (focused_item) {
ev.type = GDK_FOCUS_CHANGE;
ev.focus_change.type = GDK_FOCUS_CHANGE;
ev.focus_change.window = bin_window;
ev.focus_change.send_event = FALSE;
ev.focus_change.in = FALSE;
canvas_emit_event (item->canvas, &ev);
}
item->canvas->focused_item = item;
if (widget_too && !gtk_widget_has_focus (GTK_WIDGET (item->canvas))) {
gtk_widget_grab_focus (GTK_WIDGET (item->canvas));
}
ev.focus_change.type = GDK_FOCUS_CHANGE;
ev.focus_change.window = bin_window;
ev.focus_change.send_event = FALSE;
ev.focus_change.in = TRUE;
canvas_emit_event (item->canvas, &ev);
}
static void
e_canvas_item_invoke_reflow (GnomeCanvasItem *item,
gint flags)
{
GnomeCanvasGroup *group;
GList *list;
GnomeCanvasItem *child;
if (GNOME_IS_CANVAS_GROUP (item)) {
group = GNOME_CANVAS_GROUP (item);
for (list = group->item_list; list; list = list->next) {
child = GNOME_CANVAS_ITEM (list->data);
if (child->flags & E_CANVAS_ITEM_DESCENDENT_NEEDS_REFLOW)
e_canvas_item_invoke_reflow (child, flags);
}
}
if (item->flags & E_CANVAS_ITEM_NEEDS_REFLOW) {
ECanvasItemReflowFunc func;
func = (ECanvasItemReflowFunc)
g_object_get_data (
G_OBJECT (item),
"ECanvasItem::reflow_callback");
if (func)
func (item, flags);
}
item->flags &= ~E_CANVAS_ITEM_NEEDS_REFLOW;
item->flags &= ~E_CANVAS_ITEM_DESCENDENT_NEEDS_REFLOW;
}
static void
do_reflow (ECanvas *canvas)
{
if (GNOME_CANVAS (canvas)->root->flags & E_CANVAS_ITEM_DESCENDENT_NEEDS_REFLOW)
e_canvas_item_invoke_reflow (GNOME_CANVAS (canvas)->root, 0);
}
/* Idle handler for the e-canvas. It deals with pending reflows. */
static gint
idle_handler (gpointer data)
{
ECanvas *canvas;
canvas = E_CANVAS (data);
do_reflow (canvas);
/* Reset idle id */
canvas->idle_id = 0;
g_signal_emit (canvas, signals[REFLOW], 0);
return FALSE;
}
/* Convenience function to add an idle handler to a canvas */
static void
add_idle (ECanvas *canvas)
{
if (canvas->idle_id != 0)
return;
canvas->idle_id = g_idle_add_full (
G_PRIORITY_HIGH_IDLE, idle_handler, (gpointer) canvas, NULL);
}
static void
e_canvas_item_descendent_needs_reflow (GnomeCanvasItem *item)
{
if (item->flags & E_CANVAS_ITEM_DESCENDENT_NEEDS_REFLOW)
return;
item->flags |= E_CANVAS_ITEM_DESCENDENT_NEEDS_REFLOW;
if (item->parent)
e_canvas_item_descendent_needs_reflow (item->parent);
}
void
e_canvas_item_request_reflow (GnomeCanvasItem *item)
{
g_return_if_fail (GNOME_IS_CANVAS_ITEM (item));
if (item->flags & GNOME_CANVAS_ITEM_REALIZED) {
item->flags |= E_CANVAS_ITEM_NEEDS_REFLOW;
e_canvas_item_descendent_needs_reflow (item);
add_idle (E_CANVAS (item->canvas));
}
}
void
e_canvas_item_request_parent_reflow (GnomeCanvasItem *item)
{
g_return_if_fail (GNOME_IS_CANVAS_ITEM (item));
e_canvas_item_request_reflow (item->parent);
}
void
e_canvas_item_set_reflow_callback (GnomeCanvasItem *item,
ECanvasItemReflowFunc func)
{
g_return_if_fail (GNOME_IS_CANVAS_ITEM (item));
g_return_if_fail (func != NULL);
g_object_set_data (
G_OBJECT (item), "ECanvasItem::reflow_callback",
(gpointer) func);
}
static gboolean
grab_cancelled_check (gpointer data)
{
ECanvas *canvas = data;
if (GNOME_CANVAS (canvas)->grabbed_item == NULL) {
canvas->grab_cancelled_cb = NULL;
canvas->grab_cancelled_check_id = 0;
canvas->grab_cancelled_time = 0;
canvas->grab_cancelled_data = NULL;
return FALSE;
}
if (gtk_grab_get_current ()) {
gnome_canvas_item_ungrab (
GNOME_CANVAS (canvas)->grabbed_item,
canvas->grab_cancelled_time);
if (canvas->grab_cancelled_cb)
canvas->grab_cancelled_cb (
canvas, GNOME_CANVAS (canvas)->grabbed_item,
canvas->grab_cancelled_data);
canvas->grab_cancelled_cb = NULL;
canvas->grab_cancelled_check_id = 0;
canvas->grab_cancelled_time = 0;
canvas->grab_cancelled_data = NULL;
return FALSE;
}
return TRUE;
}
gint
e_canvas_item_grab (ECanvas *canvas,
GnomeCanvasItem *item,
guint event_mask,
GdkCursor *cursor,
GdkDevice *device,
guint32 etime,
ECanvasItemGrabCancelled cancelled_cb,
gpointer cancelled_data)
{
GdkGrabStatus grab_status;
g_return_val_if_fail (E_IS_CANVAS (canvas), -1);
g_return_val_if_fail (GNOME_IS_CANVAS_ITEM (item), -1);
g_return_val_if_fail (GDK_IS_DEVICE (device), -1);
if (gtk_grab_get_current ())
return GDK_GRAB_ALREADY_GRABBED;
grab_status = gnome_canvas_item_grab (
item, event_mask, cursor, device, etime);
if (grab_status == GDK_GRAB_SUCCESS) {
canvas->grab_cancelled_cb = cancelled_cb;
canvas->grab_cancelled_check_id = e_named_timeout_add_full (
G_PRIORITY_LOW, 100,
grab_cancelled_check, canvas, NULL);
canvas->grab_cancelled_time = etime;
canvas->grab_cancelled_data = cancelled_data;
}
return grab_status;
}
void
e_canvas_item_ungrab (ECanvas *canvas,
GnomeCanvasItem *item,
guint32 etime)
{
g_return_if_fail (E_IS_CANVAS (canvas));
g_return_if_fail (GNOME_IS_CANVAS_ITEM (item));
if (canvas->grab_cancelled_check_id) {
g_source_remove (canvas->grab_cancelled_check_id);
canvas->grab_cancelled_cb = NULL;
canvas->grab_cancelled_check_id = 0;
canvas->grab_cancelled_time = 0;
canvas->grab_cancelled_data = NULL;
gnome_canvas_item_ungrab (item, etime);
}
}
|