X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/159b66c02a8f1c724d1af3c768ad5d90f4c8a0e2..801225c191a6f6e3921bff34780c9605f76de725:/src/gtk/listbox.cpp diff --git a/src/gtk/listbox.cpp b/src/gtk/listbox.cpp index fa4b4bcdc7..c63ba920e3 100644 --- a/src/gtk/listbox.cpp +++ b/src/gtk/listbox.cpp @@ -37,14 +37,6 @@ extern void wxapp_install_idle_handler(); extern bool g_isIdle; -//------------------------------------------------------------------------- -// conditional compilation -//------------------------------------------------------------------------- - -#if (GTK_MINOR_VERSION > 0) - #define NEW_GTK_SCROLL_CODE -#endif - //----------------------------------------------------------------------------- // private functions //----------------------------------------------------------------------------- @@ -67,9 +59,10 @@ extern bool g_isIdle; // data //----------------------------------------------------------------------------- -extern bool g_blockEventsOnDrag; -extern bool g_blockEventsOnScroll; -extern wxCursor g_globalCursor; +extern bool g_blockEventsOnDrag; +extern bool g_blockEventsOnScroll; +extern wxCursor g_globalCursor; +extern wxWindowGTK *g_delayedFocus; static bool g_hasDoubleClicked = FALSE; @@ -84,17 +77,23 @@ struct wxlistbox_idle_struct gint m_tag; }; -static gint wxlistbox_idle_callback( gpointer gdata ) +extern "C" gint wxlistbox_idle_callback( gpointer gdata ) { wxlistbox_idle_struct* data = (wxlistbox_idle_struct*) gdata; gdk_threads_enter(); gtk_idle_remove( data->m_tag ); - - data->m_listbox->SetFirstItem( data->m_item ); - + + // check that the items haven't been deleted from the listbox since we had + // installed this callback + wxListBox *lbox = data->m_listbox; + if ( data->m_item < lbox->GetCount() ) + { + lbox->SetFirstItem( data->m_item ); + } + delete data; - + gdk_threads_leave(); return TRUE; @@ -219,7 +218,7 @@ gtk_listbox_key_press_callback( GtkWidget *widget, GdkEventKey *gdk_event, wxLis // eat return in all modes ret = TRUE; } - + #if wxUSE_CHECKLISTBOX if ((gdk_event->keyval == ' ') && (listbox->m_hasCheckBoxes) && (!ret)) { @@ -267,9 +266,12 @@ static void gtk_listitem_select_cb( GtkWidget *widget, wxListBox *listbox, bool if (!listbox->m_hasVMT) return; if (g_blockEventsOnDrag) return; + + if (listbox->m_blockEvent) return; wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, listbox->GetId() ); event.SetEventObject( listbox ); + // MSW doesn't do that either // event.SetExtraLong( (long) is_selection ); @@ -334,6 +336,7 @@ bool wxListBox::Create( wxWindow *parent, wxWindowID id, m_needParent = TRUE; m_acceptsFocus = TRUE; m_prevSelection = 0; // or -1 ?? + m_blockEvent = FALSE; if (!PreCreation( parent, pos, size ) || !CreateBase( parent, id, pos, size, style, validator, name )) @@ -374,11 +377,7 @@ bool wxListBox::Create( wxWindow *parent, wxWindowID id, gtk_list_set_selection_mode( GTK_LIST(m_list), mode ); -#ifdef NEW_GTK_SCROLL_CODE gtk_scrolled_window_add_with_viewport( GTK_SCROLLED_WINDOW(m_widget), GTK_WIDGET(m_list) ); -#else - gtk_container_add( GTK_CONTAINER(m_widget), GTK_WIDGET(m_list) ); -#endif /* make list scroll when moving the focus down using cursor keys */ gtk_container_set_focus_vadjustment( @@ -388,8 +387,6 @@ bool wxListBox::Create( wxWindow *parent, wxWindowID id, gtk_widget_show( GTK_WIDGET(m_list) ); - SetBestSize( size ); - if ( style & wxLB_SORT ) { // this will change DoAppend() behaviour @@ -406,11 +403,15 @@ bool wxListBox::Create( wxWindow *parent, wxWindowID id, DoAppend(choices[i]); } + // call it after appending the strings to the listbox, otherwise it doesn't + // work correctly + SetBestSize( size ); + m_parent->DoAddChild( this ); PostCreation(); - SetBackgroundColour( wxSystemSettings::GetSystemColour( wxSYS_COLOUR_LISTBOX ) ); + SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_LISTBOX ) ); SetForegroundColour( parent->GetForegroundColour() ); SetFont( parent->GetFont() ); @@ -424,7 +425,7 @@ wxListBox::~wxListBox() m_hasVMT = FALSE; Clear(); - + if (m_strings) delete m_strings; } @@ -455,7 +456,7 @@ void wxListBox::DoInsertItems(const wxArrayString& items, int pos) for (size_t n = 0; n < nItems; n++) { index = m_strings->Add( items[n] ); - + if (index != GetCount()) { GtkAddItem( items[n], index ); @@ -662,6 +663,12 @@ void wxListBox::Clear() wxCHECK_RET( m_list != NULL, wxT("invalid listbox") ); gtk_list_clear_items( m_list, 0, GetCount() ); + + if ( GTK_LIST(m_list)->last_focus_child != NULL ) + { + // This should be NULL, I think. + GTK_LIST(m_list)->last_focus_child = NULL; + } if ( HasClientObjectData() ) { @@ -855,7 +862,7 @@ void wxListBox::SetSelection( int n, bool select ) { wxCHECK_RET( m_list != NULL, wxT("invalid listbox") ); - GtkDisableEvents(); + m_blockEvent = TRUE; if (select) { @@ -867,7 +874,7 @@ void wxListBox::SetSelection( int n, bool select ) else gtk_list_unselect_item( m_list, n ); - GtkEnableEvents(); + m_blockEvent = FALSE; } void wxListBox::DoSetFirstItem( int n ) @@ -876,18 +883,18 @@ void wxListBox::DoSetFirstItem( int n ) if (gdk_pointer_is_grabbed () && GTK_WIDGET_HAS_GRAB (m_list)) return; - - // terribly efficient + + // terribly efficient const gchar *vadjustment_key = "gtk-vadjustment"; guint vadjustment_key_id = g_quark_from_static_string (vadjustment_key); - - GtkAdjustment *adjustment = + + GtkAdjustment *adjustment = (GtkAdjustment*) gtk_object_get_data_by_id (GTK_OBJECT (m_list), vadjustment_key_id); wxCHECK_RET( adjustment, wxT("invalid listbox code") ); GList *target = g_list_nth( m_list->children, n ); wxCHECK_RET( target, wxT("invalid listbox index") ); - + GtkWidget *item = GTK_WIDGET(target->data); wxCHECK_RET( item, wxT("invalid listbox code") ); @@ -897,7 +904,7 @@ void wxListBox::DoSetFirstItem( int n ) data->m_listbox = this; data->m_item = n; data->m_tag = gtk_idle_add_priority( 800, wxlistbox_idle_callback, (gpointer) data ); - + return; } @@ -939,38 +946,6 @@ void wxListBox::ApplyToolTip( GtkTooltips *tips, const wxChar *tip ) } #endif // wxUSE_TOOLTIPS -void wxListBox::GtkDisableEvents() -{ - GList *child = m_list->children; - while (child) - { - gtk_signal_disconnect_by_func( GTK_OBJECT(child->data), - GTK_SIGNAL_FUNC(gtk_listitem_select_callback), (gpointer)this ); - - if (HasFlag(wxLB_MULTIPLE)) - gtk_signal_disconnect_by_func( GTK_OBJECT(child->data), - GTK_SIGNAL_FUNC(gtk_listitem_deselect_callback), (gpointer)this ); - - child = child->next; - } -} - -void wxListBox::GtkEnableEvents() -{ - GList *child = m_list->children; - while (child) - { - gtk_signal_connect( GTK_OBJECT(child->data), "select", - GTK_SIGNAL_FUNC(gtk_listitem_select_callback), (gpointer)this ); - - if (HasFlag(wxLB_MULTIPLE)) - gtk_signal_connect( GTK_OBJECT(child->data), "deselect", - GTK_SIGNAL_FUNC(gtk_listitem_deselect_callback), (gpointer)this ); - - child = child->next; - } -} - GtkWidget *wxListBox::GetConnectWidget() { return GTK_WIDGET(m_list); @@ -1048,12 +1023,53 @@ void wxListBox::OnInternalIdle() } } + if (g_delayedFocus == this) + { + if (GTK_WIDGET_REALIZED(m_widget)) + { + gtk_widget_grab_focus( m_widget ); + g_delayedFocus = NULL; + } + } + UpdateWindowUI(); } wxSize wxListBox::DoGetBestSize() const { - return wxSize(100, 110); + int lbWidth = 100; // some defaults + int lbHeight = 110; + int wLine; + + // Find the widest line + for(int i = 0; i < GetCount(); i++) { + wxString str(GetString(i)); + GetTextExtent(str, &wLine, NULL); + lbWidth = wxMax(lbWidth, wLine); + } + + // Add room for the scrollbar + lbWidth += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X); + + // And just a bit more + int cx, cy; + GetTextExtent("X", &cx, &cy); + lbWidth += 3 * cx; + + // don't make the listbox too tall (limit height to around 10 items) but don't + // make it too small neither + lbHeight = (cy+4) * wxMin(wxMax(GetCount(), 3), 10); + + return wxSize(lbWidth, lbHeight); +} + +void wxListBox::FixUpMouseEvent(GtkWidget *widget, wxCoord& x, wxCoord& y) +{ + // the mouse event coords are relative to the listbox items, we need to + // translate them to the normal client coords + x += widget->allocation.x; + y += widget->allocation.y; } -#endif +#endif // wxUSE_LISTBOX +