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"
38 // ============================================================================
40 // ============================================================================
42 wxListBoxBase::~wxListBoxBase()
44 // this destructor is required for Darwin
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
51 wxDEFINE_FLAGS( wxListBoxStyle
)
52 wxBEGIN_FLAGS( wxListBoxStyle
)
53 // new style border flags, we put them first to
54 // use them for streaming out
55 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
56 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
57 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
58 wxFLAGS_MEMBER(wxBORDER_RAISED
)
59 wxFLAGS_MEMBER(wxBORDER_STATIC
)
60 wxFLAGS_MEMBER(wxBORDER_NONE
)
62 // old style border flags
63 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
64 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
65 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
66 wxFLAGS_MEMBER(wxRAISED_BORDER
)
67 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
68 wxFLAGS_MEMBER(wxBORDER
)
70 // standard window styles
71 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
72 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
73 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
74 wxFLAGS_MEMBER(wxWANTS_CHARS
)
75 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
76 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
77 wxFLAGS_MEMBER(wxVSCROLL
)
78 wxFLAGS_MEMBER(wxHSCROLL
)
80 wxFLAGS_MEMBER(wxLB_SINGLE
)
81 wxFLAGS_MEMBER(wxLB_MULTIPLE
)
82 wxFLAGS_MEMBER(wxLB_EXTENDED
)
83 wxFLAGS_MEMBER(wxLB_HSCROLL
)
84 wxFLAGS_MEMBER(wxLB_ALWAYS_SB
)
85 wxFLAGS_MEMBER(wxLB_NEEDED_SB
)
86 wxFLAGS_MEMBER(wxLB_SORT
)
87 wxEND_FLAGS( wxListBoxStyle
)
89 wxIMPLEMENT_DYNAMIC_CLASS_XTI(wxListBox
, wxControl
, "wx/listbox.h")
91 wxBEGIN_PROPERTIES_TABLE(wxListBox
)
92 wxEVENT_PROPERTY( Select
, wxEVT_COMMAND_LISTBOX_SELECTED
, wxCommandEvent
)
93 wxEVENT_PROPERTY( DoubleClick
, wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
, wxCommandEvent
)
95 wxPROPERTY( Font
, wxFont
, SetFont
, GetFont
, wxEMPTY_PARAMETER_VALUE
, 0 /*flags*/, \
96 wxT("Helpstring"), wxT("group"))
97 wxPROPERTY_COLLECTION( Choices
, wxArrayString
, wxString
, AppendString
, \
98 GetStrings
, 0 /*flags*/, wxT("Helpstring"), wxT("group") )
99 wxPROPERTY( Selection
, int, SetSelection
, GetSelection
, wxEMPTY_PARAMETER_VALUE
, \
100 0 /*flags*/, wxT("Helpstring"), wxT("group") )
102 wxPROPERTY_FLAGS( WindowStyle
, wxListBoxStyle
, long, SetWindowStyleFlag
, \
103 GetWindowStyleFlag
, wxEMPTY_PARAMETER_VALUE
, 0 /*flags*/, \
104 wxT("Helpstring"), wxT("group")) // style
105 wxEND_PROPERTIES_TABLE()
107 wxEMPTY_HANDLERS_TABLE(wxListBox
)
109 wxCONSTRUCTOR_4( wxListBox
, wxWindow
*, Parent
, wxWindowID
, Id
, \
110 wxPoint
, Position
, wxSize
, Size
)
119 // ----------------------------------------------------------------------------
121 // ----------------------------------------------------------------------------
123 bool wxListBoxBase::SetStringSelection(const wxString
& s
, bool select
)
125 const int sel
= FindString(s
);
126 if ( sel
== wxNOT_FOUND
)
129 SetSelection(sel
, select
);
134 void wxListBoxBase::SetSelection(int n
)
136 if ( !HasMultipleSelection() )
137 DoChangeSingleSelection(n
);
139 DoSetSelection(n
, true);
142 void wxListBoxBase::DeselectAll(int itemToLeaveSelected
)
144 if ( HasMultipleSelection() )
146 wxArrayInt selections
;
147 GetSelections(selections
);
149 size_t count
= selections
.GetCount();
150 for ( size_t n
= 0; n
< count
; n
++ )
152 int item
= selections
[n
];
153 if ( item
!= itemToLeaveSelected
)
157 else // single selection
159 int sel
= GetSelection();
160 if ( sel
!= wxNOT_FOUND
&& sel
!= itemToLeaveSelected
)
167 void wxListBoxBase::UpdateOldSelections()
169 // We need to remember the selection even in single-selection case on
170 // Windows, so that we don't send an event when the user clicks on an
171 // already selected item.
173 if (HasFlag(wxLB_MULTIPLE
) || HasFlag(wxLB_EXTENDED
))
176 GetSelections( m_oldSelections
);
180 bool wxListBoxBase::SendEvent(wxEventType evtType
, int item
, bool selected
)
182 wxCommandEvent
event(evtType
, GetId());
183 event
.SetEventObject(this);
186 event
.SetString(GetString(item
));
187 event
.SetExtraLong(selected
);
189 if ( HasClientObjectData() )
190 event
.SetClientObject(GetClientObject(item
));
191 else if ( HasClientUntypedData() )
192 event
.SetClientData(GetClientData(item
));
194 return HandleWindowEvent(event
);
197 bool wxListBoxBase::DoChangeSingleSelection(int item
)
199 // As we don't use m_oldSelections in single selection mode, we store the
200 // last item that we notified the user about in it in this case because we
201 // need to remember it to be able to filter out the dummy selection changes
202 // that we get when the user clicks on an already selected item.
203 if ( !m_oldSelections
.empty() && *m_oldSelections
.begin() == item
)
205 // Same item as the last time.
209 m_oldSelections
.clear();
210 m_oldSelections
.push_back(item
);
215 bool wxListBoxBase::CalcAndSendEvent()
217 wxArrayInt selections
;
218 GetSelections(selections
);
219 bool selected
= true;
221 if ( selections
.empty() && m_oldSelections
.empty() )
223 // nothing changed, just leave
227 const size_t countSel
= selections
.size(),
228 countSelOld
= m_oldSelections
.size();
229 if ( countSel
== countSelOld
)
231 bool changed
= false;
232 for ( size_t idx
= 0; idx
< countSel
; idx
++ )
234 if (selections
[idx
] != m_oldSelections
[idx
])
241 // nothing changed, just leave
246 int item
= wxNOT_FOUND
;
247 if ( selections
.empty() )
250 item
= m_oldSelections
[0];
252 else // we [still] have some selections
254 // Now test if any new item is selected
255 bool any_new_selected
= false;
256 for ( size_t idx
= 0; idx
< countSel
; idx
++ )
258 item
= selections
[idx
];
259 if ( m_oldSelections
.Index(item
) == wxNOT_FOUND
)
261 any_new_selected
= true;
266 if ( !any_new_selected
)
268 // No new items selected, now test if any new item is deselected
269 bool any_new_deselected
= false;
270 for ( size_t idx
= 0; idx
< countSelOld
; idx
++ )
272 item
= m_oldSelections
[idx
];
273 if ( selections
.Index(item
) == wxNOT_FOUND
)
275 any_new_deselected
= true;
280 if ( any_new_deselected
)
282 // indicate that this is a selection
287 item
= wxNOT_FOUND
; // this should be impossible
292 wxASSERT_MSG( item
!= wxNOT_FOUND
,
293 "Logic error in wxListBox selection event generation code" );
295 m_oldSelections
= selections
;
297 return SendEvent(wxEVT_COMMAND_LISTBOX_SELECTED
, item
, selected
);
300 // ----------------------------------------------------------------------------
302 // ----------------------------------------------------------------------------
304 void wxListBoxBase::Command(wxCommandEvent
& event
)
306 SetSelection(event
.GetInt(), event
.GetExtraLong() != 0);
307 (void)GetEventHandler()->ProcessEvent(event
);
310 // ----------------------------------------------------------------------------
311 // SetFirstItem() and such
312 // ----------------------------------------------------------------------------
314 void wxListBoxBase::SetFirstItem(const wxString
& s
)
316 int n
= FindString(s
);
318 wxCHECK_RET( n
!= wxNOT_FOUND
, wxT("invalid string in wxListBox::SetFirstItem") );
323 void wxListBoxBase::AppendAndEnsureVisible(const wxString
& s
)
326 EnsureVisible(GetCount() - 1);
329 void wxListBoxBase::EnsureVisible(int WXUNUSED(n
))
331 // the base class version does nothing (the only alternative would be to
332 // call SetFirstItem() but this is probably even more stupid)
335 #endif // wxUSE_LISTBOX