1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/lboxcmn.cpp
3 // Purpose: wxListBox class methods common to all platforms
4 // Author: Vadim Zeitlin
8 // Copyright: (c) wxWidgets team
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
29 #include "wx/listbox.h"
32 #include "wx/dynarray.h"
33 #include "wx/arrstr.h"
37 extern WXDLLEXPORT_DATA(const char) wxListBoxNameStr
[] = "listBox";
39 // ============================================================================
41 // ============================================================================
43 wxListBoxBase::~wxListBoxBase()
45 // this destructor is required for Darwin
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
52 wxDEFINE_FLAGS( wxListBoxStyle
)
53 wxBEGIN_FLAGS( wxListBoxStyle
)
54 // new style border flags, we put them first to
55 // use them for streaming out
56 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
57 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
58 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
59 wxFLAGS_MEMBER(wxBORDER_RAISED
)
60 wxFLAGS_MEMBER(wxBORDER_STATIC
)
61 wxFLAGS_MEMBER(wxBORDER_NONE
)
63 // old style border flags
64 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
65 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
66 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
67 wxFLAGS_MEMBER(wxRAISED_BORDER
)
68 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
69 wxFLAGS_MEMBER(wxBORDER
)
71 // standard window styles
72 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
73 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
74 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
75 wxFLAGS_MEMBER(wxWANTS_CHARS
)
76 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
77 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
78 wxFLAGS_MEMBER(wxVSCROLL
)
79 wxFLAGS_MEMBER(wxHSCROLL
)
81 wxFLAGS_MEMBER(wxLB_SINGLE
)
82 wxFLAGS_MEMBER(wxLB_MULTIPLE
)
83 wxFLAGS_MEMBER(wxLB_EXTENDED
)
84 wxFLAGS_MEMBER(wxLB_HSCROLL
)
85 wxFLAGS_MEMBER(wxLB_ALWAYS_SB
)
86 wxFLAGS_MEMBER(wxLB_NEEDED_SB
)
87 wxFLAGS_MEMBER(wxLB_SORT
)
88 wxEND_FLAGS( wxListBoxStyle
)
90 wxIMPLEMENT_DYNAMIC_CLASS_XTI(wxListBox
, wxControl
, "wx/listbox.h")
92 wxBEGIN_PROPERTIES_TABLE(wxListBox
)
93 wxEVENT_PROPERTY( Select
, wxEVT_COMMAND_LISTBOX_SELECTED
, wxCommandEvent
)
94 wxEVENT_PROPERTY( DoubleClick
, wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
, wxCommandEvent
)
96 wxPROPERTY( Font
, wxFont
, SetFont
, GetFont
, wxEMPTY_PARAMETER_VALUE
, 0 /*flags*/, \
97 wxT("Helpstring"), wxT("group"))
98 wxPROPERTY_COLLECTION( Choices
, wxArrayString
, wxString
, AppendString
, \
99 GetStrings
, 0 /*flags*/, wxT("Helpstring"), wxT("group") )
100 wxPROPERTY( Selection
, int, SetSelection
, GetSelection
, wxEMPTY_PARAMETER_VALUE
, \
101 0 /*flags*/, wxT("Helpstring"), wxT("group") )
103 wxPROPERTY_FLAGS( WindowStyle
, wxListBoxStyle
, long, SetWindowStyleFlag
, \
104 GetWindowStyleFlag
, wxEMPTY_PARAMETER_VALUE
, 0 /*flags*/, \
105 wxT("Helpstring"), wxT("group")) // style
106 wxEND_PROPERTIES_TABLE()
108 wxEMPTY_HANDLERS_TABLE(wxListBox
)
110 wxCONSTRUCTOR_4( wxListBox
, wxWindow
*, Parent
, wxWindowID
, Id
, \
111 wxPoint
, Position
, wxSize
, Size
)
120 // ----------------------------------------------------------------------------
122 // ----------------------------------------------------------------------------
124 bool wxListBoxBase::SetStringSelection(const wxString
& s
, bool select
)
126 const int sel
= FindString(s
);
127 if ( sel
== wxNOT_FOUND
)
130 SetSelection(sel
, select
);
135 void wxListBoxBase::SetSelection(int n
)
137 if ( !HasMultipleSelection() )
138 DoChangeSingleSelection(n
);
140 DoSetSelection(n
, true);
143 void wxListBoxBase::DeselectAll(int itemToLeaveSelected
)
145 if ( HasMultipleSelection() )
147 wxArrayInt selections
;
148 GetSelections(selections
);
150 size_t count
= selections
.GetCount();
151 for ( size_t n
= 0; n
< count
; n
++ )
153 int item
= selections
[n
];
154 if ( item
!= itemToLeaveSelected
)
158 else // single selection
160 int sel
= GetSelection();
161 if ( sel
!= wxNOT_FOUND
&& sel
!= itemToLeaveSelected
)
168 void wxListBoxBase::UpdateOldSelections()
170 // When the control becomes empty, any previously remembered selections are
171 // invalid anyhow, so just forget them.
174 m_oldSelections
.clear();
178 // We need to remember the selection even in single-selection case on
179 // Windows, so that we don't send an event when the user clicks on an
180 // already selected item.
182 if (HasFlag(wxLB_MULTIPLE
) || HasFlag(wxLB_EXTENDED
))
185 GetSelections( m_oldSelections
);
189 bool wxListBoxBase::SendEvent(wxEventType evtType
, int item
, bool selected
)
191 wxCommandEvent
event(evtType
, GetId());
192 event
.SetEventObject(this);
195 event
.SetString(GetString(item
));
196 event
.SetExtraLong(selected
);
198 if ( HasClientObjectData() )
199 event
.SetClientObject(GetClientObject(item
));
200 else if ( HasClientUntypedData() )
201 event
.SetClientData(GetClientData(item
));
203 return HandleWindowEvent(event
);
206 bool wxListBoxBase::DoChangeSingleSelection(int item
)
208 // As we don't use m_oldSelections in single selection mode, we store the
209 // last item that we notified the user about in it in this case because we
210 // need to remember it to be able to filter out the dummy selection changes
211 // that we get when the user clicks on an already selected item.
212 if ( !m_oldSelections
.empty() && *m_oldSelections
.begin() == item
)
214 // Same item as the last time.
218 m_oldSelections
.clear();
219 m_oldSelections
.push_back(item
);
224 bool wxListBoxBase::CalcAndSendEvent()
226 wxArrayInt selections
;
227 GetSelections(selections
);
228 bool selected
= true;
230 if ( selections
.empty() && m_oldSelections
.empty() )
232 // nothing changed, just leave
236 const size_t countSel
= selections
.size(),
237 countSelOld
= m_oldSelections
.size();
238 if ( countSel
== countSelOld
)
240 bool changed
= false;
241 for ( size_t idx
= 0; idx
< countSel
; idx
++ )
243 if (selections
[idx
] != m_oldSelections
[idx
])
250 // nothing changed, just leave
255 int item
= wxNOT_FOUND
;
256 if ( selections
.empty() )
259 item
= m_oldSelections
[0];
261 else // we [still] have some selections
263 // Now test if any new item is selected
264 bool any_new_selected
= false;
265 for ( size_t idx
= 0; idx
< countSel
; idx
++ )
267 item
= selections
[idx
];
268 if ( m_oldSelections
.Index(item
) == wxNOT_FOUND
)
270 any_new_selected
= true;
275 if ( !any_new_selected
)
277 // No new items selected, now test if any new item is deselected
278 bool any_new_deselected
= false;
279 for ( size_t idx
= 0; idx
< countSelOld
; idx
++ )
281 item
= m_oldSelections
[idx
];
282 if ( selections
.Index(item
) == wxNOT_FOUND
)
284 any_new_deselected
= true;
289 if ( any_new_deselected
)
291 // indicate that this is a selection
296 item
= wxNOT_FOUND
; // this should be impossible
301 wxASSERT_MSG( item
!= wxNOT_FOUND
,
302 "Logic error in wxListBox selection event generation code" );
304 m_oldSelections
= selections
;
306 return SendEvent(wxEVT_COMMAND_LISTBOX_SELECTED
, item
, selected
);
309 // ----------------------------------------------------------------------------
311 // ----------------------------------------------------------------------------
313 void wxListBoxBase::Command(wxCommandEvent
& event
)
315 SetSelection(event
.GetInt(), event
.GetExtraLong() != 0);
316 (void)GetEventHandler()->ProcessEvent(event
);
319 // ----------------------------------------------------------------------------
320 // SetFirstItem() and such
321 // ----------------------------------------------------------------------------
323 void wxListBoxBase::SetFirstItem(const wxString
& s
)
325 int n
= FindString(s
);
327 wxCHECK_RET( n
!= wxNOT_FOUND
, wxT("invalid string in wxListBox::SetFirstItem") );
332 void wxListBoxBase::AppendAndEnsureVisible(const wxString
& s
)
335 EnsureVisible(GetCount() - 1);
338 void wxListBoxBase::EnsureVisible(int WXUNUSED(n
))
340 // the base class version does nothing (the only alternative would be to
341 // call SetFirstItem() but this is probably even more stupid)
344 #endif // wxUSE_LISTBOX