]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - libs/gtkutil/window.cpp
Merge branch 'NateEag-master-patch-12920' into 'master'
[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         if ( gpointer_to_int( g_object_get_data( G_OBJECT( w ), "was_mapped" ) ) != 0 ) {
31
32 #ifdef WORKAROUND_WINDOWS_GTK2_GLWIDGET
33                 /* workaround for gtk 2.24 issue: not displayed glwidget after toggle */
34                 GtkWidget* glwidget = GTK_WIDGET( g_object_get_data( G_OBJECT( w ), "glwidget" ) );
35                 if ( glwidget ){
36                         gtk_widget_hide( glwidget );
37                         gtk_widget_show( glwidget );
38                 }
39 #endif // WORKAROUND_WINDOWS_GTK2_GLWIDGET
40
41                 w.show();
42 #define GARUX_GTK_WORKAROUND
43 #ifndef GARUX_GTK_WORKAROUND
44                 /* workaround for gtk 2.24 issue: not displayed glwidget after min/restore */
45                 GtkWidget* glwidget = GTK_WIDGET( g_object_get_data( G_OBJECT( w ), "glwidget" ) );
46                 if ( glwidget ){
47                         gtk_widget_hide( glwidget );
48                         gtk_widget_show( glwidget );
49                 }
50 #endif
51         }
52 }
53
54 inline void CHECK_MINIMIZE( ui::Widget w ){
55         g_object_set_data( G_OBJECT( w ), "was_mapped", gint_to_pointer( gtk_widget_get_visible( w ) ) );
56         w.hide();
57 }
58
59 static gboolean main_window_iconified( ui::Widget widget, GdkEventWindowState* event, gpointer data ){
60         if ( ( event->changed_mask & ( GDK_WINDOW_STATE_ICONIFIED | GDK_WINDOW_STATE_WITHDRAWN ) ) != 0 ) {
61                 if ( ( event->new_window_state & ( GDK_WINDOW_STATE_ICONIFIED | GDK_WINDOW_STATE_WITHDRAWN ) ) != 0 ) {
62                         CHECK_MINIMIZE( ui::Widget::from( data ) );
63                 }
64                 else
65                 {
66                         CHECK_RESTORE( ui::Widget::from( data ) );
67                 }
68         }
69         return FALSE;
70 }
71
72 unsigned int connect_floating( ui::Window main_window, ui::Window floating ){
73         return main_window.connect( "window_state_event", G_CALLBACK( main_window_iconified ), floating );
74 }
75
76 gboolean destroy_disconnect_floating( ui::Window widget, gpointer data ){
77         g_signal_handler_disconnect( G_OBJECT( data ), gpointer_to_int( g_object_get_data( G_OBJECT( widget ), "floating_handler" ) ) );
78         return FALSE;
79 }
80
81 gboolean floating_window_delete_present( ui::Window floating, GdkEventFocus *event, ui::Window main_window ){
82         if ( gtk_window_is_active( floating ) || gtk_window_is_active( main_window ) ) {
83                 gtk_window_present( main_window );
84         }
85         return FALSE;
86 }
87
88 guint connect_floating_window_delete_present( ui::Window floating, ui::Window main_window ){
89         return floating.connect( "delete_event", G_CALLBACK( floating_window_delete_present ), main_window );
90 }
91
92 gboolean floating_window_destroy_present( ui::Window floating, ui::Window main_window ){
93         if ( gtk_window_is_active( floating ) || gtk_window_is_active( main_window ) ) {
94                 gtk_window_present( main_window );
95         }
96         return FALSE;
97 }
98
99 guint connect_floating_window_destroy_present( ui::Window floating, ui::Window main_window ){
100         return floating.connect( "destroy", G_CALLBACK( floating_window_destroy_present ), main_window );
101 }
102
103 ui::Window create_floating_window( const char* title, ui::Window parent ){
104         ui::Window window = ui::Window( ui::window_type::TOP );
105         gtk_window_set_title( window, title );
106
107         /* Workaround: Windows minimizes the whole application including all the floating windows when
108          * one floating windows is minimized, this leads to an infinite loop of minimize/restore events,
109          * probably because of some race condition betwen all floating windows not having the same state
110          * at the same time, some being minimized while being restored at the same time, triggering the
111          * minimization and the restoration of the others, and so on.
112          * It's difficult to say such bug will never happen on other OS or with some window manager.
113          * While it's possible to decide this can be a design choihce since those floating windows are made
114          * to be displayed/hidden using menu or shortcuts, meaning the OS-specific way to minimize/restore
115          * them is superfluous and less efficient, the floating window mode that fixes issues on Windows
116          * is also known to be broken on KDE (the floating window does not get focus), this is likely to be
117          * a bug in kwin.
118          * In any way the mainframe is not a floating window and is not created using this function so the
119          * user minimizes the whole application including floating windows by minimizing the mainframe
120          */
121
122 #ifdef WORKAROUND_WINDOWS_FLOATING_WINDOW
123         gtk_window_set_type_hint( window, GDK_WINDOW_TYPE_HINT_UTILITY );
124 #endif // WORKAROUND_WINDOWS_FLOATING_WINDOW
125
126         if ( parent ) {
127                 gtk_window_set_transient_for( window, parent );
128                 connect_floating_window_destroy_present( window, parent );
129                 g_object_set_data( G_OBJECT( window ), "floating_handler", gint_to_pointer( connect_floating( parent, window ) ) );
130                 window.connect( "destroy", G_CALLBACK( destroy_disconnect_floating ), parent );
131 /*
132                 //gtk_window_set_type_hint (window,GDK_WINDOW_TYPE_HINT_UTILITY);
133                 //gtk_window_set_type_hint (window,GDK_WINDOW_TYPE_HINT_DIALOG);
134                 gtk_window_set_keep_above ( window, TRUE );
135                 GtkWidget* widget = GTK_WIDGET( window );
136                 gtk_widget_realize ( widget );
137                 GdkWindow* gdk_window = gtk_widget_get_window( widget );
138                 //gdk_window_set_decorations ( gdk_window, (GdkWMDecoration)(GDK_DECOR_BORDER|GDK_DECOR_RESIZEH|GDK_DECOR_TITLE|GDK_DECOR_MENU|GDK_DECOR_MINIMIZE|GDK_DECOR_MAXIMIZE) );
139                 //gdk_window_set_functions ( gdk_window, (GdkWMFunction)( GDK_FUNC_RESIZE|GDK_FUNC_MOVE|GDK_FUNC_MINIMIZE|GDK_FUNC_MAXIMIZE|GDK_FUNC_CLOSE ) );
140                 //gdk_window_set_decorations ( gdk_window, (GdkWMDecoration)( GDK_DECOR_ALL ) );
141                 //gdk_window_set_functions ( gdk_window, (GdkWMFunction)( GDK_FUNC_ALL ) );
142                 //gdk_window_set_type_hint ( gdk_window, GDK_WINDOW_TYPE_HINT_DIALOG );
143                 //gdk_window_set_type_hint ( gdk_window, GDK_WINDOW_TYPE_HINT_UTILITY );
144                 //gdk_window_set_type_hint ( gdk_window, GDK_WINDOW_TYPE_HINT_NORMAL );
145                 gdk_window_set_skip_taskbar_hint ( gdk_window, TRUE );
146                 gdk_window_set_skip_pager_hint ( gdk_window, TRUE );
147 */
148         }
149
150         return window;
151 }
152
153 void destroy_floating_window( ui::Window window ){
154         window.destroy();
155 }
156
157 gint window_realize_remove_sysmenu( ui::Widget widget, gpointer data ){
158         gdk_window_set_decorations( gtk_widget_get_window(widget), (GdkWMDecoration)( GDK_DECOR_ALL | GDK_DECOR_MENU ) );
159         return FALSE;
160 }
161
162 gboolean persistent_floating_window_delete( ui::Window floating, GdkEvent *event, ui::Window main_window ){
163         floating.hide();
164         return TRUE;
165 }
166
167 ui::Window create_persistent_floating_window( const char* title, ui::Window main_window ){
168         auto window = create_floating_window( title, main_window );
169
170         gtk_widget_set_events( window , GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK );
171
172         connect_floating_window_delete_present( window, main_window );
173         window.connect( "delete_event", G_CALLBACK( persistent_floating_window_delete ), 0 );
174
175 #if 0
176         if ( g_multimon_globals.m_bStartOnPrimMon && g_multimon_globals.m_bNoSysMenuPopups ) {
177                 window.connect( "realize", G_CALLBACK( window_realize_remove_sysmenu ), 0 );
178         }
179 #endif
180
181         return window;
182 }
183
184 gint window_realize_remove_minmax( ui::Widget widget, gpointer data ){
185         gdk_window_set_decorations( gtk_widget_get_window(widget), (GdkWMDecoration)( GDK_DECOR_ALL | GDK_DECOR_MINIMIZE | GDK_DECOR_MAXIMIZE ) );
186         return FALSE;
187 }
188
189 void window_remove_minmax( ui::Window window ){
190         window.connect( "realize", G_CALLBACK( window_realize_remove_minmax ), 0 );
191 }
192
193
194 ui::ScrolledWindow create_scrolled_window( ui::Policy hscrollbar_policy, ui::Policy vscrollbar_policy, int border ){
195         auto scr = ui::ScrolledWindow(ui::New);
196         scr.show();
197         gtk_scrolled_window_set_policy( scr, (GtkPolicyType) hscrollbar_policy, (GtkPolicyType) vscrollbar_policy );
198         gtk_scrolled_window_set_shadow_type( scr, GTK_SHADOW_IN );
199         gtk_container_set_border_width( GTK_CONTAINER( scr ), border );
200         return scr;
201 }
202
203 gboolean window_focus_in_clear_focus_widget(ui::Window widget, GdkEventKey *event, gpointer data)
204 {
205     gtk_window_set_focus( widget, NULL );
206     return FALSE;
207 }
208
209 guint window_connect_focus_in_clear_focus_widget(ui::Window window)
210 {
211         return window.connect( "focus_in_event", G_CALLBACK( window_focus_in_clear_focus_widget ), NULL );
212 }
213
214 void window_get_position(ui::Window window, WindowPosition &position)
215 {
216         ASSERT_MESSAGE( window , "error saving window position" );
217
218         gtk_window_get_position( window, &position.x, &position.y );
219         gtk_window_get_size( window, &position.w, &position.h );
220 }
221
222 void window_set_position(ui::Window window, const WindowPosition &position)
223 {
224         gtk_window_set_gravity( window, GDK_GRAVITY_STATIC );
225
226         GdkScreen* screen = gdk_screen_get_default();
227         if ( position.x < 0
228                  || position.y < 0
229                  || position.x > gdk_screen_get_width( screen )
230                  || position.y > gdk_screen_get_height( screen ) ) {
231                 gtk_window_set_position( window, GTK_WIN_POS_CENTER_ON_PARENT );
232         }
233         else
234         {
235                 gtk_window_move( window, position.x, position.y );
236         }
237
238         gtk_window_set_default_size( window, position.w, position.h );
239 }
240
241 void WindowPosition_String::Import(WindowPosition &position, const char *value)
242 {
243         if ( sscanf( value, "%d %d %d %d", &position.x, &position.y, &position.w, &position.h ) != 4 ) {
244                 position = WindowPosition( c_default_window_pos ); // ensure sane default value for window position
245         }
246 }
247
248 void WindowPosition_String::Export(const WindowPosition &self, const Callback<void(const char *)> &returnz)
249 {
250         char buffer[64];
251         sprintf( buffer, "%d %d %d %d", self.x, self.y, self.w, self.h );
252         returnz( buffer );
253 }
254
255 void WindowPositionTracker_String::Import(WindowPositionTracker &self, const char *value)
256 {
257         WindowPosition position;
258         WindowPosition_String::Import( position, value );
259         self.setPosition( position );
260 }
261
262 void WindowPositionTracker_String::Export(const WindowPositionTracker &self, const Callback<void(const char *)> &returnz)
263 {
264         WindowPosition_String::Export( self.getPosition(), returnz );
265 }
266
267 gboolean WindowPositionTracker::configure(ui::Widget widget, GdkEventConfigure *event, WindowPositionTracker *self)
268 {
269         self->m_position = WindowPosition( event->x, event->y, event->width, event->height );
270         return FALSE;
271 }
272
273 void WindowPositionTracker::sync(ui::Window window)
274 {
275         window_set_position( window, m_position );
276 }
277
278 void WindowPositionTracker::connect(ui::Window window)
279 {
280         sync( window );
281         window.connect( "configure_event", G_CALLBACK( configure ), this );
282 }
283
284 const WindowPosition &WindowPositionTracker::getPosition() const
285 {
286         return m_position;
287 }
288
289 void WindowPositionTracker::setPosition(const WindowPosition &position)
290 {
291         m_position = position;
292 }