]> Pileus Git - ~andy/gtk/blob - gdk/gdkoffscreenwindow.c
Merge branch 'master' into treeview-refactor
[~andy/gtk] / gdk / gdkoffscreenwindow.c
1 /* GDK - The GIMP Drawing Kit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /*
21  * Modified by the GTK+ Team and others 1997-2005.  See the AUTHORS
22  * file for a list of people on the GTK+ Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
25  */
26
27 #include "config.h"
28
29 #include "gdkwindow.h"
30 #include "gdkinternals.h"
31 #include "gdkwindowimpl.h"
32
33 #include <math.h>
34
35 /* LIMITATIONS:
36  *
37  * Offscreen windows can't be the child of a foreign window,
38  *   nor contain foreign windows
39  * GDK_POINTER_MOTION_HINT_MASK isn't effective
40  */
41
42 typedef struct _GdkOffscreenWindow      GdkOffscreenWindow;
43 typedef struct _GdkOffscreenWindowClass GdkOffscreenWindowClass;
44
45 struct _GdkOffscreenWindow
46 {
47   GdkWindowImpl parent_instance;
48
49   GdkWindow *wrapper;
50
51   cairo_surface_t *surface;
52   GdkWindow *embedder;
53 };
54
55 struct _GdkOffscreenWindowClass
56 {
57   GdkWindowImplClass parent_class;
58 };
59
60 #define GDK_TYPE_OFFSCREEN_WINDOW            (gdk_offscreen_window_get_type())
61 #define GDK_OFFSCREEN_WINDOW(object)         (G_TYPE_CHECK_INSTANCE_CAST ((object), GDK_TYPE_OFFSCREEN_WINDOW, GdkOffscreenWindow))
62 #define GDK_IS_OFFSCREEN_WINDOW(object)      (G_TYPE_CHECK_INSTANCE_TYPE ((object), GDK_TYPE_OFFSCREEN_WINDOW))
63 #define GDK_OFFSCREEN_WINDOW_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), GDK_TYPE_OFFSCREEN_WINDOW, GdkOffscreenWindowClass))
64 #define GDK_IS_OFFSCREEN_WINDOW_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GDK_TYPE_OFFSCREEN_WINDOW))
65 #define GDK_OFFSCREEN_WINDOW_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), GDK_TYPE_OFFSCREEN_WINDOW, GdkOffscreenWindowClass))
66
67 static void       gdk_offscreen_window_hide               (GdkWindow                  *window);
68
69 G_DEFINE_TYPE (GdkOffscreenWindow, gdk_offscreen_window, GDK_TYPE_WINDOW_IMPL)
70
71
72 static void
73 gdk_offscreen_window_finalize (GObject *object)
74 {
75   GdkOffscreenWindow *offscreen = GDK_OFFSCREEN_WINDOW (object);
76
77   if (offscreen->surface)
78     cairo_surface_destroy (offscreen->surface);
79
80   G_OBJECT_CLASS (gdk_offscreen_window_parent_class)->finalize (object);
81 }
82
83 static void
84 gdk_offscreen_window_init (GdkOffscreenWindow *window)
85 {
86 }
87
88 static void
89 gdk_offscreen_window_destroy (GdkWindow *window,
90                               gboolean   recursing,
91                               gboolean   foreign_destroy)
92 {
93   GdkOffscreenWindow *offscreen;
94
95   offscreen = GDK_OFFSCREEN_WINDOW (window->impl);
96
97   gdk_offscreen_window_set_embedder (window, NULL);
98   
99   if (!recursing)
100     gdk_offscreen_window_hide (window);
101 }
102
103 static cairo_surface_t *
104 get_surface (GdkOffscreenWindow *offscreen)
105 {
106   if (! offscreen->surface)
107     {
108       GdkWindow *window = offscreen->wrapper;
109
110       g_signal_emit_by_name (window, "create-surface",
111                              window->width,
112                              window->height,
113                              &offscreen->surface);
114     }
115
116   return offscreen->surface;
117 }
118
119 static gboolean
120 is_parent_of (GdkWindow *parent,
121               GdkWindow *child)
122 {
123   GdkWindow *w;
124
125   w = child;
126   while (w != NULL)
127     {
128       if (w == parent)
129         return TRUE;
130
131       w = gdk_window_get_parent (w);
132     }
133
134   return FALSE;
135 }
136
137 static cairo_surface_t *
138 gdk_offscreen_window_ref_cairo_surface (GdkWindow *window)
139 {
140   GdkOffscreenWindow *offscreen = GDK_OFFSCREEN_WINDOW (window->impl);
141
142   return cairo_surface_reference (get_surface (offscreen));
143 }
144
145 cairo_surface_t *
146 _gdk_offscreen_window_create_surface (GdkWindow *offscreen,
147                                       gint       width,
148                                       gint       height)
149 {
150   cairo_surface_t *similar;
151   cairo_surface_t *surface;
152   cairo_content_t  content = CAIRO_CONTENT_COLOR;
153
154   g_return_val_if_fail (GDK_IS_OFFSCREEN_WINDOW (offscreen->impl), NULL);
155
156   similar = _gdk_window_ref_cairo_surface (offscreen->parent);
157
158   if (gdk_window_get_visual (offscreen) ==
159       gdk_screen_get_rgba_visual (gdk_window_get_screen (offscreen)))
160     {
161       content = CAIRO_CONTENT_COLOR_ALPHA;
162     }
163
164   surface = cairo_surface_create_similar (similar, content, width, height);
165
166   cairo_surface_destroy (similar);
167
168   return surface;
169 }
170
171 void
172 _gdk_offscreen_window_new (GdkWindow     *window,
173                            GdkWindowAttr *attributes,
174                            gint           attributes_mask)
175 {
176   GdkOffscreenWindow *offscreen;
177
178   g_return_if_fail (attributes != NULL);
179
180   if (attributes->wclass != GDK_INPUT_OUTPUT)
181     return; /* Can't support input only offscreens */
182
183   if (window->parent != NULL && GDK_WINDOW_DESTROYED (window->parent))
184     return;
185
186   window->impl = g_object_new (GDK_TYPE_OFFSCREEN_WINDOW, NULL);
187   offscreen = GDK_OFFSCREEN_WINDOW (window->impl);
188   offscreen->wrapper = window;
189 }
190
191 static gboolean
192 gdk_offscreen_window_reparent (GdkWindow *window,
193                                GdkWindow *new_parent,
194                                gint       x,
195                                gint       y)
196 {
197   GdkWindow *old_parent;
198   gboolean was_mapped;
199
200   if (new_parent)
201     {
202       /* No input-output children of input-only windows */
203       if (new_parent->input_only && !window->input_only)
204         return FALSE;
205
206       /* Don't create loops in hierarchy */
207       if (is_parent_of (window, new_parent))
208         return FALSE;
209     }
210
211   was_mapped = GDK_WINDOW_IS_MAPPED (window);
212
213   gdk_window_hide (window);
214
215   if (window->parent)
216     window->parent->children = g_list_remove (window->parent->children, window);
217
218   old_parent = window->parent;
219   window->parent = new_parent;
220   window->x = x;
221   window->y = y;
222
223   if (new_parent)
224     window->parent->children = g_list_prepend (window->parent->children, window);
225
226   _gdk_synthesize_crossing_events_for_geometry_change (window);
227   if (old_parent)
228     _gdk_synthesize_crossing_events_for_geometry_change (old_parent);
229
230   return was_mapped;
231 }
232
233 static void
234 from_embedder (GdkWindow *window,
235                double embedder_x, double embedder_y,
236                double *offscreen_x, double *offscreen_y)
237 {
238   g_signal_emit_by_name (window->impl_window,
239                          "from-embedder",
240                          embedder_x, embedder_y,
241                          offscreen_x, offscreen_y,
242                          NULL);
243 }
244
245 static void
246 to_embedder (GdkWindow *window,
247              double offscreen_x, double offscreen_y,
248              double *embedder_x, double *embedder_y)
249 {
250   g_signal_emit_by_name (window->impl_window,
251                          "to-embedder",
252                          offscreen_x, offscreen_y,
253                          embedder_x, embedder_y,
254                          NULL);
255 }
256
257 static gint
258 gdk_offscreen_window_get_root_coords (GdkWindow *window,
259                                       gint       x,
260                                       gint       y,
261                                       gint      *root_x,
262                                       gint      *root_y)
263 {
264   GdkOffscreenWindow *offscreen;
265   int tmpx, tmpy;
266
267   tmpx = x;
268   tmpy = y;
269
270   offscreen = GDK_OFFSCREEN_WINDOW (window->impl);
271   if (offscreen->embedder)
272     {
273       double dx, dy;
274       to_embedder (window,
275                    x, y,
276                    &dx, &dy);
277       tmpx = floor (dx + 0.5);
278       tmpy = floor (dy + 0.5);
279       gdk_window_get_root_coords (offscreen->embedder,
280                                   tmpx, tmpy,
281                                   &tmpx, &tmpy);
282
283     }
284
285   if (root_x)
286     *root_x = tmpx;
287   if (root_y)
288     *root_y = tmpy;
289
290   return TRUE;
291 }
292
293 static gboolean
294 gdk_offscreen_window_get_device_state (GdkWindow       *window,
295                                        GdkDevice       *device,
296                                        gint            *x,
297                                        gint            *y,
298                                        GdkModifierType *mask)
299 {
300   GdkOffscreenWindow *offscreen;
301   int tmpx, tmpy;
302   double dtmpx, dtmpy;
303   GdkModifierType tmpmask;
304
305   tmpx = 0;
306   tmpy = 0;
307   tmpmask = 0;
308
309   offscreen = GDK_OFFSCREEN_WINDOW (window->impl);
310   if (offscreen->embedder != NULL)
311     {
312       gdk_window_get_device_position (offscreen->embedder, device, &tmpx, &tmpy, &tmpmask);
313       from_embedder (window,
314                      tmpx, tmpy,
315                      &dtmpx, &dtmpy);
316       tmpx = floor (dtmpx + 0.5);
317       tmpy = floor (dtmpy + 0.5);
318     }
319
320   if (x)
321     *x = tmpx;
322   if (y)
323     *y = tmpy;
324   if (mask)
325     *mask = tmpmask;
326   return TRUE;
327 }
328
329 /**
330  * gdk_offscreen_window_get_surface:
331  * @window: a #GdkWindow
332  *
333  * Gets the offscreen surface that an offscreen window renders into.
334  * If you need to keep this around over window resizes, you need to
335  * add a reference to it.
336  *
337  * Returns: The offscreen surface, or %NULL if not offscreen
338  */
339 cairo_surface_t *
340 gdk_offscreen_window_get_surface (GdkWindow *window)
341 {
342   GdkOffscreenWindow *offscreen;
343
344   g_return_val_if_fail (GDK_IS_WINDOW (window), FALSE);
345
346   if (!GDK_IS_OFFSCREEN_WINDOW (window->impl))
347     return NULL;
348
349   offscreen = GDK_OFFSCREEN_WINDOW (window->impl);
350
351   return get_surface (offscreen);
352 }
353
354 static void
355 gdk_offscreen_window_raise (GdkWindow *window)
356 {
357   /* gdk_window_raise already changed the stacking order */
358   _gdk_synthesize_crossing_events_for_geometry_change (window);
359 }
360
361 static void
362 gdk_offscreen_window_lower (GdkWindow *window)
363 {
364   /* gdk_window_lower already changed the stacking order */
365   _gdk_synthesize_crossing_events_for_geometry_change (window);
366 }
367
368 static void
369 gdk_offscreen_window_move_resize_internal (GdkWindow *window,
370                                            gint       x,
371                                            gint       y,
372                                            gint       width,
373                                            gint       height,
374                                            gboolean   send_expose_events)
375 {
376   GdkOffscreenWindow *offscreen;
377   gint dx, dy, dw, dh;
378
379   offscreen = GDK_OFFSCREEN_WINDOW (window->impl);
380
381   if (width < 1)
382     width = 1;
383   if (height < 1)
384     height = 1;
385
386   if (window->destroyed)
387     return;
388
389   dx = x - window->x;
390   dy = y - window->y;
391   dw = width - window->width;
392   dh = height - window->height;
393
394   window->x = x;
395   window->y = y;
396
397   if (window->width != width ||
398       window->height != height)
399     {
400       window->width = width;
401       window->height = height;
402
403       if (offscreen->surface)
404         {
405           cairo_surface_t *old_surface;
406           cairo_t *cr;
407
408           old_surface = offscreen->surface;
409           offscreen->surface = NULL;
410
411           offscreen->surface = get_surface (offscreen);
412
413           cr = cairo_create (offscreen->surface);
414           cairo_set_source_surface (cr, old_surface, 0, 0);
415           cairo_paint (cr);
416           cairo_destroy (cr);
417
418           cairo_surface_destroy (old_surface);
419         }
420     }
421
422   if (GDK_WINDOW_IS_MAPPED (window))
423     {
424       // TODO: Only invalidate new area, i.e. for larger windows
425       gdk_window_invalidate_rect (window, NULL, TRUE);
426       _gdk_synthesize_crossing_events_for_geometry_change (window);
427     }
428 }
429
430 static void
431 gdk_offscreen_window_move_resize (GdkWindow *window,
432                                   gboolean   with_move,
433                                   gint       x,
434                                   gint       y,
435                                   gint       width,
436                                   gint       height)
437 {
438   GdkOffscreenWindow *offscreen;
439
440   offscreen = GDK_OFFSCREEN_WINDOW (window->impl);
441
442   if (!with_move)
443     {
444       x = window->x;
445       y = window->y;
446     }
447
448   if (width < 0)
449     width = window->width;
450
451   if (height < 0)
452     height = window->height;
453
454   gdk_offscreen_window_move_resize_internal (window, x, y,
455                                              width, height,
456                                              TRUE);
457 }
458
459 static void
460 gdk_offscreen_window_show (GdkWindow *window,
461                            gboolean already_mapped)
462 {
463   GdkRectangle area = { 0, 0, window->width, window->height };
464
465   gdk_window_invalidate_rect (window, &area, FALSE);
466 }
467
468
469 static void
470 gdk_offscreen_window_hide (GdkWindow *window)
471 {
472   GdkOffscreenWindow *offscreen;
473   GdkDisplay *display;
474
475   g_return_if_fail (window != NULL);
476
477   offscreen = GDK_OFFSCREEN_WINDOW (window->impl);
478
479   /* May need to break grabs on children */
480   display = gdk_window_get_display (window);
481
482   /* TODO: This needs updating to the new grab world */
483 #if 0
484   if (display->pointer_grab.window != NULL)
485     {
486       if (is_parent_of (window, display->pointer_grab.window))
487         {
488           /* Call this ourselves, even though gdk_display_pointer_ungrab
489              does so too, since we want to pass implicit == TRUE so the
490              broken grab event is generated */
491           _gdk_display_unset_has_pointer_grab (display,
492                                                TRUE,
493                                                FALSE,
494                                                GDK_CURRENT_TIME);
495           gdk_display_pointer_ungrab (display, GDK_CURRENT_TIME);
496         }
497     }
498 #endif
499 }
500
501 static void
502 gdk_offscreen_window_withdraw (GdkWindow *window)
503 {
504 }
505
506 static GdkEventMask
507 gdk_offscreen_window_get_events (GdkWindow *window)
508 {
509   return 0;
510 }
511
512 static void
513 gdk_offscreen_window_set_events (GdkWindow       *window,
514                                  GdkEventMask     event_mask)
515 {
516 }
517
518 static void
519 gdk_offscreen_window_set_background (GdkWindow      *window,
520                                      cairo_pattern_t *pattern)
521 {
522 }
523
524 static void
525 gdk_offscreen_window_shape_combine_region (GdkWindow       *window,
526                                            const cairo_region_t *shape_region,
527                                            gint             offset_x,
528                                            gint             offset_y)
529 {
530 }
531
532 static void
533 gdk_offscreen_window_input_shape_combine_region (GdkWindow       *window,
534                                                  const cairo_region_t *shape_region,
535                                                  gint             offset_x,
536                                                  gint             offset_y)
537 {
538 }
539
540 static gboolean
541 gdk_offscreen_window_set_static_gravities (GdkWindow *window,
542                                            gboolean   use_static)
543 {
544   return TRUE;
545 }
546
547 static void
548 gdk_offscreen_window_get_geometry (GdkWindow *window,
549                                    gint      *x,
550                                    gint      *y,
551                                    gint      *width,
552                                    gint      *height,
553                                    gint      *depth)
554 {
555   if (!GDK_WINDOW_DESTROYED (window))
556     {
557       if (x)
558         *x = window->x;
559       if (y)
560         *y = window->y;
561       if (width)
562         *width = window->width;
563       if (height)
564         *height = window->height;
565       if (depth)
566         *depth = window->depth;
567     }
568 }
569
570 static gboolean
571 gdk_offscreen_window_queue_antiexpose (GdkWindow *window,
572                                        cairo_region_t *area)
573 {
574   return FALSE;
575 }
576
577 static void
578 gdk_offscreen_window_translate (GdkWindow      *window,
579                                 cairo_region_t *area,
580                                 gint            dx,
581                                 gint            dy)
582 {
583   GdkOffscreenWindow *offscreen = GDK_OFFSCREEN_WINDOW (window->impl);
584
585   if (offscreen->surface)
586     {
587       cairo_t *cr;
588
589       cr = cairo_create (offscreen->surface);
590
591       area = cairo_region_copy (area);
592
593       gdk_cairo_region (cr, area);
594       cairo_clip (cr);
595
596       /* NB: This is a self-copy and Cairo doesn't support that yet.
597        * So we do a litle trick.
598        */
599       cairo_push_group (cr);
600
601       cairo_set_source_surface (cr, offscreen->surface, dx, dy);
602       cairo_paint (cr);
603
604       cairo_pop_group_to_source (cr);
605       cairo_paint (cr);
606
607       cairo_destroy (cr);
608     }
609
610   _gdk_window_add_damage (window, area);
611 }
612
613 static cairo_surface_t *
614 gdk_offscreen_window_resize_cairo_surface (GdkWindow       *window,
615                                            cairo_surface_t *surface,
616                                            gint             width,
617                                            gint             height)
618 {
619   /* No-op.  The surface gets resized in
620    * gdk_offscreen_window_move_resize_internal().
621    */
622   return surface;
623 }
624
625 /**
626  * gdk_offscreen_window_set_embedder:
627  * @window: a #GdkWindow
628  * @embedder: the #GdkWindow that @window gets embedded in
629  *
630  * Sets @window to be embedded in @embedder.
631  *
632  * To fully embed an offscreen window, in addition to calling this
633  * function, it is also necessary to handle the #GdkWindow::pick-embedded-child
634  * signal on the @embedder and the #GdkWindow::to-embedder and
635  * #GdkWindow::from-embedder signals on @window.
636  *
637  * Since: 2.18
638  */
639 void
640 gdk_offscreen_window_set_embedder (GdkWindow     *window,
641                                    GdkWindow     *embedder)
642 {
643   GdkOffscreenWindow *offscreen;
644
645   g_return_if_fail (GDK_IS_WINDOW (window));
646
647   if (!GDK_IS_OFFSCREEN_WINDOW (window->impl))
648     return;
649
650   offscreen = GDK_OFFSCREEN_WINDOW (window->impl);
651
652   if (embedder)
653     {
654       g_object_ref (embedder);
655       embedder->num_offscreen_children++;
656     }
657
658   if (offscreen->embedder)
659     {
660       g_object_unref (offscreen->embedder);
661       offscreen->embedder->num_offscreen_children--;
662     }
663
664   offscreen->embedder = embedder;
665 }
666
667 /**
668  * gdk_offscreen_window_get_embedder:
669  * @window: a #GdkWindow
670  *
671  * Gets the window that @window is embedded in.
672  *
673  * Returns: (transfer none): the embedding #GdkWindow, or %NULL
674  *     if @window is not an mbedded offscreen window
675  *
676  * Since: 2.18
677  */
678 GdkWindow *
679 gdk_offscreen_window_get_embedder (GdkWindow *window)
680 {
681   GdkOffscreenWindow *offscreen;
682
683   g_return_val_if_fail (GDK_IS_WINDOW (window), NULL);
684
685   if (!GDK_IS_OFFSCREEN_WINDOW (window->impl))
686     return NULL;
687
688   offscreen = GDK_OFFSCREEN_WINDOW (window->impl);
689
690   return offscreen->embedder;
691 }
692
693 static void
694 gdk_offscreen_window_class_init (GdkOffscreenWindowClass *klass)
695 {
696   GdkWindowImplClass *impl_class = GDK_WINDOW_IMPL_CLASS (klass);
697   GObjectClass *object_class = G_OBJECT_CLASS (klass);
698
699   object_class->finalize = gdk_offscreen_window_finalize;
700
701   impl_class->ref_cairo_surface = gdk_offscreen_window_ref_cairo_surface;
702   impl_class->show = gdk_offscreen_window_show;
703   impl_class->hide = gdk_offscreen_window_hide;
704   impl_class->withdraw = gdk_offscreen_window_withdraw;
705   impl_class->raise = gdk_offscreen_window_raise;
706   impl_class->lower = gdk_offscreen_window_lower;
707   impl_class->move_resize = gdk_offscreen_window_move_resize;
708   impl_class->set_background = gdk_offscreen_window_set_background;
709   impl_class->get_events = gdk_offscreen_window_get_events;
710   impl_class->set_events = gdk_offscreen_window_set_events;
711   impl_class->reparent = gdk_offscreen_window_reparent;
712   impl_class->get_geometry = gdk_offscreen_window_get_geometry;
713   impl_class->shape_combine_region = gdk_offscreen_window_shape_combine_region;
714   impl_class->input_shape_combine_region = gdk_offscreen_window_input_shape_combine_region;
715   impl_class->set_static_gravities = gdk_offscreen_window_set_static_gravities;
716   impl_class->queue_antiexpose = gdk_offscreen_window_queue_antiexpose;
717   impl_class->translate = gdk_offscreen_window_translate;
718   impl_class->get_root_coords = gdk_offscreen_window_get_root_coords;
719   impl_class->get_device_state = gdk_offscreen_window_get_device_state;
720   impl_class->destroy = gdk_offscreen_window_destroy;
721   impl_class->resize_cairo_surface = gdk_offscreen_window_resize_cairo_surface;
722 }