gimpoverlaybox.c 15.2 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/* GIMP - The GNU Image Manipulation Program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * GimpOverlayBox
 * Copyright (C) 2009 Michael Natterer <mitch@gimp.org>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "config.h"

#include <gtk/gtk.h>

#include "widgets-types.h"

#include "gimpoverlaybox.h"
#include "gimpoverlaychild.h"


/*  local function prototypes  */

static void        gimp_overlay_box_set_property        (GObject        *object,
                                                         guint           property_id,
                                                         const GValue   *value,
                                                         GParamSpec     *pspec);
static void        gimp_overlay_box_get_property        (GObject        *object,
                                                         guint           property_id,
                                                         GValue         *value,
                                                         GParamSpec     *pspec);

static void        gimp_overlay_box_realize             (GtkWidget      *widget);
static void        gimp_overlay_box_unrealize           (GtkWidget      *widget);
static void        gimp_overlay_box_size_request        (GtkWidget      *widget,
                                                         GtkRequisition *requisition);
static void        gimp_overlay_box_size_allocate       (GtkWidget      *widget,
                                                         GtkAllocation  *allocation);
static gboolean    gimp_overlay_box_expose              (GtkWidget      *widget,
                                                         GdkEventExpose *event);
static gboolean    gimp_overlay_box_damage              (GtkWidget      *widget,
                                                         GdkEventExpose *event);

static void        gimp_overlay_box_add                 (GtkContainer   *container,
                                                         GtkWidget      *widget);
static void        gimp_overlay_box_remove              (GtkContainer   *container,
                                                         GtkWidget      *widget);
static void        gimp_overlay_box_forall              (GtkContainer   *container,
                                                         gboolean        include_internals,
                                                         GtkCallback     callback,
                                                         gpointer        callback_data);
static GType       gimp_overlay_box_child_type          (GtkContainer   *container);

static GdkWindow * gimp_overlay_box_pick_embedded_child (GdkWindow      *window,
                                                         gdouble         x,
                                                         gdouble         y,
                                                         GimpOverlayBox *box);


G_DEFINE_TYPE (GimpOverlayBox, gimp_overlay_box, GTK_TYPE_CONTAINER)

#define parent_class gimp_overlay_box_parent_class


static void
gimp_overlay_box_class_init (GimpOverlayBoxClass *klass)
{
  GObjectClass      *object_class    = G_OBJECT_CLASS (klass);
  GtkWidgetClass    *widget_class    = GTK_WIDGET_CLASS (klass);
  GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);

  object_class->set_property  = gimp_overlay_box_set_property;
  object_class->get_property  = gimp_overlay_box_get_property;

  widget_class->realize       = gimp_overlay_box_realize;
  widget_class->unrealize     = gimp_overlay_box_unrealize;
  widget_class->size_request  = gimp_overlay_box_size_request;
  widget_class->size_allocate = gimp_overlay_box_size_allocate;
  widget_class->expose_event  = gimp_overlay_box_expose;

  g_signal_override_class_handler ("damage-event",
                                   GIMP_TYPE_OVERLAY_BOX,
                                   G_CALLBACK (gimp_overlay_box_damage));

  container_class->add        = gimp_overlay_box_add;
  container_class->remove     = gimp_overlay_box_remove;
  container_class->forall     = gimp_overlay_box_forall;
  container_class->child_type = gimp_overlay_box_child_type;
}

static void
gimp_overlay_box_init (GimpOverlayBox *box)
{
103
  gtk_widget_set_has_window (GTK_WIDGET (box), TRUE);
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
}

static void
gimp_overlay_box_set_property (GObject      *object,
                               guint         property_id,
                               const GValue *value,
                               GParamSpec   *pspec)
{
  switch (property_id)
    {
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static void
gimp_overlay_box_get_property (GObject    *object,
                               guint       property_id,
                               GValue     *value,
                               GParamSpec *pspec)
{
  switch (property_id)
    {
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static void
gimp_overlay_box_realize (GtkWidget *widget)
{
  GimpOverlayBox *box = GIMP_OVERLAY_BOX (widget);
  GtkAllocation   allocation;
  GdkWindowAttr   attributes;
  gint            attributes_mask;
  GList          *list;

143
  gtk_widget_set_realized (widget, TRUE);
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167

  gtk_widget_get_allocation (widget, &allocation);

  attributes.x           = allocation.x;
  attributes.y           = allocation.y;
  attributes.width       = allocation.width;
  attributes.height      = allocation.height;
  attributes.window_type = GDK_WINDOW_CHILD;
  attributes.wclass      = GDK_INPUT_OUTPUT;
  attributes.visual      = gtk_widget_get_visual (widget);
  attributes.colormap    = gtk_widget_get_colormap (widget);
  attributes.event_mask  = gtk_widget_get_events (widget);

  attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;

  gtk_widget_set_window (widget,
                         gdk_window_new (gtk_widget_get_parent_window (widget),
                                         &attributes, attributes_mask));
  gdk_window_set_user_data (gtk_widget_get_window (widget), widget);

  g_signal_connect (gtk_widget_get_window (widget), "pick-embedded-child",
                    G_CALLBACK (gimp_overlay_box_pick_embedded_child),
                    widget);

168
  gtk_widget_style_attach (widget);
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
  gtk_style_set_background (gtk_widget_get_style (widget),
                            gtk_widget_get_window (widget),
                            GTK_STATE_NORMAL);

  for (list = box->children; list; list = g_list_next (list))
    gimp_overlay_child_realize (box, list->data);
}

static void
gimp_overlay_box_unrealize (GtkWidget *widget)
{
  GimpOverlayBox *box = GIMP_OVERLAY_BOX (widget);
  GList          *list;

  for (list = box->children; list; list = g_list_next (list))
    gimp_overlay_child_unrealize (box, list->data);

  GTK_WIDGET_CLASS (parent_class)->unrealize (widget);
}

static void
gimp_overlay_box_size_request (GtkWidget      *widget,
                               GtkRequisition *requisition)
{
  GimpOverlayBox *box = GIMP_OVERLAY_BOX (widget);
  GList          *list;
  gint            border_width;

  border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));

  requisition->width  = 1 + 2 * border_width;
  requisition->height = 1 + 2 * border_width;

  for (list = box->children; list; list = g_list_next (list))
    gimp_overlay_child_size_request (box, list->data);
}

static void
gimp_overlay_box_size_allocate (GtkWidget     *widget,
                                GtkAllocation *allocation)
{
  GimpOverlayBox *box = GIMP_OVERLAY_BOX (widget);
  GList          *list;

  gtk_widget_set_allocation (widget, allocation);

215
  if (gtk_widget_get_realized (widget))
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
    gdk_window_move_resize (gtk_widget_get_window (widget),
                            allocation->x,
                            allocation->y,
                            allocation->width,
                            allocation->height);

  for (list = box->children; list; list = g_list_next (list))
    gimp_overlay_child_size_allocate (box, list->data);
}

static gboolean
gimp_overlay_box_expose (GtkWidget      *widget,
                         GdkEventExpose *event)
{
  if (gtk_widget_is_drawable (widget))
    {
      GimpOverlayBox *box = GIMP_OVERLAY_BOX (widget);
      GList          *list;

      for (list = box->children; list; list = g_list_next (list))
        {
          if (gimp_overlay_child_expose (box, list->data, event))
            return TRUE;
        }
    }

  return FALSE;
}

static gboolean
gimp_overlay_box_damage (GtkWidget      *widget,
                         GdkEventExpose *event)
{
  GimpOverlayBox *box = GIMP_OVERLAY_BOX (widget);
  GList          *list;

  for (list = box->children; list; list = g_list_next (list))
    {
      if (gimp_overlay_child_damage (box, list->data, event))
        return TRUE;
    }

  return FALSE;
}

static void
gimp_overlay_box_add (GtkContainer *container,
                      GtkWidget    *widget)
{
  gimp_overlay_box_add_child (GIMP_OVERLAY_BOX (container), widget, 0.5, 0.5);
}

static void
gimp_overlay_box_remove (GtkContainer *container,
                         GtkWidget    *widget)
{
  GimpOverlayBox   *box   = GIMP_OVERLAY_BOX (container);
  GimpOverlayChild *child = gimp_overlay_child_find (box, widget);

  if (child)
    {
      if (gtk_widget_get_visible (widget))
        gimp_overlay_child_invalidate (box, child);

      box->children = g_list_remove (box->children, child);

      gimp_overlay_child_free (box, child);
    }
}

static void
gimp_overlay_box_forall (GtkContainer *container,
                         gboolean      include_internals,
                         GtkCallback   callback,
                         gpointer      callback_data)
{
  GimpOverlayBox *box = GIMP_OVERLAY_BOX (container);
  GList          *list;

  list = box->children;
  while (list)
    {
      GimpOverlayChild *child = list->data;

      list = list->next;

      (* callback) (child->widget, callback_data);
    }
}

static GType
gimp_overlay_box_child_type (GtkContainer *container)
{
  return GTK_TYPE_WIDGET;
}

static GdkWindow *
gimp_overlay_box_pick_embedded_child (GdkWindow      *parent,
                                      gdouble         parent_x,
                                      gdouble         parent_y,
                                      GimpOverlayBox *box)
{
  GList *list;

  for (list = box->children; list; list = g_list_next (list))
    {
      GimpOverlayChild *child = list->data;

      if (gimp_overlay_child_pick (box, child, parent_x, parent_y))
        return child->window;
    }

  return NULL;
}


/*  public functions  */

/**
 * gimp_overlay_box_new:
 *
 * Creates a new #GimpOverlayBox widget.
 *
 * Return value: a new #GimpOverlayBox widget
 **/
GtkWidget *
gimp_overlay_box_new (void)
{
  return g_object_new (GIMP_TYPE_OVERLAY_BOX, NULL);
}

void
gimp_overlay_box_add_child (GimpOverlayBox *box,
                            GtkWidget      *widget,
                            gdouble         xalign,
                            gdouble         yalign)
{
  GimpOverlayChild *child;

  g_return_if_fail (GIMP_IS_OVERLAY_BOX (box));
  g_return_if_fail (GTK_IS_WIDGET (widget));

358
359
  gtk_widget_set_double_buffered (widget, FALSE);

360
361
362
363
364
365
  child = gimp_overlay_child_new (box, widget, xalign, yalign, 0.0, 0.7);

  box->children = g_list_append (box->children, child);
}

void
366
367
368
369
gimp_overlay_box_set_child_alignment (GimpOverlayBox *box,
                                      GtkWidget      *widget,
                                      gdouble         xalign,
                                      gdouble         yalign)
370
371
372
373
374
375
376
377
{
  GimpOverlayChild *child = gimp_overlay_child_find (box, widget);

  if (child)
    {
      xalign = CLAMP (xalign, 0.0, 1.0);
      yalign = CLAMP (yalign, 0.0, 1.0);

378
379
      if (child->has_position     ||
          child->xalign != xalign ||
380
381
382
383
          child->yalign != yalign)
        {
          gimp_overlay_child_invalidate (box, child);

384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
          child->has_position = FALSE;
          child->xalign       = xalign;
          child->yalign       = yalign;

          gtk_widget_queue_resize (widget);
        }
    }
}

void
gimp_overlay_box_set_child_position (GimpOverlayBox *box,
                                     GtkWidget      *widget,
                                     gdouble         x,
                                     gdouble         y)
{
  GimpOverlayChild *child = gimp_overlay_child_find (box, widget);

  if (child)
    {
      if (! child->has_position ||
          child->x != x         ||
          child->y != y)
        {
          gimp_overlay_child_invalidate (box, child);

          child->has_position = TRUE;
          child->x            = x;
          child->y            = y;
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482

          gtk_widget_queue_resize (widget);
        }
    }
}

void
gimp_overlay_box_set_child_angle (GimpOverlayBox *box,
                                  GtkWidget      *widget,
                                  gdouble         angle)
{
  GimpOverlayChild *child = gimp_overlay_child_find (box, widget);

  if (child)
    {
      if (child->angle != angle)
        {
          gimp_overlay_child_invalidate (box, child);

          child->angle = angle;

          gtk_widget_queue_draw (widget);
        }
    }
}

void
gimp_overlay_box_set_child_opacity (GimpOverlayBox *box,
                                    GtkWidget      *widget,
                                    gdouble         opacity)
{
  GimpOverlayChild *child = gimp_overlay_child_find (box, widget);

  if (child)
    {
      opacity = CLAMP (opacity, 0.0, 1.0);

      if (child->opacity != opacity)
        {
          child->opacity = opacity;

          gtk_widget_queue_draw (widget);
        }
    }
}

/**
 * gimp_overlay_box_scroll:
 * @box: the #GimpOverlayBox widget to scroll.
 * @offset_x: the x scroll amount.
 * @offset_y: the y scroll amount.
 *
 * Scrolls the box using gdk_window_scroll() and makes sure the result
 * is displayed immediately by calling gdk_window_process_updates().
 **/
void
gimp_overlay_box_scroll (GimpOverlayBox *box,
                         gint            offset_x,
                         gint            offset_y)
{
  GtkWidget *widget;
  GdkWindow *window;
  GList     *list;

  g_return_if_fail (GIMP_IS_OVERLAY_BOX (box));

  widget = GTK_WIDGET (box);
  window = gtk_widget_get_window (widget);

  /*  Undraw all overlays  */
  for (list = box->children; list; list = g_list_next (list))
483
484
485
486
487
    {
      GimpOverlayChild *child = list->data;

      gimp_overlay_child_invalidate (box, child);
    }
488
489
490
491
492

  gdk_window_scroll (window, offset_x, offset_y);

  /*  Re-draw all overlays  */
  for (list = box->children; list; list = g_list_next (list))
493
494
495
496
497
    {
      GimpOverlayChild *child = list->data;

      gimp_overlay_child_invalidate (box, child);
    }
498
499
500
501

  /*  Make sure expose events are processed before scrolling again  */
  gdk_window_process_updates (window, FALSE);
}