From 243dbf1a5088be31980df51b97831b52cb6986cd Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Fri, 9 May 2003 12:58:28 +0000 Subject: [PATCH] Insert() patch from John Labenski git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@20564 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- docs/latex/wx/ctrlsub.tex | 33 ++++++++++++- include/wx/ctrlsub.h | 6 +++ include/wx/gtk/choice.h | 3 +- include/wx/gtk/combobox.h | 5 ++ include/wx/gtk1/choice.h | 3 +- include/wx/gtk1/combobox.h | 5 ++ include/wx/listbox.h | 4 +- include/wx/mac/choice.h | 1 + include/wx/motif/choice.h | 1 + include/wx/motif/combobox.h | 1 + include/wx/msw/choice.h | 1 + include/wx/os2/choice.h | 1 + include/wx/univ/combobox.h | 1 + samples/widgets/combobox.cpp | 50 ++++++++++++++++++-- src/common/ctrlsub.cpp | 19 ++++++++ src/gtk/choice.cpp | 37 ++++++++++++--- src/gtk/combobox.cpp | 87 +++++++++++++++++++++++++++++++++++ src/gtk1/choice.cpp | 37 ++++++++++++--- src/gtk1/combobox.cpp | 87 +++++++++++++++++++++++++++++++++++ src/mac/carbon/choice.cpp | 16 +++++++ src/mac/choice.cpp | 16 +++++++ src/motif/choice.cpp | 8 ++++ src/motif/combobox.cpp | 16 +++++++ src/motif/combobox_native.cpp | 16 +++++++ src/msw/choice.cpp | 14 ++++++ src/os2/choice.cpp | 26 +++++++++++ src/univ/combobox.cpp | 12 +++++ 27 files changed, 483 insertions(+), 23 deletions(-) diff --git a/docs/latex/wx/ctrlsub.tex b/docs/latex/wx/ctrlsub.tex index 55d6c1ac36..80e9c90e60 100644 --- a/docs/latex/wx/ctrlsub.tex +++ b/docs/latex/wx/ctrlsub.tex @@ -1,4 +1,4 @@ -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% Name: ctrlsub.tex %% Purpose: wxControlWithItems documentation %% Author: Vadim Zeitlin @@ -7,7 +7,7 @@ %% RCS-ID: $Id$ %% Copyright: (c) 2003 Vadim Zeitlin %% License: wxWindows license -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \section{\class{wxControlWithItems}}\label{wxcontrolwithitems} @@ -218,6 +218,35 @@ selected. \helpref{GetSelection}{wxcontrolwithitemsgetselection} +\membersection{wxControlWithItems::Insert}\label{wxcontrolwithitemsinsert} + +\func{int}{Insert}{\param{const wxString\& }{ item}, \param{int }{pos}} + +Inserts the item into the list before pos. +Not valid for {\tt wxLB\_SORT} or {\tt wxCB\_SORT} styles, use Append instead. + +\func{int}{Insert}{\param{const wxString\& }{ item}, \param{int }{pos}, \param{void *}{clientData}} + +\func{int}{Insert}{\param{const wxString\& }{ item}, \param{int }{pos}, \param{wxClientData *}{clientData}} + +Inserts the item into the list before pos, associating the given, typed or +untyped, client data pointer with the item. +Not valid for {\tt wxLB\_SORT} or {\tt wxCB\_SORT} styles, use Append instead. + +\wxheading{Parameters} + +\docparam{item}{String to add.} + +\docparam{pos}{Position to insert item before, zero based.} + +\docparam{clientData}{Client data to associate with the item.} + +\wxheading{Return value} + +The return value is the index of the newly inserted item. If the insertion failed +for some reason, -1 is returned. + + \membersection{wxControlWithItems::IsEmpty}\label{wxcontrolwithitemsisempty} \constfunc{bool}{IsEmpty}{\void} diff --git a/include/wx/ctrlsub.h b/include/wx/ctrlsub.h index 4052e56e87..20c3e5b5a4 100644 --- a/include/wx/ctrlsub.h +++ b/include/wx/ctrlsub.h @@ -47,6 +47,11 @@ public: // append several items at once to the control void Append(const wxArrayString& strings); + int Insert(const wxString& item, int pos) + { return DoInsert(item, pos); } + int Insert(const wxString& item, int pos, void *clientData); + int Insert(const wxString& item, int pos, wxClientData *clientData); + // deleting items // -------------- @@ -94,6 +99,7 @@ public: protected: virtual int DoAppend(const wxString& item) = 0; + virtual int DoInsert(const wxString& item, int pos) = 0; virtual void DoSetItemClientData(int n, void* clientData) = 0; virtual void* DoGetItemClientData(int n) const = 0; diff --git a/include/wx/gtk/choice.h b/include/wx/gtk/choice.h index 6acce9a5de..5f550d732a 100644 --- a/include/wx/gtk/choice.h +++ b/include/wx/gtk/choice.h @@ -60,6 +60,7 @@ protected: void ApplyWidgetStyle(); virtual int DoAppend(const wxString& item); + virtual int DoInsert(const wxString& item, int pos); virtual void DoSetItemClientData( int n, void* clientData ); virtual void* DoGetItemClientData( int n ) const; @@ -70,7 +71,7 @@ protected: private: // common part of Create() and DoAppend() - size_t GtkAppendHelper(GtkWidget *menu, const wxString& item); + int GtkAddHelper(GtkWidget *menu, int pos, const wxString& item); // this array is only used for controls with wxCB_SORT style, so only // allocate it if it's needed (hence using pointer) diff --git a/include/wx/gtk/combobox.h b/include/wx/gtk/combobox.h index 63304d121f..84e9e2be49 100644 --- a/include/wx/gtk/combobox.h +++ b/include/wx/gtk/combobox.h @@ -69,6 +69,10 @@ public: void Append( const wxString &item, void* clientData ); void Append( const wxString &item, wxClientData* clientData ); + void Insert( const wxString &item, int pos ); + void Insert( const wxString &item, int pos, void* clientData ); + void Insert( const wxString &item, int pos, wxClientData* clientData ); + void SetClientData( int n, void* clientData ); void* GetClientData( int n ); void SetClientObject( int n, wxClientData* clientData ); @@ -122,6 +126,7 @@ public: void DisableEvents(); void EnableEvents(); void AppendCommon( const wxString &item ); + void InsertCommon( const wxString &item, int pos ); GtkWidget* GetConnectWidget(); bool IsOwnGtkWindow( GdkWindow *window ); void ApplyWidgetStyle(); diff --git a/include/wx/gtk1/choice.h b/include/wx/gtk1/choice.h index 6acce9a5de..5f550d732a 100644 --- a/include/wx/gtk1/choice.h +++ b/include/wx/gtk1/choice.h @@ -60,6 +60,7 @@ protected: void ApplyWidgetStyle(); virtual int DoAppend(const wxString& item); + virtual int DoInsert(const wxString& item, int pos); virtual void DoSetItemClientData( int n, void* clientData ); virtual void* DoGetItemClientData( int n ) const; @@ -70,7 +71,7 @@ protected: private: // common part of Create() and DoAppend() - size_t GtkAppendHelper(GtkWidget *menu, const wxString& item); + int GtkAddHelper(GtkWidget *menu, int pos, const wxString& item); // this array is only used for controls with wxCB_SORT style, so only // allocate it if it's needed (hence using pointer) diff --git a/include/wx/gtk1/combobox.h b/include/wx/gtk1/combobox.h index 63304d121f..84e9e2be49 100644 --- a/include/wx/gtk1/combobox.h +++ b/include/wx/gtk1/combobox.h @@ -69,6 +69,10 @@ public: void Append( const wxString &item, void* clientData ); void Append( const wxString &item, wxClientData* clientData ); + void Insert( const wxString &item, int pos ); + void Insert( const wxString &item, int pos, void* clientData ); + void Insert( const wxString &item, int pos, wxClientData* clientData ); + void SetClientData( int n, void* clientData ); void* GetClientData( int n ); void SetClientObject( int n, wxClientData* clientData ); @@ -122,6 +126,7 @@ public: void DisableEvents(); void EnableEvents(); void AppendCommon( const wxString &item ); + void InsertCommon( const wxString &item, int pos ); GtkWidget* GetConnectWidget(); bool IsOwnGtkWindow( GdkWindow *window ); void ApplyWidgetStyle(); diff --git a/include/wx/listbox.h b/include/wx/listbox.h index 4e56164b3d..1c8c397b24 100644 --- a/include/wx/listbox.h +++ b/include/wx/listbox.h @@ -109,8 +109,8 @@ public: protected: // NB: due to wxGTK implementation details, DoInsert() is implemented // using DoInsertItems() and not the other way round - void DoInsert(const wxString& item, int pos) - { InsertItems(1, &item, pos); } + virtual int DoInsert(const wxString& item, int pos) + { InsertItems(1, &item, pos); return pos; } // to be implemented in derived classes virtual void DoInsertItems(const wxArrayString& items, int pos) = 0; diff --git a/include/wx/mac/choice.h b/include/wx/mac/choice.h index 86419c148b..d22ca43d68 100644 --- a/include/wx/mac/choice.h +++ b/include/wx/mac/choice.h @@ -57,6 +57,7 @@ public: // implement base class pure virtuals virtual int DoAppend(const wxString& item); + virtual int DoInsert(const wxString& item, int pos); virtual void Delete(int n); virtual void Clear(); diff --git a/include/wx/motif/choice.h b/include/wx/motif/choice.h index a2d0195418..a0a51e42e3 100644 --- a/include/wx/motif/choice.h +++ b/include/wx/motif/choice.h @@ -55,6 +55,7 @@ public: // implementation of wxControlWithItems virtual int GetCount() const; virtual int DoAppend(const wxString& item); + virtual int DoInsert(const wxString& item, int pos); virtual void DoSetItemClientData(int n, void* clientData); virtual void* DoGetItemClientData(int n) const; virtual void DoSetItemClientObject(int n, wxClientData* clientData); diff --git a/include/wx/motif/combobox.h b/include/wx/motif/combobox.h index 20681ffb4f..84389e24c5 100644 --- a/include/wx/motif/combobox.h +++ b/include/wx/motif/combobox.h @@ -55,6 +55,7 @@ public: // implementation of wxControlWithItems virtual int DoAppend(const wxString& item); + virtual int DoInsert(const wxString& item, int pos); virtual void Delete(int n); virtual void Clear(); virtual int GetSelection() const ; diff --git a/include/wx/msw/choice.h b/include/wx/msw/choice.h index fdc9fef5b8..9ca576cd7c 100644 --- a/include/wx/msw/choice.h +++ b/include/wx/msw/choice.h @@ -52,6 +52,7 @@ public: // implement base class pure virtuals virtual int DoAppend(const wxString& item); + virtual int DoInsert(const wxString& item, int pos); virtual void Delete(int n); virtual void Clear(); diff --git a/include/wx/os2/choice.h b/include/wx/os2/choice.h index 098c711752..9c4e0f47a5 100644 --- a/include/wx/os2/choice.h +++ b/include/wx/os2/choice.h @@ -59,6 +59,7 @@ public: // Implement base class virtuals // virtual int DoAppend(const wxString& rsItem); + virtual int DoInsert(const wxString& rsItem, int pos); virtual void Delete(int n); virtual void Clear(void); diff --git a/include/wx/univ/combobox.h b/include/wx/univ/combobox.h index 2bb7faccb6..5ef092afbd 100644 --- a/include/wx/univ/combobox.h +++ b/include/wx/univ/combobox.h @@ -330,6 +330,7 @@ public: protected: virtual int DoAppend(const wxString& item); + virtual int DoInsert(const wxString& item, int pos); virtual void DoSetItemClientData(int n, void* clientData); virtual void* DoGetItemClientData(int n) const; virtual void DoSetItemClientObject(int n, wxClientData* clientData); diff --git a/samples/widgets/combobox.cpp b/samples/widgets/combobox.cpp index d41699167e..0fb153f667 100644 --- a/samples/widgets/combobox.cpp +++ b/samples/widgets/combobox.cpp @@ -54,6 +54,8 @@ enum { ComboPage_Reset = 100, ComboPage_CurText, + ComboPage_Insert, + ComboPage_InsertText, ComboPage_Add, ComboPage_AddText, ComboPage_AddSeveral, @@ -91,6 +93,7 @@ protected: void OnButtonDelete(wxCommandEvent& event); void OnButtonDeleteSel(wxCommandEvent& event); void OnButtonClear(wxCommandEvent& event); + void OnButtonInsert(wxCommandEvent &event); void OnButtonAdd(wxCommandEvent& event); void OnButtonAddSeveral(wxCommandEvent& event); void OnButtonAddMany(wxCommandEvent& event); @@ -102,6 +105,7 @@ protected: void OnUpdateUICurText(wxUpdateUIEvent& event); + void OnUpdateUIInsert(wxUpdateUIEvent& event); void OnUpdateUIAddSeveral(wxUpdateUIEvent& event); void OnUpdateUIClearButton(wxUpdateUIEvent& event); void OnUpdateUIDeleteButton(wxUpdateUIEvent& event); @@ -129,7 +133,8 @@ protected: wxSizer *m_sizerCombo; // the text entries for "Add/change string" and "Delete" buttons - wxTextCtrl *m_textAdd, + wxTextCtrl *m_textInsert, + *m_textAdd, *m_textChange, *m_textDelete; @@ -148,16 +153,19 @@ BEGIN_EVENT_TABLE(ComboboxWidgetsPage, WidgetsPage) EVT_BUTTON(ComboPage_Delete, ComboboxWidgetsPage::OnButtonDelete) EVT_BUTTON(ComboPage_DeleteSel, ComboboxWidgetsPage::OnButtonDeleteSel) EVT_BUTTON(ComboPage_Clear, ComboboxWidgetsPage::OnButtonClear) + EVT_BUTTON(ComboPage_Insert, ComboboxWidgetsPage::OnButtonInsert) EVT_BUTTON(ComboPage_Add, ComboboxWidgetsPage::OnButtonAdd) EVT_BUTTON(ComboPage_AddSeveral, ComboboxWidgetsPage::OnButtonAddSeveral) EVT_BUTTON(ComboPage_AddMany, ComboboxWidgetsPage::OnButtonAddMany) + EVT_TEXT_ENTER(ComboPage_InsertText, ComboboxWidgetsPage::OnButtonInsert) EVT_TEXT_ENTER(ComboPage_AddText, ComboboxWidgetsPage::OnButtonAdd) EVT_TEXT_ENTER(ComboPage_DeleteText, ComboboxWidgetsPage::OnButtonDelete) EVT_UPDATE_UI(ComboPage_CurText, ComboboxWidgetsPage::OnUpdateUICurText) EVT_UPDATE_UI(ComboPage_Reset, ComboboxWidgetsPage::OnUpdateUIResetButton) + EVT_UPDATE_UI(ComboPage_Insert, ComboboxWidgetsPage::OnUpdateUIInsert) EVT_UPDATE_UI(ComboPage_AddSeveral, ComboboxWidgetsPage::OnUpdateUIAddSeveral) EVT_UPDATE_UI(ComboPage_Clear, ComboboxWidgetsPage::OnUpdateUIClearButton) EVT_UPDATE_UI(ComboPage_DeleteText, ComboboxWidgetsPage::OnUpdateUIClearButton) @@ -168,6 +176,7 @@ BEGIN_EVENT_TABLE(ComboboxWidgetsPage, WidgetsPage) EVT_COMBOBOX(ComboPage_Combo, ComboboxWidgetsPage::OnComboBox) EVT_TEXT(ComboPage_Combo, ComboboxWidgetsPage::OnComboText) + EVT_TEXT_ENTER(ComboPage_Combo, ComboboxWidgetsPage::OnComboText) EVT_CHECKBOX(-1, ComboboxWidgetsPage::OnCheckOrRadioBox) EVT_RADIOBOX(-1, ComboboxWidgetsPage::OnCheckOrRadioBox) @@ -241,16 +250,22 @@ ComboboxWidgetsPage::ComboboxWidgetsPage(wxNotebook *notebook, sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5); + sizerRow = CreateSizerWithTextAndButton(ComboPage_Insert, + _T("&Insert this string"), + ComboPage_InsertText, + &m_textInsert); + sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5); + sizerRow = CreateSizerWithTextAndButton(ComboPage_Add, _T("&Add this string"), ComboPage_AddText, &m_textAdd); sizerMiddle->Add(sizerRow, 0, wxALL | wxGROW, 5); - btn = new wxButton(this, ComboPage_AddSeveral, _T("&Insert a few strings")); + btn = new wxButton(this, ComboPage_AddSeveral, _T("&Append a few strings")); sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5); - btn = new wxButton(this, ComboPage_AddMany, _T("Add &many strings")); + btn = new wxButton(this, ComboPage_AddMany, _T("Append &many strings")); sizerMiddle->Add(btn, 0, wxALL | wxGROW, 5); sizerRow = CreateSizerWithTextAndButton(ComboPage_Change, @@ -410,6 +425,21 @@ void ComboboxWidgetsPage::OnButtonClear(wxCommandEvent& event) m_combobox->Clear(); } +void ComboboxWidgetsPage::OnButtonInsert(wxCommandEvent& event) +{ + static unsigned int s_item = 0; + + wxString s = m_textInsert->GetValue(); + if ( !m_textInsert->IsModified() ) + { + // update the default string + m_textInsert->SetValue(wxString::Format(_T("test item %u"), ++s_item)); + } + + if (m_combobox->GetSelection() >= 0) + m_combobox->Insert(s, m_combobox->GetSelection()); +} + void ComboboxWidgetsPage::OnButtonAdd(wxCommandEvent& event) { static unsigned int s_item = 0; @@ -452,6 +482,17 @@ void ComboboxWidgetsPage::OnUpdateUIResetButton(wxUpdateUIEvent& event) event.Enable( m_chkSort->GetValue() || m_chkReadonly->GetValue() ); } +void ComboboxWidgetsPage::OnUpdateUIInsert(wxUpdateUIEvent& event) +{ + if (m_combobox) + { + bool enable = !(m_combobox->GetWindowStyle() & wxCB_SORT) && + (m_combobox->GetSelection() >= 0); + + event.Enable(enable); + } +} + void ComboboxWidgetsPage::OnUpdateUIDeleteButton(wxUpdateUIEvent& event) { if (m_combobox) @@ -490,6 +531,9 @@ void ComboboxWidgetsPage::OnComboText(wxCommandEvent& event) wxASSERT_MSG( s == m_combobox->GetValue(), _T("event and combobox values should be the same") ); + if (event.GetEventType() == wxEVT_COMMAND_TEXT_ENTER) + wxLogMessage(_T("Combobox enter pressed (now '%s')"), s.c_str()); + else wxLogMessage(_T("Combobox text changed (now '%s')"), s.c_str()); } diff --git a/src/common/ctrlsub.cpp b/src/common/ctrlsub.cpp index 67e8a80333..975812f8d4 100644 --- a/src/common/ctrlsub.cpp +++ b/src/common/ctrlsub.cpp @@ -70,6 +70,25 @@ void wxItemContainer::Append(const wxArrayString& strings) } } +int wxItemContainer::Insert(const wxString& item, int pos, void *clientData) +{ + int n = DoInsert(item, pos); + if ( n != wxNOT_FOUND ) + SetClientData(n, clientData); + + return n; +} + +int +wxItemContainer::Insert(const wxString& item, int pos, wxClientData *clientData) +{ + int n = DoInsert(item, pos); + if ( n != wxNOT_FOUND ) + SetClientObject(n, clientData); + + return n; +} + // ---------------------------------------------------------------------------- // client data // ---------------------------------------------------------------------------- diff --git a/src/gtk/choice.cpp b/src/gtk/choice.cpp index 4edcdc8b94..d746df7874 100644 --- a/src/gtk/choice.cpp +++ b/src/gtk/choice.cpp @@ -102,7 +102,7 @@ bool wxChoice::Create( wxWindow *parent, wxWindowID id, for (int i = 0; i < n; i++) { - GtkAppendHelper(menu, choices[i]); + GtkAddHelper(menu, i, choices[i]); } gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu ); @@ -136,7 +136,20 @@ int wxChoice::DoAppend( const wxString &item ) GtkWidget *menu = gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ); - return GtkAppendHelper(menu, item); + return GtkAddHelper(menu, GetCount(), item); +} + +int wxChoice::DoInsert( const wxString &item, int pos ) +{ + wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice control") ); + wxCHECK_MSG( (pos>=0) && (pos<=GetCount()), -1, wxT("invalid index")); + + if (pos == GetCount()) + return DoAppend(item); + + GtkWidget *menu = gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ); + + return GtkAddHelper(menu, pos, item); } void wxChoice::DoSetItemClientData( int n, void* clientData ) @@ -388,8 +401,10 @@ void wxChoice::ApplyWidgetStyle() } } -size_t wxChoice::GtkAppendHelper(GtkWidget *menu, const wxString& item) +int wxChoice::GtkAddHelper(GtkWidget *menu, int pos, const wxString& item) { + wxCHECK_MSG((pos>=0) && (pos<=(int)m_clientList.GetCount()), -1, wxT("invalid index")); + GtkWidget *menu_item = gtk_menu_item_new_with_label( wxGTK_CONV( item ) ); size_t index; @@ -412,14 +427,22 @@ size_t wxChoice::GtkAppendHelper(GtkWidget *menu, const wxString& item) } else { + // don't call wxChoice::GetCount() from here because it doesn't work + // if we're called from ctor (and GtkMenuShell is still NULL) + // normal control, just append + if (pos == (int)m_clientList.GetCount()) + { gtk_menu_append( GTK_MENU(menu), menu_item ); - m_clientList.Append( (wxObject*) NULL ); - - // don't call wxChoice::GetCount() from here because it doesn't work - // if we're called from ctor (and GtkMenuShell is still NULL) index = m_clientList.GetCount() - 1; + } + else + { + gtk_menu_insert( GTK_MENU(menu), menu_item, pos ); + m_clientList.Insert( pos, (wxObject*) NULL ); + index = pos; + } } if (GTK_WIDGET_REALIZED(m_widget)) diff --git a/src/gtk/combobox.cpp b/src/gtk/combobox.cpp index 976e127c42..f002558d15 100644 --- a/src/gtk/combobox.cpp +++ b/src/gtk/combobox.cpp @@ -269,6 +269,93 @@ void wxComboBox::Append( const wxString &item, wxClientData *clientData ) AppendCommon( item ); } +void wxComboBox::InsertCommon( const wxString &item, int pos ) +{ + wxCHECK_RET(!(GetWindowStyle() & wxCB_SORT), wxT("can't insert into sorted list")); + wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); + + int count = GetCount(); + wxCHECK_RET((pos>=0) && (pos<=count), wxT("invalid index")); + if (pos == count) + { + AppendCommon(item); + return; + } + + DisableEvents(); + + GtkWidget *list = GTK_COMBO(m_widget)->list; + + GtkWidget *list_item = gtk_list_item_new_with_label( wxGTK_CONV( item ) ); + + GList *gitem_list = g_list_alloc (); + gitem_list->data = list_item; + gtk_list_insert_items( GTK_LIST (list), gitem_list, pos ); + + if (GTK_WIDGET_REALIZED(m_widget)) + { + gtk_widget_realize( list_item ); + gtk_widget_realize( GTK_BIN(list_item)->child ); + + if (m_widgetStyle) ApplyWidgetStyle(); + } + + gtk_widget_show( list_item ); + + EnableEvents(); +} + +void wxComboBox::Insert( const wxString &item, int pos ) +{ + wxCHECK_RET(!(GetWindowStyle() & wxCB_SORT), wxT("can't insert into sorted list")); + int count = GetCount(); + wxCHECK_RET((pos>=0) && (pos<=count), wxT("invalid index")); + if (pos == count) + { + Append(item); + return; + } + + m_clientDataList.Insert( pos, (wxObject*) NULL ); + m_clientObjectList.Insert( pos, (wxObject*) NULL ); + + InsertCommon( item, pos ); +} + +void wxComboBox::Insert( const wxString &item, int pos, void *clientData ) +{ + wxCHECK_RET(!(GetWindowStyle() & wxCB_SORT), wxT("can't insert into sorted list")); + int count = GetCount(); + wxCHECK_RET((pos>=0) && (pos<=count), wxT("invalid index")); + if (pos == count) + { + Append(item, clientData); + return; + } + + m_clientDataList.Insert( pos, (wxObject*) clientData ); + m_clientObjectList.Insert( pos, (wxObject*)NULL ); + + InsertCommon( item, pos ); +} + +void wxComboBox::Insert( const wxString &item, int pos, wxClientData *clientData ) +{ + wxCHECK_RET(!(GetWindowStyle() & wxCB_SORT), wxT("can't insert into sorted list")); + int count = GetCount(); + wxCHECK_RET((pos>=0) && (pos<=count), wxT("invalid index")); + if (pos == count) + { + Append(item, clientData); + return; + } + + m_clientDataList.Insert( pos, (wxObject*) NULL ); + m_clientObjectList.Insert( pos, (wxObject*) clientData ); + + InsertCommon( item, pos ); +} + void wxComboBox::SetClientData( int n, void* clientData ) { wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); diff --git a/src/gtk1/choice.cpp b/src/gtk1/choice.cpp index 4edcdc8b94..d746df7874 100644 --- a/src/gtk1/choice.cpp +++ b/src/gtk1/choice.cpp @@ -102,7 +102,7 @@ bool wxChoice::Create( wxWindow *parent, wxWindowID id, for (int i = 0; i < n; i++) { - GtkAppendHelper(menu, choices[i]); + GtkAddHelper(menu, i, choices[i]); } gtk_option_menu_set_menu( GTK_OPTION_MENU(m_widget), menu ); @@ -136,7 +136,20 @@ int wxChoice::DoAppend( const wxString &item ) GtkWidget *menu = gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ); - return GtkAppendHelper(menu, item); + return GtkAddHelper(menu, GetCount(), item); +} + +int wxChoice::DoInsert( const wxString &item, int pos ) +{ + wxCHECK_MSG( m_widget != NULL, -1, wxT("invalid choice control") ); + wxCHECK_MSG( (pos>=0) && (pos<=GetCount()), -1, wxT("invalid index")); + + if (pos == GetCount()) + return DoAppend(item); + + GtkWidget *menu = gtk_option_menu_get_menu( GTK_OPTION_MENU(m_widget) ); + + return GtkAddHelper(menu, pos, item); } void wxChoice::DoSetItemClientData( int n, void* clientData ) @@ -388,8 +401,10 @@ void wxChoice::ApplyWidgetStyle() } } -size_t wxChoice::GtkAppendHelper(GtkWidget *menu, const wxString& item) +int wxChoice::GtkAddHelper(GtkWidget *menu, int pos, const wxString& item) { + wxCHECK_MSG((pos>=0) && (pos<=(int)m_clientList.GetCount()), -1, wxT("invalid index")); + GtkWidget *menu_item = gtk_menu_item_new_with_label( wxGTK_CONV( item ) ); size_t index; @@ -412,14 +427,22 @@ size_t wxChoice::GtkAppendHelper(GtkWidget *menu, const wxString& item) } else { + // don't call wxChoice::GetCount() from here because it doesn't work + // if we're called from ctor (and GtkMenuShell is still NULL) + // normal control, just append + if (pos == (int)m_clientList.GetCount()) + { gtk_menu_append( GTK_MENU(menu), menu_item ); - m_clientList.Append( (wxObject*) NULL ); - - // don't call wxChoice::GetCount() from here because it doesn't work - // if we're called from ctor (and GtkMenuShell is still NULL) index = m_clientList.GetCount() - 1; + } + else + { + gtk_menu_insert( GTK_MENU(menu), menu_item, pos ); + m_clientList.Insert( pos, (wxObject*) NULL ); + index = pos; + } } if (GTK_WIDGET_REALIZED(m_widget)) diff --git a/src/gtk1/combobox.cpp b/src/gtk1/combobox.cpp index 976e127c42..f002558d15 100644 --- a/src/gtk1/combobox.cpp +++ b/src/gtk1/combobox.cpp @@ -269,6 +269,93 @@ void wxComboBox::Append( const wxString &item, wxClientData *clientData ) AppendCommon( item ); } +void wxComboBox::InsertCommon( const wxString &item, int pos ) +{ + wxCHECK_RET(!(GetWindowStyle() & wxCB_SORT), wxT("can't insert into sorted list")); + wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); + + int count = GetCount(); + wxCHECK_RET((pos>=0) && (pos<=count), wxT("invalid index")); + if (pos == count) + { + AppendCommon(item); + return; + } + + DisableEvents(); + + GtkWidget *list = GTK_COMBO(m_widget)->list; + + GtkWidget *list_item = gtk_list_item_new_with_label( wxGTK_CONV( item ) ); + + GList *gitem_list = g_list_alloc (); + gitem_list->data = list_item; + gtk_list_insert_items( GTK_LIST (list), gitem_list, pos ); + + if (GTK_WIDGET_REALIZED(m_widget)) + { + gtk_widget_realize( list_item ); + gtk_widget_realize( GTK_BIN(list_item)->child ); + + if (m_widgetStyle) ApplyWidgetStyle(); + } + + gtk_widget_show( list_item ); + + EnableEvents(); +} + +void wxComboBox::Insert( const wxString &item, int pos ) +{ + wxCHECK_RET(!(GetWindowStyle() & wxCB_SORT), wxT("can't insert into sorted list")); + int count = GetCount(); + wxCHECK_RET((pos>=0) && (pos<=count), wxT("invalid index")); + if (pos == count) + { + Append(item); + return; + } + + m_clientDataList.Insert( pos, (wxObject*) NULL ); + m_clientObjectList.Insert( pos, (wxObject*) NULL ); + + InsertCommon( item, pos ); +} + +void wxComboBox::Insert( const wxString &item, int pos, void *clientData ) +{ + wxCHECK_RET(!(GetWindowStyle() & wxCB_SORT), wxT("can't insert into sorted list")); + int count = GetCount(); + wxCHECK_RET((pos>=0) && (pos<=count), wxT("invalid index")); + if (pos == count) + { + Append(item, clientData); + return; + } + + m_clientDataList.Insert( pos, (wxObject*) clientData ); + m_clientObjectList.Insert( pos, (wxObject*)NULL ); + + InsertCommon( item, pos ); +} + +void wxComboBox::Insert( const wxString &item, int pos, wxClientData *clientData ) +{ + wxCHECK_RET(!(GetWindowStyle() & wxCB_SORT), wxT("can't insert into sorted list")); + int count = GetCount(); + wxCHECK_RET((pos>=0) && (pos<=count), wxT("invalid index")); + if (pos == count) + { + Append(item, clientData); + return; + } + + m_clientDataList.Insert( pos, (wxObject*) NULL ); + m_clientObjectList.Insert( pos, (wxObject*) clientData ); + + InsertCommon( item, pos ); +} + void wxComboBox::SetClientData( int n, void* clientData ) { wxCHECK_RET( m_widget != NULL, wxT("invalid combobox") ); diff --git a/src/mac/carbon/choice.cpp b/src/mac/carbon/choice.cpp index 37f432cff0..bc77b99ad8 100644 --- a/src/mac/carbon/choice.cpp +++ b/src/mac/carbon/choice.cpp @@ -81,6 +81,22 @@ int wxChoice::DoAppend(const wxString& item) return index ; } +int wxChoice::DoInsert(const wxString& item, int pos) +{ + wxCHECK_MSG(!(GetWindowStyle() & wxCB_SORT), -1, wxT("can't insert into sorted list")); + wxCHECK_MSG((pos>=0) && (pos<=GetCount()), -1, wxT("invalid index")); + + if (pos == GetCount()) + return DoAppend(item); + + UMAAppendMenuItem(MAC_WXHMENU( m_macPopUpMenuHandle ) , item); + m_strings.Insert( item, pos ) ; + m_datas.Insert( NULL, pos ) ; + DoSetItemClientData( pos , NULL ) ; + SetControl32BitMaximum( (ControlHandle) m_macControl , pos) ; + return pos ; +} + void wxChoice::Delete(int n) { wxCHECK_RET( n < GetCount(), wxT("invalid item index in wxChoice::Delete") ); diff --git a/src/mac/choice.cpp b/src/mac/choice.cpp index 37f432cff0..bc77b99ad8 100644 --- a/src/mac/choice.cpp +++ b/src/mac/choice.cpp @@ -81,6 +81,22 @@ int wxChoice::DoAppend(const wxString& item) return index ; } +int wxChoice::DoInsert(const wxString& item, int pos) +{ + wxCHECK_MSG(!(GetWindowStyle() & wxCB_SORT), -1, wxT("can't insert into sorted list")); + wxCHECK_MSG((pos>=0) && (pos<=GetCount()), -1, wxT("invalid index")); + + if (pos == GetCount()) + return DoAppend(item); + + UMAAppendMenuItem(MAC_WXHMENU( m_macPopUpMenuHandle ) , item); + m_strings.Insert( item, pos ) ; + m_datas.Insert( NULL, pos ) ; + DoSetItemClientData( pos , NULL ) ; + SetControl32BitMaximum( (ControlHandle) m_macControl , pos) ; + return pos ; +} + void wxChoice::Delete(int n) { wxCHECK_RET( n < GetCount(), wxT("invalid item index in wxChoice::Delete") ); diff --git a/src/motif/choice.cpp b/src/motif/choice.cpp index d8f2533de4..466b14e7e5 100644 --- a/src/motif/choice.cpp +++ b/src/motif/choice.cpp @@ -209,6 +209,14 @@ int wxChoice::DoAppend(const wxString& item) return GetCount() - 1; } +int wxChoice::DoInsert(const wxString& item, int pos) +{ + wxCHECK_MSG(FALSE, -1, wxT("insert not implemented")); + +// wxCHECK_MSG((pos>=0) && (pos<=GetCount()), -1, wxT("invalid index")); +// if (pos == GetCount()) return DoAppend(item); +} + void wxChoice::Delete(int n) { Widget w = (Widget)m_widgetArray[n]; diff --git a/src/motif/combobox.cpp b/src/motif/combobox.cpp index ef51038de6..61dce03a2b 100644 --- a/src/motif/combobox.cpp +++ b/src/motif/combobox.cpp @@ -145,6 +145,22 @@ int wxComboBox::DoAppend(const wxString& item) return GetCount() - 1; } +int wxComboBox::DoInsert(const wxString& item, int pos) +{ + wxCHECK_MSG(!(GetWindowStyle() & wxCB_SORT), -1, wxT("can't insert into sorted list")); + wxCHECK_MSG((pos>=0) && (pos<=GetCount()), -1, wxT("invalid index")); + + if (pos == GetCount()) + return DoAppend(item); + + wxXmString str( item.c_str() ); + XmComboBoxAddItem((Widget) m_mainWidget, str(), pos+1); + m_stringList.Insert(pos, item); + m_noStrings ++; + + return pos; +} + void wxComboBox::Delete(int n) { XmComboBoxDeletePos((Widget) m_mainWidget, n+1); diff --git a/src/motif/combobox_native.cpp b/src/motif/combobox_native.cpp index 95294fe5f8..9e1eec75f9 100644 --- a/src/motif/combobox_native.cpp +++ b/src/motif/combobox_native.cpp @@ -197,6 +197,22 @@ int wxComboBox::DoAppend(const wxString& item) return GetCount() - 1; } +int wxComboBox::DoInsert(const wxString& item, int pos) +{ + wxCHECK_MSG(!(GetWindowStyle() & wxCB_SORT), -1, wxT("can't insert into sorted list")); + wxCHECK_MSG((pos>=0) && (pos<=GetCount()), -1, wxT("invalid index")); + + if (pos == GetCount()) + return DoAppend(item); + + wxXmString str( item.c_str() ); + XmComboBoxAddItem((Widget) m_mainWidget, str(), pos+1, False); + m_noStrings ++; + AdjustDropDownListSize(); + + return GetCount() - 1; +} + void wxComboBox::Delete(int n) { #ifdef LESSTIF_VERSION diff --git a/src/msw/choice.cpp b/src/msw/choice.cpp index f8620938f2..f92b66426a 100644 --- a/src/msw/choice.cpp +++ b/src/msw/choice.cpp @@ -115,6 +115,20 @@ int wxChoice::DoAppend(const wxString& item) return n; } +int wxChoice::DoInsert(const wxString& item, int pos) +{ + wxCHECK_MSG(!(GetWindowStyle() & wxCB_SORT), -1, wxT("can't insert into sorted list")); + wxCHECK_MSG((pos>=0) && (pos<=GetCount()), -1, wxT("invalid index")); + + int n = (int)SendMessage(GetHwnd(), CB_INSERTSTRING, pos, (LONG)item.c_str()); + if ( n == CB_ERR ) + { + wxLogLastError(wxT("SendMessage(CB_INSERTSTRING)")); + } + + return n; +} + void wxChoice::Delete(int n) { wxCHECK_RET( n < GetCount(), wxT("invalid item index in wxChoice::Delete") ); diff --git a/src/os2/choice.cpp b/src/os2/choice.cpp index 5ae8def9d9..c989624c46 100644 --- a/src/os2/choice.cpp +++ b/src/os2/choice.cpp @@ -111,6 +111,32 @@ int wxChoice::DoAppend( return nIndex; } // end of wxChoice::DoAppend +int wxChoice::DoInsert( + const wxString& rsItem + int pos +) +{ + wxCHECK_MSG(!(GetWindowStyle() & wxCB_SORT), -1, wxT("can't insert into sorted list")); + wxCHECK_MSG((pos>=0) && (pos<=GetCount()), -1, wxT("invalid index")); + + if (pos == GetCount()) + return DoAppend(item); + + int nIndex; + SHORT nIndexType = 0; + + if (m_windowStyle & wxLB_SORT) + nIndexType = LIT_SORTASCENDING; + else + nIndexType = pos; + nIndex = (int)::WinSendMsg( GetHwnd() + ,LM_INSERTITEM + ,(MPARAM)nIndexType + ,(MPARAM)rsItem.c_str() + ); + return nIndex; +} // end of wxChoice::DoInsert + void wxChoice::Delete( int n ) diff --git a/src/univ/combobox.cpp b/src/univ/combobox.cpp index 554986b033..4248176caa 100644 --- a/src/univ/combobox.cpp +++ b/src/univ/combobox.cpp @@ -825,6 +825,18 @@ int wxComboBox::DoAppend(const wxString& item) return GetLBox()->Append(item); } +int wxComboBox::DoInsert(const wxString& item, int pos) +{ + wxCHECK_MSG(!(GetWindowStyle() & wxCB_SORT), -1, wxT("can't insert into sorted list")); + wxCHECK_MSG((pos>=0) && (pos<=GetCount()), -1, wxT("invalid index")); + + if (pos == GetCount()) + return DoAppend(item); + + GetLBox()->Insert(1, &item, pos); + return pos; +} + void wxComboBox::DoSetItemClientData(int n, void* clientData) { GetLBox()->SetClientData(n, clientData); -- 2.45.2