]> 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 25429853798311ab146dcfabca28fb3413a2b374..8f41635cf2f48279f60db0428405a27a78869a74 100644 (file)
@@ -2,7 +2,6 @@
 // Name:        src/gtk/window.cpp
 // Purpose:     wxWindowGTK implementation
 // Author:      Robert Roebling
-// Id:          $Id$
 // Copyright:   (c) 1998 Robert Roebling, Julian Smart
 // Licence:     wxWindows licence
 /////////////////////////////////////////////////////////////////////////////
@@ -43,6 +42,7 @@
 #include "wx/gtk/private/gtk2-compat.h"
 #include "wx/gtk/private/event.h"
 #include "wx/gtk/private/win_gtk.h"
+#include "wx/private/textmeasure.h"
 using namespace wxGTKImpl;
 
 #ifdef GDK_WINDOWING_X11
@@ -754,10 +754,10 @@ wxTranslateGTKKeyEventToWx(wxKeyEvent& event,
 
     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"),
-               keysym);
+               static_cast<unsigned long>(keysym));
 
     long key_code = wxTranslateKeySymToWXKey(keysym, false /* !isChar */);
 
@@ -841,7 +841,7 @@ wxTranslateGTKKeyEventToWx(wxKeyEvent& event,
     event.m_keyCode = key_code;
 
 #if wxUSE_UNICODE
-    event.m_uniChar = gdk_keyval_to_unicode(key_code ? key_code : keysym);
+    event.m_uniChar = gdk_keyval_to_unicode(key_code);
     if ( !event.m_uniChar && event.m_keyCode <= WXK_DELETE )
     {
         // Set Unicode key code to the ASCII equivalent for compatibility. E.g.
@@ -858,22 +858,6 @@ wxTranslateGTKKeyEventToWx(wxKeyEvent& event,
 }
 
 
-struct wxGtkIMData
-{
-    GtkIMContext *context;
-    GdkEventKey  *lastKeyEvent;
-
-    wxGtkIMData()
-    {
-        context = gtk_im_multicontext_new();
-        lastKeyEvent = NULL;
-    }
-    ~wxGtkIMData()
-    {
-        g_object_unref (context);
-    }
-};
-
 namespace
 {
 
@@ -976,7 +960,7 @@ gtk_window_key_press_callback( GtkWidget *WXUNUSED(widget),
             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 )
@@ -984,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):
-                    wxCommandEvent button_event( wxEVT_COMMAND_BUTTON_CLICKED, command );
+                    wxCommandEvent button_event( wxEVT_BUTTON, command );
                     ret = ancestor->HandleWindowEvent( button_event );
                 }
 
@@ -1007,17 +991,21 @@ gtk_window_key_press_callback( GtkWidget *WXUNUSED(widget),
         return_after_IM = true;
     }
 
-    if (!ret && win->m_imData)
+    if ( !ret )
     {
-        win->m_imData->lastKeyEvent = gdk_event;
+        // Indicate that IM handling is in process by setting this pointer
+        // (which will remain valid for all the code called during IM key
+        // handling).
+        win->m_imKeyEvent = gdk_event;
 
         // We should let GTK+ IM filter key event first. According to GTK+ 2.0 API
         // docs, if IM filter returns true, no further processing should be done.
         // we should send the key_down event anyway.
-        bool intercepted_by_IM =
-            gtk_im_context_filter_keypress(win->m_imData->context, gdk_event) != 0;
-        win->m_imData->lastKeyEvent = NULL;
-        if (intercepted_by_IM)
+        const int intercepted_by_IM = win->GTKIMFilterKeypress(gdk_event);
+
+        win->m_imKeyEvent = NULL;
+
+        if ( intercepted_by_IM )
         {
             wxLogTrace(TRACE_KEYS, wxT("Key event intercepted by IM"));
             return TRUE;
@@ -1069,30 +1057,43 @@ gtk_window_key_press_callback( GtkWidget *WXUNUSED(widget),
 }
 }
 
+int wxWindowGTK::GTKIMFilterKeypress(GdkEventKey* event) const
+{
+    return m_imContext ? gtk_im_context_filter_keypress(m_imContext, event)
+                       : FALSE;
+}
+
 extern "C" {
 static void
 gtk_wxwindow_commit_cb (GtkIMContext * WXUNUSED(context),
                         const gchar  *str,
                         wxWindow     *window)
+{
+    // Ignore the return value here, it doesn't matter for the "commit" signal.
+    window->GTKDoInsertTextFromIM(str);
+}
+}
+
+bool wxWindowGTK::GTKDoInsertTextFromIM(const char* str)
 {
     wxKeyEvent event( wxEVT_CHAR );
 
     // take modifiers, cursor position, timestamp etc. from the last
     // key_press_event that was fed into Input Method:
-    if (window->m_imData->lastKeyEvent)
+    if ( m_imKeyEvent )
     {
-        wxFillOtherKeyEventFields(event,
-                                  window, window->m_imData->lastKeyEvent);
+        wxFillOtherKeyEventFields(event, this, m_imKeyEvent);
     }
     else
     {
-        event.SetEventObject( window );
+        event.SetEventObject(this);
     }
 
     const wxString data(wxGTK_CONV_BACK_SYS(str));
     if( data.empty() )
-        return;
+        return false;
 
+    bool processed = false;
     for( wxString::const_iterator pstr = data.begin(); pstr != data.end(); ++pstr )
     {
 #if wxUSE_UNICODE
@@ -1106,9 +1107,22 @@ gtk_wxwindow_commit_cb (GtkIMContext * WXUNUSED(context),
 
         AdjustCharEventKeyCodes(event);
 
-        window->HandleWindowEvent(event);
+        if ( HandleWindowEvent(event) )
+            processed = true;
     }
+
+    return processed;
 }
+
+bool wxWindowGTK::GTKOnInsertText(const char* text)
+{
+    if ( !m_imKeyEvent )
+    {
+        // We're not inside IM key handling at all.
+        return false;
+    }
+
+    return GTKDoInsertTextFromIM(text);
 }
 
 
@@ -1531,8 +1545,11 @@ gtk_window_motion_notify_callback( GtkWidget * WXUNUSED(widget),
     {
         int x = 0;
         int y = 0;
-        GdkModifierType state;
-        gdk_window_get_pointer(gdk_event->window, &x, &y, &state);
+#ifdef __WXGTK3__
+        gdk_window_get_device_position(gdk_event->window, gdk_event->device, &x, &y, NULL);
+#else
+        gdk_window_get_pointer(gdk_event->window, &x, &y, NULL);
+#endif
         gdk_event->x = x;
         gdk_event->y = y;
     }
@@ -1545,7 +1562,12 @@ gtk_window_motion_notify_callback( GtkWidget * WXUNUSED(widget),
     if ( g_captureWindow )
     {
         // synthesise a mouse enter or leave event if needed
-        GdkWindow *winUnderMouse = gdk_window_at_pointer(NULL, NULL);
+        GdkWindow* winUnderMouse =
+#ifdef __WXGTK3__
+            gdk_device_get_window_at_position(gdk_event->device, NULL, NULL);
+#else
+            gdk_window_at_pointer(NULL, NULL);
+#endif
         // This seems to be necessary and actually been added to
         // GDK itself in version 2.0.X
         gdk_flush();
@@ -1592,91 +1614,121 @@ gtk_window_motion_notify_callback( GtkWidget * WXUNUSED(widget),
 // "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);
-        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
-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);
 
-    // FIXME: Get these values from GTK or GDK
-    event.m_linesPerAction = 3;
     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_RIGHT:
-            event.m_wheelRotation = 120;
+            if (is_range_h)
+                direction = GDK_SCROLL_LEFT;
             break;
-
         case GDK_SCROLL_DOWN:
+            if (is_range_h)
+                direction = GDK_SCROLL_RIGHT;
+            break;
         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:
-            return false;  // Unknown/unhandled direction
+            break;
+#if GTK_CHECK_VERSION(3,4,0)
+        case GDK_SCROLL_SMOOTH:
+            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:
+            range = range_v;
             event.m_wheelAxis = wxMOUSE_WHEEL_VERTICAL;
+            step = event.m_linesPerAction;
             break;
-
         case GDK_SCROLL_LEFT:
         case GDK_SCROLL_RIGHT:
+            range = range_h;
             event.m_wheelAxis = wxMOUSE_WHEEL_HORIZONTAL;
+            step = event.m_columnsPerAction;
             break;
+        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;
 }
 
 //-----------------------------------------------------------------------------
@@ -1741,7 +1793,7 @@ wx_window_focus_callback(GtkWidget *widget,
 //-----------------------------------------------------------------------------
 
 static gboolean
-gtk_window_enter_callback( GtkWidget *widget,
+gtk_window_enter_callback( GtkWidget*,
                            GdkEventCrossing *gdk_event,
                            wxWindowGTK *win )
 {
@@ -1750,17 +1802,8 @@ gtk_window_enter_callback( GtkWidget *widget,
     // Event was emitted after a grab
     if (gdk_event->mode != GDK_CROSSING_NORMAL) return FALSE;
 
-    int x = 0;
-    int y = 0;
-    GdkModifierType state = (GdkModifierType)0;
-
-    gdk_window_get_pointer(gtk_widget_get_window(widget), &x, &y, &state);
-
     wxMouseEvent event( wxEVT_ENTER_WINDOW );
     InitMouseEvent(win, event, gdk_event);
-    wxPoint pt = win->GetClientAreaOrigin();
-    event.m_x = x + pt.x;
-    event.m_y = y + pt.y;
 
     if ( !g_captureWindow )
     {
@@ -1779,7 +1822,7 @@ gtk_window_enter_callback( GtkWidget *widget,
 //-----------------------------------------------------------------------------
 
 static gboolean
-gtk_window_leave_callback( GtkWidget *widget,
+gtk_window_leave_callback( GtkWidget*,
                            GdkEventCrossing *gdk_event,
                            wxWindowGTK *win )
 {
@@ -1789,13 +1832,6 @@ gtk_window_leave_callback( GtkWidget *widget,
     if (gdk_event->mode != GDK_CROSSING_NORMAL) return FALSE;
 
     wxMouseEvent event( wxEVT_LEAVE_WINDOW );
-
-    int x = 0;
-    int y = 0;
-    GdkModifierType state = (GdkModifierType)0;
-
-    gdk_window_get_pointer(gtk_widget_get_window(widget), &x, &y, &state);
-
     InitMouseEvent(win, event, gdk_event);
 
     return win->GTKProcessEvent(event);
@@ -1909,14 +1945,21 @@ size_allocate(GtkWidget*, GtkAllocation* alloc, wxWindow* win)
         if (w < 0) w = 0;
         if (h < 0) h = 0;
     }
-    if (win->m_oldClientWidth != w || win->m_oldClientHeight != h)
+    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_oldClientWidth  = w;
-        win->m_oldClientHeight = h;
+        win->m_clientWidth  = w;
+        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)
@@ -1959,18 +2002,9 @@ static void style_updated(GtkWidget*, wxWindow* win)
 static void style_updated(GtkWidget*, GtkStyle*, wxWindow* win)
 #endif
 {
-    if (win->IsTopLevel())
-    {
-        wxSysColourChangedEvent event;
-        event.SetEventObject(win);
-        win->GTKProcessEvent(event);
-    }
-    else
-    {
-        // Border width could change, which will change client size.
-        // Make sure size event occurs for this
-        win->m_oldClientWidth = 0;
-    }
+    wxSysColourChangedEvent event;
+    event.SetEventObject(win);
+    win->GTKProcessEvent(event);
 }
 
 //-----------------------------------------------------------------------------
@@ -1989,12 +2023,14 @@ void wxWindowGTK::GTKHandleRealized()
     if (IsFrozen())
         DoFreeze();
 
-    if (m_imData)
+    GdkWindow* const window = GTKGetDrawingWindow();
+
+    if (m_imContext)
     {
         gtk_im_context_set_client_window
         (
-            m_imData->context,
-            m_wxwindow ? GTKGetDrawingWindow()
+            m_imContext,
+            window ? window
                        : gtk_widget_get_window(m_widget)
         );
     }
@@ -2005,7 +2041,6 @@ void wxWindowGTK::GTKHandleRealized()
 #if wxGTK_HAS_COMPOSITING_SUPPORT
         if (IsTransparentBackgroundSupported())
         {
-            GdkWindow* const window = GTKGetDrawingWindow();
             if (window)
                 gdk_window_set_composited(window, true);
         }
@@ -2017,16 +2052,14 @@ void wxWindowGTK::GTKHandleRealized()
         }
     }
 
-
-    // We cannot set colours and fonts before the widget
-    // been realized, so we do this directly after realization
-    // or otherwise in idle time
-
-    if (m_needsStyleChange)
+#ifndef __WXGTK3__
+    if (window && (
+        m_backgroundStyle == wxBG_STYLE_PAINT ||
+        m_backgroundStyle == wxBG_STYLE_TRANSPARENT))
     {
-        SetBackgroundStyle(GetBackgroundStyle());
-        m_needsStyleChange = false;
+        gdk_window_set_back_pixmap(window, NULL, false);
     }
+#endif
 
     wxWindowCreateEvent event(static_cast<wxWindow*>(this));
     event.SetEventObject( this );
@@ -2034,8 +2067,7 @@ void wxWindowGTK::GTKHandleRealized()
 
     GTKUpdateCursor(true, false);
 
-    if (m_wxwindow &&
-        (IsTopLevel() || HasFlag(wxBORDER_RAISED | wxBORDER_SUNKEN | wxBORDER_THEME)))
+    if (m_wxwindow && IsTopLevel())
     {
         // attaching to style changed signal after realization avoids initial
         // changes we don't care about
@@ -2060,11 +2092,14 @@ void wxWindowGTK::GTKHandleUnrealize()
 
     if (m_wxwindow)
     {
-        if (m_imData)
-            gtk_im_context_set_client_window(m_imData->context, NULL);
+        if (m_imContext)
+            gtk_im_context_set_client_window(m_imContext, NULL);
 
-        g_signal_handlers_disconnect_by_func(
-            m_wxwindow, (void*)style_updated, this);
+        if (IsTopLevel())
+        {
+            g_signal_handlers_disconnect_by_func(
+                m_wxwindow, (void*)style_updated, this);
+        }
     }
 }
 
@@ -2075,12 +2110,14 @@ void wxWindowGTK::GTKHandleUnrealize()
 
 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();
+#endif // wxUSE_MENUS
 
     wxWindowGTK *focus = gs_pendingFocus ? gs_pendingFocus : gs_currentFocus;
     // the cast is necessary when we compile in wxUniversal mode
@@ -2121,7 +2158,7 @@ bool wxGetKeyState(wxKeyCode WXUNUSED(key))
 }
 #endif // __WINDOWS__
 
-static void GetMouseState(int& x, int& y, GdkModifierType& mask)
+static GdkDisplay* GetDisplay()
 {
     wxWindow* tlw = NULL;
     if (!wxTopLevelWindows.empty())
@@ -2131,7 +2168,7 @@ static void GetMouseState(int& x, int& y, GdkModifierType& mask)
         display = gtk_widget_get_display(tlw->m_widget);
     else
         display = gdk_display_get_default();
-    gdk_display_get_pointer(display, NULL, &x, &y, &mask);
+    return display;
 }
 
 wxMouseState wxGetMouseState()
@@ -2142,7 +2179,17 @@ wxMouseState wxGetMouseState()
     gint y;
     GdkModifierType mask;
 
-    GetMouseState(x, y, mask);
+    GdkDisplay* display = GetDisplay();
+#ifdef __WXGTK3__
+    GdkDeviceManager* manager = gdk_display_get_device_manager(display);
+    GdkDevice* device = gdk_device_manager_get_client_pointer(manager);
+    GdkScreen* screen;
+    gdk_device_get_position(device, &screen, &x, &y);
+    GdkWindow* window = gdk_screen_get_root_window(screen);
+    gdk_device_get_state(device, window, NULL, &mask);
+#else
+    gdk_display_get_pointer(display, NULL, &x, &y, &mask);
+#endif
 
     ms.SetX(x);
     ms.SetY(y);
@@ -2190,6 +2237,7 @@ void wxWindowGTK::Init()
     m_nativeSizeEvent = false;
 #ifdef __WXGTK3__
     m_paintContext = NULL;
+    m_styleProvider = NULL;
 #endif
 
     m_isScrolling = false;
@@ -2202,16 +2250,17 @@ void wxWindowGTK::Init()
         m_scrollPos[dir] = 0;
     }
 
-    m_oldClientWidth =
-    m_oldClientHeight = 0;
+    m_clientWidth =
+    m_clientHeight = 0;
+    m_useCachedClientSize = false;
 
     m_clipPaintRegion = false;
 
-    m_needsStyleChange = false;
-
     m_cursor = *wxSTANDARD_CURSOR;
 
-    m_imData = NULL;
+    m_imContext = NULL;
+    m_imKeyEvent = NULL;
+
     m_dirtyTabOrder = false;
 }
 
@@ -2258,14 +2307,20 @@ void wxWindowGTK::GTKCreateScrolledWindowWith(GtkWidget* view)
         }
     }
 
-    if (HasFlag(wxALWAYS_SHOW_SB))
-    {
-        gtk_scrolled_window_set_policy( scrolledWindow, GTK_POLICY_ALWAYS, GTK_POLICY_ALWAYS );
-    }
-    else
-    {
-        gtk_scrolled_window_set_policy( scrolledWindow, GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
-    }
+    // If wx[HV]SCROLL is not given, the corresponding scrollbar is not shown
+    // at all. Otherwise it may be shown only on demand (default) or always, if
+    // the wxALWAYS_SHOW_SB is specified.
+    GtkPolicyType horzPolicy = HasFlag(wxHSCROLL)
+                                ? HasFlag(wxALWAYS_SHOW_SB)
+                                    ? GTK_POLICY_ALWAYS
+                                    : GTK_POLICY_AUTOMATIC
+                                : GTK_POLICY_NEVER;
+    GtkPolicyType vertPolicy = HasFlag(wxVSCROLL)
+                                ? HasFlag(wxALWAYS_SHOW_SB)
+                                    ? GTK_POLICY_ALWAYS
+                                    : GTK_POLICY_AUTOMATIC
+                                : GTK_POLICY_NEVER;
+    gtk_scrolled_window_set_policy( scrolledWindow, horzPolicy, vertPolicy );
 
     m_scrollBar[ScrollDir_Horz] = GTK_RANGE(gtk_scrolled_window_get_hscrollbar(scrolledWindow));
     m_scrollBar[ScrollDir_Vert] = GTK_RANGE(gtk_scrolled_window_get_vscrollbar(scrolledWindow));
@@ -2368,20 +2423,35 @@ wxWindowGTK::~wxWindowGTK()
     if ( gs_deferredFocusOut == this )
         gs_deferredFocusOut = NULL;
 
-    if (m_widget)
-        GTKDisconnect(m_widget);
-    if (m_wxwindow && m_wxwindow != m_widget)
+    // Unlike the above cases, which can happen in normal circumstances, a
+    // window shouldn't be destroyed while it still has capture, so even though
+    // we still reset the global pointer to avoid leaving it dangling and
+    // crashing afterwards, also complain about it.
+    if ( g_captureWindow == this )
+    {
+        wxFAIL_MSG( wxS("Destroying window with mouse capture") );
+        g_captureWindow = NULL;
+    }
+
+    if (m_wxwindow)
+    {
         GTKDisconnect(m_wxwindow);
+        GtkWidget* parent = gtk_widget_get_parent(m_wxwindow);
+        if (parent)
+            GTKDisconnect(parent);
+    }
+    if (m_widget && m_widget != m_wxwindow)
+        GTKDisconnect(m_widget);
 
     // destroy children before destroying this window itself
     DestroyChildren();
 
-    if (m_widget)
-        Show( false );
-
     // delete before the widgets to avoid a crash on solaris
-    delete m_imData;
-    m_imData = NULL;
+    if ( m_imContext )
+    {
+        g_object_unref(m_imContext);
+        m_imContext = NULL;
+    }
 
     // avoid problem with GTK+ 2.18 where a frozen window causes the whole
     // TLW to be frozen, and if the window is then destroyed, nothing ever
@@ -2389,6 +2459,11 @@ wxWindowGTK::~wxWindowGTK()
     while (IsFrozen())
         Thaw();
 
+#ifdef __WXGTK3__
+    if (m_styleProvider)
+        g_object_unref(m_styleProvider);
+#endif
+
     if (m_widget)
     {
         // Note that gtk_widget_destroy() does not destroy the widget, it just
@@ -2461,12 +2536,12 @@ void wxWindowGTK::PostCreation()
         }
 
         // Create input method handler
-        m_imData = new wxGtkIMData;
+        m_imContext = gtk_im_multicontext_new();
 
         // Cannot handle drawing preedited text yet
-        gtk_im_context_set_use_preedit( m_imData->context, FALSE );
+        gtk_im_context_set_use_preedit( m_imContext, FALSE );
 
-        g_signal_connect (m_imData->context, "commit",
+        g_signal_connect (m_imContext, "commit",
                           G_CALLBACK (gtk_wxwindow_commit_cb), this);
     }
 
@@ -2550,6 +2625,9 @@ void wxWindowGTK::PostCreation()
     if (!WX_IS_PIZZA(gtk_widget_get_parent(m_widget)) && !GTK_IS_WINDOW(m_widget))
         gtk_widget_set_size_request(m_widget, m_width, m_height);
 
+    // apply any font or color changes made before creation
+    GTKApplyWidgetStyle();
+
     InheritAttributes();
 
     SetLayoutDirection(wxLayout_Default);
@@ -2579,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, "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);
@@ -2693,6 +2770,10 @@ void wxWindowGTK::DoSetSize( int x, int y, int width, int height, int sizeFlags
         height = m_height;
 
     const bool sizeChange = m_width != width || m_height != height;
+
+    if (sizeChange)
+        m_useCachedClientSize = false;
+
     if (sizeChange || m_x != x || m_y != y)
     {
         m_x = x;
@@ -2722,7 +2803,7 @@ void wxWindowGTK::DoSetSize( int x, int y, int width, int height, int sizeFlags
     {
         // update these variables to keep size_allocate handler
         // from sending another size event for this change
-        GetClientSize( &m_oldClientWidth, &m_oldClientHeight );
+        DoGetClientSize(&m_clientWidth, &m_clientHeight);
 
         wxSizeEvent event( wxSize(m_width,m_height), GetId() );
         event.SetEventObject( this );
@@ -2787,6 +2868,13 @@ void wxWindowGTK::DoGetClientSize( int *width, int *height ) const
 {
     wxCHECK_RET( (m_widget != NULL), wxT("invalid window") );
 
+    if (m_useCachedClientSize)
+    {
+        if (width)  *width  = m_clientWidth;
+        if (height) *height = m_clientHeight;
+        return;
+    }
+
     int w = m_width;
     int h = m_height;
 
@@ -2799,8 +2887,16 @@ void wxWindowGTK::DoGetClientSize( int *width, int *height ) const
             gtk_scrolled_window_get_policy(GTK_SCROLLED_WINDOW(m_widget),
                                            &policy[ScrollDir_Horz],
                                            &policy[ScrollDir_Vert]);
-            const int scrollbar_spacing =
+
+            // get scrollbar spacing the same way the GTK-private function
+            // _gtk_scrolled_window_get_scrollbar_spacing() does it
+            int scrollbar_spacing =
                 GTK_SCROLLED_WINDOW_GET_CLASS(m_widget)->scrollbar_spacing;
+            if (scrollbar_spacing < 0)
+            {
+                gtk_widget_style_get(
+                    m_widget, "scrollbar-spacing", &scrollbar_spacing, NULL);
+            }
 
             for ( int i = 0; i < ScrollDir_Max; i++ )
             {
@@ -3080,52 +3176,18 @@ void wxWindowGTK::DoGetTextExtent( const wxString& string,
                                    int *externalLeading,
                                    const wxFont *theFont ) const
 {
-    wxFont fontToUse = theFont ? *theFont : GetFont();
+    // ensure we work with a valid font
+    wxFont fontToUse;
+    if ( !theFont || !theFont->IsOk() )
+        fontToUse = GetFont();
+    else
+        fontToUse = *theFont;
 
     wxCHECK_RET( fontToUse.IsOk(), wxT("invalid font") );
 
-    if (string.empty())
-    {
-        if (x) (*x) = 0;
-        if (y) (*y) = 0;
-        return;
-    }
-
-    PangoContext *context = NULL;
-    if (m_widget)
-        context = gtk_widget_get_pango_context( m_widget );
-
-    if (!context)
-    {
-        if (x) (*x) = 0;
-        if (y) (*y) = 0;
-        return;
-    }
-
-    PangoFontDescription *desc = fontToUse.GetNativeFontInfo()->description;
-    PangoLayout *layout = pango_layout_new(context);
-    pango_layout_set_font_description(layout, desc);
-    {
-        const wxCharBuffer data = wxGTK_CONV( string );
-        if ( data )
-            pango_layout_set_text(layout, data, strlen(data));
-    }
-
-    PangoRectangle rect;
-    pango_layout_get_extents(layout, NULL, &rect);
-
-    if (x) (*x) = (wxCoord) PANGO_PIXELS(rect.width);
-    if (y) (*y) = (wxCoord) PANGO_PIXELS(rect.height);
-    if (descent)
-    {
-        PangoLayoutIter *iter = pango_layout_get_iter(layout);
-        int baseline = pango_layout_iter_get_baseline(iter);
-        pango_layout_iter_free(iter);
-        *descent = *y - PANGO_PIXELS(baseline);
-    }
-    if (externalLeading) (*externalLeading) = 0;  // ??
-
-    g_object_unref (layout);
+    const wxWindow* win = static_cast<const wxWindow*>(this);
+    wxTextMeasure txm(win, &fontToUse);
+    txm.GetTextExtent(string, x, y, descent, externalLeading);
 }
 
 void wxWindowGTK::GTKDisableFocusOutEvent()
@@ -3176,8 +3238,8 @@ bool wxWindowGTK::GTKHandleFocusIn()
                "handling focus_in event for %s(%p, %s)",
                GetClassInfo()->GetClassName(), this, GetLabel());
 
-    if (m_imData)
-        gtk_im_context_focus_in(m_imData->context);
+    if (m_imContext)
+        gtk_im_context_focus_in(m_imContext);
 
     gs_currentFocus = this;
     gs_pendingFocus = NULL;
@@ -3239,8 +3301,8 @@ void wxWindowGTK::GTKHandleFocusOutNoDeferring()
                "handling focus_out event for %s(%p, %s)",
                GetClassInfo()->GetClassName(), this, GetLabel());
 
-    if (m_imData)
-        gtk_im_context_focus_out(m_imData->context);
+    if (m_imContext)
+        gtk_im_context_focus_out(m_imContext);
 
     if ( gs_currentFocus != this )
     {
@@ -3332,6 +3394,8 @@ void wxWindowGTK::SetFocus()
 
 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) )
@@ -3994,46 +4058,46 @@ void wxWindowGTK::GTKApplyToolTip(const char* tip)
 
 bool wxWindowGTK::SetBackgroundColour( const wxColour &colour )
 {
-    wxCHECK_MSG( m_widget != NULL, false, wxT("invalid window") );
-
     if (!wxWindowBase::SetBackgroundColour(colour))
         return false;
 
-#ifndef __WXGTK3__
-    if (colour.IsOk())
+    if (m_widget)
     {
-        // We need the pixel value e.g. for background clearing.
-        m_backgroundColour.CalcPixel(gtk_widget_get_colormap(m_widget));
-    }
+#ifndef __WXGTK3__
+        if (colour.IsOk())
+        {
+            // We need the pixel value e.g. for background clearing.
+            m_backgroundColour.CalcPixel(gtk_widget_get_colormap(m_widget));
+        }
 #endif
 
-    // apply style change (forceStyle=true so that new style is applied
-    // even if the bg colour changed from valid to wxNullColour)
-    GTKApplyWidgetStyle(true);
+        // apply style change (forceStyle=true so that new style is applied
+        // even if the bg colour changed from valid to wxNullColour)
+        GTKApplyWidgetStyle(true);
+    }
 
     return true;
 }
 
 bool wxWindowGTK::SetForegroundColour( const wxColour &colour )
 {
-    wxCHECK_MSG( m_widget != NULL, false, wxT("invalid window") );
-
     if (!wxWindowBase::SetForegroundColour(colour))
-    {
         return false;
-    }
 
-#ifndef __WXGTK3__
-    if (colour.IsOk())
+    if (m_widget)
     {
-        // We need the pixel value e.g. for background clearing.
-        m_foregroundColour.CalcPixel(gtk_widget_get_colormap(m_widget));
-    }
+#ifndef __WXGTK3__
+        if (colour.IsOk())
+        {
+            // We need the pixel value e.g. for background clearing.
+            m_foregroundColour.CalcPixel(gtk_widget_get_colormap(m_widget));
+        }
 #endif
 
-    // apply style change (forceStyle=true so that new style is applied
-    // even if the bg colour changed from valid to wxNullColour):
-    GTKApplyWidgetStyle(true);
+        // apply style change (forceStyle=true so that new style is applied
+        // even if the bg colour changed from valid to wxNullColour):
+        GTKApplyWidgetStyle(true);
+    }
 
     return true;
 }
@@ -4044,16 +4108,8 @@ PangoContext *wxWindowGTK::GTKGetPangoDefaultContext()
 }
 
 #ifndef __WXGTK3__
-GtkRcStyle *wxWindowGTK::GTKCreateWidgetStyle(bool forceStyle)
+GtkRcStyle* wxWindowGTK::GTKCreateWidgetStyle()
 {
-    // do we need to apply any changes at all?
-    if ( !forceStyle &&
-         !m_font.IsOk() &&
-         !m_foregroundColour.IsOk() && !m_backgroundColour.IsOk() )
-    {
-        return NULL;
-    }
-
     GtkRcStyle *style = gtk_rc_style_new();
 
     if ( m_font.IsOk() )
@@ -4114,18 +4170,35 @@ GtkRcStyle *wxWindowGTK::GTKCreateWidgetStyle(bool forceStyle)
 }
 #endif // !__WXGTK3__
 
-void wxWindowGTK::GTKApplyWidgetStyle(bool WXUNUSED_IN_GTK3(forceStyle))
+void wxWindowGTK::GTKApplyWidgetStyle(bool forceStyle)
 {
+    if (forceStyle || m_font.IsOk() ||
+        m_foregroundColour.IsOk() || m_backgroundColour.IsOk())
+    {
 #ifdef __WXGTK3__
-    DoApplyWidgetStyle(NULL);
+        if (m_backgroundColour.IsOk())
+        {
+            // create a GtkStyleProvider to override "background-image"
+            if (m_styleProvider == NULL)
+                m_styleProvider = GTK_STYLE_PROVIDER(gtk_css_provider_new());
+            const char css[] =
+                "*{background-image:-gtk-gradient(linear,0 0,0 1,"
+                "from(rgba(%u,%u,%u,%g)),to(rgba(%u,%u,%u,%g)))}";
+            char buf[sizeof(css) + 20];
+            const unsigned r = m_backgroundColour.Red();
+            const unsigned g = m_backgroundColour.Green();
+            const unsigned b = m_backgroundColour.Blue();
+            const double a = m_backgroundColour.Alpha() / 255.0;
+            g_snprintf(buf, sizeof(buf), css, r, g, b, a, r, g, b, a);
+            gtk_css_provider_load_from_data(GTK_CSS_PROVIDER(m_styleProvider), buf, -1, NULL);
+        }
+        DoApplyWidgetStyle(NULL);
 #else
-    GtkRcStyle *style = GTKCreateWidgetStyle(forceStyle);
-    if ( style )
-    {
+        GtkRcStyle* style = GTKCreateWidgetStyle();
         DoApplyWidgetStyle(style);
         g_object_unref(style);
-    }
 #endif
+    }
 
     // Style change may affect GTK+'s size calculation:
     InvalidateBestSize();
@@ -4163,6 +4236,25 @@ void wxWindowGTK::GTKApplyStyle(GtkWidget* widget, GtkRcStyle* WXUNUSED_IN_GTK3(
     gtk_widget_override_font(widget, pfd);
     gtk_widget_override_color(widget, GTK_STATE_FLAG_NORMAL, m_foregroundColour);
     gtk_widget_override_background_color(widget, GTK_STATE_FLAG_NORMAL, m_backgroundColour);
+
+    // setting background color has no effect with some themes when the widget style
+    // has a "background-image" property, so we need to override that as well
+
+    GtkStyleContext* context = gtk_widget_get_style_context(widget);
+    if (m_styleProvider)
+        gtk_style_context_remove_provider(context, m_styleProvider);
+    cairo_pattern_t* pattern = NULL;
+    if (m_backgroundColour.IsOk())
+    {
+        gtk_style_context_get(context,
+            GTK_STATE_FLAG_NORMAL, "background-image", &pattern, NULL);
+    }
+    if (pattern)
+    {
+        cairo_pattern_destroy(pattern);
+        gtk_style_context_add_provider(context,
+            m_styleProvider, GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
+    }
 #else
     gtk_widget_modify_style(widget, style);
 #endif
@@ -4175,40 +4267,10 @@ bool wxWindowGTK::SetBackgroundStyle(wxBackgroundStyle style)
 
 #ifndef __WXGTK3__
     GdkWindow *window;
-    if ( m_wxwindow )
-    {
-        window = GTKGetDrawingWindow();
-    }
-    else
-    {
-        GtkWidget * const w = GetConnectWidget();
-        window = w ? gtk_widget_get_window(w) : NULL;
-    }
-
-    bool wantNoBackPixmap = style == wxBG_STYLE_PAINT || style == wxBG_STYLE_TRANSPARENT;
-
-    if ( wantNoBackPixmap )
-    {
-        if (window)
-        {
-            // Make sure GDK/X11 doesn't refresh the window
-            // automatically.
-            gdk_window_set_back_pixmap( window, NULL, FALSE );
-            m_needsStyleChange = false;
-        }
-        else // window not realized yet
-        {
-            // Do when window is realized
-            m_needsStyleChange = true;
-        }
-
-        // Don't apply widget style, or we get a grey background
-    }
-    else
+    if ((style == wxBG_STYLE_PAINT || style == wxBG_STYLE_TRANSPARENT) &&
+        (window = GTKGetDrawingWindow()))
     {
-        // apply style change (forceStyle=true so that new style is applied
-        // even if the bg colour changed from valid to wxNullColour):
-        GTKApplyWidgetStyle(true);
+        gdk_window_set_back_pixmap(window, NULL, false);
     }
 #endif // !__WXGTK3__
 
@@ -4275,7 +4337,11 @@ void wxPopupMenuPositionCallback( GtkMenu *menu,
 {
     // ensure that the menu appears entirely on screen
     GtkRequisition req;
+#ifdef __WXGTK3__
+    gtk_widget_get_preferred_size(GTK_WIDGET(menu), &req, NULL);
+#else
     gtk_widget_get_child_requisition(GTK_WIDGET(menu), &req);
+#endif
 
     wxSize sizeScreen = wxGetDisplaySize();
     wxPoint *pos = (wxPoint*)user_data;
@@ -4321,6 +4387,13 @@ bool wxWindowGTK::DoPopupMenu( wxMenu *menu, int x, int y )
                   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();
@@ -4373,14 +4446,15 @@ GdkWindow *wxWindowGTK::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const
 
 bool wxWindowGTK::SetFont( const wxFont &font )
 {
-    wxCHECK_MSG( m_widget != NULL, false, wxT("invalid window") );
-
     if (!wxWindowBase::SetFont(font))
         return false;
 
-    // apply style change (forceStyle=true so that new style is applied
-    // even if the font changed from valid to wxNullFont):
-    GTKApplyWidgetStyle(true);
+    if (m_widget)
+    {
+        // apply style change (forceStyle=true so that new style is applied
+        // even if the font changed from valid to wxNullFont):
+        GTKApplyWidgetStyle(true);
+    }
 
     return true;
 }
@@ -4401,15 +4475,25 @@ void wxWindowGTK::DoCaptureMouse()
     if (!cursor->IsOk())
         cursor = wxSTANDARD_CURSOR;
 
+    const GdkEventMask mask = GdkEventMask(
+        GDK_BUTTON_PRESS_MASK |
+        GDK_BUTTON_RELEASE_MASK |
+        GDK_POINTER_MOTION_HINT_MASK |
+        GDK_POINTER_MOTION_MASK);
+#ifdef __WXGTK3__
+    GdkDisplay* display = gdk_window_get_display(window);
+    GdkDeviceManager* manager = gdk_display_get_device_manager(display);
+    GdkDevice* device = gdk_device_manager_get_client_pointer(manager);
+    gdk_device_grab(
+        device, window, GDK_OWNERSHIP_NONE, false, mask,
+        cursor->GetCursor(), unsigned(GDK_CURRENT_TIME));
+#else
     gdk_pointer_grab( window, FALSE,
-                      (GdkEventMask)
-                         (GDK_BUTTON_PRESS_MASK |
-                          GDK_BUTTON_RELEASE_MASK |
-                          GDK_POINTER_MOTION_HINT_MASK |
-                          GDK_POINTER_MOTION_MASK),
+                      mask,
                       NULL,
                       cursor->GetCursor(),
                       (guint32)GDK_CURRENT_TIME );
+#endif
     g_captureWindow = this;
     g_captureWindowHasMouse = true;
 }
@@ -4431,7 +4515,14 @@ void wxWindowGTK::DoReleaseMouse()
     if (!window)
         return;
 
+#ifdef __WXGTK3__
+    GdkDisplay* display = gdk_window_get_display(window);
+    GdkDeviceManager* manager = gdk_display_get_device_manager(display);
+    GdkDevice* device = gdk_device_manager_get_client_pointer(manager);
+    gdk_device_ungrab(device, unsigned(GDK_CURRENT_TIME));
+#else
     gdk_pointer_ungrab ( (guint32)GDK_CURRENT_TIME );
+#endif
 }
 
 void wxWindowGTK::GTKReleaseMouseAndNotify()
@@ -4532,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)
 {
-    wxASSERT(increment > 0);
+    wxASSERT(increment >= 0);
+    if ( increment == 0. )
+        return false;
     const double tolerance = 1.0 / 1024;
     return fabs(increment - fabs(x)) < tolerance;
 }
@@ -4654,12 +4747,23 @@ wxWindow* wxFindWindowAtPointer(wxPoint& pt)
 }
 
 // Get the current mouse position.
+void wxGetMousePosition(int* x, int* y)
+{
+    GdkDisplay* display = GetDisplay();
+#ifdef __WXGTK3__
+    GdkDeviceManager* manager = gdk_display_get_device_manager(display);
+    GdkDevice* device = gdk_device_manager_get_client_pointer(manager);
+    gdk_device_get_position(device, NULL, x, y);
+#else
+    gdk_display_get_pointer(display, NULL, x, y, NULL);
+#endif
+}
+
 wxPoint wxGetMousePosition()
 {
-    int x, y;
-    GdkModifierType unused;
-    GetMouseState(x, y, unused);
-    return wxPoint(x, y);
+    wxPoint pt;
+    wxGetMousePosition(&pt.x, &pt.y);
+    return pt;
 }
 
 GdkWindow* wxWindowGTK::GTKGetDrawingWindow() const