fixed sending duplicate EVT_COMBOBOX events; documented that GetValue() returns new value when called from EVT_COMBOBOX handler

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@29147 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2004-09-15 23:07:15 +00:00
parent 35960bbf22
commit 13bcc34881
4 changed files with 43 additions and 17 deletions

View File

@@ -273,6 +273,7 @@ wxMSW:
- fixed truncation of transferred data in wxConnection under unicode build - fixed truncation of transferred data in wxConnection under unicode build
- wxChoice and wxComboBox dropdown background can be set now too (Adrian Lupei) - wxChoice and wxComboBox dropdown background can be set now too (Adrian Lupei)
- fixed wxMaximizeEvent generation in wxFrame - fixed wxMaximizeEvent generation in wxFrame
- don't send duplicate EVT_COMBOBOX events whenever selection changes any more
wxUniv/X11: wxUniv/X11:

View File

@@ -36,7 +36,8 @@ See also \helpref{window styles overview}{windowstyles}.
\twocolwidtha{7cm} \twocolwidtha{7cm}
\begin{twocollist}\itemsep=0pt \begin{twocollist}\itemsep=0pt
\twocolitem{{\bf EVT\_COMBOBOX(id, func)}}{Process a wxEVT\_COMMAND\_COMBOBOX\_SELECTED event, \twocolitem{{\bf EVT\_COMBOBOX(id, func)}}{Process a wxEVT\_COMMAND\_COMBOBOX\_SELECTED event,
when an item on the list is selected.} when an item on the list is selected. Note that calling
\helpref{GetValue}{wxcomboboxgetvalue} returns the new value of selection.}
\twocolitem{{\bf EVT\_TEXT(id, func)}}{Process a wxEVT\_COMMAND\_TEXT\_UPDATED event, \twocolitem{{\bf EVT\_TEXT(id, func)}}{Process a wxEVT\_COMMAND\_TEXT\_UPDATED event,
when the combobox text changes.} when the combobox text changes.}
\end{twocollist} \end{twocollist}

View File

@@ -27,7 +27,7 @@
class WXDLLEXPORT wxComboBox: public wxChoice class WXDLLEXPORT wxComboBox: public wxChoice
{ {
public: public:
wxComboBox() { } wxComboBox() { Init(); }
wxComboBox(wxWindow *parent, wxWindowID id, wxComboBox(wxWindow *parent, wxWindowID id,
const wxString& value = wxEmptyString, const wxString& value = wxEmptyString,
@@ -38,6 +38,8 @@ public:
const wxValidator& validator = wxDefaultValidator, const wxValidator& validator = wxDefaultValidator,
const wxString& name = wxComboBoxNameStr) const wxString& name = wxComboBoxNameStr)
{ {
Init();
Create(parent, id, value, pos, size, n, choices, style, validator, name); Create(parent, id, value, pos, size, n, choices, style, validator, name);
} }
wxComboBox(wxWindow *parent, wxWindowID id, wxComboBox(wxWindow *parent, wxWindowID id,
@@ -49,6 +51,8 @@ public:
const wxValidator& validator = wxDefaultValidator, const wxValidator& validator = wxDefaultValidator,
const wxString& name = wxComboBoxNameStr) const wxString& name = wxComboBoxNameStr)
{ {
Init();
Create(parent, id, value, pos, size, choices, style, validator, name); Create(parent, id, value, pos, size, choices, style, validator, name);
} }
@@ -75,7 +79,7 @@ public:
// List functions: see wxChoice // List functions: see wxChoice
// Text field functions // Text field functions
wxString GetValue() const { return GetLabel(); } wxString GetValue() const { return m_value; }
virtual void SetValue(const wxString& value); virtual void SetValue(const wxString& value);
// Clipboard operations // Clipboard operations
@@ -105,6 +109,16 @@ public:
protected: protected:
virtual WXDWORD MSWGetStyle(long style, WXDWORD *exstyle) const; virtual WXDWORD MSWGetStyle(long style, WXDWORD *exstyle) const;
// common part of all ctors
void Init() { m_selectionOld = -1; }
// the previous selection (see MSWCommand() to see why it is needed)
int m_selectionOld;
// the current selection (also see MSWCommand())
wxString m_value;
private: private:
DECLARE_DYNAMIC_CLASS_NO_COPY(wxComboBox) DECLARE_DYNAMIC_CLASS_NO_COPY(wxComboBox)
}; };

View File

@@ -301,18 +301,30 @@ bool wxComboBox::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
{ {
case CBN_SELCHANGE: case CBN_SELCHANGE:
sel = GetSelection(); sel = GetSelection();
if ( sel > -1 )
// somehow we get 2 CBN_SELCHANGE events with the same index when
// the user selects an item in the combobox -- ignore duplicates
if ( sel > -1 && sel != m_selectionOld )
{ {
value = GetString(sel); m_selectionOld = sel;
// GetValue() would still return the old value from here but
// according to the docs we should return the new value if the
// user calls it in his event handler, so update internal
// m_value
m_value = GetString(sel);
wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, GetId()); wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, GetId());
event.SetInt(sel); event.SetInt(sel);
event.SetEventObject(this); event.SetEventObject(this);
event.SetString(value); event.SetString(m_value);
ProcessCommand(event); ProcessCommand(event);
} }
else else // no valid selection
{ {
m_selectionOld = sel;
// hence no EVT_TEXT neither
break; break;
} }
@@ -329,25 +341,21 @@ bool wxComboBox::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
// want the new one) // want the new one)
if ( sel == -1 ) if ( sel == -1 )
{ {
value = GetValue(); m_value = GetValue();
} }
else // we're synthesizing text updated event from sel change else // we're synthesizing text updated event from sel change
{ {
// We need to retrieve the current selection because the user // We need to retrieve the current selection because the
// may have changed it in the previous handler (for CBN_SELCHANGE // user may have changed it in the previous handler (for
// above). // CBN_SELCHANGE above).
sel = GetSelection(); sel = GetSelection();
if ( sel > -1 ) if ( sel > -1 )
{ {
value = GetString(sel); m_value = GetString(sel);
} }
// we need to do this because the user code expects
// wxComboBox::GetValue() to return the new value from
// "text updated" handler but it hadn't been updated yet
SetValue(value);
} }
event.SetString(value); event.SetString(m_value);
event.SetEventObject(this); event.SetEventObject(this);
ProcessCommand(event); ProcessCommand(event);
} }
@@ -472,6 +480,8 @@ void wxComboBox::SetValue(const wxString& value)
SetStringSelection(value); SetStringSelection(value);
else else
SetWindowText(GetHwnd(), value.c_str()); SetWindowText(GetHwnd(), value.c_str());
m_selectionOld = GetSelection();
} }
// Clipboard operations // Clipboard operations