]> git.saurik.com Git - wxWidgets.git/blobdiff - src/gtk/window.cpp
Misc validity fixes to samples/xrc/rc/*.xrc.
[wxWidgets.git] / src / gtk / window.cpp
index 7eb931409346784b9038735cd01c25fe8414883f..8f41635cf2f48279f60db0428405a27a78869a74 100644 (file)
@@ -2,7 +2,6 @@
 // Name:        src/gtk/window.cpp
 // Purpose:     wxWindowGTK implementation
 // Author:      Robert Roebling
 // Name:        src/gtk/window.cpp
 // Purpose:     wxWindowGTK implementation
 // Author:      Robert Roebling
-// Id:          $Id$
 // Copyright:   (c) 1998 Robert Roebling, Julian Smart
 // Licence:     wxWindows licence
 /////////////////////////////////////////////////////////////////////////////
 // Copyright:   (c) 1998 Robert Roebling, Julian Smart
 // Licence:     wxWindows licence
 /////////////////////////////////////////////////////////////////////////////
@@ -755,10 +754,10 @@ wxTranslateGTKKeyEventToWx(wxKeyEvent& event,
 
     KeySym keysym = gdk_event->keyval;
 
 
     KeySym keysym = gdk_event->keyval;
 
-    wxLogTrace(TRACE_KEYS, wxT("Key %s event: keysym = %ld"),
+    wxLogTrace(TRACE_KEYS, wxT("Key %s event: keysym = %lu"),
                event.GetEventType() == wxEVT_KEY_UP ? wxT("release")
                                                     : wxT("press"),
                event.GetEventType() == wxEVT_KEY_UP ? wxT("release")
                                                     : wxT("press"),
-               keysym);
+               static_cast<unsigned long>(keysym));
 
     long key_code = wxTranslateKeySymToWXKey(keysym, false /* !isChar */);
 
 
     long key_code = wxTranslateKeySymToWXKey(keysym, false /* !isChar */);
 
@@ -961,7 +960,7 @@ gtk_window_key_press_callback( GtkWidget *WXUNUSED(widget),
             int command = ancestor->GetAcceleratorTable()->GetCommand( event );
             if (command != -1)
             {
             int command = ancestor->GetAcceleratorTable()->GetCommand( event );
             if (command != -1)
             {
-                wxCommandEvent menu_event( wxEVT_COMMAND_MENU_SELECTED, command );
+                wxCommandEvent menu_event( wxEVT_MENU, command );
                 ret = ancestor->HandleWindowEvent( menu_event );
 
                 if ( !ret )
                 ret = ancestor->HandleWindowEvent( menu_event );
 
                 if ( !ret )
@@ -969,7 +968,7 @@ gtk_window_key_press_callback( GtkWidget *WXUNUSED(widget),
                     // if the accelerator wasn't handled as menu event, try
                     // it as button click (for compatibility with other
                     // platforms):
                     // if the accelerator wasn't handled as menu event, try
                     // it as button click (for compatibility with other
                     // platforms):
-                    wxCommandEvent button_event( wxEVT_COMMAND_BUTTON_CLICKED, command );
+                    wxCommandEvent button_event( wxEVT_BUTTON, command );
                     ret = ancestor->HandleWindowEvent( button_event );
                 }
 
                     ret = ancestor->HandleWindowEvent( button_event );
                 }
 
@@ -1615,99 +1614,121 @@ gtk_window_motion_notify_callback( GtkWidget * WXUNUSED(widget),
 // "scroll_event" (mouse wheel event)
 //-----------------------------------------------------------------------------
 
 // "scroll_event" (mouse wheel event)
 //-----------------------------------------------------------------------------
 
-static gboolean
-window_scroll_event_hscrollbar(GtkWidget*, GdkEventScroll* gdk_event, wxWindow* win)
+static void AdjustRangeValue(GtkRange* range, double step)
 {
 {
-    if (gdk_event->direction != GDK_SCROLL_LEFT &&
-        gdk_event->direction != GDK_SCROLL_RIGHT)
-    {
-        return false;
-    }
-
-    GtkRange *range = win->m_scrollBar[wxWindow::ScrollDir_Horz];
-
     if (range && gtk_widget_get_visible(GTK_WIDGET(range)))
     {
         GtkAdjustment* adj = gtk_range_get_adjustment(range);
     if (range && gtk_widget_get_visible(GTK_WIDGET(range)))
     {
         GtkAdjustment* adj = gtk_range_get_adjustment(range);
-        double delta = gtk_adjustment_get_step_increment(adj) * 3;
-        if (gdk_event->direction == GDK_SCROLL_LEFT)
-            delta = -delta;
-
-        gtk_range_set_value(range, gtk_adjustment_get_value(adj) + delta);
-
-        return TRUE;
+        double value = gtk_adjustment_get_value(adj);
+        value += step * gtk_adjustment_get_step_increment(adj);
+        gtk_range_set_value(range, value);
     }
     }
-
-    return FALSE;
 }
 
 static gboolean
 }
 
 static gboolean
-window_scroll_event(GtkWidget*, GdkEventScroll* gdk_event, wxWindow* win)
+scroll_event(GtkWidget* widget, GdkEventScroll* gdk_event, wxWindow* win)
 {
     wxMouseEvent event(wxEVT_MOUSEWHEEL);
     InitMouseEvent(win, event, gdk_event);
 
 {
     wxMouseEvent event(wxEVT_MOUSEWHEEL);
     InitMouseEvent(win, event, gdk_event);
 
-    // FIXME: Get these values from GTK or GDK
-    event.m_linesPerAction = 3;
     event.m_wheelDelta = 120;
     event.m_wheelDelta = 120;
+    event.m_linesPerAction = 3;
+    event.m_columnsPerAction = 3;
 
 
-    // Determine the scroll direction.
-    switch (gdk_event->direction)
+    GtkRange* range_h = win->m_scrollBar[wxWindow::ScrollDir_Horz];
+    GtkRange* range_v = win->m_scrollBar[wxWindow::ScrollDir_Vert];
+    const bool is_range_h = (void*)widget == range_h;
+    const bool is_range_v = (void*)widget == range_v;
+    GdkScrollDirection direction = gdk_event->direction;
+    switch (direction)
     {
         case GDK_SCROLL_UP:
     {
         case GDK_SCROLL_UP:
-        case GDK_SCROLL_RIGHT:
-            event.m_wheelRotation = 120;
+            if (is_range_h)
+                direction = GDK_SCROLL_LEFT;
             break;
             break;
-
         case GDK_SCROLL_DOWN:
         case GDK_SCROLL_DOWN:
+            if (is_range_h)
+                direction = GDK_SCROLL_RIGHT;
+            break;
         case GDK_SCROLL_LEFT:
         case GDK_SCROLL_LEFT:
-            event.m_wheelRotation = -120;
+            if (is_range_v)
+                direction = GDK_SCROLL_UP;
+            break;
+        case GDK_SCROLL_RIGHT:
+            if (is_range_v)
+                direction = GDK_SCROLL_DOWN;
+            break;
+        default:
             break;
 #if GTK_CHECK_VERSION(3,4,0)
         case GDK_SCROLL_SMOOTH:
             break;
 #if GTK_CHECK_VERSION(3,4,0)
         case GDK_SCROLL_SMOOTH:
-            // TODO
-#endif
-        default:
-            return false;  // Unknown/unhandled direction
+            double delta_x = gdk_event->delta_x;
+            double delta_y = gdk_event->delta_y;
+            if (delta_x == 0)
+            {
+                if (is_range_h)
+                {
+                    delta_x = delta_y;
+                    delta_y = 0;
+                }
+            }
+            else if (delta_y == 0)
+            {
+                if (is_range_v)
+                {
+                    delta_y = delta_x;
+                    delta_x = 0;
+                }
+            }
+            if (delta_x)
+            {
+                event.m_wheelAxis = wxMOUSE_WHEEL_HORIZONTAL;
+                event.m_wheelRotation = int(event.m_wheelDelta * delta_x);
+                if (!win->GTKProcessEvent(event))
+                    AdjustRangeValue(range_h, event.m_columnsPerAction * delta_x);
+            }
+            if (delta_y)
+            {
+                event.m_wheelAxis = wxMOUSE_WHEEL_VERTICAL;
+                event.m_wheelRotation = int(event.m_wheelDelta * -delta_y);
+                if (!win->GTKProcessEvent(event))
+                    AdjustRangeValue(range_v, event.m_linesPerAction * delta_y);
+            }
+            return true;
+#endif // GTK_CHECK_VERSION(3,4,0)
     }
     }
-
-    // And the scroll axis.
-    switch (gdk_event->direction)
+    GtkRange *range;
+    double step;
+    switch (direction)
     {
         case GDK_SCROLL_UP:
         case GDK_SCROLL_DOWN:
     {
         case GDK_SCROLL_UP:
         case GDK_SCROLL_DOWN:
+            range = range_v;
             event.m_wheelAxis = wxMOUSE_WHEEL_VERTICAL;
             event.m_wheelAxis = wxMOUSE_WHEEL_VERTICAL;
+            step = event.m_linesPerAction;
             break;
             break;
-
         case GDK_SCROLL_LEFT:
         case GDK_SCROLL_RIGHT:
         case GDK_SCROLL_LEFT:
         case GDK_SCROLL_RIGHT:
+            range = range_h;
             event.m_wheelAxis = wxMOUSE_WHEEL_HORIZONTAL;
             event.m_wheelAxis = wxMOUSE_WHEEL_HORIZONTAL;
+            step = event.m_columnsPerAction;
             break;
             break;
-#if GTK_CHECK_VERSION(3,4,0)
-        case GDK_SCROLL_SMOOTH:
-            // TODO
-            break;
-#endif
+        default:
+            return false;
     }
 
     }
 
-    if (win->GTKProcessEvent(event))
-      return TRUE;
+    event.m_wheelRotation = event.m_wheelDelta;
+    if (direction == GDK_SCROLL_DOWN || direction == GDK_SCROLL_LEFT)
+        event.m_wheelRotation = -event.m_wheelRotation;
 
 
-    GtkRange *range = win->m_scrollBar[wxWindow::ScrollDir_Vert];
-
-    if (range && gtk_widget_get_visible(GTK_WIDGET(range)))
+    if (!win->GTKProcessEvent(event))
     {
     {
-        GtkAdjustment* adj = gtk_range_get_adjustment(range);
-        double delta = gtk_adjustment_get_step_increment(adj) * 3;
-        if (gdk_event->direction == GDK_SCROLL_UP)
-            delta = -delta;
-
-        gtk_range_set_value(range, gtk_adjustment_get_value(adj) + delta);
-
-        return TRUE;
+        if (direction == GDK_SCROLL_UP || direction == GDK_SCROLL_LEFT)
+            step = -step;
+        AdjustRangeValue(range, step);
     }
 
     }
 
-    return FALSE;
+    return true;
 }
 
 //-----------------------------------------------------------------------------
 }
 
 //-----------------------------------------------------------------------------
@@ -1924,6 +1945,14 @@ size_allocate(GtkWidget*, GtkAllocation* alloc, wxWindow* win)
         if (w < 0) w = 0;
         if (h < 0) h = 0;
     }
         if (w < 0) w = 0;
         if (h < 0) h = 0;
     }
+    GtkAllocation a;
+    gtk_widget_get_allocation(win->m_widget, &a);
+    // update position for widgets in native containers, such as wxToolBar
+    if (!WX_IS_PIZZA(gtk_widget_get_parent(win->m_widget)))
+    {
+        win->m_x = a.x;
+        win->m_y = a.y;
+    }
     win->m_useCachedClientSize = true;
     if (win->m_clientWidth != w || win->m_clientHeight != h)
     {
     win->m_useCachedClientSize = true;
     if (win->m_clientWidth != w || win->m_clientHeight != h)
     {
@@ -1931,8 +1960,6 @@ size_allocate(GtkWidget*, GtkAllocation* alloc, wxWindow* win)
         win->m_clientHeight = h;
         // this callback can be connected to m_wxwindow,
         // so always get size from m_widget->allocation
         win->m_clientHeight = h;
         // this callback can be connected to m_wxwindow,
         // so always get size from m_widget->allocation
-        GtkAllocation a;
-        gtk_widget_get_allocation(win->m_widget, &a);
         win->m_width  = a.width;
         win->m_height = a.height;
         if (!win->m_nativeSizeEvent)
         win->m_width  = a.width;
         win->m_height = a.height;
         if (!win->m_nativeSizeEvent)
@@ -2083,12 +2110,14 @@ void wxWindowGTK::GTKHandleUnrealize()
 
 wxWindow *wxWindowBase::DoFindFocus()
 {
 
 wxWindow *wxWindowBase::DoFindFocus()
 {
+#if wxUSE_MENUS
     // For compatibility with wxMSW, pretend that showing a popup menu doesn't
     // change the focus and that it remains on the window showing it, even
     // though the real focus does change in GTK.
     extern wxMenu *wxCurrentPopupMenu;
     if ( wxCurrentPopupMenu )
         return wxCurrentPopupMenu->GetInvokingWindow();
     // For compatibility with wxMSW, pretend that showing a popup menu doesn't
     // change the focus and that it remains on the window showing it, even
     // though the real focus does change in GTK.
     extern wxMenu *wxCurrentPopupMenu;
     if ( wxCurrentPopupMenu )
         return wxCurrentPopupMenu->GetInvokingWindow();
+#endif // wxUSE_MENUS
 
     wxWindowGTK *focus = gs_pendingFocus ? gs_pendingFocus : gs_currentFocus;
     // the cast is necessary when we compile in wxUniversal mode
 
     wxWindowGTK *focus = gs_pendingFocus ? gs_pendingFocus : gs_currentFocus;
     // the cast is necessary when we compile in wxUniversal mode
@@ -2417,9 +2446,6 @@ wxWindowGTK::~wxWindowGTK()
     // destroy children before destroying this window itself
     DestroyChildren();
 
     // destroy children before destroying this window itself
     DestroyChildren();
 
-    if (m_widget)
-        Show( false );
-
     // delete before the widgets to avoid a crash on solaris
     if ( m_imContext )
     {
     // delete before the widgets to avoid a crash on solaris
     if ( m_imContext )
     {
@@ -2631,14 +2657,13 @@ void wxWindowGTK::ConnectWidget( GtkWidget *widget )
     g_signal_connect (widget, "motion_notify_event",
                       G_CALLBACK (gtk_window_motion_notify_callback), this);
 
     g_signal_connect (widget, "motion_notify_event",
                       G_CALLBACK (gtk_window_motion_notify_callback), this);
 
-    g_signal_connect (widget, "scroll_event",
-                      G_CALLBACK (window_scroll_event), this);
-    if (m_scrollBar[ScrollDir_Horz])
-        g_signal_connect (m_scrollBar[ScrollDir_Horz], "scroll_event",
-                      G_CALLBACK (window_scroll_event_hscrollbar), this);
-    if (m_scrollBar[ScrollDir_Vert])
-        g_signal_connect (m_scrollBar[ScrollDir_Vert], "scroll_event",
-                      G_CALLBACK (window_scroll_event), this);
+    g_signal_connect(widget, "scroll_event", G_CALLBACK(scroll_event), this);
+    GtkRange* range = m_scrollBar[ScrollDir_Horz];
+    if (range)
+        g_signal_connect(range, "scroll_event", G_CALLBACK(scroll_event), this);
+    range = m_scrollBar[ScrollDir_Vert];
+    if (range)
+        g_signal_connect(range, "scroll_event", G_CALLBACK(scroll_event), this);
 
     g_signal_connect (widget, "popup_menu",
                      G_CALLBACK (wxgtk_window_popup_menu_callback), this);
 
     g_signal_connect (widget, "popup_menu",
                      G_CALLBACK (wxgtk_window_popup_menu_callback), this);
@@ -3369,6 +3394,8 @@ void wxWindowGTK::SetFocus()
 
 void wxWindowGTK::SetCanFocus(bool canFocus)
 {
 
 void wxWindowGTK::SetCanFocus(bool canFocus)
 {
+    wxCHECK_RET(m_widget, "invalid window");
+
     gtk_widget_set_can_focus(m_widget, canFocus);
 
     if ( m_wxwindow && (m_widget != m_wxwindow) )
     gtk_widget_set_can_focus(m_widget, canFocus);
 
     if ( m_wxwindow && (m_widget != m_wxwindow) )
@@ -4360,6 +4387,13 @@ bool wxWindowGTK::DoPopupMenu( wxMenu *menu, int x, int y )
                   gtk_get_current_event_time()
                 );
 
                   gtk_get_current_event_time()
                 );
 
+    // it is possible for gtk_menu_popup() to fail
+    if (!gtk_widget_get_visible(GTK_WIDGET(menu->m_menu)))
+    {
+        menu->m_popupShown = false;
+        return false;
+    }
+
     while (menu->m_popupShown)
     {
         gtk_main_iteration();
     while (menu->m_popupShown)
     {
         gtk_main_iteration();
@@ -4589,7 +4623,9 @@ int wxWindowGTK::GetScrollRange( int orient ) const
 //   difference due to possible inexactness in floating point arithmetic
 static inline bool IsScrollIncrement(double increment, double x)
 {
 //   difference due to possible inexactness in floating point arithmetic
 static inline bool IsScrollIncrement(double increment, double x)
 {
-    wxASSERT(increment > 0);
+    wxASSERT(increment >= 0);
+    if ( increment == 0. )
+        return false;
     const double tolerance = 1.0 / 1024;
     return fabs(increment - fabs(x)) < tolerance;
 }
     const double tolerance = 1.0 / 1024;
     return fabs(increment - fabs(x)) < tolerance;
 }