]> de.git.xonotic.org Git - xonotic/netradiant.git/blobdiff - libs/gtkutil/window.cpp
floating window: use HINT_UTILITY on Windows but normal window everywhere else, ref...
[xonotic/netradiant.git] / libs / gtkutil / window.cpp
index 7770821695f3eb76a6bae48076d95f4aa72ed8c6..80b79738892daabe2590316fc95c1cfa999fa3ba 100644 (file)
 
 inline void CHECK_RESTORE( ui::Widget w ){
        if ( gpointer_to_int( g_object_get_data( G_OBJECT( w ), "was_mapped" ) ) != 0 ) {
-               gtk_widget_show( w );
+
+#ifdef WORKAROUND_WINDOWS_GTK2_GLWIDGET
+               /* workaround for gtk 2.24 issue: not displayed glwidget after toggle */
+               GtkWidget* glwidget = GTK_WIDGET( g_object_get_data( G_OBJECT( w ), "glwidget" ) );
+               if ( glwidget ){
+                       gtk_widget_hide( glwidget );
+                       gtk_widget_show( glwidget );
+               }
+#endif // WORKAROUND_WINDOWS_GTK2_GLWIDGET
+
+               w.show();
        }
 }
 
 inline void CHECK_MINIMIZE( ui::Widget w ){
        g_object_set_data( G_OBJECT( w ), "was_mapped", gint_to_pointer( gtk_widget_get_visible( w ) ) );
-       gtk_widget_hide( w );
+       w.hide();
 }
 
 static gboolean main_window_iconified( ui::Widget widget, GdkEventWindowState* event, gpointer data ){
        if ( ( event->changed_mask & ( GDK_WINDOW_STATE_ICONIFIED | GDK_WINDOW_STATE_WITHDRAWN ) ) != 0 ) {
                if ( ( event->new_window_state & ( GDK_WINDOW_STATE_ICONIFIED | GDK_WINDOW_STATE_WITHDRAWN ) ) != 0 ) {
-                       CHECK_MINIMIZE( ui::Widget(GTK_WIDGET( data )) );
+                       CHECK_MINIMIZE( ui::Widget::from( data ) );
                }
                else
                {
-                       CHECK_RESTORE( ui::Widget(GTK_WIDGET( data )) );
+                       CHECK_RESTORE( ui::Widget::from( data ) );
                }
        }
        return FALSE;
 }
 
 unsigned int connect_floating( ui::Window main_window, ui::Window floating ){
-       return g_signal_connect( G_OBJECT( main_window ), "window_state_event", G_CALLBACK( main_window_iconified ), floating );
+       return main_window.connect( "window_state_event", G_CALLBACK( main_window_iconified ), floating );
 }
 
 gboolean destroy_disconnect_floating( ui::Window widget, gpointer data ){
@@ -67,7 +77,7 @@ gboolean floating_window_delete_present( ui::Window floating, GdkEventFocus *eve
 }
 
 guint connect_floating_window_delete_present( ui::Window floating, ui::Window main_window ){
-       return g_signal_connect( G_OBJECT( floating ), "delete_event", G_CALLBACK( floating_window_delete_present ), main_window );
+       return floating.connect( "delete_event", G_CALLBACK( floating_window_delete_present ), main_window );
 }
 
 gboolean floating_window_destroy_present( ui::Window floating, ui::Window main_window ){
@@ -78,25 +88,44 @@ gboolean floating_window_destroy_present( ui::Window floating, ui::Window main_w
 }
 
 guint connect_floating_window_destroy_present( ui::Window floating, ui::Window main_window ){
-       return g_signal_connect( G_OBJECT( floating ), "destroy", G_CALLBACK( floating_window_destroy_present ), main_window );
+       return floating.connect( "destroy", G_CALLBACK( floating_window_destroy_present ), main_window );
 }
 
 ui::Window create_floating_window( const char* title, ui::Window parent ){
        ui::Window window = ui::Window( ui::window_type::TOP );
        gtk_window_set_title( window, title );
 
+       /* Workaround: Windows minimizes the whole application including all the floating windows when
+        * one floating windows is minimized, this leads to an infinite loop of minimize/restore events,
+        * probably because of some race condition betwen all floating windows not having the same state
+        * at the same time, some being minimized while being restored at the same time, triggering the
+        * minimization and the restoration of the others, and so on.
+        * It's difficult to say such bug will never happen on other OS or with some window manager.
+        * While it's possible to decide this can be a design choihce since those floating windows are made
+        * to be displayed/hidden using menu or shortcuts, meaning the OS-specific way to minimize/restore
+        * them is superfluous and less efficient, the floating window mode that fixes issues on Windows
+        * is also known to be broken on KDE (the floating window does not get focus), this is likely to be
+        * a bug in kwin.
+        * In any way the mainframe is not a floating window and is not created using this function so the
+        * user minimizes the whole application including floating windows by minimizing the mainframe
+        */
+
+#ifdef WORKAROUND_WINDOWS_FLOATING_WINDOW
+       gtk_window_set_type_hint( window, GDK_WINDOW_TYPE_HINT_UTILITY );
+#endif // WORKAROUND_WINDOWS_FLOATING_WINDOW
+
        if ( parent ) {
                gtk_window_set_transient_for( window, parent );
                connect_floating_window_destroy_present( window, parent );
                g_object_set_data( G_OBJECT( window ), "floating_handler", gint_to_pointer( connect_floating( parent, window ) ) );
-               g_signal_connect( G_OBJECT( window ), "destroy", G_CALLBACK( destroy_disconnect_floating ), parent );
+               window.connect( "destroy", G_CALLBACK( destroy_disconnect_floating ), parent );
        }
 
        return window;
 }
 
 void destroy_floating_window( ui::Window window ){
-       gtk_widget_destroy( GTK_WIDGET( window ) );
+       window.destroy();
 }
 
 gint window_realize_remove_sysmenu( ui::Widget widget, gpointer data ){
@@ -105,21 +134,21 @@ gint window_realize_remove_sysmenu( ui::Widget widget, gpointer data ){
 }
 
 gboolean persistent_floating_window_delete( ui::Window floating, GdkEvent *event, ui::Window main_window ){
-       gtk_widget_hide( GTK_WIDGET( floating ) );
+       floating.hide();
        return TRUE;
 }
 
 ui::Window create_persistent_floating_window( const char* title, ui::Window main_window ){
-       ui::Window window = ui::Window(GTK_WINDOW( create_floating_window( title, main_window ) ));
+       auto window = create_floating_window( title, main_window );
 
-       gtk_widget_set_events( GTK_WIDGET( window ), GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK );
+       gtk_widget_set_events( window , GDK_KEY_PRESS_MASK | GDK_KEY_RELEASE_MASK );
 
        connect_floating_window_delete_present( window, main_window );
-       g_signal_connect( G_OBJECT( window ), "delete_event", G_CALLBACK( persistent_floating_window_delete ), 0 );
+       window.connect( "delete_event", G_CALLBACK( persistent_floating_window_delete ), 0 );
 
 #if 0
        if ( g_multimon_globals.m_bStartOnPrimMon && g_multimon_globals.m_bNoSysMenuPopups ) {
-               g_signal_connect( G_OBJECT( window ), "realize", G_CALLBACK( window_realize_remove_sysmenu ), 0 );
+               window.connect( "realize", G_CALLBACK( window_realize_remove_sysmenu ), 0 );
        }
 #endif
 
@@ -132,28 +161,28 @@ gint window_realize_remove_minmax( ui::Widget widget, gpointer data ){
 }
 
 void window_remove_minmax( ui::Window window ){
-       g_signal_connect( G_OBJECT( window ), "realize", G_CALLBACK( window_realize_remove_minmax ), 0 );
+       window.connect( "realize", G_CALLBACK( window_realize_remove_minmax ), 0 );
 }
 
 
 ui::ScrolledWindow create_scrolled_window( ui::Policy hscrollbar_policy, ui::Policy vscrollbar_policy, int border ){
-       auto scr = ui::ScrolledWindow();
-       gtk_widget_show( GTK_WIDGET( scr ) );
+       auto scr = ui::ScrolledWindow(ui::New);
+       scr.show();
        gtk_scrolled_window_set_policy( scr, (GtkPolicyType) hscrollbar_policy, (GtkPolicyType) vscrollbar_policy );
        gtk_scrolled_window_set_shadow_type( scr, GTK_SHADOW_IN );
        gtk_container_set_border_width( GTK_CONTAINER( scr ), border );
        return scr;
 }
 
-gboolean window_focus_in_clear_focus_widget(ui::Widget widget, GdkEventKey *event, gpointer data)
+gboolean window_focus_in_clear_focus_widget(ui::Window widget, GdkEventKey *event, gpointer data)
 {
-    gtk_window_set_focus( GTK_WINDOW( widget ), NULL );
+    gtk_window_set_focus( widget, NULL );
     return FALSE;
 }
 
 guint window_connect_focus_in_clear_focus_widget(ui::Window window)
 {
-       return g_signal_connect( G_OBJECT( window ), "focus_in_event", G_CALLBACK( window_focus_in_clear_focus_widget ), NULL );
+       return window.connect( "focus_in_event", G_CALLBACK( window_focus_in_clear_focus_widget ), NULL );
 }
 
 void window_get_position(ui::Window window, WindowPosition &position)
@@ -183,30 +212,30 @@ void window_set_position(ui::Window window, const WindowPosition &position)
        gtk_window_set_default_size( window, position.w, position.h );
 }
 
-void WindowPosition_Parse(WindowPosition &position, const char *value)
+void WindowPosition_String::Import(WindowPosition &position, const char *value)
 {
        if ( sscanf( value, "%d %d %d %d", &position.x, &position.y, &position.w, &position.h ) != 4 ) {
                position = WindowPosition( c_default_window_pos ); // ensure sane default value for window position
        }
 }
 
-void WindowPosition_Write(const WindowPosition &position, const StringImportCallback &importCallback)
+void WindowPosition_String::Export(const WindowPosition &self, const Callback<void(const char *)> &returnz)
 {
        char buffer[64];
-       sprintf( buffer, "%d %d %d %d", position.x, position.y, position.w, position.h );
-       importCallback( buffer );
+       sprintf( buffer, "%d %d %d %d", self.x, self.y, self.w, self.h );
+       returnz( buffer );
 }
 
-void WindowPositionTracker_importString(WindowPositionTracker &self, const char *value)
+void WindowPositionTracker_String::Import(WindowPositionTracker &self, const char *value)
 {
        WindowPosition position;
-       WindowPosition_Parse( position, value );
+       WindowPosition_String::Import( position, value );
        self.setPosition( position );
 }
 
-void WindowPositionTracker_exportString(const WindowPositionTracker &self, const StringImportCallback &importer)
+void WindowPositionTracker_String::Export(const WindowPositionTracker &self, const Callback<void(const char *)> &returnz)
 {
-       WindowPosition_Write( self.getPosition(), importer );
+       WindowPosition_String::Export( self.getPosition(), returnz );
 }
 
 gboolean WindowPositionTracker::configure(ui::Widget widget, GdkEventConfigure *event, WindowPositionTracker *self)
@@ -223,7 +252,7 @@ void WindowPositionTracker::sync(ui::Window window)
 void WindowPositionTracker::connect(ui::Window window)
 {
        sync( window );
-       g_signal_connect( G_OBJECT( window ), "configure_event", G_CALLBACK( configure ), this );
+       window.connect( "configure_event", G_CALLBACK( configure ), this );
 }
 
 const WindowPosition &WindowPositionTracker::getPosition() const