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