X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/2e1d71048207f3d008dac28d0c290ae0a6b59997..90b85bfcde1752db19ab8453e8aa77986b3f4273:/src/gtk/listbox.cpp diff --git a/src/gtk/listbox.cpp b/src/gtk/listbox.cpp index b81275e629..9f06846793 100644 --- a/src/gtk/listbox.cpp +++ b/src/gtk/listbox.cpp @@ -8,15 +8,20 @@ ///////////////////////////////////////////////////////////////////////////// -#ifdef __GNUG__ +#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) #pragma implementation "listbox.h" #endif -#include "wx/listbox.h" +// For compilers that support precompilation, includes "wx.h". +#include "wx/wxprec.h" + +#include "wx/defs.h" #if wxUSE_LISTBOX +#include "wx/listbox.h" #include "wx/dynarray.h" +#include "wx/arrstr.h" #include "wx/utils.h" #include "wx/intl.h" #include "wx/checklst.h" @@ -38,24 +43,6 @@ extern void wxapp_install_idle_handler(); extern bool g_isIdle; -//----------------------------------------------------------------------------- -// private functions -//----------------------------------------------------------------------------- - -#if wxUSE_CHECKLISTBOX - -// checklistboxes have "[±] " prepended to their lables, this macro removes it -// (NB: 4 below is the length of wxCHECKLBOX_STRING above) -// -// the argument to it is a "const char *" pointer -#define GET_REAL_LABEL(label) ((m_hasCheckBoxes)?(label)+4 : (label)) - -#else // !wxUSE_CHECKLISTBOX - -#define GET_REAL_LABEL(label) (label) - -#endif // wxUSE_CHECKLISTBOX - //----------------------------------------------------------------------------- // data //----------------------------------------------------------------------------- @@ -261,21 +248,22 @@ static void gtk_listitem_deselect_callback( GtkWidget *widget, wxListBox *listbo gtk_listitem_select_cb( widget, listbox, FALSE ); } -static void gtk_listitem_select_cb( GtkWidget *widget, wxListBox *listbox, bool is_selection ) +static void gtk_listitem_select_cb( GtkWidget *widget, + wxListBox *listbox, + bool is_selection ) { if (g_isIdle) wxapp_install_idle_handler(); 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 ); + // indicate whether this is a selection or a deselection + event.SetExtraLong( is_selection ); if ((listbox->GetWindowStyleFlag() & wxLB_SINGLE) != 0) { @@ -328,6 +316,18 @@ wxListBox::wxListBox() #endif // wxUSE_CHECKLISTBOX } +bool wxListBox::Create( wxWindow *parent, wxWindowID id, + const wxPoint &pos, const wxSize &size, + const wxArrayString& choices, + long style, const wxValidator& validator, + const wxString &name ) +{ + wxCArrayString chs(choices); + + return Create( parent, id, pos, size, chs.GetCount(), chs.GetStrings(), + style, validator, name ); +} + bool wxListBox::Create( wxWindow *parent, wxWindowID id, const wxPoint &pos, const wxSize &size, int n, const wxString choices[], @@ -404,19 +404,10 @@ 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::GetColour( wxSYS_COLOUR_LISTBOX ) ); - SetForegroundColour( parent->GetForegroundColour() ); - SetFont( parent->GetFont() ); - - Show( TRUE ); + PostCreation(size); + SetBestSize(size); // need this too because this is a wxControlWithItems return TRUE; } @@ -431,6 +422,10 @@ wxListBox::~wxListBox() delete m_strings; } +// ---------------------------------------------------------------------------- +// adding items +// ---------------------------------------------------------------------------- + void wxListBox::DoInsertItems(const wxArrayString& items, int pos) { wxCHECK_RET( m_list != NULL, wxT("invalid listbox") ); @@ -461,7 +456,7 @@ void wxListBox::DoInsertItems(const wxArrayString& items, int pos) if (index != GetCount()) { GtkAddItem( items[n], index ); - wxNode *node = m_clientList.Nth( index ); + wxList::compatibility_iterator node = m_clientList.Item( index ); m_clientList.Insert( node, (wxObject*) NULL ); } else @@ -484,7 +479,7 @@ void wxListBox::DoInsertItems(const wxArrayString& items, int pos) } else { - wxNode *node = m_clientList.Nth( pos ); + wxList::compatibility_iterator node = m_clientList.Item( pos ); for ( size_t n = 0; n < nItems; n++ ) { GtkAddItem( items[n], pos+n ); @@ -510,7 +505,7 @@ int wxListBox::DoAppend( const wxString& item ) { GtkAddItem( item, index ); - wxNode *node = m_clientList.Nth( index ); + wxList::compatibility_iterator node = m_clientList.Item( index ); m_clientList.Insert( node, (wxObject *)NULL ); return index; @@ -612,59 +607,15 @@ void wxListBox::DoSetItems( const wxArrayString& items, } // ---------------------------------------------------------------------------- -// client data +// deleting items // ---------------------------------------------------------------------------- -void wxListBox::DoSetItemClientData( int n, void* clientData ) -{ - wxCHECK_RET( m_widget != NULL, wxT("invalid listbox control") ); - - wxNode *node = m_clientList.Nth( n ); - wxCHECK_RET( node, wxT("invalid index in wxListBox::DoSetItemClientData") ); - - node->SetData( (wxObject*) clientData ); -} - -void* wxListBox::DoGetItemClientData( int n ) const -{ - wxCHECK_MSG( m_widget != NULL, NULL, wxT("invalid listbox control") ); - - wxNode *node = m_clientList.Nth( n ); - wxCHECK_MSG( node, NULL, wxT("invalid index in wxListBox::DoGetItemClientData") ); - - return node->Data(); -} - -void wxListBox::DoSetItemClientObject( int n, wxClientData* clientData ) -{ - wxCHECK_RET( m_widget != NULL, wxT("invalid listbox control") ); - - wxNode *node = m_clientList.Nth( n ); - wxCHECK_RET( node, wxT("invalid index in wxListBox::DoSetItemClientObject") ); - - wxClientData *cd = (wxClientData*) node->Data(); - delete cd; - - node->SetData( (wxObject*) clientData ); -} - -wxClientData* wxListBox::DoGetItemClientObject( int n ) const -{ - wxCHECK_MSG( m_widget != NULL, (wxClientData*) NULL, wxT("invalid listbox control") ); - - wxNode *node = m_clientList.Nth( n ); - wxCHECK_MSG( node, (wxClientData *)NULL, - wxT("invalid index in wxListBox::DoGetItemClientObject") ); - - return (wxClientData*) node->Data(); -} - 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. @@ -676,11 +627,11 @@ void wxListBox::Clear() // destroy the data (due to Robert's idea of using wxList // and not wxList we can't just say // m_clientList.DeleteContents(TRUE) - this would crash! - wxNode *node = m_clientList.First(); + wxList::compatibility_iterator node = m_clientList.GetFirst(); while ( node ) { - delete (wxClientData *)node->Data(); - node = node->Next(); + delete (wxClientData *)node->GetData(); + node = node->GetNext(); } } m_clientList.Clear(); @@ -701,26 +652,97 @@ void wxListBox::Delete( int n ) gtk_list_remove_items( m_list, list ); g_list_free( list ); - wxNode *node = m_clientList.Nth( n ); + wxList::compatibility_iterator node = m_clientList.Item( n ); if ( node ) { if ( m_clientDataItemsType == wxClientData_Object ) { - wxClientData *cd = (wxClientData*)node->Data(); + wxClientData *cd = (wxClientData*)node->GetData(); delete cd; } - m_clientList.DeleteNode( node ); + m_clientList.Erase( node ); } if ( m_strings ) - m_strings->Remove(n); + m_strings->RemoveAt(n); +} + +// ---------------------------------------------------------------------------- +// client data +// ---------------------------------------------------------------------------- + +void wxListBox::DoSetItemClientData( int n, void* clientData ) +{ + wxCHECK_RET( m_widget != NULL, wxT("invalid listbox control") ); + + wxList::compatibility_iterator node = m_clientList.Item( n ); + wxCHECK_RET( node, wxT("invalid index in wxListBox::DoSetItemClientData") ); + + node->SetData( (wxObject*) clientData ); +} + +void* wxListBox::DoGetItemClientData( int n ) const +{ + wxCHECK_MSG( m_widget != NULL, NULL, wxT("invalid listbox control") ); + + wxList::compatibility_iterator node = m_clientList.Item( n ); + wxCHECK_MSG( node, NULL, wxT("invalid index in wxListBox::DoGetItemClientData") ); + + return node->GetData(); +} + +void wxListBox::DoSetItemClientObject( int n, wxClientData* clientData ) +{ + wxCHECK_RET( m_widget != NULL, wxT("invalid listbox control") ); + + wxList::compatibility_iterator node = m_clientList.Item( n ); + wxCHECK_RET( node, wxT("invalid index in wxListBox::DoSetItemClientObject") ); + + // wxItemContainer already deletes data for us + + node->SetData( (wxObject*) clientData ); +} + +wxClientData* wxListBox::DoGetItemClientObject( int n ) const +{ + wxCHECK_MSG( m_widget != NULL, (wxClientData*) NULL, wxT("invalid listbox control") ); + + wxList::compatibility_iterator node = m_clientList.Item( n ); + wxCHECK_MSG( node, (wxClientData *)NULL, + wxT("invalid index in wxListBox::DoGetItemClientObject") ); + + return (wxClientData*) node->GetData(); } // ---------------------------------------------------------------------------- // string list access // ---------------------------------------------------------------------------- +wxString wxListBox::GetRealLabel(GList *item) const +{ + GtkBin *bin = GTK_BIN( item->data ); + GtkLabel *label = GTK_LABEL( bin->child ); + + wxString str; + +#ifdef __WXGTK20__ + str = wxGTK_CONV_BACK( gtk_label_get_text( label ) ); +#else + str = wxString( label->label ); +#endif + +#if wxUSE_CHECKLISTBOX + // checklistboxes have "[±] " prepended to their lables, remove it + // + // NB: 4 below is the length of wxCHECKLBOX_STRING from wx/gtk/checklst.h + if ( m_hasCheckBoxes ) + str.erase(0, 4); +#endif // wxUSE_CHECKLISTBOX + + return str; +} + void wxListBox::SetString( int n, const wxString &string ) { wxCHECK_RET( m_list != NULL, wxT("invalid listbox") ); @@ -753,16 +775,7 @@ wxString wxListBox::GetString( int n ) const GList *child = g_list_nth( m_list->children, n ); if (child) { - GtkBin *bin = GTK_BIN( child->data ); - GtkLabel *label = GTK_LABEL( bin->child ); - -#ifdef __WXGTK20__ - wxString str = wxGTK_CONV_BACK( gtk_label_get_text( label ) ); -#else - wxString str = wxString( label->label ); -#endif - - return str; + return GetRealLabel(child); } wxFAIL_MSG(wxT("wrong listbox index")); @@ -786,16 +799,7 @@ int wxListBox::FindString( const wxString &item ) const int count = 0; while (child) { - GtkBin *bin = GTK_BIN( child->data ); - GtkLabel *label = GTK_LABEL( bin->child ); - -#ifdef __WXGTK20__ - wxString str = wxGTK_CONV_BACK( gtk_label_get_text( label ) ); -#else - wxString str = wxString( label->label ); -#endif - - if (str == item) + if ( GetRealLabel(child) == item ) return count; count++; @@ -883,7 +887,7 @@ void wxListBox::SetSelection( int n, bool select ) else gtk_list_unselect_item( m_list, n ); - m_blockEvent = FALSE; + m_blockEvent = FALSE; } void wxListBox::DoSetFirstItem( int n ) @@ -920,7 +924,7 @@ void wxListBox::DoSetFirstItem( int n ) float y = item->allocation.y; if (y > adjustment->upper - adjustment->page_size) y = adjustment->upper - adjustment->page_size; - gtk_adjustment_set_value( adjustment, y ); + gtk_adjustment_set_value( adjustment, y ); } // ---------------------------------------------------------------------------- @@ -962,6 +966,8 @@ GtkWidget *wxListBox::GetConnectWidget() bool wxListBox::IsOwnGtkWindow( GdkWindow *window ) { + if (m_widget->window == window) return TRUE; + if (GTK_WIDGET(m_list)->window == window) return TRUE; GList *child = m_list->children; @@ -1041,7 +1047,8 @@ void wxListBox::OnInternalIdle() } } - UpdateWindowUI(); + if (wxUpdateUIEvent::CanUpdate(this)) + UpdateWindowUI(wxUPDATE_UI_FROMIDLE); } wxSize wxListBox::DoGetBestSize() const @@ -1062,7 +1069,7 @@ wxSize wxListBox::DoGetBestSize() const // And just a bit more int cx, cy; - GetTextExtent("X", &cx, &cy); + GetTextExtent( wxT("X"), &cx, &cy); lbWidth += 3 * cx; // don't make the listbox too tall (limit height to around 10 items) but don't @@ -1080,5 +1087,13 @@ void wxListBox::FixUpMouseEvent(GtkWidget *widget, wxCoord& x, wxCoord& y) y += widget->allocation.y; } + +// static +wxVisualAttributes +wxListBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant)) +{ + return GetDefaultAttributesFromGTKWidget(gtk_list_new, true); +} + #endif // wxUSE_LISTBOX