~kroq-gar78/ubuntu/precise/gnome-control-center/fix-885947

« back to all changes in this revision

Viewing changes to panels/display/scrollarea.c

  • Committer: Bazaar Package Importer
  • Author(s): Rodrigo Moya
  • Date: 2011-05-17 10:47:27 UTC
  • mfrom: (0.1.11 experimental) (1.1.45 upstream)
  • Revision ID: james.westby@ubuntu.com-20110517104727-lqel6m8vhfw5jby1
Tags: 1:3.0.1.1-1ubuntu1
* Rebase on Debian, remaining Ubuntu changes:
* debian/control:
  - Build-Depend on hardening-wrapper, dpkg-dev and dh-autoreconf
  - Add dependency on ubuntu-system-service
  - Remove dependency on gnome-icon-theme-symbolic
  - Move dependency on apg, gnome-icon-theme-symbolic and accountsservice to
    be a Recommends: until we get them in main
* debian/rules:
  - Use autoreconf
  - Add binary-post-install rule for gnome-control-center-data
  - Run dh-autoreconf
* debian/gnome-control-center.dirs:
* debian/gnome-control-center.links:
  - Add a link to the control center shell for indicators
* debian/patches/00_disable-nm.patch:
  - Temporary patch to disable building with NetworkManager until we get
    the new one in the archive
* debian/patches/01_git_remove_gettext_calls.patch:
  - Remove calls to AM_GNU_GETTEXT, IT_PROG_INTLTOOL should be enough
* debian/patches/01_git_kill_warning.patch:
  - Kill warning
* debian/patches/50_ubuntu_systemwide_prefs.patch:
  - Ubuntu specific proxy preferences
* debian/patches/51_ubuntu_system_keyboard.patch:
  - Implement the global keyboard spec at https://wiki.ubuntu.com/DefaultKeyboardSettings

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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., 59 Temple Place - Suite 330,
 
16
 * Boston, MA 02111-1307, 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
 
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
 
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
 
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
 
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
 
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
 
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 *
 
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
 
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
 
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
 
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
 
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
 
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
 
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
 
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
 
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
 
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
 
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
 
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
 
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
 
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
 
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
 
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
 
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 *
 
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
 
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
 
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
 
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
 
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
 
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
 
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
 
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
 
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
 
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
 
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
 
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
 
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
 
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
 
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
 
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
 
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
 
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
 
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 *
 
1163
foo_scroll_area_new (void)
 
1164
{
 
1165
  return g_object_new (FOO_TYPE_SCROLL_AREA, NULL);
 
1166
}
 
1167
 
 
1168
void
 
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
static void
 
1186
user_to_device (double *x, double *y,
 
1187
                gpointer data)
 
1188
{
 
1189
#if 0
 
1190
  cairo_t *cr = data;
 
1191
 
 
1192
  /* FIXME: not set transform in first place? */
 
1193
  cairo_user_to_device (cr, x, y);
 
1194
#endif
 
1195
}
 
1196
 
 
1197
static InputPath *
 
1198
make_path (FooScrollArea *area,
 
1199
           cairo_t *cr,
 
1200
           gboolean is_stroke,
 
1201
           FooScrollAreaEventFunc func,
 
1202
           gpointer data)
 
1203
{
 
1204
  InputPath *path = g_new0 (InputPath, 1);
 
1205
 
 
1206
  path->is_stroke = is_stroke;
 
1207
  path->fill_rule = cairo_get_fill_rule (cr);
 
1208
  path->line_width = cairo_get_line_width (cr);
 
1209
  path->path = cairo_copy_path (cr);
 
1210
  path_foreach_point (path->path, user_to_device, cr);
 
1211
  path->func = func;
 
1212
  path->data = data;
 
1213
  path->next = area->priv->current_input->paths;
 
1214
  area->priv->current_input->paths = path;
 
1215
  return path;
 
1216
}
 
1217
 
 
1218
/* FIXME: we probably really want a
 
1219
 *
 
1220
 *      foo_scroll_area_add_input_from_fill (area, cr, ...);
 
1221
 * and
 
1222
 *      foo_scroll_area_add_input_from_stroke (area, cr, ...);
 
1223
 * as well.
 
1224
 */
 
1225
void
 
1226
foo_scroll_area_add_input_from_fill (FooScrollArea           *scroll_area,
 
1227
                                     cairo_t                 *cr,
 
1228
                                     FooScrollAreaEventFunc   func,
 
1229
                                     gpointer                 data)
 
1230
{
 
1231
  g_return_if_fail (FOO_IS_SCROLL_AREA (scroll_area));
 
1232
  g_return_if_fail (cr != NULL);
 
1233
  g_return_if_fail (scroll_area->priv->current_input);
 
1234
 
 
1235
  make_path (scroll_area, cr, FALSE, func, data);
 
1236
}
 
1237
 
 
1238
void
 
1239
foo_scroll_area_add_input_from_stroke (FooScrollArea           *scroll_area,
 
1240
                                       cairo_t                  *cr,
 
1241
                                       FooScrollAreaEventFunc   func,
 
1242
                                       gpointer                 data)
 
1243
{
 
1244
  g_return_if_fail (FOO_IS_SCROLL_AREA (scroll_area));
 
1245
  g_return_if_fail (cr != NULL);
 
1246
  g_return_if_fail (scroll_area->priv->current_input);
 
1247
 
 
1248
  make_path (scroll_area, cr, TRUE, func, data);
 
1249
}
 
1250
 
 
1251
void
 
1252
foo_scroll_area_invalidate (FooScrollArea *scroll_area)
 
1253
{
 
1254
  GtkAllocation allocation;
 
1255
  GtkWidget *widget = GTK_WIDGET (scroll_area);
 
1256
 
 
1257
  gtk_widget_get_allocation (widget, &allocation);
 
1258
  foo_scroll_area_invalidate_rect (scroll_area,
 
1259
                                   scroll_area->priv->x_offset, scroll_area->priv->y_offset,
 
1260
                                   allocation.width,
 
1261
                                   allocation.height);
 
1262
}
 
1263
 
 
1264
static void
 
1265
canvas_to_window (FooScrollArea  *area,
 
1266
                  cairo_region_t *region)
 
1267
{
 
1268
  GtkAllocation allocation;
 
1269
  GtkWidget *widget = GTK_WIDGET (area);
 
1270
 
 
1271
  gtk_widget_get_allocation (widget, &allocation);
 
1272
  cairo_region_translate (region,
 
1273
                          -area->priv->x_offset + allocation.x,
 
1274
                          -area->priv->y_offset + allocation.y);
 
1275
}
 
1276
 
 
1277
static void
 
1278
window_to_canvas (FooScrollArea  *area,
 
1279
                  cairo_region_t *region)
 
1280
{
 
1281
  GtkAllocation allocation;
 
1282
  GtkWidget *widget = GTK_WIDGET (area);
 
1283
 
 
1284
  gtk_widget_get_allocation (widget, &allocation);
 
1285
  cairo_region_translate (region,
 
1286
                          area->priv->x_offset - allocation.x,
 
1287
                          area->priv->y_offset - allocation.y);
 
1288
}
 
1289
 
 
1290
void
 
1291
foo_scroll_area_invalidate_region (FooScrollArea  *area,
 
1292
                                   cairo_region_t *region)
 
1293
{
 
1294
  GtkWidget *widget;
 
1295
 
 
1296
  g_return_if_fail (FOO_IS_SCROLL_AREA (area));
 
1297
 
 
1298
  widget = GTK_WIDGET (area);
 
1299
 
 
1300
  cairo_region_union (area->priv->update_region, region);
 
1301
 
 
1302
  if (gtk_widget_get_realized (widget))
 
1303
    {
 
1304
      canvas_to_window (area, region);
 
1305
 
 
1306
      gdk_window_invalidate_region (gtk_widget_get_window (widget),
 
1307
                                    region, TRUE);
 
1308
 
 
1309
      window_to_canvas (area, region);
 
1310
    }
 
1311
}
 
1312
 
 
1313
void
 
1314
foo_scroll_area_invalidate_rect (FooScrollArea *scroll_area,
 
1315
                                 int            x,
 
1316
                                 int            y,
 
1317
                                 int            width,
 
1318
                                 int            height)
 
1319
{
 
1320
  cairo_rectangle_int_t rect = { x, y, width, height };
 
1321
  cairo_region_t *region;
 
1322
 
 
1323
  g_return_if_fail (FOO_IS_SCROLL_AREA (scroll_area));
 
1324
 
 
1325
  region = cairo_region_create_rectangle (&rect);
 
1326
 
 
1327
  foo_scroll_area_invalidate_region (scroll_area, region);
 
1328
 
 
1329
  cairo_region_destroy (region);
 
1330
}
 
1331
 
 
1332
void
 
1333
foo_scroll_area_begin_grab (FooScrollArea *scroll_area,
 
1334
                            FooScrollAreaEventFunc func,
 
1335
                            gpointer       input_data)
 
1336
{
 
1337
  g_return_if_fail (FOO_IS_SCROLL_AREA (scroll_area));
 
1338
  g_return_if_fail (!scroll_area->priv->grabbed);
 
1339
 
 
1340
  scroll_area->priv->grabbed = TRUE;
 
1341
  scroll_area->priv->grab_func = func;
 
1342
  scroll_area->priv->grab_data = input_data;
 
1343
 
 
1344
  /* FIXME: we should probably take a server grab */
 
1345
  /* Also, maybe there should be support for setting the grab cursor */
 
1346
}
 
1347
 
 
1348
void
 
1349
foo_scroll_area_end_grab (FooScrollArea *scroll_area,
 
1350
                          FooScrollAreaEvent *event)
 
1351
{
 
1352
  g_return_if_fail (FOO_IS_SCROLL_AREA (scroll_area));
 
1353
 
 
1354
  scroll_area->priv->grabbed = FALSE;
 
1355
  scroll_area->priv->grab_func = NULL;
 
1356
  scroll_area->priv->grab_data = NULL;
 
1357
 
 
1358
  if (event != NULL)
 
1359
    process_event (scroll_area, FOO_DROP, event->x, event->y);
 
1360
}
 
1361
 
 
1362
gboolean
 
1363
foo_scroll_area_is_grabbed (FooScrollArea *scroll_area)
 
1364
{
 
1365
  return scroll_area->priv->grabbed;
 
1366
}
 
1367
 
 
1368
void
 
1369
foo_scroll_area_set_viewport_pos (FooScrollArea  *scroll_area,
 
1370
                                  int             x,
 
1371
                                  int             y)
 
1372
{
 
1373
  g_object_freeze_notify (G_OBJECT (scroll_area->priv->hadj));
 
1374
  g_object_freeze_notify (G_OBJECT (scroll_area->priv->vadj));
 
1375
  gtk_adjustment_set_value (scroll_area->priv->hadj, x);
 
1376
  gtk_adjustment_set_value (scroll_area->priv->vadj, y);
 
1377
 
 
1378
  set_adjustment_values (scroll_area);
 
1379
  g_object_thaw_notify (G_OBJECT (scroll_area->priv->hadj));
 
1380
  g_object_thaw_notify (G_OBJECT (scroll_area->priv->vadj));
 
1381
}
 
1382
 
 
1383
static gboolean
 
1384
rect_contains (const GdkRectangle *rect, int x, int y)
 
1385
{
 
1386
  return (x >= rect->x                &&
 
1387
          y >= rect->y                &&
 
1388
          x  < rect->x + rect->width  &&
 
1389
          y  < rect->y + rect->height);
 
1390
}
 
1391
 
 
1392
static void
 
1393
stop_scrolling (FooScrollArea *area)
 
1394
{
 
1395
#if 0
 
1396
  g_print ("stop scrolling\n");
 
1397
#endif
 
1398
  if (area->priv->auto_scroll_info)
 
1399
    {
 
1400
      g_source_remove (area->priv->auto_scroll_info->timeout_id);
 
1401
      g_timer_destroy (area->priv->auto_scroll_info->timer);
 
1402
      g_free (area->priv->auto_scroll_info);
 
1403
 
 
1404
      area->priv->auto_scroll_info = NULL;
 
1405
    }
 
1406
}
 
1407
 
 
1408
static gboolean
 
1409
scroll_idle (gpointer data)
 
1410
{
 
1411
  GdkRectangle viewport, new_viewport;
 
1412
  FooScrollArea *area = data;
 
1413
  AutoScrollInfo *info = area->priv->auto_scroll_info;
 
1414
  int new_x, new_y;
 
1415
  double elapsed;
 
1416
 
 
1417
  get_viewport (area, &viewport);
 
1418
 
 
1419
#if 0
 
1420
  g_print ("old info: %d %d\n", info->dx, info->dy);
 
1421
 
 
1422
  g_print ("timeout (%d %d)\n", dx, dy);
 
1423
#endif
 
1424
 
 
1425
#if 0
 
1426
  g_print ("new info %d %d\n", info->dx, info->dy);
 
1427
#endif
 
1428
 
 
1429
  elapsed = g_timer_elapsed (info->timer, NULL);
 
1430
 
 
1431
  info->res_x = elapsed * info->dx / 0.2;
 
1432
  info->res_y = elapsed * info->dy / 0.2;
 
1433
 
 
1434
#if 0
 
1435
  g_print ("%f %f\n", info->res_x, info->res_y);
 
1436
#endif
 
1437
 
 
1438
  new_x = viewport.x + info->res_x;
 
1439
  new_y = viewport.y + info->res_y;
 
1440
 
 
1441
#if 0
 
1442
  g_print ("%f\n", elapsed * (info->dx / 0.2));
 
1443
#endif
 
1444
 
 
1445
#if 0
 
1446
  g_print ("new_x, new_y\n: %d %d\n", new_x, new_y);
 
1447
#endif
 
1448
 
 
1449
  foo_scroll_area_set_viewport_pos (area, new_x, new_y);
 
1450
#if 0
 
1451
  viewport.x + info->dx,
 
1452
    viewport.y + info->dy);
 
1453
#endif
 
1454
 
 
1455
get_viewport (area, &new_viewport);
 
1456
 
 
1457
if (viewport.x == new_viewport.x            &&
 
1458
    viewport.y == new_viewport.y            &&
 
1459
    (info->res_x > 1.0                      ||
 
1460
     info->res_y > 1.0                      ||
 
1461
     info->res_x < -1.0                     ||
 
1462
     info->res_y < -1.0))
 
1463
  {
 
1464
    stop_scrolling (area);
 
1465
 
 
1466
    /* stop scrolling if it didn't have an effect */
 
1467
    return FALSE;
 
1468
  }
 
1469
 
 
1470
return TRUE;
 
1471
}
 
1472
 
 
1473
static void
 
1474
ensure_scrolling (FooScrollArea *area,
 
1475
                  int            dx,
 
1476
                  int            dy)
 
1477
{
 
1478
  if (!area->priv->auto_scroll_info)
 
1479
    {
 
1480
#if 0
 
1481
      g_print ("start scrolling\n");
 
1482
#endif
 
1483
      area->priv->auto_scroll_info = g_new0 (AutoScrollInfo, 1);
 
1484
      area->priv->auto_scroll_info->timeout_id =
 
1485
        g_idle_add (scroll_idle, area);
 
1486
      area->priv->auto_scroll_info->timer = g_timer_new ();
 
1487
    }
 
1488
 
 
1489
#if 0
 
1490
  g_print ("setting scrolling to %d %d\n", dx, dy);
 
1491
#endif
 
1492
 
 
1493
#if 0
 
1494
  g_print ("dx, dy: %d %d\n", dx, dy);
 
1495
#endif
 
1496
 
 
1497
  area->priv->auto_scroll_info->dx = dx;
 
1498
  area->priv->auto_scroll_info->dy = dy;
 
1499
}
 
1500
 
 
1501
void
 
1502
foo_scroll_area_auto_scroll (FooScrollArea *scroll_area,
 
1503
                             FooScrollAreaEvent *event)
 
1504
{
 
1505
  GdkRectangle viewport;
 
1506
 
 
1507
  get_viewport (scroll_area, &viewport);
 
1508
 
 
1509
  if (rect_contains (&viewport, event->x, event->y))
 
1510
    {
 
1511
      stop_scrolling (scroll_area);
 
1512
    }
 
1513
  else
 
1514
    {
 
1515
      int dx, dy;
 
1516
 
 
1517
      dx = dy = 0;
 
1518
 
 
1519
      if (event->y < viewport.y)
 
1520
        {
 
1521
          dy = event->y - viewport.y;
 
1522
          dy = MIN (dy + 2, 0);
 
1523
        }
 
1524
      else if (event->y >= viewport.y + viewport.height)
 
1525
        {
 
1526
          dy = event->y - (viewport.y + viewport.height - 1);
 
1527
          dy = MAX (dy - 2, 0);
 
1528
        }
 
1529
 
 
1530
      if (event->x < viewport.x)
 
1531
        {
 
1532
          dx = event->x - viewport.x;
 
1533
          dx = MIN (dx + 2, 0);
 
1534
        }
 
1535
      else if (event->x >= viewport.x + viewport.width)
 
1536
        {
 
1537
          dx = event->x - (viewport.x + viewport.width - 1);
 
1538
          dx = MAX (dx - 2, 0);
 
1539
        }
 
1540
 
 
1541
#if 0
 
1542
      g_print ("dx, dy: %d %d\n", dx, dy);
 
1543
#endif
 
1544
 
 
1545
      ensure_scrolling (scroll_area, dx, dy);
 
1546
    }
 
1547
}
 
1548
 
 
1549
void
 
1550
foo_scroll_area_begin_auto_scroll (FooScrollArea *scroll_area)
 
1551
{
 
1552
  /* noop  for now */
 
1553
}
 
1554
 
 
1555
void
 
1556
foo_scroll_area_end_auto_scroll (FooScrollArea *scroll_area)
 
1557
{
 
1558
  stop_scrolling (scroll_area);
 
1559
}
 
1560