1 /* Copyright 2006, 2007, 2008, Soren Sandmann <sandmann@daimi.au.dk>
2  *
3  * This library is free software; you can redistribute it and/or
4  * modify it under the terms of the GNU Lesser General Public
5  * License as published by the Free Software Foundation; either
6  * version 2 of the License, or (at your option) any later version.
7  *
8  * This library is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public
14  * License along with this library; if not, write to the
15  * Free Software Foundation, Inc., 51 Franklin Street - Suite 500,
16  * Boston, MA 02110-1335, USA.
17  */
18 
19 #include "scrollarea.h"
20 
21 #include <gdk/gdk.h>
22 
23 #include "foo-marshal.h"
24 
25 G_DEFINE_TYPE_WITH_CODE (FooScrollArea, foo_scroll_area, GTK_TYPE_CONTAINER,
26                          G_IMPLEMENT_INTERFACE (GTK_TYPE_SCROLLABLE, NULL));
27 
28 static GtkWidgetClass *parent_class;
29 
30 typedef struct BackingStore BackingStore;
31 
32 typedef struct InputPath InputPath;
33 typedef struct InputRegion InputRegion;
34 typedef struct AutoScrollInfo AutoScrollInfo;
35 
36 struct InputPath
37 {
38   gboolean                    is_stroke;
39   cairo_fill_rule_t           fill_rule;
40   double                      line_width;
41   cairo_path_t               *path;           /* In canvas coordinates */
42 
43   FooScrollAreaEventFunc      func;
44   gpointer                    data;
45 
46   InputPath                  *next;
47 };
48 
49 /* InputRegions are mutually disjoint */
50 struct InputRegion
51 {
52   /* the boundary of this area in canvas coordinates */
53   cairo_region_t *region;
54 
55   InputPath *paths;
56 };
57 
58 struct AutoScrollInfo
59 {
60   int                         dx;
61   int                         dy;
62   int                         timeout_id;
63   int                         begin_x;
64   int                         begin_y;
65   double                      res_x;
66   double                      res_y;
67   GTimer                     *timer;
68 };
69 
70 struct FooScrollAreaPrivate
71 {
72   GdkWindow                  *input_window;
73 
74   int                         width;
75   int                         height;
76 
77   GtkAdjustment              *hadj;
78   GtkAdjustment              *vadj;
79   GtkScrollablePolicy         hscroll_policy;
80   GtkScrollablePolicy         vscroll_policy;
81   int                         x_offset;
82   int                         y_offset;
83 
84   int                         min_width;
85   int                         min_height;
86 
87   GPtrArray                  *input_regions;
88 
89   AutoScrollInfo             *auto_scroll_info;
90 
91   /* During expose, this region is set to the region
92    * being exposed. At other times, it is NULL
93    *
94    * It is used for clipping of input areas
95    */
96   InputRegion                *current_input;
97 
98   gboolean                    grabbed;
99   FooScrollAreaEventFunc      grab_func;
100   gpointer                    grab_data;
101 
102   cairo_surface_t            *surface;
103   cairo_region_t             *update_region; /* In canvas coordinates */
104 };
105 
106 enum
107   {
108     VIEWPORT_CHANGED,
109     PAINT,
110     INPUT,
111     LAST_SIGNAL,
112   };
113 
114 enum {
115   PROP_0,
116   PROP_VADJUSTMENT,
117   PROP_HADJUSTMENT,
118   PROP_HSCROLL_POLICY,
119   PROP_VSCROLL_POLICY
120 };
121 
122 static guint signals [LAST_SIGNAL] = { 0 };
123 
124 static gboolean foo_scroll_area_draw (GtkWidget *widget,
125                                       cairo_t *cr);
126 static void foo_scroll_area_get_preferred_width (GtkWidget *widget,
127                                                  gint      *minimum,
128                                                  gint      *natural);
129 static void foo_scroll_area_get_preferred_height (GtkWidget *widget,
130                                                   gint      *minimum,
131                                                   gint      *natural);
132 static void foo_scroll_area_size_allocate (GtkWidget *widget,
133                                            GtkAllocation *allocation);
134 static void foo_scroll_area_set_hadjustment (FooScrollArea *scroll_area,
135                                              GtkAdjustment *hadjustment);
136 static void foo_scroll_area_set_vadjustment (FooScrollArea *scroll_area,
137                                              GtkAdjustment *vadjustment);
138 static void foo_scroll_area_realize (GtkWidget *widget);
139 static void foo_scroll_area_unrealize (GtkWidget *widget);
140 static void foo_scroll_area_map (GtkWidget *widget);
141 static void foo_scroll_area_unmap (GtkWidget *widget);
142 static gboolean foo_scroll_area_button_press (GtkWidget *widget,
143                                               GdkEventButton *event);
144 static gboolean foo_scroll_area_button_release (GtkWidget *widget,
145                                                 GdkEventButton *event);
146 static gboolean foo_scroll_area_motion (GtkWidget *widget,
147                                         GdkEventMotion *event);
148 
149 static void
foo_scroll_area_map(GtkWidget * widget)150 foo_scroll_area_map (GtkWidget *widget)
151 {
152   FooScrollArea *area = FOO_SCROLL_AREA (widget);
153 
154   GTK_WIDGET_CLASS (parent_class)->map (widget);
155 
156   if (area->priv->input_window)
157     gdk_window_show (area->priv->input_window);
158 }
159 
160 static void
foo_scroll_area_unmap(GtkWidget * widget)161 foo_scroll_area_unmap (GtkWidget *widget)
162 {
163   FooScrollArea *area = FOO_SCROLL_AREA (widget);
164 
165   if (area->priv->input_window)
166     gdk_window_hide (area->priv->input_window);
167 
168   GTK_WIDGET_CLASS (parent_class)->unmap (widget);
169 }
170 
171 static void
foo_scroll_area_finalize(GObject * object)172 foo_scroll_area_finalize (GObject *object)
173 {
174   FooScrollArea *scroll_area = FOO_SCROLL_AREA (object);
175 
176   g_object_unref (scroll_area->priv->hadj);
177   g_object_unref (scroll_area->priv->vadj);
178 
179   g_ptr_array_free (scroll_area->priv->input_regions, TRUE);
180 
181   g_free (scroll_area->priv);
182 
183   G_OBJECT_CLASS (foo_scroll_area_parent_class)->finalize (object);
184 }
185 
186 static void
foo_scroll_area_get_property(GObject * object,guint property_id,GValue * value,GParamSpec * pspec)187 foo_scroll_area_get_property (GObject    *object,
188                               guint       property_id,
189                               GValue     *value,
190                               GParamSpec *pspec)
191 {
192   FooScrollArea *scroll_area = FOO_SCROLL_AREA (object);
193 
194   switch (property_id)
195     {
196     case PROP_VADJUSTMENT:
197       g_value_set_object (value, &scroll_area->priv->vadj);
198       break;
199     case PROP_HADJUSTMENT:
200       g_value_set_object (value, &scroll_area->priv->hadj);
201       break;
202     case PROP_HSCROLL_POLICY:
203       g_value_set_enum (value, scroll_area->priv->hscroll_policy);
204       break;
205     case PROP_VSCROLL_POLICY:
206       g_value_set_enum (value, scroll_area->priv->vscroll_policy);
207       break;
208     default:
209       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
210     }
211 }
212 
213 static void
foo_scroll_area_set_property(GObject * object,guint property_id,const GValue * value,GParamSpec * pspec)214 foo_scroll_area_set_property (GObject      *object,
215                               guint         property_id,
216                               const GValue *value,
217                               GParamSpec   *pspec)
218 {
219   FooScrollArea *scroll_area = FOO_SCROLL_AREA (object);
220 
221   switch (property_id) {
222   case PROP_VADJUSTMENT:
223     foo_scroll_area_set_vadjustment (FOO_SCROLL_AREA (object), g_value_get_object (value));
224     break;
225   case PROP_HADJUSTMENT:
226     foo_scroll_area_set_hadjustment (FOO_SCROLL_AREA (object), g_value_get_object (value));
227     break;
228   case PROP_HSCROLL_POLICY:
229     scroll_area->priv->hscroll_policy = g_value_get_enum (value);
230     break;
231   case PROP_VSCROLL_POLICY:
232     scroll_area->priv->vscroll_policy = g_value_get_enum (value);
233     break;
234   default:
235     G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
236   }
237 }
238 
239 static void
foo_scroll_area_class_init(FooScrollAreaClass * class)240 foo_scroll_area_class_init (FooScrollAreaClass *class)
241 {
242   GObjectClass *object_class = G_OBJECT_CLASS (class);
243   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
244 
245   object_class->finalize = foo_scroll_area_finalize;
246   object_class->set_property = foo_scroll_area_set_property;
247   object_class->get_property = foo_scroll_area_get_property;
248 
249   widget_class->get_preferred_width = foo_scroll_area_get_preferred_width;
250   widget_class->get_preferred_height = foo_scroll_area_get_preferred_height;
251   widget_class->draw = foo_scroll_area_draw;
252   widget_class->size_allocate = foo_scroll_area_size_allocate;
253   widget_class->realize = foo_scroll_area_realize;
254   widget_class->unrealize = foo_scroll_area_unrealize;
255   widget_class->button_press_event = foo_scroll_area_button_press;
256   widget_class->button_release_event = foo_scroll_area_button_release;
257   widget_class->motion_notify_event = foo_scroll_area_motion;
258   widget_class->map = foo_scroll_area_map;
259   widget_class->unmap = foo_scroll_area_unmap;
260 
261   parent_class = g_type_class_peek_parent (class);
262 
263   /* Scrollable interface properties */
264   g_object_class_override_property (object_class, PROP_HADJUSTMENT, "hadjustment");
265   g_object_class_override_property (object_class, PROP_VADJUSTMENT, "vadjustment");
266   g_object_class_override_property (object_class, PROP_HSCROLL_POLICY, "hscroll-policy");
267   g_object_class_override_property (object_class, PROP_VSCROLL_POLICY, "vscroll-policy");
268 
269   signals[VIEWPORT_CHANGED] =
270     g_signal_new ("viewport_changed",
271                   G_OBJECT_CLASS_TYPE (object_class),
272                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
273                   G_STRUCT_OFFSET (FooScrollAreaClass,
274                                    viewport_changed),
275                   NULL, NULL,
276                   foo_marshal_VOID__BOXED_BOXED,
277                   G_TYPE_NONE, 2,
278                   GDK_TYPE_RECTANGLE,
279                   GDK_TYPE_RECTANGLE);
280 
281   signals[PAINT] =
282     g_signal_new ("paint",
283                   G_OBJECT_CLASS_TYPE (object_class),
284                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
285                   G_STRUCT_OFFSET (FooScrollAreaClass,
286                                    paint),
287                   NULL, NULL,
288                   g_cclosure_marshal_VOID__POINTER,
289                   G_TYPE_NONE, 1,
290                   G_TYPE_POINTER);
291 }
292 
293 static GtkAdjustment *
new_adjustment(void)294 new_adjustment (void)
295 {
296   return GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
297 }
298 
299 static void
foo_scroll_area_init(FooScrollArea * scroll_area)300 foo_scroll_area_init (FooScrollArea *scroll_area)
301 {
302   GtkWidget *widget;
303 
304   widget = GTK_WIDGET (scroll_area);
305 
306   gtk_widget_set_has_window (widget, FALSE);
307   gtk_widget_set_redraw_on_allocate (widget, FALSE);
308 
309   scroll_area->priv = g_new0 (FooScrollAreaPrivate, 1);
310   scroll_area->priv->width = 0;
311   scroll_area->priv->height = 0;
312   scroll_area->priv->hadj = g_object_ref_sink (new_adjustment());
313   scroll_area->priv->vadj = g_object_ref_sink (new_adjustment());
314   scroll_area->priv->x_offset = 0.0;
315   scroll_area->priv->y_offset = 0.0;
316   scroll_area->priv->min_width = 0;
317   scroll_area->priv->min_height = 0;
318   scroll_area->priv->auto_scroll_info = NULL;
319   scroll_area->priv->input_regions = g_ptr_array_new ();
320   scroll_area->priv->surface = NULL;
321   scroll_area->priv->update_region = cairo_region_create ();
322 }
323 
324 typedef void (* PathForeachFunc) (double  *x,
325                                   double  *y,
326                                   gpointer data);
327 
328 static void
path_foreach_point(cairo_path_t * path,PathForeachFunc func,gpointer user_data)329 path_foreach_point (cairo_path_t     *path,
330                     PathForeachFunc   func,
331                     gpointer          user_data)
332 {
333   int i;
334 
335   for (i = 0; i < path->num_data; i += path->data[i].header.length)
336     {
337       cairo_path_data_t *data = &(path->data[i]);
338 
339       switch (data->header.type)
340         {
341         case CAIRO_PATH_MOVE_TO:
342         case CAIRO_PATH_LINE_TO:
343           func (&(data[1].point.x), &(data[1].point.y), user_data);
344           break;
345 
346         case CAIRO_PATH_CURVE_TO:
347           func (&(data[1].point.x), &(data[1].point.y), user_data);
348           func (&(data[2].point.x), &(data[2].point.y), user_data);
349           func (&(data[3].point.x), &(data[3].point.y), user_data);
350           break;
351 
352         case CAIRO_PATH_CLOSE_PATH:
353           break;
354         }
355     }
356 }
357 
358 typedef struct
359 {
360   double x1, y1, x2, y2;
361 } Box;
362 
363 static void
input_path_free_list(InputPath * paths)364 input_path_free_list (InputPath *paths)
365 {
366   if (!paths)
367     return;
368 
369   input_path_free_list (paths->next);
370   cairo_path_destroy (paths->path);
371   g_free (paths);
372 }
373 
374 static void
input_region_free(InputRegion * region)375 input_region_free (InputRegion *region)
376 {
377   input_path_free_list (region->paths);
378   cairo_region_destroy (region->region);
379 
380   g_free (region);
381 }
382 
383 static void
get_viewport(FooScrollArea * scroll_area,GdkRectangle * viewport)384 get_viewport (FooScrollArea *scroll_area,
385               GdkRectangle  *viewport)
386 {
387   GtkAllocation allocation;
388   GtkWidget *widget = GTK_WIDGET (scroll_area);
389 
390   gtk_widget_get_allocation (widget, &allocation);
391 
392   viewport->x = scroll_area->priv->x_offset;
393   viewport->y = scroll_area->priv->y_offset;
394   viewport->width = allocation.width;
395   viewport->height = allocation.height;
396 }
397 
398 static void
allocation_to_canvas(FooScrollArea * area,int * x,int * y)399 allocation_to_canvas (FooScrollArea *area,
400                       int           *x,
401                       int           *y)
402 {
403   *x += area->priv->x_offset;
404   *y += area->priv->y_offset;
405 }
406 
407 static void
clear_exposed_input_region(FooScrollArea * area,cairo_region_t * exposed)408 clear_exposed_input_region (FooScrollArea  *area,
409                             cairo_region_t *exposed) /* in canvas coordinates */
410 {
411   int i;
412   cairo_region_t *viewport;
413   GdkRectangle allocation;
414 
415   gtk_widget_get_allocation (GTK_WIDGET (area), &allocation);
416   allocation.x = 0;
417   allocation.y = 0;
418   allocation_to_canvas (area, &allocation.x, &allocation.y);
419 
420   viewport = cairo_region_create_rectangle (&allocation);
421   cairo_region_subtract (viewport, exposed);
422 
423   for (i = 0; i < area->priv->input_regions->len; ++i)
424     {
425       InputRegion *region = area->priv->input_regions->pdata[i];
426 
427       cairo_region_intersect (region->region, viewport);
428 
429       if (cairo_region_is_empty (region->region))
430         {
431           input_region_free (region);
432           g_ptr_array_remove_index_fast (area->priv->input_regions, i--);
433         }
434     }
435 
436   cairo_region_destroy (viewport);
437 }
438 
439 /* taken from mutter */
440 static void
setup_background_cr(GdkWindow * window,cairo_t * cr,int x_offset,int y_offset)441 setup_background_cr (GdkWindow *window, cairo_t *cr, int x_offset, int y_offset)
442 {
443   GdkWindow *parent = gdk_window_get_parent (window);
444   cairo_pattern_t *bg_pattern;
445 
446   bg_pattern = gdk_window_get_background_pattern (window);
447   if (bg_pattern == NULL && parent)
448     {
449       gint window_x, window_y;
450 
451       gdk_window_get_position (window, &window_x, &window_y);
452       setup_background_cr (parent, cr, x_offset + window_x, y_offset + window_y);
453     }
454   else if (bg_pattern)
455     {
456       cairo_translate (cr, - x_offset, - y_offset);
457       cairo_set_source (cr, bg_pattern);
458       cairo_translate (cr, x_offset, y_offset);
459     }
460 }
461 
462 static void
initialize_background(GtkWidget * widget,cairo_t * cr)463 initialize_background (GtkWidget *widget,
464                        cairo_t   *cr)
465 {
466   setup_background_cr (gtk_widget_get_window (widget), cr, 0, 0);
467 
468   cairo_paint (cr);
469 }
470 
471 static gboolean
foo_scroll_area_draw(GtkWidget * widget,cairo_t * cr)472 foo_scroll_area_draw (GtkWidget *widget,
473                       cairo_t   *cr)
474 {
475   FooScrollArea *scroll_area = FOO_SCROLL_AREA (widget);
476   cairo_region_t *region;
477   GtkAllocation widget_allocation;
478 
479   /* Note that this function can be called at a time
480    * where the adj->value is different from x_offset.
481    * Ie., the GtkScrolledWindow changed the adj->value
482    * without emitting the value_changed signal.
483    *
484    * Hence we must always use the value we got
485    * the last time the signal was emitted, ie.,
486    * priv->{x,y}_offset.
487    */
488 
489   /* Setup input areas */
490   clear_exposed_input_region (scroll_area, scroll_area->priv->update_region);
491 
492   scroll_area->priv->current_input = g_new0 (InputRegion, 1);
493   scroll_area->priv->current_input->region = cairo_region_copy (scroll_area->priv->update_region);
494   scroll_area->priv->current_input->paths = NULL;
495   g_ptr_array_add (scroll_area->priv->input_regions,
496                    scroll_area->priv->current_input);
497 
498   region = scroll_area->priv->update_region;
499   scroll_area->priv->update_region = cairo_region_create ();
500 
501   initialize_background (widget, cr);
502 
503   g_signal_emit (widget, signals[PAINT], 0, cr);
504 
505   scroll_area->priv->current_input = NULL;
506 
507   gtk_widget_get_allocation (widget, &widget_allocation);
508 
509   /* Finally draw the backing surface */
510   cairo_set_source_surface (cr, scroll_area->priv->surface,
511                             widget_allocation.x, widget_allocation.y);
512   cairo_fill (cr);
513 
514   cairo_region_destroy (region);
515 
516   return TRUE;
517 }
518 
519 void
foo_scroll_area_get_viewport(FooScrollArea * scroll_area,GdkRectangle * viewport)520 foo_scroll_area_get_viewport (FooScrollArea *scroll_area,
521                               GdkRectangle  *viewport)
522 {
523   g_return_if_fail (FOO_IS_SCROLL_AREA (scroll_area));
524 
525   if (!viewport)
526     return;
527 
528   get_viewport (scroll_area, viewport);
529 }
530 
531 static void
532 process_event (FooScrollArea           *scroll_area,
533                FooScrollAreaEventType   input_type,
534                int                      x,
535                int                      y);
536 
537 static void
emit_viewport_changed(FooScrollArea * scroll_area,GdkRectangle * new_viewport,GdkRectangle * old_viewport)538 emit_viewport_changed (FooScrollArea *scroll_area,
539                        GdkRectangle  *new_viewport,
540                        GdkRectangle  *old_viewport)
541 {
542   int px, py;
543   g_signal_emit (scroll_area, signals[VIEWPORT_CHANGED], 0,
544                  new_viewport, old_viewport);
545 
546   if (scroll_area->priv->input_window == NULL)
547     return;
548 
549   gdk_window_get_pointer (scroll_area->priv->input_window, &px, &py, NULL);
550 
551 #if 0
552   g_print ("procc\n");
553 #endif
554 
555   process_event (scroll_area, FOO_MOTION, px, py);
556 }
557 
558 static void
clamp_adjustment(GtkAdjustment * adj)559 clamp_adjustment (GtkAdjustment *adj)
560 {
561   if (gtk_adjustment_get_upper (adj) >= gtk_adjustment_get_page_size (adj))
562     gtk_adjustment_set_value (adj, CLAMP (gtk_adjustment_get_value (adj), 0.0,
563                                           gtk_adjustment_get_upper (adj)
564                                           - gtk_adjustment_get_page_size (adj)));
565   else
566     gtk_adjustment_set_value (adj, 0.0);
567 
568   gtk_adjustment_changed (adj);
569 }
570 
571 static gboolean
set_adjustment_values(FooScrollArea * scroll_area)572 set_adjustment_values (FooScrollArea *scroll_area)
573 {
574   GtkAllocation allocation;
575 
576   GtkAdjustment *hadj = scroll_area->priv->hadj;
577   GtkAdjustment *vadj = scroll_area->priv->vadj;
578 
579   /* Horizontal */
580   gtk_widget_get_allocation (GTK_WIDGET (scroll_area), &allocation);
581   g_object_freeze_notify (G_OBJECT (hadj));
582   gtk_adjustment_set_page_size (hadj, allocation.width);
583   gtk_adjustment_set_step_increment (hadj, 0.1 * allocation.width);
584   gtk_adjustment_set_page_increment (hadj, 0.9 * allocation.width);
585   gtk_adjustment_set_lower (hadj, 0.0);
586   gtk_adjustment_set_upper (hadj, scroll_area->priv->width);
587   g_object_thaw_notify (G_OBJECT (hadj));
588 
589   /* Vertical */
590   g_object_freeze_notify (G_OBJECT (vadj));
591   gtk_adjustment_set_page_size (vadj, allocation.height);
592   gtk_adjustment_set_step_increment (vadj, 0.1 * allocation.height);
593   gtk_adjustment_set_page_increment (vadj, 0.9 * allocation.height);
594   gtk_adjustment_set_lower (vadj, 0.0);
595   gtk_adjustment_set_upper (vadj, scroll_area->priv->height);
596   g_object_thaw_notify (G_OBJECT (vadj));
597 
598   clamp_adjustment (hadj);
599   clamp_adjustment (vadj);
600 
601   return TRUE;
602 }
603 
604 static void
foo_scroll_area_realize(GtkWidget * widget)605 foo_scroll_area_realize (GtkWidget *widget)
606 {
607   FooScrollArea *area = FOO_SCROLL_AREA (widget);
608   GdkWindowAttr attributes;
609   GtkAllocation widget_allocation;
610   GdkWindow *window;
611   gint attributes_mask;
612   cairo_t *cr;
613 
614   gtk_widget_get_allocation (widget, &widget_allocation);
615   gtk_widget_set_realized (widget, TRUE);
616 
617   attributes.window_type = GDK_WINDOW_CHILD;
618   attributes.x = widget_allocation.x;
619   attributes.y = widget_allocation.y;
620   attributes.width = widget_allocation.width;
621   attributes.height = widget_allocation.height;
622   attributes.wclass = GDK_INPUT_ONLY;
623   attributes.event_mask = gtk_widget_get_events (widget);
624   attributes.event_mask |= (GDK_BUTTON_PRESS_MASK |
625                             GDK_BUTTON_RELEASE_MASK |
626                             GDK_BUTTON1_MOTION_MASK |
627                             GDK_BUTTON2_MOTION_MASK |
628                             GDK_BUTTON3_MOTION_MASK |
629                             GDK_POINTER_MOTION_MASK |
630                             GDK_ENTER_NOTIFY_MASK |
631                             GDK_LEAVE_NOTIFY_MASK);
632 
633   attributes_mask = GDK_WA_X | GDK_WA_Y;
634 
635   window = gtk_widget_get_parent_window (widget);
636   gtk_widget_set_window (widget, window);
637   g_object_ref (window);
638 
639   area->priv->input_window = gdk_window_new (window,
640                                              &attributes, attributes_mask);
641 
642   cr = gdk_cairo_create (gtk_widget_get_window (widget));
643   area->priv->surface = cairo_surface_create_similar (cairo_get_target (cr),
644                                                       CAIRO_CONTENT_COLOR,
645                                                       widget_allocation.width,
646                                                       widget_allocation.height);
647   cairo_destroy (cr);
648 
649   gdk_window_set_user_data (area->priv->input_window, area);
650 }
651 
652 static void
foo_scroll_area_unrealize(GtkWidget * widget)653 foo_scroll_area_unrealize (GtkWidget *widget)
654 {
655   FooScrollArea *area = FOO_SCROLL_AREA (widget);
656 
657   if (area->priv->input_window)
658     {
659       gdk_window_set_user_data (area->priv->input_window, NULL);
660       gdk_window_destroy (area->priv->input_window);
661       area->priv->input_window = NULL;
662     }
663 
664   GTK_WIDGET_CLASS (parent_class)->unrealize (widget);
665 }
666 
667 static cairo_surface_t *
create_new_surface(GtkWidget * widget,cairo_surface_t * old)668 create_new_surface (GtkWidget *widget,
669                     cairo_surface_t *old)
670 {
671   GtkAllocation widget_allocation;
672   cairo_surface_t *new;
673   cairo_t *cr;
674 
675   gtk_widget_get_allocation (widget, &widget_allocation);
676   cr = gdk_cairo_create (gtk_widget_get_window (widget));
677   new = cairo_surface_create_similar (cairo_get_target (cr),
678                                       CAIRO_CONTENT_COLOR,
679                                       widget_allocation.width,
680                                       widget_allocation.height);
681   cairo_destroy (cr);
682 
683   /* Unfortunately we don't know in which direction we were resized,
684    * so we just assume we were dragged from the south-east corner.
685    *
686    * Although, maybe we could get the root coordinates of the input-window?
687    * That might just work, actually. We need to make sure metacity uses
688    * static gravity for the window before this will be useful.
689    */
690   cr = cairo_create (new);
691   cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
692   cairo_set_source_surface (cr, old, 0, 0);
693   cairo_paint (cr);
694   cairo_destroy (cr);
695 
696   return new;
697 }
698 
699 static void
allocation_to_canvas_region(FooScrollArea * area,cairo_region_t * region)700 allocation_to_canvas_region (FooScrollArea  *area,
701                              cairo_region_t *region)
702 {
703   cairo_region_translate (region, area->priv->x_offset, area->priv->y_offset);
704 }
705 
706 static void
_cairo_region_xor(cairo_region_t * dst,const cairo_region_t * src)707 _cairo_region_xor (cairo_region_t       *dst,
708                    const cairo_region_t *src)
709 {
710   cairo_region_t *trb;
711 
712   trb = cairo_region_copy (src);
713 
714   cairo_region_subtract (trb, dst);
715   cairo_region_subtract (dst, src);
716 
717   cairo_region_union (dst, trb);
718 
719   cairo_region_destroy (trb);
720 }
721 
722 static void
foo_scroll_area_size_allocate(GtkWidget * widget,GtkAllocation * allocation)723 foo_scroll_area_size_allocate (GtkWidget     *widget,
724                                GtkAllocation *allocation)
725 {
726   FooScrollArea *scroll_area = FOO_SCROLL_AREA (widget);
727   GdkRectangle new_viewport;
728   GdkRectangle old_viewport;
729   cairo_region_t *old_allocation;
730   cairo_region_t *invalid;
731   GtkAllocation widget_allocation;
732 
733   get_viewport (scroll_area, &old_viewport);
734 
735   gtk_widget_get_allocation (widget, &widget_allocation);
736 
737   old_allocation = cairo_region_create_rectangle (&widget_allocation);
738   cairo_region_translate (old_allocation,
739                           -widget_allocation.x, -widget_allocation.y);
740 
741   invalid = cairo_region_create_rectangle (allocation);
742   cairo_region_translate (invalid, -allocation->x, -allocation->y);
743   _cairo_region_xor (invalid, old_allocation);
744   allocation_to_canvas_region (scroll_area, invalid);
745   foo_scroll_area_invalidate_region (scroll_area, invalid);
746 
747   cairo_region_destroy (old_allocation);
748   cairo_region_destroy (invalid);
749 
750   gtk_widget_set_allocation (widget, allocation);
751 
752   if (scroll_area->priv->input_window)
753     {
754       cairo_surface_t *new_surface;
755 
756       gdk_window_move_resize (scroll_area->priv->input_window,
757                               allocation->x, allocation->y,
758                               allocation->width, allocation->height);
759 
760       new_surface = create_new_surface (widget, scroll_area->priv->surface);
761       cairo_surface_destroy (scroll_area->priv->surface);
762 
763       scroll_area->priv->surface = new_surface;
764     }
765 
766   get_viewport (scroll_area, &new_viewport);
767 
768   emit_viewport_changed (scroll_area, &new_viewport, &old_viewport);
769 }
770 
771 static void
emit_input(FooScrollArea * scroll_area,FooScrollAreaEventType type,int x,int y,FooScrollAreaEventFunc func,gpointer data)772 emit_input (FooScrollArea *scroll_area,
773             FooScrollAreaEventType type,
774             int                    x,
775             int                    y,
776             FooScrollAreaEventFunc func,
777             gpointer            data)
778 {
779   FooScrollAreaEvent event;
780 
781   if (!func)
782     return;
783 
784   event.type = type;
785   event.x = x;
786   event.y = y;
787 
788   func (scroll_area, &event, data);
789 }
790 
791 static void
process_event(FooScrollArea * scroll_area,FooScrollAreaEventType input_type,int x,int y)792 process_event (FooScrollArea           *scroll_area,
793                FooScrollAreaEventType   input_type,
794                int                      x,
795                int                      y)
796 {
797   GtkWidget *widget = GTK_WIDGET (scroll_area);
798   int i;
799 
800   allocation_to_canvas (scroll_area, &x, &y);
801 
802   if (scroll_area->priv->grabbed)
803     {
804       emit_input (scroll_area, input_type, x, y,
805                   scroll_area->priv->grab_func,
806                   scroll_area->priv->grab_data);
807     }
808 
809 #if 0
810   g_print ("number of input regions: %d\n", scroll_area->priv->input_regions->len);
811 #endif
812 
813   for (i = 0; i < scroll_area->priv->input_regions->len; ++i)
814     {
815       InputRegion *region = scroll_area->priv->input_regions->pdata[i];
816 
817 #if 0
818       g_print ("region %d (looking for %d,%d) ", i, x, y);
819 #endif
820 
821       if (cairo_region_contains_point (region->region, x, y))
822         {
823           InputPath *path;
824 
825           path = region->paths;
826           while (path)
827             {
828               cairo_t *cr;
829               gboolean inside;
830 
831               cr = gdk_cairo_create (gtk_widget_get_window (widget));
832               cairo_set_fill_rule (cr, path->fill_rule);
833               cairo_set_line_width (cr, path->line_width);
834               cairo_append_path (cr, path->path);
835 
836               if (path->is_stroke)
837                 inside = cairo_in_stroke (cr, x, y);
838               else
839                 inside = cairo_in_fill (cr, x, y);
840 
841               cairo_destroy (cr);
842 
843               if (inside)
844                 {
845                   if (scroll_area->priv->grabbed)
846                     {
847                       emit_input (scroll_area, FOO_DRAG_HOVER,
848                                   x, y,
849                                   path->func,
850                                   path->data);
851                     }
852                   else
853                     {
854                       emit_input (scroll_area, input_type,
855                                   x, y,
856                                   path->func,
857                                   path->data);
858                     }
859                   return;
860                 }
861 
862               path = path->next;
863             }
864 
865           /* Since the regions are all disjoint, no other region
866            * can match. Of course we could be clever and try and
867            * sort the regions, but so far I have been unable to
868            * make this loop show up on a profile.
869            */
870           return;
871         }
872     }
873 }
874 
875 static void
process_gdk_event(FooScrollArea * scroll_area,int x,int y,GdkEvent * event)876 process_gdk_event (FooScrollArea *scroll_area,
877                    int            x,
878                    int            y,
879                    GdkEvent      *event)
880 {
881   FooScrollAreaEventType input_type;
882 
883   if (event->type == GDK_BUTTON_PRESS)
884     input_type = FOO_BUTTON_PRESS;
885   else if (event->type == GDK_BUTTON_RELEASE)
886     input_type = FOO_BUTTON_RELEASE;
887   else if (event->type == GDK_MOTION_NOTIFY)
888     input_type = FOO_MOTION;
889   else
890     return;
891 
892   process_event (scroll_area, input_type, x, y);
893 }
894 
895 static gboolean
foo_scroll_area_button_press(GtkWidget * widget,GdkEventButton * event)896 foo_scroll_area_button_press (GtkWidget *widget,
897                               GdkEventButton *event)
898 {
899   FooScrollArea *area = FOO_SCROLL_AREA (widget);
900 
901   process_gdk_event (area, event->x, event->y, (GdkEvent *)event);
902 
903   return TRUE;
904 }
905 
906 static gboolean
foo_scroll_area_button_release(GtkWidget * widget,GdkEventButton * event)907 foo_scroll_area_button_release (GtkWidget *widget,
908                                 GdkEventButton *event)
909 {
910   FooScrollArea *area = FOO_SCROLL_AREA (widget);
911 
912   process_gdk_event (area, event->x, event->y, (GdkEvent *)event);
913 
914   return FALSE;
915 }
916 
917 static gboolean
foo_scroll_area_motion(GtkWidget * widget,GdkEventMotion * event)918 foo_scroll_area_motion (GtkWidget *widget,
919                         GdkEventMotion *event)
920 {
921   FooScrollArea *area = FOO_SCROLL_AREA (widget);
922 
923   process_gdk_event (area, event->x, event->y, (GdkEvent *)event);
924   return TRUE;
925 }
926 
927 void
foo_scroll_area_set_size_fixed_y(FooScrollArea * scroll_area,int width,int height,int old_y,int new_y)928 foo_scroll_area_set_size_fixed_y (FooScrollArea        *scroll_area,
929                                   int                   width,
930                                   int                   height,
931                                   int                   old_y,
932                                   int                   new_y)
933 {
934   scroll_area->priv->width = width;
935   scroll_area->priv->height = height;
936 
937 #if 0
938   g_print ("diff: %d\n", new_y - old_y);
939 #endif
940   g_object_thaw_notify (G_OBJECT (scroll_area->priv->vadj));
941   gtk_adjustment_set_value (scroll_area->priv->vadj, new_y);
942 
943   set_adjustment_values (scroll_area);
944   g_object_thaw_notify (G_OBJECT (scroll_area->priv->vadj));
945 }
946 
947 void
foo_scroll_area_set_size(FooScrollArea * scroll_area,int width,int height)948 foo_scroll_area_set_size (FooScrollArea        *scroll_area,
949                           int                   width,
950                           int                   height)
951 {
952   g_return_if_fail (FOO_IS_SCROLL_AREA (scroll_area));
953 
954   /* FIXME: Default scroll algorithm should probably be to
955    * keep the same *area* outside the screen as before.
956    *
957    * For wrapper widgets that will do something roughly
958    * right. For widgets that don't change size, it
959    * will do the right thing. Except for idle-layouting
960    * widgets.
961    *
962    * Maybe there should be some generic support for those
963    * widgets. Can that even be done?
964    *
965    * Should we have a version of this function using
966    * fixed points?
967    */
968 
969   scroll_area->priv->width = width;
970   scroll_area->priv->height = height;
971 
972   set_adjustment_values (scroll_area);
973 }
974 
975 static void
foo_scroll_area_get_preferred_width(GtkWidget * widget,gint * minimum,gint * natural)976 foo_scroll_area_get_preferred_width (GtkWidget *widget,
977                                      gint      *minimum,
978                                      gint      *natural)
979 {
980   FooScrollArea *scroll_area = FOO_SCROLL_AREA (widget);
981 
982   if (minimum != NULL) {
983     *minimum = scroll_area->priv->min_width;
984   }
985   if (natural != NULL) {
986     *natural = scroll_area->priv->min_width;
987   }
988 }
989 
990 static void
foo_scroll_area_get_preferred_height(GtkWidget * widget,gint * minimum,gint * natural)991 foo_scroll_area_get_preferred_height (GtkWidget *widget,
992                                       gint      *minimum,
993                                       gint      *natural)
994 {
995   FooScrollArea *scroll_area = FOO_SCROLL_AREA (widget);
996 
997   if (minimum != NULL) {
998     *minimum = scroll_area->priv->min_height;
999   }
1000   if (natural != NULL) {
1001     *natural = scroll_area->priv->min_height;
1002   }
1003 }
1004 
1005 static void
foo_scroll_area_scroll(FooScrollArea * area,gint dx,gint dy)1006 foo_scroll_area_scroll (FooScrollArea *area,
1007                         gint dx,
1008                         gint dy)
1009 {
1010   GdkRectangle allocation;
1011   GdkRectangle src_area;
1012   GdkRectangle move_area;
1013   cairo_region_t *invalid_region;
1014 
1015   gtk_widget_get_allocation (GTK_WIDGET (area), &allocation);
1016   allocation.x = 0;
1017   allocation.y = 0;
1018 
1019   src_area = allocation;
1020   src_area.x -= dx;
1021   src_area.y -= dy;
1022 
1023   invalid_region = cairo_region_create_rectangle (&allocation);
1024 
1025   if (gdk_rectangle_intersect (&allocation, &src_area, &move_area))
1026     {
1027       cairo_region_t *move_region;
1028       cairo_t *cr;
1029 
1030 #if 0
1031       g_print ("scrolling %d %d %d %d (%d %d)\n",
1032                move_area.x, move_area.y,
1033                move_area.width, move_area.height,
1034                dx, dy);
1035 #endif
1036       cr = cairo_create (area->priv->surface);
1037 
1038       /* Cairo doesn't allow self-copies, so we do this little trick instead:
1039        * 1) Clip so the group size is small.
1040        * 2) Call push_group() which creates a temporary pixmap as a workaround
1041        */
1042       gdk_cairo_rectangle (cr, &move_area);
1043       cairo_clip (cr);
1044       cairo_push_group (cr);
1045 
1046       cairo_set_source_surface (cr, area->priv->surface, dx, dy);
1047       gdk_cairo_rectangle (cr, &move_area);
1048       cairo_fill (cr);
1049 
1050       cairo_pop_group_to_source (cr);
1051       cairo_paint (cr);
1052 
1053       cairo_destroy (cr);
1054 
1055       gtk_widget_queue_draw (GTK_WIDGET (area));
1056 
1057       move_region = cairo_region_create_rectangle (&move_area);
1058       cairo_region_translate (move_region, dx, dy);
1059       cairo_region_subtract (invalid_region, move_region);
1060       cairo_region_destroy (move_region);
1061     }
1062 
1063   allocation_to_canvas_region (area, invalid_region);
1064 
1065   foo_scroll_area_invalidate_region (area, invalid_region);
1066 
1067   cairo_region_destroy (invalid_region);
1068 }
1069 
1070 static void
foo_scrollbar_adjustment_changed(GtkAdjustment * adj,FooScrollArea * scroll_area)1071 foo_scrollbar_adjustment_changed (GtkAdjustment *adj,
1072                                   FooScrollArea *scroll_area)
1073 {
1074   GtkWidget *widget = GTK_WIDGET (scroll_area);
1075   gint dx = 0;
1076   gint dy = 0;
1077   GdkRectangle old_viewport, new_viewport;
1078 
1079   get_viewport (scroll_area, &old_viewport);
1080 
1081   if (adj == scroll_area->priv->hadj)
1082     {
1083       /* FIXME: do we treat the offset as int or double, and,
1084        * if int, how do we round?
1085        */
1086       dx = (int)gtk_adjustment_get_value (adj) - scroll_area->priv->x_offset;
1087       scroll_area->priv->x_offset = gtk_adjustment_get_value (adj);
1088     }
1089   else if (adj == scroll_area->priv->vadj)
1090     {
1091       dy = (int)gtk_adjustment_get_value (adj) - scroll_area->priv->y_offset;
1092       scroll_area->priv->y_offset = gtk_adjustment_get_value (adj);
1093     }
1094   else
1095     {
1096       g_assert_not_reached ();
1097     }
1098 
1099   if (gtk_widget_get_realized (widget))
1100     {
1101       foo_scroll_area_scroll (scroll_area, -dx, -dy);
1102 
1103       //translate_input_regions (scroll_area, -dx, -dy);
1104 
1105     }
1106 
1107   get_viewport (scroll_area, &new_viewport);
1108 
1109   emit_viewport_changed (scroll_area, &new_viewport, &old_viewport);
1110 }
1111 
1112 static void
set_one_adjustment(FooScrollArea * scroll_area,GtkAdjustment * adjustment,GtkAdjustment ** location)1113 set_one_adjustment (FooScrollArea *scroll_area,
1114                     GtkAdjustment *adjustment,
1115                     GtkAdjustment **location)
1116 {
1117   g_return_if_fail (location != NULL);
1118 
1119   if (adjustment == *location)
1120     return;
1121 
1122   if (!adjustment)
1123     adjustment = new_adjustment ();
1124 
1125   g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
1126 
1127   if (*location)
1128     {
1129       g_signal_handlers_disconnect_by_func (
1130                                             *location, foo_scrollbar_adjustment_changed, scroll_area);
1131 
1132       g_object_unref (*location);
1133     }
1134 
1135   *location = adjustment;
1136 
1137   g_object_ref_sink (*location);
1138 
1139   g_signal_connect (*location, "value_changed",
1140                     G_CALLBACK (foo_scrollbar_adjustment_changed),
1141                     scroll_area);
1142 }
1143 
1144 static void
foo_scroll_area_set_hadjustment(FooScrollArea * scroll_area,GtkAdjustment * hadjustment)1145 foo_scroll_area_set_hadjustment (FooScrollArea *scroll_area,
1146                                  GtkAdjustment *hadjustment)
1147 {
1148   set_one_adjustment (scroll_area, hadjustment, &scroll_area->priv->hadj);
1149 
1150   set_adjustment_values (scroll_area);
1151 }
1152 
1153 static void
foo_scroll_area_set_vadjustment(FooScrollArea * scroll_area,GtkAdjustment * vadjustment)1154 foo_scroll_area_set_vadjustment (FooScrollArea *scroll_area,
1155                                  GtkAdjustment *vadjustment)
1156 {
1157   set_one_adjustment (scroll_area, vadjustment, &scroll_area->priv->vadj);
1158 
1159   set_adjustment_values (scroll_area);
1160 }
1161 
1162 FooScrollArea *
foo_scroll_area_new(void)1163 foo_scroll_area_new (void)
1164 {
1165   return g_object_new (FOO_TYPE_SCROLL_AREA, NULL);
1166 }
1167 
1168 void
foo_scroll_area_set_min_size(FooScrollArea * scroll_area,int min_width,int min_height)1169 foo_scroll_area_set_min_size (FooScrollArea *scroll_area,
1170                               int                  min_width,
1171                               int            min_height)
1172 {
1173   scroll_area->priv->min_width = min_width;
1174   scroll_area->priv->min_height = min_height;
1175 
1176   /* FIXME: think through invalidation.
1177    *
1178    * Goals: - no repainting everything on size_allocate(),
1179    *        - make sure input boxes are invalidated when
1180    *          needed
1181    */
1182   gtk_widget_queue_resize (GTK_WIDGET (scroll_area));
1183 }
1184 
1185 typedef struct {
1186   cairo_t *cr;
1187   GtkAllocation allocation;
1188 } user_to_device_data;
1189 
1190 static void
user_to_device(double * x,double * y,gpointer user_data)1191 user_to_device (double *x, double *y,
1192                 gpointer user_data)
1193 {
1194   user_to_device_data* data = user_data;
1195 
1196   /* The translations by the user */
1197   cairo_user_to_device (data->cr, x, y);
1198 
1199   /* The position of the widget on the window. */
1200   *x -= data->allocation.x;
1201   *y -= data->allocation.y;
1202 }
1203 
1204 static InputPath *
make_path(FooScrollArea * area,cairo_t * cr,gboolean is_stroke,FooScrollAreaEventFunc func,gpointer data)1205 make_path (FooScrollArea *area,
1206            cairo_t *cr,
1207            gboolean is_stroke,
1208            FooScrollAreaEventFunc func,
1209            gpointer data)
1210 {
1211   user_to_device_data conversion_data;
1212 
1213   InputPath *path = g_new0 (InputPath, 1);
1214 
1215   conversion_data.cr = cr;
1216   gtk_widget_get_allocation(GTK_WIDGET (area), &conversion_data.allocation);
1217 
1218   path->is_stroke = is_stroke;
1219   path->fill_rule = cairo_get_fill_rule (cr);
1220   path->line_width = cairo_get_line_width (cr);
1221   path->path = cairo_copy_path (cr);
1222   /* FIXME: this shifts the input region for some reason - half below the actual
1223      monitor widget - or is the drawing incorrectly shifted?  The drag region
1224      corresponds to what would be a centered monitor vertically.
1225      path_foreach_point (path->path, user_to_device, &conversion_data);
1226   */
1227   path->func = func;
1228   path->data = data;
1229   path->next = area->priv->current_input->paths;
1230   area->priv->current_input->paths = path;
1231   return path;
1232 }
1233 
1234 /* FIXME: we probably really want a
1235  *
1236  *      foo_scroll_area_add_input_from_fill (area, cr, ...);
1237  * and
1238  *      foo_scroll_area_add_input_from_stroke (area, cr, ...);
1239  * as well.
1240  */
1241 void
foo_scroll_area_add_input_from_fill(FooScrollArea * scroll_area,cairo_t * cr,FooScrollAreaEventFunc func,gpointer data)1242 foo_scroll_area_add_input_from_fill (FooScrollArea           *scroll_area,
1243                                      cairo_t                 *cr,
1244                                      FooScrollAreaEventFunc   func,
1245                                      gpointer                 data)
1246 {
1247   g_return_if_fail (FOO_IS_SCROLL_AREA (scroll_area));
1248   g_return_if_fail (cr != NULL);
1249   g_return_if_fail (scroll_area->priv->current_input);
1250 
1251   make_path (scroll_area, cr, FALSE, func, data);
1252 }
1253 
1254 void
foo_scroll_area_add_input_from_stroke(FooScrollArea * scroll_area,cairo_t * cr,FooScrollAreaEventFunc func,gpointer data)1255 foo_scroll_area_add_input_from_stroke (FooScrollArea           *scroll_area,
1256                                        cairo_t                  *cr,
1257                                        FooScrollAreaEventFunc   func,
1258                                        gpointer                 data)
1259 {
1260   g_return_if_fail (FOO_IS_SCROLL_AREA (scroll_area));
1261   g_return_if_fail (cr != NULL);
1262   g_return_if_fail (scroll_area->priv->current_input);
1263 
1264   make_path (scroll_area, cr, TRUE, func, data);
1265 }
1266 
1267 void
foo_scroll_area_invalidate(FooScrollArea * scroll_area)1268 foo_scroll_area_invalidate (FooScrollArea *scroll_area)
1269 {
1270   GtkAllocation allocation;
1271   GtkWidget *widget = GTK_WIDGET (scroll_area);
1272 
1273   gtk_widget_get_allocation (widget, &allocation);
1274   foo_scroll_area_invalidate_rect (scroll_area,
1275                                    scroll_area->priv->x_offset, scroll_area->priv->y_offset,
1276                                    allocation.width,
1277                                    allocation.height);
1278 }
1279 
1280 static void
canvas_to_window(FooScrollArea * area,cairo_region_t * region)1281 canvas_to_window (FooScrollArea  *area,
1282                   cairo_region_t *region)
1283 {
1284   GtkAllocation allocation;
1285   GtkWidget *widget = GTK_WIDGET (area);
1286 
1287   gtk_widget_get_allocation (widget, &allocation);
1288   cairo_region_translate (region,
1289                           -area->priv->x_offset + allocation.x,
1290                           -area->priv->y_offset + allocation.y);
1291 }
1292 
1293 static void
window_to_canvas(FooScrollArea * area,cairo_region_t * region)1294 window_to_canvas (FooScrollArea  *area,
1295                   cairo_region_t *region)
1296 {
1297   GtkAllocation allocation;
1298   GtkWidget *widget = GTK_WIDGET (area);
1299 
1300   gtk_widget_get_allocation (widget, &allocation);
1301   cairo_region_translate (region,
1302                           area->priv->x_offset - allocation.x,
1303                           area->priv->y_offset - allocation.y);
1304 }
1305 
1306 void
foo_scroll_area_invalidate_region(FooScrollArea * area,cairo_region_t * region)1307 foo_scroll_area_invalidate_region (FooScrollArea  *area,
1308                                    cairo_region_t *region)
1309 {
1310   GtkWidget *widget;
1311 
1312   g_return_if_fail (FOO_IS_SCROLL_AREA (area));
1313 
1314   widget = GTK_WIDGET (area);
1315 
1316   cairo_region_union (area->priv->update_region, region);
1317 
1318   if (gtk_widget_get_realized (widget))
1319     {
1320       canvas_to_window (area, region);
1321 
1322       gdk_window_invalidate_region (gtk_widget_get_window (widget),
1323                                     region, TRUE);
1324 
1325       window_to_canvas (area, region);
1326     }
1327 }
1328 
1329 void
foo_scroll_area_invalidate_rect(FooScrollArea * scroll_area,int x,int y,int width,int height)1330 foo_scroll_area_invalidate_rect (FooScrollArea *scroll_area,
1331                                  int            x,
1332                                  int            y,
1333                                  int            width,
1334                                  int            height)
1335 {
1336   cairo_rectangle_int_t rect = { x, y, width, height };
1337   cairo_region_t *region;
1338 
1339   g_return_if_fail (FOO_IS_SCROLL_AREA (scroll_area));
1340 
1341   region = cairo_region_create_rectangle (&rect);
1342 
1343   foo_scroll_area_invalidate_region (scroll_area, region);
1344 
1345   cairo_region_destroy (region);
1346 }
1347 
1348 void
foo_scroll_area_begin_grab(FooScrollArea * scroll_area,FooScrollAreaEventFunc func,gpointer input_data)1349 foo_scroll_area_begin_grab (FooScrollArea *scroll_area,
1350                             FooScrollAreaEventFunc func,
1351                             gpointer       input_data)
1352 {
1353   g_return_if_fail (FOO_IS_SCROLL_AREA (scroll_area));
1354   g_return_if_fail (!scroll_area->priv->grabbed);
1355 
1356   scroll_area->priv->grabbed = TRUE;
1357   scroll_area->priv->grab_func = func;
1358   scroll_area->priv->grab_data = input_data;
1359 
1360   /* FIXME: we should probably take a server grab */
1361   /* Also, maybe there should be support for setting the grab cursor */
1362 }
1363 
1364 void
foo_scroll_area_end_grab(FooScrollArea * scroll_area,FooScrollAreaEvent * event)1365 foo_scroll_area_end_grab (FooScrollArea *scroll_area,
1366                           FooScrollAreaEvent *event)
1367 {
1368   g_return_if_fail (FOO_IS_SCROLL_AREA (scroll_area));
1369 
1370   scroll_area->priv->grabbed = FALSE;
1371   scroll_area->priv->grab_func = NULL;
1372   scroll_area->priv->grab_data = NULL;
1373 
1374   if (event != NULL)
1375     process_event (scroll_area, FOO_DROP, event->x, event->y);
1376 }
1377 
1378 gboolean
foo_scroll_area_is_grabbed(FooScrollArea * scroll_area)1379 foo_scroll_area_is_grabbed (FooScrollArea *scroll_area)
1380 {
1381   return scroll_area->priv->grabbed;
1382 }
1383 
1384 void
foo_scroll_area_set_viewport_pos(FooScrollArea * scroll_area,int x,int y)1385 foo_scroll_area_set_viewport_pos (FooScrollArea  *scroll_area,
1386                                   int             x,
1387                                   int             y)
1388 {
1389   g_object_freeze_notify (G_OBJECT (scroll_area->priv->hadj));
1390   g_object_freeze_notify (G_OBJECT (scroll_area->priv->vadj));
1391   gtk_adjustment_set_value (scroll_area->priv->hadj, x);
1392   gtk_adjustment_set_value (scroll_area->priv->vadj, y);
1393 
1394   set_adjustment_values (scroll_area);
1395   g_object_thaw_notify (G_OBJECT (scroll_area->priv->hadj));
1396   g_object_thaw_notify (G_OBJECT (scroll_area->priv->vadj));
1397 }
1398 
1399 static gboolean
rect_contains(const GdkRectangle * rect,int x,int y)1400 rect_contains (const GdkRectangle *rect, int x, int y)
1401 {
1402   return (x >= rect->x                &&
1403           y >= rect->y                &&
1404           x  < rect->x + rect->width  &&
1405           y  < rect->y + rect->height);
1406 }
1407 
1408 static void
stop_scrolling(FooScrollArea * area)1409 stop_scrolling (FooScrollArea *area)
1410 {
1411 #if 0
1412   g_print ("stop scrolling\n");
1413 #endif
1414   if (area->priv->auto_scroll_info)
1415     {
1416       if (area->priv->auto_scroll_info->timeout_id) {
1417         g_source_remove (area->priv->auto_scroll_info->timeout_id);
1418         area->priv->auto_scroll_info->timeout_id = 0;
1419       }
1420       g_timer_destroy (area->priv->auto_scroll_info->timer);
1421       g_free (area->priv->auto_scroll_info);
1422 
1423       area->priv->auto_scroll_info = NULL;
1424     }
1425 }
1426 
1427 static gboolean
scroll_idle(gpointer data)1428 scroll_idle (gpointer data)
1429 {
1430   GdkRectangle viewport, new_viewport;
1431   FooScrollArea *area = data;
1432   AutoScrollInfo *info = area->priv->auto_scroll_info;
1433   int new_x, new_y;
1434   double elapsed;
1435 
1436   get_viewport (area, &viewport);
1437 
1438 #if 0
1439   g_print ("old info: %d %d\n", info->dx, info->dy);
1440 
1441   g_print ("timeout (%d %d)\n", dx, dy);
1442 #endif
1443 
1444 #if 0
1445   g_print ("new info %d %d\n", info->dx, info->dy);
1446 #endif
1447 
1448   elapsed = g_timer_elapsed (info->timer, NULL);
1449 
1450   info->res_x = elapsed * info->dx / 0.2;
1451   info->res_y = elapsed * info->dy / 0.2;
1452 
1453 #if 0
1454   g_print ("%f %f\n", info->res_x, info->res_y);
1455 #endif
1456 
1457   new_x = viewport.x + info->res_x;
1458   new_y = viewport.y + info->res_y;
1459 
1460 #if 0
1461   g_print ("%f\n", elapsed * (info->dx / 0.2));
1462 #endif
1463 
1464 #if 0
1465   g_print ("new_x, new_y\n: %d %d\n", new_x, new_y);
1466 #endif
1467 
1468   foo_scroll_area_set_viewport_pos (area, new_x, new_y);
1469 #if 0
1470   viewport.x + info->dx,
1471     viewport.y + info->dy);
1472 #endif
1473 
1474 get_viewport (area, &new_viewport);
1475 
1476 if (viewport.x == new_viewport.x            &&
1477     viewport.y == new_viewport.y            &&
1478     (info->res_x > 1.0                      ||
1479      info->res_y > 1.0                      ||
1480      info->res_x < -1.0                     ||
1481      info->res_y < -1.0))
1482   {
1483     stop_scrolling (area);
1484 
1485     /* stop scrolling if it didn't have an effect */
1486     return FALSE;
1487   }
1488 
1489 return TRUE;
1490 }
1491 
1492 static void
ensure_scrolling(FooScrollArea * area,int dx,int dy)1493 ensure_scrolling (FooScrollArea *area,
1494                   int            dx,
1495                   int            dy)
1496 {
1497   if (!area->priv->auto_scroll_info)
1498     {
1499 #if 0
1500       g_print ("start scrolling\n");
1501 #endif
1502       area->priv->auto_scroll_info = g_new0 (AutoScrollInfo, 1);
1503       area->priv->auto_scroll_info->timeout_id =
1504         g_idle_add (scroll_idle, area);
1505       area->priv->auto_scroll_info->timer = g_timer_new ();
1506     }
1507 
1508 #if 0
1509   g_print ("setting scrolling to %d %d\n", dx, dy);
1510 #endif
1511 
1512 #if 0
1513   g_print ("dx, dy: %d %d\n", dx, dy);
1514 #endif
1515 
1516   area->priv->auto_scroll_info->dx = dx;
1517   area->priv->auto_scroll_info->dy = dy;
1518 }
1519 
1520 void
foo_scroll_area_auto_scroll(FooScrollArea * scroll_area,FooScrollAreaEvent * event)1521 foo_scroll_area_auto_scroll (FooScrollArea *scroll_area,
1522                              FooScrollAreaEvent *event)
1523 {
1524   GdkRectangle viewport;
1525 
1526   get_viewport (scroll_area, &viewport);
1527 
1528   if (rect_contains (&viewport, event->x, event->y))
1529     {
1530       stop_scrolling (scroll_area);
1531     }
1532   else
1533     {
1534       int dx, dy;
1535 
1536       dx = dy = 0;
1537 
1538       if (event->y < viewport.y)
1539         {
1540           dy = event->y - viewport.y;
1541           dy = MIN (dy + 2, 0);
1542         }
1543       else if (event->y >= viewport.y + viewport.height)
1544         {
1545           dy = event->y - (viewport.y + viewport.height - 1);
1546           dy = MAX (dy - 2, 0);
1547         }
1548 
1549       if (event->x < viewport.x)
1550         {
1551           dx = event->x - viewport.x;
1552           dx = MIN (dx + 2, 0);
1553         }
1554       else if (event->x >= viewport.x + viewport.width)
1555         {
1556           dx = event->x - (viewport.x + viewport.width - 1);
1557           dx = MAX (dx - 2, 0);
1558         }
1559 
1560 #if 0
1561       g_print ("dx, dy: %d %d\n", dx, dy);
1562 #endif
1563 
1564       ensure_scrolling (scroll_area, dx, dy);
1565     }
1566 }
1567 
1568 void
foo_scroll_area_begin_auto_scroll(FooScrollArea * scroll_area)1569 foo_scroll_area_begin_auto_scroll (FooScrollArea *scroll_area)
1570 {
1571   /* noop  for now */
1572 }
1573 
1574 void
foo_scroll_area_end_auto_scroll(FooScrollArea * scroll_area)1575 foo_scroll_area_end_auto_scroll (FooScrollArea *scroll_area)
1576 {
1577   stop_scrolling (scroll_area);
1578 }
1579 
1580