core: programming by coincidence
[luccawm.git] / bin / lucca-bin.c
blob07df0f1dcc1bbea9ff308e68a8db98efc4b13cf1
1 /* Copyright (c) 2008 Vincent Povirk
3 Permission is hereby granted, free of charge, to any person
4 obtaining a copy of this software and associated documentation
5 files (the "Software"), to deal in the Software without
6 restriction, including without limitation the rights to use,
7 copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the
9 Software is furnished to do so, subject to the following
10 conditions:
12 The above copyright notice and this permission notice shall be
13 included in all copies or substantial portions of the Software.
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 OTHER DEALINGS IN THE SOFTWARE.
25 #include "lucca-bin.h"
27 /* Implementation of LuccaBin */
29 static GtkWidgetClass* parent_class = NULL;
30 static GtkBinClass* bin_class = NULL;
32 /* method override declarations */
33 static void lucca_bin_realize(GtkWidget* widget);
34 static void lucca_bin_size_allocate(GtkWidget* widget, GtkAllocation *allocation);
36 static void lucca_bin_init(GTypeInstance* instance, gpointer g_class) {
37 LuccaBin* bin = LUCCA_BIN(instance);
38 GtkWindow* window = GTK_WINDOW(instance);
40 window->type = GTK_WINDOW_POPUP;
42 bin->dispose_has_run = FALSE;
43 bin->parent_window = NULL;
46 static void lucca_bin_dispose(GObject* obj) {
47 LuccaBin* bin = LUCCA_BIN(obj);
49 if (bin->dispose_has_run) {
50 return;
52 bin->dispose_has_run = TRUE;
54 g_object_unref(bin->parent_window);
56 G_OBJECT_CLASS(parent_class)->dispose(obj);
59 static void lucca_bin_class_init(gpointer g_class, gpointer class_data) {
60 LuccaBinClass* klass = (LuccaBinClass*)g_class;
61 GObjectClass* gobject_class = G_OBJECT_CLASS(klass);
62 GtkWidgetClass* widget_class = GTK_WIDGET_CLASS(g_class);
64 /* method overrides */
65 gobject_class->dispose = lucca_bin_dispose;
66 widget_class->realize = lucca_bin_realize;
67 widget_class->size_allocate = lucca_bin_size_allocate;
69 parent_class = g_type_class_peek_parent(klass);
71 bin_class = g_type_class_peek(GTK_TYPE_BIN);
74 GType lucca_bin_get_type() {
75 static GType type = 0;
76 if (type == 0) {
77 static const GTypeInfo info = {
78 sizeof (LuccaBinClass),
79 NULL, /* base_init */
80 NULL, /* base_finalize */
81 lucca_bin_class_init,
82 NULL, /* class_finalize */
83 NULL, /* class_data */
84 sizeof (LuccaBin),
85 0, /* n_preallocs */
86 lucca_bin_init
88 type = g_type_register_static (GTK_TYPE_WINDOW, "LuccaBinType", &info, 0);
90 return type;
93 /* method override defintions */
94 static void lucca_bin_realize(GtkWidget* widget) {
95 LuccaBin* bin = LUCCA_BIN(widget);
96 GtkWindow* window = GTK_WINDOW(widget);
97 GdkWindowAttr window_attributes;
98 gint attributes_mask=0;
100 window->type = GTK_WINDOW_POPUP; /* FIXME: For a reason I do not understand, this is reset to GTK_WINDOW_TOPLEVEL when a LuccaBin is created; there's probably a better way to deal with this problem, but I don't know what it is */
102 window_attributes.window_type = GDK_WINDOW_TEMP;
104 if (window->title != NULL) {
105 window_attributes.title = window->title;
106 attributes_mask |= GDK_WA_TITLE;
109 window_attributes.wmclass_name = window->wmclass_name;
110 window_attributes.wmclass_class = window->wmclass_class;
111 attributes_mask |= GDK_WA_WMCLASS;
113 window_attributes.wclass = GDK_INPUT_OUTPUT;
115 window_attributes.visual = gtk_widget_get_visual(widget);
116 window_attributes.colormap = gtk_widget_get_colormap(widget);
117 attributes_mask |= GDK_WA_VISUAL|GDK_WA_COLORMAP;
119 window_attributes.x = widget->allocation.x;
120 window_attributes.y = widget->allocation.y;
121 window_attributes.width = widget->allocation.width;
122 window_attributes.height = widget->allocation.height;
123 attributes_mask |= GDK_WA_X|GDK_WA_Y;
125 window_attributes.event_mask = gtk_widget_get_events (widget);
126 window_attributes.event_mask |= GDK_EXPOSURE_MASK|GDK_KEY_PRESS_MASK|GDK_KEY_RELEASE_MASK|GDK_ENTER_NOTIFY_MASK|GDK_LEAVE_NOTIFY_MASK|GDK_FOCUS_CHANGE_MASK|GDK_STRUCTURE_MASK; /* GtkWindows set these events; I have no idea why */
128 window_attributes.override_redirect = TRUE;
129 attributes_mask |= GDK_WA_NOREDIR;
131 GTK_WIDGET_SET_FLAGS(widget, GTK_REALIZED);
133 widget->window = gdk_window_new(bin->parent_window, &window_attributes, attributes_mask);
135 gdk_window_set_user_data(widget->window, bin);
137 widget->style = gtk_style_attach(widget->style, widget->window);
138 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
141 static void _size_allocate(LuccaBin* bin) {
142 GtkBin* gtkbin = GTK_BIN(bin);
143 GtkWidget* widget = GTK_WIDGET(bin);
144 GtkAllocation child_allocation;
146 child_allocation.x = 0;
147 child_allocation.y = 0;
148 child_allocation.width = widget->allocation.width;
149 child_allocation.height = widget->allocation.height;
151 if (gtkbin->child)
152 gtk_widget_size_allocate(gtkbin->child, &child_allocation);
155 static void lucca_bin_size_allocate(GtkWidget* widget, GtkAllocation *allocation) {
156 LuccaBin* bin = LUCCA_BIN(widget);
158 widget->allocation = *allocation;
160 _size_allocate(bin);
163 void lucca_bin_set_parent(LuccaBin* bin, GdkWindow* parent) {
164 GtkWidget* widget = GTK_WIDGET(bin);
165 gboolean remap=FALSE;
167 if (bin->parent_window)
168 g_object_unref(bin->parent_window);
169 bin->parent_window = g_object_ref(parent);
171 if (GTK_WIDGET_REALIZED(widget)) {
172 if (GTK_WIDGET_MAPPED(widget)) {
173 gtk_widget_unmap(widget);
174 remap = TRUE;
177 gdk_window_reparent(widget->window, bin->parent_window, widget->allocation.x, widget->allocation.y);
179 if (remap) {
180 gtk_widget_map(widget);
185 void lucca_bin_move_resize(LuccaBin* bin, gint x, gint y, gint width, gint height) {
186 GtkWidget* widget = GTK_WIDGET(bin);
188 gtk_widget_set_size_request(widget, width, height);
190 widget->allocation.x = x;
191 widget->allocation.y = y;
193 if (GTK_WIDGET_REALIZED(widget)) {
194 gtk_window_move(GTK_WINDOW(widget), x, y);
195 gtk_window_resize(GTK_WINDOW(widget), width, height);
197 _size_allocate(bin);