]> Pileus Git - ~andy/gtk/blob - gtk/gtkoffscreenwindow.c
GtkOffscreenWindow implementation for #604901
[~andy/gtk] / gtk / gtkoffscreenwindow.c
1 #include "gtkoffscreenwindow.h"
2
3 G_DEFINE_TYPE (GtkOffscreenWindow, gtk_offscreen_window, GTK_TYPE_WINDOW);
4
5 static void
6 gtk_offscreen_window_size_request (GtkWidget *widget,
7                                    GtkRequisition *requisition)
8 {
9   GtkBin *bin = GTK_BIN (widget);
10   gint border_width;
11   gint default_width, default_height;
12
13   border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
14
15   requisition->width = border_width * 2;
16   requisition->height = border_width * 2;
17
18   if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
19     {
20       GtkRequisition child_req;
21
22       gtk_widget_size_request (bin->child, &child_req);
23
24       requisition->width += child_req.width;
25       requisition->height += child_req.height;
26     }
27
28   gtk_window_get_default_size (GTK_WINDOW (widget),
29                                &default_width, &default_height);
30   if (default_width > 0)
31     requisition->width = default_width;
32
33   if (default_height > 0)
34     requisition->height = default_height;
35 }
36
37 static void
38 gtk_offscreen_window_size_allocate (GtkWidget *widget,
39                                     GtkAllocation *allocation)
40 {
41   GtkBin *bin = GTK_BIN (widget);
42   gint border_width;
43
44   widget->allocation = *allocation;
45
46   border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
47
48   if (GTK_WIDGET_REALIZED (widget))
49     gdk_window_move_resize (widget->window,
50                             allocation->x,
51                             allocation->y,
52                             allocation->width,
53                             allocation->height);
54
55   if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
56     {
57       GtkAllocation  child_alloc;
58
59       child_alloc.x = border_width;
60       child_alloc.y = border_width;
61       child_alloc.width = allocation->width - 2 * border_width;
62       child_alloc.height = allocation->height - 2 * border_width;
63
64       gtk_widget_size_allocate (bin->child, &child_alloc);
65     }
66
67   gtk_widget_queue_draw (widget);
68 }
69
70 static void
71 gtk_offscreen_window_realize (GtkWidget *widget)
72 {
73   GtkBin *bin;
74   GdkWindowAttr attributes;
75   gint attributes_mask;
76   gint border_width;
77
78   bin = GTK_BIN (widget);
79
80   GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
81
82   border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
83
84   attributes.x = widget->allocation.x;
85   attributes.y = widget->allocation.y;
86   attributes.width = widget->allocation.width;
87   attributes.height = widget->allocation.height;
88   attributes.window_type = GDK_WINDOW_OFFSCREEN;
89   attributes.event_mask = gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK;
90   attributes.visual = gtk_widget_get_visual (widget);
91   attributes.colormap = gtk_widget_get_colormap (widget);
92   attributes.wclass = GDK_INPUT_OUTPUT;
93
94   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
95
96   widget->window = gdk_window_new (gtk_widget_get_parent_window (widget),
97                                    &attributes, attributes_mask);
98   gdk_window_set_user_data (widget->window, widget);
99
100   if (bin->child)
101     gtk_widget_set_parent_window (bin->child, widget->window);
102
103   widget->style = gtk_style_attach (widget->style, widget->window);
104
105   gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
106 }
107
108 static void
109 gtk_offscreen_window_resize (GtkWidget *widget)
110 {
111   GtkAllocation allocation = { 0, 0 };
112   GtkRequisition requisition;
113
114   gtk_widget_size_request (widget, &requisition);
115
116   allocation.width  = requisition.width;
117   allocation.height = requisition.height;
118   gtk_widget_size_allocate (widget, &allocation);
119 }
120
121 static void
122 move_focus (GtkWidget       *widget,
123             GtkDirectionType dir)
124 {
125   gtk_widget_child_focus (widget, dir);
126
127   if (!GTK_CONTAINER (widget)->focus_child)
128     gtk_window_set_focus (GTK_WINDOW (widget), NULL);
129 }
130
131 static void
132 gtk_offscreen_window_show (GtkWidget *widget)
133 {
134   gboolean need_resize;
135   GtkContainer *container;
136
137   GTK_WIDGET_SET_FLAGS (widget, GTK_VISIBLE);
138
139   container = GTK_CONTAINER (widget);
140   need_resize = container->need_resize || !GTK_WIDGET_REALIZED (widget);
141   container->need_resize = FALSE;
142
143   if (need_resize)
144     gtk_offscreen_window_resize (widget);
145
146   gtk_widget_map (widget);
147
148   /* Try to make sure that we have some focused widget */
149   if (!gtk_window_get_focus (GTK_WINDOW (widget)))
150     move_focus (widget, GTK_DIR_TAB_FORWARD);
151 }
152
153 static void
154 gtk_offscreen_window_hide (GtkWidget *widget)
155 {
156   GTK_WIDGET_UNSET_FLAGS (widget, GTK_VISIBLE);
157   gtk_widget_unmap (widget);
158 }
159
160 static void
161 gtk_offscreen_window_check_resize (GtkContainer *container)
162 {
163   GtkWidget *widget = GTK_WIDGET (container);
164
165   if (GTK_WIDGET_VISIBLE (widget))
166     gtk_offscreen_window_resize (widget);
167 }
168
169 static void
170 gtk_offscreen_window_class_init (GtkOffscreenWindowClass *class)
171 {
172   GtkWidgetClass *widget_class;
173   GtkContainerClass *container_class;
174
175   widget_class = GTK_WIDGET_CLASS (class);
176   container_class = GTK_CONTAINER_CLASS (class);
177
178   widget_class->realize = gtk_offscreen_window_realize;
179   widget_class->show = gtk_offscreen_window_show;
180   widget_class->hide = gtk_offscreen_window_hide;
181   widget_class->size_request = gtk_offscreen_window_size_request;
182   widget_class->size_allocate = gtk_offscreen_window_size_allocate;
183
184   container_class->check_resize = gtk_offscreen_window_check_resize;
185 }
186
187 static void
188 gtk_offscreen_window_init (GtkOffscreenWindow *bin)
189 {
190 }
191
192 GtkWidget *
193 gtk_offscreen_window_new (void)
194 {
195   return g_object_new (gtk_offscreen_window_get_type (), NULL);
196 }
197
198
199 #define __GTK_OFFSCREEN_WINDOW_C__
200 #include "gtkaliasdef.c"