X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/92ef38ee6b5174a22abc8cf1436c5002b4b9d887..0d1903dbda864780eec30efdc4e91776bdbfd21b:/src/gtk/window.cpp diff --git a/src/gtk/window.cpp b/src/gtk/window.cpp index 81d7995629..e163078b98 100644 --- a/src/gtk/window.cpp +++ b/src/gtk/window.cpp @@ -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 ///////////////////////////////////////////////////////////////////////////// @@ -755,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(keysym)); long key_code = wxTranslateKeySymToWXKey(keysym, false /* !isChar */); @@ -859,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 { @@ -977,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 ) @@ -985,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 ); } @@ -1008,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; @@ -1070,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 @@ -1107,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); } @@ -1601,30 +1614,18 @@ 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) +// Compute lines/columns per action the same way as private GTK+ function +// _gtk_range_get_wheel_delta() +static inline int GetWheelScrollActionDelta(GtkRange* range) { - 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))) + int delta = 3; + if (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; + const double page_size = gtk_adjustment_get_page_size(adj); + delta = wxRound(pow(page_size, 2.0 / 3.0)); } - - return FALSE; + return delta; } static gboolean @@ -1633,68 +1634,92 @@ window_scroll_event(GtkWidget*, 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; - // Determine the scroll direction. - switch (gdk_event->direction) - { - case GDK_SCROLL_UP: - case GDK_SCROLL_RIGHT: - event.m_wheelRotation = 120; - break; - - case GDK_SCROLL_DOWN: - case GDK_SCROLL_LEFT: - event.m_wheelRotation = -120; - break; #if GTK_CHECK_VERSION(3,4,0) - case GDK_SCROLL_SMOOTH: - // TODO -#endif - default: - return false; // Unknown/unhandled direction + if (gdk_event->direction == GDK_SCROLL_SMOOTH) + { + bool processed_x = false; + if (gdk_event->delta_x) + { + event.m_wheelAxis = wxMOUSE_WHEEL_HORIZONTAL; + event.m_wheelRotation = int(event.m_wheelDelta * gdk_event->delta_x); + GtkRange* range = win->m_scrollBar[wxWindow::ScrollDir_Horz]; + event.m_linesPerAction = GetWheelScrollActionDelta(range); + event.m_columnsPerAction = event.m_linesPerAction; + processed_x = win->GTKProcessEvent(event); + } + bool processed_y = false; + if (gdk_event->delta_y) + { + event.m_wheelAxis = wxMOUSE_WHEEL_VERTICAL; + event.m_wheelRotation = int(event.m_wheelDelta * -gdk_event->delta_y); + GtkRange* range = win->m_scrollBar[wxWindow::ScrollDir_Vert]; + event.m_linesPerAction = GetWheelScrollActionDelta(range); + event.m_columnsPerAction = event.m_linesPerAction; + processed_y = win->GTKProcessEvent(event); + } + return processed_x || processed_y; } - - // And the scroll axis. +#endif // GTK_CHECK_VERSION(3,4,0) + GtkRange *range; switch (gdk_event->direction) { case GDK_SCROLL_UP: case GDK_SCROLL_DOWN: + range = win->m_scrollBar[wxWindow::ScrollDir_Vert]; event.m_wheelAxis = wxMOUSE_WHEEL_VERTICAL; break; - case GDK_SCROLL_LEFT: case GDK_SCROLL_RIGHT: + range = win->m_scrollBar[wxWindow::ScrollDir_Horz]; event.m_wheelAxis = wxMOUSE_WHEEL_HORIZONTAL; break; -#if GTK_CHECK_VERSION(3,4,0) - case GDK_SCROLL_SMOOTH: - // TODO - break; -#endif + default: + return false; } - if (win->GTKProcessEvent(event)) - return TRUE; - - GtkRange *range = win->m_scrollBar[wxWindow::ScrollDir_Vert]; - - if (range && gtk_widget_get_visible(GTK_WIDGET(range))) + event.m_wheelRotation = event.m_wheelDelta; + if (gdk_event->direction == GDK_SCROLL_DOWN || + gdk_event->direction == GDK_SCROLL_LEFT) { - 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; + event.m_wheelRotation = -event.m_wheelRotation; + } + event.m_linesPerAction = GetWheelScrollActionDelta(range); + event.m_columnsPerAction = event.m_linesPerAction; - gtk_range_set_value(range, gtk_adjustment_get_value(adj) + delta); + return win->GTKProcessEvent(event); +} - return TRUE; +#if GTK_CHECK_VERSION(3,4,0) +static gboolean +hscrollbar_scroll_event(GtkWidget* widget, GdkEventScroll* gdk_event, wxWindow* win) +{ + GdkEventScroll event2; + if (gdk_event->direction == GDK_SCROLL_SMOOTH && gdk_event->delta_x == 0) + { + memcpy(&event2, gdk_event, sizeof(event2)); + event2.delta_x = event2.delta_y; + event2.delta_y = 0; + gdk_event = &event2; } + return window_scroll_event(widget, gdk_event, win); +} - return FALSE; +static gboolean +vscrollbar_scroll_event(GtkWidget* widget, GdkEventScroll* gdk_event, wxWindow* win) +{ + GdkEventScroll event2; + if (gdk_event->direction == GDK_SCROLL_SMOOTH && gdk_event->delta_y == 0) + { + memcpy(&event2, gdk_event, sizeof(event2)); + event2.delta_y = event2.delta_x; + event2.delta_x = 0; + gdk_event = &event2; + } + return window_scroll_event(widget, gdk_event, win); } +#endif // GTK_CHECK_VERSION(3,4,0) //----------------------------------------------------------------------------- // "popup-menu" @@ -1910,6 +1935,14 @@ size_allocate(GtkWidget*, GtkAllocation* alloc, wxWindow* win) 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) { @@ -1917,8 +1950,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 - GtkAllocation a; - gtk_widget_get_allocation(win->m_widget, &a); win->m_width = a.width; win->m_height = a.height; if (!win->m_nativeSizeEvent) @@ -1984,11 +2015,11 @@ void wxWindowGTK::GTKHandleRealized() GdkWindow* const window = GTKGetDrawingWindow(); - if (m_imData) + if (m_imContext) { gtk_im_context_set_client_window ( - m_imData->context, + m_imContext, window ? window : gtk_widget_get_window(m_widget) ); @@ -2020,8 +2051,6 @@ void wxWindowGTK::GTKHandleRealized() } #endif - GTKApplyWidgetStyle(); - wxWindowCreateEvent event(static_cast(this)); event.SetEventObject( this ); GTKProcessEvent( event ); @@ -2053,8 +2082,8 @@ 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); if (IsTopLevel()) { @@ -2071,12 +2100,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 @@ -2217,7 +2248,9 @@ void wxWindowGTK::Init() m_cursor = *wxSTANDARD_CURSOR; - m_imData = NULL; + m_imContext = NULL; + m_imKeyEvent = NULL; + m_dirtyTabOrder = false; } @@ -2403,12 +2436,12 @@ wxWindowGTK::~wxWindowGTK() // 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 @@ -2493,12 +2526,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); } @@ -2582,6 +2615,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); @@ -2613,12 +2649,21 @@ void wxWindowGTK::ConnectWidget( GtkWidget *widget ) 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); + for (int i = 0; i < 2; i++) + { + GtkRange* range = m_scrollBar[i]; + if (range) + { +#if GTK_CHECK_VERSION(3,4,0) + GCallback cb = GCallback(i == ScrollDir_Horz + ? hscrollbar_scroll_event + : vscrollbar_scroll_event); +#else + GCallback cb = GCallback(window_scroll_event); +#endif + g_signal_connect(range, "scroll_event", cb, this); + } + } g_signal_connect (widget, "popup_menu", G_CALLBACK (wxgtk_window_popup_menu_callback), this); @@ -3193,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; @@ -3256,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 ) { @@ -3349,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) ) @@ -4340,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(); @@ -4569,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; }