From c9433ea97645cab5f36ad5ae62619efc283de186 Mon Sep 17 00:00:00 2001 From: Robert Roebling Date: Sun, 17 Oct 2004 09:58:46 +0000 Subject: [PATCH] Fixed lost focus events in wxListBox. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@29935 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- src/gtk/listbox.cpp | 72 ++++++++++++++++++++++++++++++++++++++++++++ src/gtk1/listbox.cpp | 72 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 144 insertions(+) diff --git a/src/gtk/listbox.cpp b/src/gtk/listbox.cpp index 37154d6eda..d8810ddae0 100644 --- a/src/gtk/listbox.cpp +++ b/src/gtk/listbox.cpp @@ -51,6 +51,8 @@ extern bool g_blockEventsOnDrag; extern bool g_blockEventsOnScroll; extern wxCursor g_globalCursor; extern wxWindowGTK *g_delayedFocus; +extern wxWindowGTK *g_focusWindow; +extern wxWindowGTK *g_focusWindowLast; static bool g_hasDoubleClicked = FALSE; @@ -87,6 +89,68 @@ extern "C" gint wxlistbox_idle_callback( gpointer gdata ) return TRUE; } +//----------------------------------------------------------------------------- +// "focus_in_event" +//----------------------------------------------------------------------------- + +static gint gtk_listitem_focus_in_callback( GtkWidget *widget, + GdkEvent *WXUNUSED(event), + wxWindow *win ) +{ + if (g_isIdle) + wxapp_install_idle_handler(); + + g_focusWindowLast = + g_focusWindow = win; + + // does the window itself think that it has the focus? + if ( !win->m_hasFocus ) + { + // not yet, notify it + win->m_hasFocus = TRUE; + + wxChildFocusEvent eventChildFocus(win); + (void)win->GetEventHandler()->ProcessEvent(eventChildFocus); + + wxFocusEvent eventFocus(wxEVT_SET_FOCUS, win->GetId()); + eventFocus.SetEventObject(win); + + (void)win->GetEventHandler()->ProcessEvent(eventFocus); + } + + return FALSE; +} + +//----------------------------------------------------------------------------- +// "focus_out_event" +//----------------------------------------------------------------------------- + +static gint gtk_listitem_focus_out_callback( GtkWidget *widget, GdkEventFocus *gdk_event, wxWindowGTK *win ) +{ + if (g_isIdle) + wxapp_install_idle_handler(); + + g_focusWindow = (wxWindowGTK *)NULL; + + // don't send the window a kill focus event if it thinks that it doesn't + // have focus already + if ( win->m_hasFocus ) + { + win->m_hasFocus = FALSE; + + wxFocusEvent event( wxEVT_KILL_FOCUS, win->GetId() ); + event.SetEventObject( win ); + + // even if we did process the event in wx code, still let GTK itself + // process it too as otherwise bad things happen, especially in GTK2 + // where the text control simply aborts the program if it doesn't get + // the matching focus out event + (void)win->GetEventHandler()->ProcessEvent( event ); + } + + return FALSE; +} + //----------------------------------------------------------------------------- // "button_release_event" //----------------------------------------------------------------------------- @@ -607,6 +671,14 @@ void wxListBox::GtkAddItem( const wxString &item, int pos ) (GtkSignalFunc)gtk_listbox_key_press_callback, (gpointer)this ); + + gtk_signal_connect( GTK_OBJECT(list_item), "focus_in_event", + GTK_SIGNAL_FUNC(gtk_listitem_focus_in_callback), (gpointer)this ); + + gtk_signal_connect( GTK_OBJECT(list_item), "focus_out_event", + GTK_SIGNAL_FUNC(gtk_listitem_focus_out_callback), (gpointer)this ); + + ConnectWidget( list_item ); gtk_widget_show( list_item ); diff --git a/src/gtk1/listbox.cpp b/src/gtk1/listbox.cpp index 37154d6eda..d8810ddae0 100644 --- a/src/gtk1/listbox.cpp +++ b/src/gtk1/listbox.cpp @@ -51,6 +51,8 @@ extern bool g_blockEventsOnDrag; extern bool g_blockEventsOnScroll; extern wxCursor g_globalCursor; extern wxWindowGTK *g_delayedFocus; +extern wxWindowGTK *g_focusWindow; +extern wxWindowGTK *g_focusWindowLast; static bool g_hasDoubleClicked = FALSE; @@ -87,6 +89,68 @@ extern "C" gint wxlistbox_idle_callback( gpointer gdata ) return TRUE; } +//----------------------------------------------------------------------------- +// "focus_in_event" +//----------------------------------------------------------------------------- + +static gint gtk_listitem_focus_in_callback( GtkWidget *widget, + GdkEvent *WXUNUSED(event), + wxWindow *win ) +{ + if (g_isIdle) + wxapp_install_idle_handler(); + + g_focusWindowLast = + g_focusWindow = win; + + // does the window itself think that it has the focus? + if ( !win->m_hasFocus ) + { + // not yet, notify it + win->m_hasFocus = TRUE; + + wxChildFocusEvent eventChildFocus(win); + (void)win->GetEventHandler()->ProcessEvent(eventChildFocus); + + wxFocusEvent eventFocus(wxEVT_SET_FOCUS, win->GetId()); + eventFocus.SetEventObject(win); + + (void)win->GetEventHandler()->ProcessEvent(eventFocus); + } + + return FALSE; +} + +//----------------------------------------------------------------------------- +// "focus_out_event" +//----------------------------------------------------------------------------- + +static gint gtk_listitem_focus_out_callback( GtkWidget *widget, GdkEventFocus *gdk_event, wxWindowGTK *win ) +{ + if (g_isIdle) + wxapp_install_idle_handler(); + + g_focusWindow = (wxWindowGTK *)NULL; + + // don't send the window a kill focus event if it thinks that it doesn't + // have focus already + if ( win->m_hasFocus ) + { + win->m_hasFocus = FALSE; + + wxFocusEvent event( wxEVT_KILL_FOCUS, win->GetId() ); + event.SetEventObject( win ); + + // even if we did process the event in wx code, still let GTK itself + // process it too as otherwise bad things happen, especially in GTK2 + // where the text control simply aborts the program if it doesn't get + // the matching focus out event + (void)win->GetEventHandler()->ProcessEvent( event ); + } + + return FALSE; +} + //----------------------------------------------------------------------------- // "button_release_event" //----------------------------------------------------------------------------- @@ -607,6 +671,14 @@ void wxListBox::GtkAddItem( const wxString &item, int pos ) (GtkSignalFunc)gtk_listbox_key_press_callback, (gpointer)this ); + + gtk_signal_connect( GTK_OBJECT(list_item), "focus_in_event", + GTK_SIGNAL_FUNC(gtk_listitem_focus_in_callback), (gpointer)this ); + + gtk_signal_connect( GTK_OBJECT(list_item), "focus_out_event", + GTK_SIGNAL_FUNC(gtk_listitem_focus_out_callback), (gpointer)this ); + + ConnectWidget( list_item ); gtk_widget_show( list_item ); -- 2.45.2