]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - libs/gtkutil/window.cpp
allow undo “make detail/structural”, <3 @SpiKe, thanks @Garux, fix #76
[xonotic/netradiant.git] / libs / gtkutil / window.cpp
1 /*
2    Copyright (C) 2001-2006, William Joseph.
3    All Rights Reserved.
4
5    This file is part of GtkRadiant.
6
7    GtkRadiant is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    GtkRadiant is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with GtkRadiant; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20  */
21
22 #include "window.h"
23
24 #include <gtk/gtk.h>
25
26 #include "pointer.h"
27 #include "accelerator.h"
28
29 inline void CHECK_RESTORE(ui::Widget w)
30 {
31     if (gpointer_to_int(g_object_get_data(G_OBJECT(w), "was_mapped")) != 0) {
32         w.show();
33     }
34 }
35
36 inline void CHECK_MINIMIZE(ui::Widget w)
37 {
38     g_object_set_data(G_OBJECT(w), "was_mapped", gint_to_pointer(gtk_widget_get_visible(w)));
39     w.hide();
40 }
41
42 static gboolean main_window_iconified(ui::Widget widget, GdkEventWindowState *event, gpointer data)
43 {
44     if ((event->changed_mask & (GDK_WINDOW_STATE_ICONIFIED | GDK_WINDOW_STATE_WITHDRAWN)) != 0) {
45         if ((event->new_window_state & (GDK_WINDOW_STATE_ICONIFIED | GDK_WINDOW_STATE_WITHDRAWN)) != 0) {
46             CHECK_MINIMIZE(ui::Widget::from(data));
47         } else {
48             CHECK_RESTORE(ui::Widget::from(data));
49         }
50     }
51     return FALSE;
52 }
53
54 unsigned int connect_floating(ui::Window main_window, ui::Window floating)
55 {
56     return main_window.connect("window_state_event", G_CALLBACK(main_window_iconified), floating);
57 }
58
59 gboolean destroy_disconnect_floating(ui::Window widget, gpointer data)
60 {
61     g_signal_handler_disconnect(G_OBJECT(data),
62                                 gpointer_to_int(g_object_get_data(G_OBJECT(widget), "floating_handler")));
63     return FALSE;
64 }
65
66 gboolean floating_window_delete_present(ui::Window floating, GdkEventFocus *event, ui::Window main_window)
67 {
68     if (gtk_window_is_active(floating) || gtk_window_is_active(main_window)) {
69         gtk_window_present(main_window);
70     }
71     return FALSE;
72 }
73
74 guint connect_floating_window_delete_present(ui::Window floating, ui::Window main_window)
75 {
76     return floating.connect("delete_event", G_CALLBACK(floating_window_delete_present), main_window);
77 }
78
79 gboolean floating_window_destroy_present(ui::Window floating, ui::Window main_window)
80 {
81     if (gtk_window_is_active(floating) || gtk_window_is_active(main_window)) {
82         gtk_window_present(main_window);
83     }
84     return FALSE;
85 }
86
87 guint connect_floating_window_destroy_present(ui::Window floating, ui::Window main_window)
88 {
89     return floating.connect("destroy", G_CALLBACK(floating_window_destroy_present), main_window);
90 }
91
92 ui::Window create_floating_window(const char *title, ui::Window parent)
93 {
94     ui::Window window = ui::Window(ui::window_type::TOP);
95     gtk_window_set_title(window, title);
96
97     if (parent) {
98         gtk_window_set_transient_for(window, parent);
99         connect_floating_window_destroy_present(window, parent);
100         g_object_set_data(G_OBJECT(window), "floating_handler", gint_to_pointer(connect_floating(parent, window)));
101         window.connect("destroy", G_CALLBACK(destroy_disconnect_floating), parent);
102     }
103
104     return window;
105 }
106
107 void destroy_floating_window(ui::Window window)
108 {
109     window.destroy();
110 }
111
112 gint window_realize_remove_sysmenu(ui::Widget widget, gpointer data)
113 {
114     gdk_window_set_decorations(gtk_widget_get_window(widget), (GdkWMDecoration) (GDK_DECOR_ALL | GDK_DECOR_MENU));
115     return FALSE;
116 }
117
118 gboolean persistent_floating_window_delete(ui::Window floating, GdkEvent *event, ui::Window main_window)
119 {
120     floating.hide();
121     return TRUE;
122 }
123
124 ui::Window create_persistent_floating_window(const char *title, ui::Window main_window)
125 {
126     auto window = create_floating_window(title, main_window);
127
128     gtk_widget_set_events(window, GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK);
129
130     connect_floating_window_delete_present(window, main_window);
131     window.connect("delete_event", G_CALLBACK(persistent_floating_window_delete), 0);
132
133 #if 0
134     if ( g_multimon_globals.m_bStartOnPrimMon && g_multimon_globals.m_bNoSysMenuPopups ) {
135         window.connect( "realize", G_CALLBACK( window_realize_remove_sysmenu ), 0 );
136     }
137 #endif
138
139     return window;
140 }
141
142 gint window_realize_remove_minmax(ui::Widget widget, gpointer data)
143 {
144     gdk_window_set_decorations(gtk_widget_get_window(widget),
145                                (GdkWMDecoration) (GDK_DECOR_ALL | GDK_DECOR_MINIMIZE | GDK_DECOR_MAXIMIZE));
146     return FALSE;
147 }
148
149 void window_remove_minmax(ui::Window window)
150 {
151     window.connect("realize", G_CALLBACK(window_realize_remove_minmax), 0);
152 }
153
154
155 ui::ScrolledWindow create_scrolled_window(ui::Policy hscrollbar_policy, ui::Policy vscrollbar_policy, int border)
156 {
157     auto scr = ui::ScrolledWindow(ui::New);
158     scr.show();
159     gtk_scrolled_window_set_policy(scr, (GtkPolicyType) hscrollbar_policy, (GtkPolicyType) vscrollbar_policy);
160     gtk_scrolled_window_set_shadow_type(scr, GTK_SHADOW_IN);
161     gtk_container_set_border_width(GTK_CONTAINER(scr), border);
162     return scr;
163 }
164
165 gboolean window_focus_in_clear_focus_widget(ui::Window widget, GdkEventKey *event, gpointer data)
166 {
167     gtk_window_set_focus(widget, NULL);
168     return FALSE;
169 }
170
171 guint window_connect_focus_in_clear_focus_widget(ui::Window window)
172 {
173     return window.connect("focus_in_event", G_CALLBACK(window_focus_in_clear_focus_widget), NULL);
174 }
175
176 void window_get_position(ui::Window window, WindowPosition &position)
177 {
178     ASSERT_MESSAGE(window, "error saving window position");
179
180     gtk_window_get_position(window, &position.x, &position.y);
181     gtk_window_get_size(window, &position.w, &position.h);
182 }
183
184 void window_set_position(ui::Window window, const WindowPosition &position)
185 {
186     gtk_window_set_gravity(window, GDK_GRAVITY_STATIC);
187
188     GdkScreen *screen = gdk_screen_get_default();
189     if (position.x < 0
190         || position.y < 0
191         || position.x > gdk_screen_get_width(screen)
192         || position.y > gdk_screen_get_height(screen)) {
193         gtk_window_set_position(window, GTK_WIN_POS_CENTER_ON_PARENT);
194     } else {
195         gtk_window_move(window, position.x, position.y);
196     }
197
198     gtk_window_set_default_size(window, position.w, position.h);
199 }
200
201 void WindowPosition_String::Import(WindowPosition &position, const char *value)
202 {
203     if (sscanf(value, "%d %d %d %d", &position.x, &position.y, &position.w, &position.h) != 4) {
204         position = WindowPosition(c_default_window_pos); // ensure sane default value for window position
205     }
206 }
207
208 void WindowPosition_String::Export(const WindowPosition &self, const Callback<void(const char *)> &returnz)
209 {
210     char buffer[64];
211     sprintf(buffer, "%d %d %d %d", self.x, self.y, self.w, self.h);
212     returnz(buffer);
213 }
214
215 void WindowPositionTracker_String::Import(WindowPositionTracker &self, const char *value)
216 {
217     WindowPosition position;
218     WindowPosition_String::Import(position, value);
219     self.setPosition(position);
220 }
221
222 void
223 WindowPositionTracker_String::Export(const WindowPositionTracker &self, const Callback<void(const char *)> &returnz)
224 {
225     WindowPosition_String::Export(self.getPosition(), returnz);
226 }
227
228 gboolean WindowPositionTracker::configure(ui::Widget widget, GdkEventConfigure *event, WindowPositionTracker *self)
229 {
230     self->m_position = WindowPosition(event->x, event->y, event->width, event->height);
231     return FALSE;
232 }
233
234 void WindowPositionTracker::sync(ui::Window window)
235 {
236     window_set_position(window, m_position);
237 }
238
239 void WindowPositionTracker::connect(ui::Window window)
240 {
241     sync(window);
242     window.connect("configure_event", G_CALLBACK(configure), this);
243 }
244
245 const WindowPosition &WindowPositionTracker::getPosition() const
246 {
247     return m_position;
248 }
249
250 void WindowPositionTracker::setPosition(const WindowPosition &position)
251 {
252     m_position = position;
253 }