OLD | NEW |
| (Empty) |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "gfx/gtk_preserve_window.h" | |
6 | |
7 #include <gdk/gdkwindow.h> | |
8 #include <gtk/gtk.h> | |
9 #include <gtk/gtkwidget.h> | |
10 #include <gtk/gtkfixed.h> | |
11 | |
12 G_BEGIN_DECLS | |
13 | |
14 #define GTK_PRESERVE_WINDOW_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), \ | |
15 GTK_TYPE_PRESERVE_WINDOW, \ | |
16 GtkPreserveWindowPrivate)) | |
17 | |
18 typedef struct _GtkPreserveWindowPrivate GtkPreserveWindowPrivate; | |
19 | |
20 struct _GtkPreserveWindowPrivate { | |
21 // If true, don't create/destroy windows on realize/unrealize. | |
22 gboolean preserve_window; | |
23 | |
24 // Whether or not we delegate the resize of the GdkWindow | |
25 // to someone else. | |
26 gboolean delegate_resize; | |
27 }; | |
28 | |
29 G_DEFINE_TYPE(GtkPreserveWindow, gtk_preserve_window, GTK_TYPE_FIXED) | |
30 | |
31 static void gtk_preserve_window_destroy(GtkObject* object); | |
32 static void gtk_preserve_window_realize(GtkWidget* widget); | |
33 static void gtk_preserve_window_unrealize(GtkWidget* widget); | |
34 static void gtk_preserve_window_size_allocate(GtkWidget* widget, | |
35 GtkAllocation* allocation); | |
36 | |
37 static void gtk_preserve_window_class_init(GtkPreserveWindowClass *klass) { | |
38 GtkWidgetClass* widget_class = reinterpret_cast<GtkWidgetClass*>(klass); | |
39 widget_class->realize = gtk_preserve_window_realize; | |
40 widget_class->unrealize = gtk_preserve_window_unrealize; | |
41 widget_class->size_allocate = gtk_preserve_window_size_allocate; | |
42 | |
43 GtkObjectClass* object_class = reinterpret_cast<GtkObjectClass*>(klass); | |
44 object_class->destroy = gtk_preserve_window_destroy; | |
45 | |
46 GObjectClass* gobject_class = G_OBJECT_CLASS(klass); | |
47 g_type_class_add_private(gobject_class, sizeof(GtkPreserveWindowPrivate)); | |
48 } | |
49 | |
50 static void gtk_preserve_window_init(GtkPreserveWindow* widget) { | |
51 GtkPreserveWindowPrivate* priv = GTK_PRESERVE_WINDOW_GET_PRIVATE(widget); | |
52 priv->preserve_window = FALSE; | |
53 | |
54 // These widgets always have their own window. | |
55 gtk_fixed_set_has_window(GTK_FIXED(widget), TRUE); | |
56 } | |
57 | |
58 GtkWidget* gtk_preserve_window_new() { | |
59 return GTK_WIDGET(g_object_new(GTK_TYPE_PRESERVE_WINDOW, NULL)); | |
60 } | |
61 | |
62 static void gtk_preserve_window_destroy(GtkObject* object) { | |
63 GtkWidget* widget = reinterpret_cast<GtkWidget*>(object); | |
64 GtkPreserveWindowPrivate* priv = GTK_PRESERVE_WINDOW_GET_PRIVATE(widget); | |
65 | |
66 if (widget->window) { | |
67 gdk_window_set_user_data(widget->window, NULL); | |
68 // If the window is preserved, someone else must destroy it. | |
69 if (!priv->preserve_window) | |
70 gdk_window_destroy(widget->window); | |
71 widget->window = NULL; | |
72 } | |
73 | |
74 GTK_OBJECT_CLASS(gtk_preserve_window_parent_class)->destroy(object); | |
75 } | |
76 | |
77 static void gtk_preserve_window_realize(GtkWidget* widget) { | |
78 g_return_if_fail(GTK_IS_PRESERVE_WINDOW(widget)); | |
79 | |
80 if (widget->window) { | |
81 gdk_window_reparent(widget->window, | |
82 gtk_widget_get_parent_window(widget), | |
83 widget->allocation.x, | |
84 widget->allocation.y); | |
85 widget->style = gtk_style_attach(widget->style, widget->window); | |
86 gtk_style_set_background(widget->style, widget->window, GTK_STATE_NORMAL); | |
87 | |
88 gint event_mask = gtk_widget_get_events(widget); | |
89 event_mask |= GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK; | |
90 gdk_window_set_events(widget->window, (GdkEventMask) event_mask); | |
91 gdk_window_set_user_data(widget->window, widget); | |
92 | |
93 // Deprecated as of GTK 2.22. Used for compatibility. | |
94 // It should be: gtk_widget_set_realized(widget, TRUE) | |
95 GTK_WIDGET_SET_FLAGS(widget, GTK_REALIZED); | |
96 } else { | |
97 GTK_WIDGET_CLASS(gtk_preserve_window_parent_class)->realize(widget); | |
98 } | |
99 } | |
100 | |
101 static void gtk_preserve_window_unrealize(GtkWidget* widget) { | |
102 g_return_if_fail(GTK_IS_PRESERVE_WINDOW(widget)); | |
103 | |
104 GtkPreserveWindowPrivate* priv = GTK_PRESERVE_WINDOW_GET_PRIVATE(widget); | |
105 if (priv->preserve_window) { | |
106 GtkWidgetClass* widget_class = | |
107 GTK_WIDGET_CLASS(gtk_preserve_window_parent_class); | |
108 GtkContainerClass* container_class = | |
109 GTK_CONTAINER_CLASS(gtk_preserve_window_parent_class); | |
110 | |
111 // Deprecated as of GTK 2.22. Used for compatibility. | |
112 // It should be: gtk_widget_get_mapped() | |
113 if (GTK_WIDGET_MAPPED(widget)) { | |
114 widget_class->unmap(widget); | |
115 | |
116 // Deprecated as of GTK 2.22. Used for compatibility. | |
117 // It should be: gtk_widget_set_mapped(widget, FALSE) | |
118 GTK_WIDGET_UNSET_FLAGS(widget, GTK_MAPPED); | |
119 } | |
120 | |
121 // This is the behavior from GtkWidget, inherited by GtkFixed. | |
122 // It is unclear why we should not call the potentially overridden | |
123 // unrealize method (via the callback), but doing so causes errors. | |
124 container_class->forall( | |
125 GTK_CONTAINER(widget), FALSE, | |
126 reinterpret_cast<GtkCallback>(gtk_widget_unrealize), NULL); | |
127 | |
128 gtk_style_detach(widget->style); | |
129 gdk_window_reparent(widget->window, gdk_get_default_root_window(), 0, 0); | |
130 gtk_selection_remove_all(widget); | |
131 gdk_window_set_user_data(widget->window, NULL); | |
132 | |
133 // Deprecated as of GTK 2.22. Used for compatibility. | |
134 // It should be: gtk_widget_set_realized(widget, FALSE) | |
135 GTK_WIDGET_UNSET_FLAGS(widget, GTK_REALIZED); | |
136 } else { | |
137 GTK_WIDGET_CLASS(gtk_preserve_window_parent_class)->unrealize(widget); | |
138 } | |
139 } | |
140 | |
141 gboolean gtk_preserve_window_get_preserve(GtkPreserveWindow* window) { | |
142 g_return_val_if_fail(GTK_IS_PRESERVE_WINDOW(window), FALSE); | |
143 GtkPreserveWindowPrivate* priv = GTK_PRESERVE_WINDOW_GET_PRIVATE(window); | |
144 | |
145 return priv->preserve_window; | |
146 } | |
147 | |
148 void gtk_preserve_window_set_preserve(GtkPreserveWindow* window, | |
149 gboolean value) { | |
150 g_return_if_fail(GTK_IS_PRESERVE_WINDOW(window)); | |
151 GtkPreserveWindowPrivate* priv = GTK_PRESERVE_WINDOW_GET_PRIVATE(window); | |
152 priv->preserve_window = value; | |
153 | |
154 GtkWidget* widget = GTK_WIDGET(window); | |
155 if (value && !widget->window) { | |
156 GdkWindowAttr attributes; | |
157 gint attributes_mask; | |
158 | |
159 // We may not know the width and height, so we rely on the fact | |
160 // that a size-allocation will resize it later. | |
161 attributes.width = 1; | |
162 attributes.height = 1; | |
163 | |
164 attributes.window_type = GDK_WINDOW_CHILD; | |
165 attributes.wclass = GDK_INPUT_OUTPUT; | |
166 | |
167 attributes.visual = gtk_widget_get_visual(widget); | |
168 attributes.colormap = gtk_widget_get_colormap(widget); | |
169 | |
170 attributes_mask = GDK_WA_VISUAL | GDK_WA_COLORMAP; | |
171 widget->window = gdk_window_new( | |
172 gdk_get_default_root_window(), &attributes, attributes_mask); | |
173 } else if (!value && widget->window && !GTK_WIDGET_REALIZED(widget)) { | |
174 gdk_window_destroy(widget->window); | |
175 widget->window = NULL; | |
176 } | |
177 } | |
178 | |
179 void gtk_preserve_window_size_allocate(GtkWidget* widget, | |
180 GtkAllocation* allocation) { | |
181 g_return_if_fail(GTK_IS_PRESERVE_WINDOW(widget)); | |
182 GtkPreserveWindowPrivate* priv = GTK_PRESERVE_WINDOW_GET_PRIVATE(widget); | |
183 | |
184 if (priv->delegate_resize) { | |
185 // Just update the state. Someone else will gdk_window_resize the | |
186 // associated GdkWindow. | |
187 widget->allocation = *allocation; | |
188 } else { | |
189 GTK_WIDGET_CLASS(gtk_preserve_window_parent_class)->size_allocate( | |
190 widget, allocation); | |
191 } | |
192 } | |
193 | |
194 void gtk_preserve_window_delegate_resize(GtkPreserveWindow* widget, | |
195 gboolean delegate) { | |
196 GtkPreserveWindowPrivate* priv = GTK_PRESERVE_WINDOW_GET_PRIVATE(widget); | |
197 priv->delegate_resize = delegate; | |
198 } | |
199 | |
200 G_END_DECLS | |
OLD | NEW |