]> Pileus Git - ~andy/gtk/blob - gtk/gtkoffscreenwindow.c
Add headers
[~andy/gtk] / gtk / gtkoffscreenwindow.c
1 /*
2  * This library is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU Lesser General Public
4  * License as published by the Free Software Foundation; either
5  * version 2 of the License, or (at your option) any later version.
6  *
7  * This library is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10  * Lesser General Public License for more details.
11  *
12  * You should have received a copy of the GNU Lesser General Public
13  * License along with this library; if not, write to the
14  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
15  * Boston, MA 02111-1307, USA.
16  *
17  * Authors: Cody Russell <crussell@canonical.com>
18  *          Alexander Larsson <alexl@redhat.com>
19  */
20
21 #include "gtkoffscreenwindow.h"
22 #include "gtkalias.h"
23
24 G_DEFINE_TYPE (GtkOffscreenWindow, gtk_offscreen_window, GTK_TYPE_WINDOW);
25
26 static void
27 gtk_offscreen_window_size_request (GtkWidget *widget,
28                                    GtkRequisition *requisition)
29 {
30   GtkBin *bin = GTK_BIN (widget);
31   gint border_width;
32   gint default_width, default_height;
33
34   border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
35
36   requisition->width = border_width * 2;
37   requisition->height = border_width * 2;
38
39   if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
40     {
41       GtkRequisition child_req;
42
43       gtk_widget_size_request (bin->child, &child_req);
44
45       requisition->width += child_req.width;
46       requisition->height += child_req.height;
47     }
48
49   gtk_window_get_default_size (GTK_WINDOW (widget),
50                                &default_width, &default_height);
51   if (default_width > 0)
52     requisition->width = default_width;
53
54   if (default_height > 0)
55     requisition->height = default_height;
56 }
57
58 static void
59 gtk_offscreen_window_size_allocate (GtkWidget *widget,
60                                     GtkAllocation *allocation)
61 {
62   GtkBin *bin = GTK_BIN (widget);
63   gint border_width;
64
65   widget->allocation = *allocation;
66
67   border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
68
69   if (GTK_WIDGET_REALIZED (widget))
70     gdk_window_move_resize (widget->window,
71                             allocation->x,
72                             allocation->y,
73                             allocation->width,
74                             allocation->height);
75
76   if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
77     {
78       GtkAllocation  child_alloc;
79
80       child_alloc.x = border_width;
81       child_alloc.y = border_width;
82       child_alloc.width = allocation->width - 2 * border_width;
83       child_alloc.height = allocation->height - 2 * border_width;
84
85       gtk_widget_size_allocate (bin->child, &child_alloc);
86     }
87
88   gtk_widget_queue_draw (widget);
89 }
90
91 static void
92 gtk_offscreen_window_realize (GtkWidget *widget)
93 {
94   GtkBin *bin;
95   GdkWindowAttr attributes;
96   gint attributes_mask;
97   gint border_width;
98
99   bin = GTK_BIN (widget);
100
101   GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
102
103   border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
104
105   attributes.x = widget->allocation.x;
106   attributes.y = widget->allocation.y;
107   attributes.width = widget->allocation.width;
108   attributes.height = widget->allocation.height;
109   attributes.window_type = GDK_WINDOW_OFFSCREEN;
110   attributes.event_mask = gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK;
111   attributes.visual = gtk_widget_get_visual (widget);
112   attributes.colormap = gtk_widget_get_colormap (widget);
113   attributes.wclass = GDK_INPUT_OUTPUT;
114
115   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
116
117   widget->window = gdk_window_new (gtk_widget_get_parent_window (widget),
118                                    &attributes, attributes_mask);
119   gdk_window_set_user_data (widget->window, widget);
120
121   if (bin->child)
122     gtk_widget_set_parent_window (bin->child, widget->window);
123
124   widget->style = gtk_style_attach (widget->style, widget->window);
125
126   gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
127 }
128
129 static void
130 gtk_offscreen_window_resize (GtkWidget *widget)
131 {
132   GtkAllocation allocation = { 0, 0 };
133   GtkRequisition requisition;
134
135   gtk_widget_size_request (widget, &requisition);
136
137   allocation.width  = requisition.width;
138   allocation.height = requisition.height;
139   gtk_widget_size_allocate (widget, &allocation);
140 }
141
142 static void
143 move_focus (GtkWidget       *widget,
144             GtkDirectionType dir)
145 {
146   gtk_widget_child_focus (widget, dir);
147
148   if (!GTK_CONTAINER (widget)->focus_child)
149     gtk_window_set_focus (GTK_WINDOW (widget), NULL);
150 }
151
152 static void
153 gtk_offscreen_window_show (GtkWidget *widget)
154 {
155   gboolean need_resize;
156   GtkContainer *container;
157
158   GTK_WIDGET_SET_FLAGS (widget, GTK_VISIBLE);
159
160   container = GTK_CONTAINER (widget);
161   need_resize = container->need_resize || !GTK_WIDGET_REALIZED (widget);
162   container->need_resize = FALSE;
163
164   if (need_resize)
165     gtk_offscreen_window_resize (widget);
166
167   gtk_widget_map (widget);
168
169   /* Try to make sure that we have some focused widget */
170   if (!gtk_window_get_focus (GTK_WINDOW (widget)))
171     move_focus (widget, GTK_DIR_TAB_FORWARD);
172 }
173
174 static void
175 gtk_offscreen_window_hide (GtkWidget *widget)
176 {
177   GTK_WIDGET_UNSET_FLAGS (widget, GTK_VISIBLE);
178   gtk_widget_unmap (widget);
179 }
180
181 static void
182 gtk_offscreen_window_check_resize (GtkContainer *container)
183 {
184   GtkWidget *widget = GTK_WIDGET (container);
185
186   if (GTK_WIDGET_VISIBLE (widget))
187     gtk_offscreen_window_resize (widget);
188 }
189
190 static void
191 gtk_offscreen_window_class_init (GtkOffscreenWindowClass *class)
192 {
193   GtkWidgetClass *widget_class;
194   GtkContainerClass *container_class;
195
196   widget_class = GTK_WIDGET_CLASS (class);
197   container_class = GTK_CONTAINER_CLASS (class);
198
199   widget_class->realize = gtk_offscreen_window_realize;
200   widget_class->show = gtk_offscreen_window_show;
201   widget_class->hide = gtk_offscreen_window_hide;
202   widget_class->size_request = gtk_offscreen_window_size_request;
203   widget_class->size_allocate = gtk_offscreen_window_size_allocate;
204
205   container_class->check_resize = gtk_offscreen_window_check_resize;
206 }
207
208 static void
209 gtk_offscreen_window_init (GtkOffscreenWindow *window)
210 {
211 }
212
213 GtkWidget *
214 gtk_offscreen_window_new (void)
215 {
216   return g_object_new (gtk_offscreen_window_get_type (), NULL);
217 }
218
219
220 #define __GTK_OFFSCREEN_WINDOW_C__
221 #include "gtkaliasdef.c"