]>
Commit | Line | Data |
---|---|---|
a340b80d | 1 | ///////////////////////////////////////////////////////////////////////////// |
85fed18c | 2 | // Name: src/generic/odcombo.cpp |
a340b80d VZ |
3 | // Purpose: wxOwnerDrawnComboBox, wxVListBoxComboPopup |
4 | // Author: Jaakko Salli | |
5 | // Modified by: | |
6 | // Created: Apr-30-2006 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 2005 Jaakko Salli | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | #include "wx/wxprec.h" | |
21 | ||
22 | #ifdef __BORLANDC__ | |
23 | #pragma hdrstop | |
24 | #endif | |
25 | ||
a57d600f | 26 | #if wxUSE_ODCOMBOBOX |
a340b80d | 27 | |
85fed18c WS |
28 | #include "wx/odcombo.h" |
29 | ||
a340b80d VZ |
30 | #ifndef WX_PRECOMP |
31 | #include "wx/log.h" | |
32 | #include "wx/combobox.h" | |
33 | #include "wx/dcclient.h" | |
34 | #include "wx/settings.h" | |
35 | #include "wx/dialog.h" | |
0ec1179b | 36 | #include "wx/textctrl.h" |
a340b80d VZ |
37 | #endif |
38 | ||
39 | #include "wx/combo.h" | |
a340b80d VZ |
40 | |
41 | // ============================================================================ | |
42 | // implementation | |
43 | // ============================================================================ | |
44 | ||
dc8a1aa5 AB |
45 | // time in milliseconds before partial completion buffer drops |
46 | #define wxODCB_PARTIAL_COMPLETION_TIME 1000 | |
a340b80d VZ |
47 | |
48 | // ---------------------------------------------------------------------------- | |
49 | // wxVListBoxComboPopup is a wxVListBox customized to act as a popup control | |
50 | // | |
51 | // ---------------------------------------------------------------------------- | |
52 | ||
53 | ||
54 | BEGIN_EVENT_TABLE(wxVListBoxComboPopup, wxVListBox) | |
55 | EVT_MOTION(wxVListBoxComboPopup::OnMouseMove) | |
56 | EVT_KEY_DOWN(wxVListBoxComboPopup::OnKey) | |
57 | EVT_LEFT_UP(wxVListBoxComboPopup::OnLeftClick) | |
58 | END_EVENT_TABLE() | |
59 | ||
60 | ||
6d0ce565 | 61 | void wxVListBoxComboPopup::Init() |
a340b80d VZ |
62 | { |
63 | m_widestWidth = 0; | |
e5d63342 WS |
64 | m_widestItem = -1; |
65 | m_widthsDirty = false; | |
66 | m_findWidest = false; | |
a340b80d VZ |
67 | m_itemHeight = 0; |
68 | m_value = -1; | |
69 | m_itemHover = -1; | |
70 | m_clientDataItemsType = wxClientData_None; | |
dc8a1aa5 | 71 | m_partialCompletionString = wxEmptyString; |
a340b80d VZ |
72 | } |
73 | ||
74 | bool wxVListBoxComboPopup::Create(wxWindow* parent) | |
75 | { | |
76 | if ( !wxVListBox::Create(parent, | |
77 | wxID_ANY, | |
78 | wxDefaultPosition, | |
79 | wxDefaultSize, | |
80 | wxBORDER_SIMPLE | wxLB_INT_HEIGHT | wxWANTS_CHARS) ) | |
81 | return false; | |
82 | ||
6d0ce565 | 83 | m_useFont = m_combo->GetFont(); |
a340b80d VZ |
84 | |
85 | wxVListBox::SetItemCount(m_strings.GetCount()); | |
86 | ||
87 | // TODO: Move this to SetFont | |
88 | m_itemHeight = GetCharHeight() + 0; | |
89 | ||
90 | return true; | |
91 | } | |
92 | ||
93 | wxVListBoxComboPopup::~wxVListBoxComboPopup() | |
94 | { | |
95 | Clear(); | |
96 | } | |
97 | ||
98 | bool wxVListBoxComboPopup::LazyCreate() | |
99 | { | |
100 | // NB: There is a bug with wxVListBox that can be avoided by creating | |
101 | // it later (bug causes empty space to be shown if initial selection | |
102 | // is at the end of a list longer than the control can show at once). | |
103 | return true; | |
104 | } | |
105 | ||
106 | // paint the control itself | |
107 | void wxVListBoxComboPopup::PaintComboControl( wxDC& dc, const wxRect& rect ) | |
108 | { | |
109 | if ( !(m_combo->GetWindowStyle() & wxODCB_STD_CONTROL_PAINT) ) | |
110 | { | |
ce22ac45 RR |
111 | int flags = wxODCB_PAINTING_CONTROL; |
112 | ||
113 | if ( m_combo->ShouldDrawFocus() ) | |
114 | flags |= wxODCB_PAINTING_SELECTED; | |
115 | ||
116 | OnDrawBg(dc, rect, m_value, flags); | |
117 | ||
a340b80d VZ |
118 | if ( m_value >= 0 ) |
119 | { | |
b4ff336e | 120 | OnDrawItem(dc,rect,m_value,flags); |
6d0ce565 | 121 | return; |
a340b80d VZ |
122 | } |
123 | } | |
124 | ||
125 | wxComboPopup::PaintComboControl(dc,rect); | |
126 | } | |
127 | ||
128 | void wxVListBoxComboPopup::OnDrawItem(wxDC& dc, const wxRect& rect, size_t n) const | |
129 | { | |
6d0ce565 VZ |
130 | // TODO: Maybe this code could be moved to wxVListBox::OnPaint? |
131 | dc.SetFont(m_useFont); | |
a340b80d | 132 | |
b4ff336e VZ |
133 | int flags = 0; |
134 | ||
a340b80d | 135 | // Set correct text colour for selected items |
6d0ce565 | 136 | if ( wxVListBox::GetSelection() == (int) n ) |
b4ff336e | 137 | { |
a340b80d | 138 | dc.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT) ); |
b4ff336e VZ |
139 | flags |= wxODCB_PAINTING_SELECTED; |
140 | } | |
a340b80d | 141 | else |
b4ff336e | 142 | { |
a340b80d | 143 | dc.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT) ); |
b4ff336e | 144 | } |
a340b80d | 145 | |
b4ff336e | 146 | OnDrawItem(dc,rect,(int)n,flags); |
a340b80d VZ |
147 | } |
148 | ||
40b26d75 | 149 | wxCoord wxVListBoxComboPopup::OnMeasureItem(size_t n) const |
a340b80d | 150 | { |
40b26d75 WS |
151 | wxOwnerDrawnComboBox* combo = (wxOwnerDrawnComboBox*) m_combo; |
152 | ||
153 | wxASSERT_MSG( combo->IsKindOf(CLASSINFO(wxOwnerDrawnComboBox)), | |
154 | wxT("you must subclass wxVListBoxComboPopup for drawing and measuring methods") ); | |
155 | ||
156 | wxCoord h = combo->OnMeasureItem(n); | |
157 | if ( h < 0 ) | |
158 | h = m_itemHeight; | |
159 | return h; | |
6d0ce565 | 160 | } |
a340b80d | 161 | |
40b26d75 | 162 | wxCoord wxVListBoxComboPopup::OnMeasureItemWidth(size_t n) const |
6d0ce565 | 163 | { |
40b26d75 WS |
164 | wxOwnerDrawnComboBox* combo = (wxOwnerDrawnComboBox*) m_combo; |
165 | ||
166 | wxASSERT_MSG( combo->IsKindOf(CLASSINFO(wxOwnerDrawnComboBox)), | |
167 | wxT("you must subclass wxVListBoxComboPopup for drawing and measuring methods") ); | |
168 | ||
169 | return combo->OnMeasureItemWidth(n); | |
170 | } | |
171 | ||
172 | void wxVListBoxComboPopup::OnDrawBg( wxDC& dc, | |
173 | const wxRect& rect, | |
174 | int item, | |
175 | int flags ) const | |
176 | { | |
177 | wxOwnerDrawnComboBox* combo = (wxOwnerDrawnComboBox*) m_combo; | |
178 | ||
179 | wxASSERT_MSG( combo->IsKindOf(CLASSINFO(wxOwnerDrawnComboBox)), | |
180 | wxT("you must subclass wxVListBoxComboPopup for drawing and measuring methods") ); | |
181 | ||
ce22ac45 RR |
182 | if ( IsCurrent((size_t)item) && !(flags & wxODCB_PAINTING_CONTROL) ) |
183 | flags |= wxODCB_PAINTING_SELECTED; | |
184 | ||
40b26d75 | 185 | combo->OnDrawBackground(dc,rect,item,flags); |
a340b80d VZ |
186 | } |
187 | ||
188 | void wxVListBoxComboPopup::OnDrawBackground(wxDC& dc, const wxRect& rect, size_t n) const | |
189 | { | |
40b26d75 | 190 | OnDrawBg(dc,rect,(int)n,0); |
a340b80d VZ |
191 | } |
192 | ||
6d0ce565 VZ |
193 | // This is called from wxVListBoxComboPopup::OnDrawItem, with text colour and font prepared |
194 | void wxVListBoxComboPopup::OnDrawItem( wxDC& dc, const wxRect& rect, int item, int flags ) const | |
195 | { | |
40b26d75 WS |
196 | wxOwnerDrawnComboBox* combo = (wxOwnerDrawnComboBox*) m_combo; |
197 | ||
198 | wxASSERT_MSG( combo->IsKindOf(CLASSINFO(wxOwnerDrawnComboBox)), | |
199 | wxT("you must subclass wxVListBoxComboPopup for drawing and measuring methods") ); | |
200 | ||
201 | combo->OnDrawItem(dc,rect,item,flags); | |
6d0ce565 VZ |
202 | } |
203 | ||
204 | void wxVListBoxComboPopup::DismissWithEvent() | |
205 | { | |
dc8a1aa5 AB |
206 | StopPartialCompletion(); |
207 | ||
6d0ce565 VZ |
208 | int selection = wxVListBox::GetSelection(); |
209 | ||
210 | Dismiss(); | |
211 | ||
212 | wxString valStr; | |
213 | if ( selection != wxNOT_FOUND ) | |
214 | valStr = m_strings[selection]; | |
215 | else | |
216 | valStr = wxEmptyString; | |
217 | ||
218 | m_value = selection; | |
219 | ||
220 | if ( valStr != m_combo->GetValue() ) | |
ce968519 | 221 | m_combo->SetValueWithEvent(valStr); |
6d0ce565 VZ |
222 | |
223 | SendComboBoxEvent(selection); | |
224 | } | |
225 | ||
226 | void wxVListBoxComboPopup::SendComboBoxEvent( int selection ) | |
a340b80d VZ |
227 | { |
228 | wxCommandEvent evt(wxEVT_COMMAND_COMBOBOX_SELECTED,m_combo->GetId()); | |
a340b80d VZ |
229 | |
230 | evt.SetEventObject(m_combo); | |
6d0ce565 | 231 | |
a340b80d VZ |
232 | evt.SetInt(selection); |
233 | ||
234 | // Set client data, if any | |
235 | if ( selection >= 0 && (int)m_clientDatas.GetCount() > selection ) | |
236 | { | |
237 | void* clientData = m_clientDatas[selection]; | |
238 | if ( m_clientDataItemsType == wxClientData_Object ) | |
239 | evt.SetClientObject((wxClientData*)clientData); | |
240 | else | |
241 | evt.SetClientData(clientData); | |
242 | } | |
243 | ||
244 | m_combo->GetEventHandler()->AddPendingEvent(evt); | |
245 | } | |
246 | ||
247 | // returns true if key was consumed | |
dc8a1aa5 | 248 | bool wxVListBoxComboPopup::HandleKey( int keycode, bool saturate, wxChar unicode ) |
a340b80d VZ |
249 | { |
250 | int value = m_value; | |
251 | int itemCount = GetCount(); | |
dc8a1aa5 AB |
252 | int comboStyle = m_combo->GetWindowStyle(); |
253 | ||
254 | // this is the character equivalent of the code | |
255 | wxChar keychar=0; | |
256 | if ((keycode >= WXK_SPACE) && (keycode <=255) && (keycode != WXK_DELETE) && wxIsprint(keycode)) | |
257 | { | |
f0fa8b47 | 258 | keychar = (wxChar)keycode; |
dc8a1aa5 AB |
259 | } |
260 | else if (unicode>0) | |
261 | { | |
4dd31ff5 | 262 | keychar = unicode; |
dc8a1aa5 | 263 | } |
a340b80d VZ |
264 | |
265 | if ( keycode == WXK_DOWN || keycode == WXK_RIGHT ) | |
266 | { | |
267 | value++; | |
dc8a1aa5 | 268 | StopPartialCompletion(); |
a340b80d VZ |
269 | } |
270 | else if ( keycode == WXK_UP || keycode == WXK_LEFT ) | |
271 | { | |
272 | value--; | |
dc8a1aa5 | 273 | StopPartialCompletion(); |
a340b80d VZ |
274 | } |
275 | else if ( keycode == WXK_PAGEDOWN ) | |
276 | { | |
277 | value+=10; | |
dc8a1aa5 | 278 | StopPartialCompletion(); |
a340b80d VZ |
279 | } |
280 | else if ( keycode == WXK_PAGEUP ) | |
281 | { | |
282 | value-=10; | |
dc8a1aa5 AB |
283 | StopPartialCompletion(); |
284 | } | |
d9b72d25 | 285 | else if ( comboStyle & wxCB_READONLY ) |
dc8a1aa5 AB |
286 | { |
287 | // Try partial completion | |
288 | ||
289 | // find the new partial completion string | |
f0fa8b47 | 290 | #if wxUSE_TIMER |
dc8a1aa5 AB |
291 | if (m_partialCompletionTimer.IsRunning()) |
292 | m_partialCompletionString+=wxString(keychar); | |
293 | else | |
f0fa8b47 | 294 | #endif // wxUSE_TIMER |
dc8a1aa5 AB |
295 | m_partialCompletionString=wxString(keychar); |
296 | ||
297 | // now search through the values to see if this is found | |
298 | int found = -1; | |
4dd31ff5 | 299 | unsigned int length=m_partialCompletionString.length(); |
dc8a1aa5 AB |
300 | int i; |
301 | for (i=0; i<itemCount; i++) | |
302 | { | |
303 | wxString item=GetString(i); | |
4dd31ff5 | 304 | if (( item.length() >= length) && (! m_partialCompletionString.CmpNoCase(item.Left(length)))) |
dc8a1aa5 AB |
305 | { |
306 | found=i; | |
307 | break; | |
308 | } | |
309 | } | |
310 | ||
311 | if (found<0) | |
312 | { | |
313 | StopPartialCompletion(); | |
314 | ::wxBell(); | |
315 | return true; // to stop the first value being set | |
316 | } | |
317 | else | |
318 | { | |
319 | value=i; | |
f0fa8b47 | 320 | #if wxUSE_TIMER |
dc8a1aa5 | 321 | m_partialCompletionTimer.Start(wxODCB_PARTIAL_COMPLETION_TIME, true); |
f0fa8b47 | 322 | #endif // wxUSE_TIMER |
dc8a1aa5 | 323 | } |
a340b80d | 324 | } |
a340b80d VZ |
325 | else |
326 | return false; | |
327 | ||
328 | if ( saturate ) | |
329 | { | |
330 | if ( value >= itemCount ) | |
331 | value = itemCount - 1; | |
332 | else if ( value < 0 ) | |
333 | value = 0; | |
334 | } | |
335 | else | |
336 | { | |
337 | if ( value >= itemCount ) | |
338 | value -= itemCount; | |
339 | else if ( value < 0 ) | |
340 | value += itemCount; | |
341 | } | |
342 | ||
343 | if ( value == m_value ) | |
344 | // Even if value was same, don't skip the event | |
345 | // (good for consistency) | |
346 | return true; | |
347 | ||
348 | m_value = value; | |
349 | ||
a340b80d VZ |
350 | if ( value >= 0 ) |
351 | m_combo->SetValue(m_strings[value]); | |
352 | ||
6d0ce565 | 353 | SendComboBoxEvent(m_value); |
a340b80d VZ |
354 | |
355 | return true; | |
356 | } | |
357 | ||
dc8a1aa5 AB |
358 | // stop partial completion |
359 | void wxVListBoxComboPopup::StopPartialCompletion() | |
360 | { | |
361 | m_partialCompletionString = wxEmptyString; | |
f0fa8b47 | 362 | #if wxUSE_TIMER |
dc8a1aa5 | 363 | m_partialCompletionTimer.Stop(); |
f0fa8b47 | 364 | #endif // wxUSE_TIMER |
dc8a1aa5 AB |
365 | } |
366 | ||
a340b80d VZ |
367 | void wxVListBoxComboPopup::OnComboDoubleClick() |
368 | { | |
369 | // Cycle on dclick (disable saturation to allow true cycling). | |
370 | if ( !::wxGetKeyState(WXK_SHIFT) ) | |
371 | HandleKey(WXK_DOWN,false); | |
372 | else | |
373 | HandleKey(WXK_UP,false); | |
374 | } | |
375 | ||
376 | void wxVListBoxComboPopup::OnComboKeyEvent( wxKeyEvent& event ) | |
377 | { | |
378 | // Saturated key movement on | |
dc8a1aa5 AB |
379 | if ( !HandleKey(event.GetKeyCode(),true, |
380 | #if wxUSE_UNICODE | |
381 | event.GetUnicodeKey() | |
382 | #else | |
383 | 0 | |
384 | #endif | |
385 | ) ) | |
a340b80d VZ |
386 | event.Skip(); |
387 | } | |
388 | ||
389 | void wxVListBoxComboPopup::OnPopup() | |
390 | { | |
391 | // *must* set value after size is set (this is because of a vlbox bug) | |
392 | wxVListBox::SetSelection(m_value); | |
393 | } | |
394 | ||
395 | void wxVListBoxComboPopup::OnMouseMove(wxMouseEvent& event) | |
396 | { | |
276ebe79 WS |
397 | event.Skip(); |
398 | ||
a340b80d | 399 | // Move selection to cursor if it is inside the popup |
a340b80d | 400 | |
276ebe79 WS |
401 | int y = event.GetPosition().y; |
402 | int fromBottom = GetClientSize().y - y; | |
403 | ||
404 | // Since in any case we need to find out if the last item is only | |
405 | // partially visible, we might just as well replicate the HitTest | |
406 | // loop here. | |
407 | const size_t lineMax = GetVisibleEnd(); | |
408 | for ( size_t line = GetVisibleBegin(); line < lineMax; line++ ) | |
409 | { | |
e02c72fa | 410 | y -= OnGetRowHeight(line); |
276ebe79 WS |
411 | if ( y < 0 ) |
412 | { | |
413 | // Only change selection if item is fully visible | |
414 | if ( (y + fromBottom) >= 0 ) | |
415 | { | |
416 | wxVListBox::SetSelection((int)line); | |
417 | return; | |
418 | } | |
419 | } | |
420 | } | |
a340b80d VZ |
421 | } |
422 | ||
423 | void wxVListBoxComboPopup::OnLeftClick(wxMouseEvent& WXUNUSED(event)) | |
424 | { | |
6d0ce565 | 425 | DismissWithEvent(); |
a340b80d VZ |
426 | } |
427 | ||
428 | void wxVListBoxComboPopup::OnKey(wxKeyEvent& event) | |
429 | { | |
b445b6a7 VZ |
430 | // Hide popup if certain key or key combination was pressed |
431 | if ( m_combo->IsKeyPopupToggle(event) ) | |
dc8a1aa5 AB |
432 | { |
433 | StopPartialCompletion(); | |
a340b80d | 434 | Dismiss(); |
dc8a1aa5 | 435 | } |
b445b6a7 VZ |
436 | else if ( event.AltDown() ) |
437 | { | |
438 | // On both wxGTK and wxMSW, pressing Alt down seems to | |
439 | // completely freeze things in popup (ie. arrow keys and | |
440 | // enter won't work). | |
441 | return; | |
442 | } | |
443 | // Select item if ENTER is pressed | |
444 | else if ( event.GetKeyCode() == WXK_RETURN || event.GetKeyCode() == WXK_NUMPAD_ENTER ) | |
445 | { | |
446 | DismissWithEvent(); | |
447 | } | |
a340b80d | 448 | else |
dc8a1aa5 AB |
449 | { |
450 | int comboStyle = m_combo->GetWindowStyle(); | |
451 | int keycode = event.GetKeyCode(); | |
452 | // Process partial completion key codes here, but not the arrow keys as the base class will do that for us | |
d9b72d25 | 453 | if ((comboStyle & wxCB_READONLY) && |
dc8a1aa5 AB |
454 | (keycode >= WXK_SPACE) && (keycode <=255) && (keycode != WXK_DELETE) && wxIsprint(keycode)) |
455 | { | |
456 | OnComboKeyEvent(event); | |
457 | SetSelection(m_value); // ensure the highlight bar moves | |
458 | } | |
459 | else | |
460 | event.Skip(); | |
461 | } | |
a340b80d VZ |
462 | } |
463 | ||
a340b80d VZ |
464 | void wxVListBoxComboPopup::Insert( const wxString& item, int pos ) |
465 | { | |
466 | // Need to change selection? | |
467 | wxString strValue; | |
468 | if ( !(m_combo->GetWindowStyle() & wxCB_READONLY) && | |
469 | m_combo->GetValue() == item ) | |
6d0ce565 | 470 | { |
a340b80d | 471 | m_value = pos; |
6d0ce565 | 472 | } |
a340b80d VZ |
473 | |
474 | m_strings.Insert(item,pos); | |
a236aa20 VZ |
475 | m_clientDatas.Insert(NULL, pos); |
476 | ||
e5d63342 WS |
477 | m_widths.Insert(-1,pos); |
478 | m_widthsDirty = true; | |
a340b80d VZ |
479 | |
480 | if ( IsCreated() ) | |
481 | wxVListBox::SetItemCount( wxVListBox::GetItemCount()+1 ); | |
a340b80d VZ |
482 | } |
483 | ||
484 | int wxVListBoxComboPopup::Append(const wxString& item) | |
485 | { | |
486 | int pos = (int)m_strings.GetCount(); | |
487 | ||
488 | if ( m_combo->GetWindowStyle() & wxCB_SORT ) | |
489 | { | |
490 | // Find position | |
491 | // TODO: Could be optimized with binary search | |
492 | wxArrayString strings = m_strings; | |
493 | unsigned int i; | |
494 | ||
495 | for ( i=0; i<strings.GetCount(); i++ ) | |
496 | { | |
497 | if ( item.Cmp(strings.Item(i)) < 0 ) | |
498 | { | |
499 | pos = (int)i; | |
500 | break; | |
501 | } | |
502 | } | |
503 | } | |
504 | ||
505 | Insert(item,pos); | |
506 | ||
507 | return pos; | |
508 | } | |
509 | ||
510 | void wxVListBoxComboPopup::Clear() | |
511 | { | |
512 | wxASSERT(m_combo); | |
513 | ||
514 | m_strings.Empty(); | |
e5d63342 WS |
515 | m_widths.Empty(); |
516 | ||
517 | m_widestWidth = 0; | |
518 | m_widestItem = -1; | |
a340b80d VZ |
519 | |
520 | ClearClientDatas(); | |
521 | ||
cdc99ddd WS |
522 | m_value = wxNOT_FOUND; |
523 | ||
a340b80d VZ |
524 | if ( IsCreated() ) |
525 | wxVListBox::SetItemCount(0); | |
526 | } | |
527 | ||
528 | void wxVListBoxComboPopup::ClearClientDatas() | |
529 | { | |
530 | if ( m_clientDataItemsType == wxClientData_Object ) | |
531 | { | |
532 | size_t i; | |
533 | for ( i=0; i<m_clientDatas.GetCount(); i++ ) | |
534 | delete (wxClientData*) m_clientDatas[i]; | |
535 | } | |
536 | ||
537 | m_clientDatas.Empty(); | |
538 | } | |
539 | ||
540 | void wxVListBoxComboPopup::SetItemClientData( unsigned int n, | |
541 | void* clientData, | |
542 | wxClientDataType clientDataItemsType ) | |
543 | { | |
544 | // It should be sufficient to update this variable only here | |
545 | m_clientDataItemsType = clientDataItemsType; | |
546 | ||
a340b80d | 547 | m_clientDatas[n] = clientData; |
e5d63342 WS |
548 | |
549 | ItemWidthChanged(n); | |
a340b80d VZ |
550 | } |
551 | ||
552 | void* wxVListBoxComboPopup::GetItemClientData(unsigned int n) const | |
553 | { | |
554 | if ( m_clientDatas.GetCount() > n ) | |
555 | return m_clientDatas[n]; | |
556 | ||
557 | return NULL; | |
558 | } | |
559 | ||
560 | void wxVListBoxComboPopup::Delete( unsigned int item ) | |
561 | { | |
562 | // Remove client data, if set | |
563 | if ( m_clientDatas.GetCount() ) | |
564 | { | |
565 | if ( m_clientDataItemsType == wxClientData_Object ) | |
566 | delete (wxClientData*) m_clientDatas[item]; | |
567 | ||
568 | m_clientDatas.RemoveAt(item); | |
569 | } | |
570 | ||
571 | m_strings.RemoveAt(item); | |
e5d63342 WS |
572 | m_widths.RemoveAt(item); |
573 | ||
574 | if ( (int)item == m_widestItem ) | |
575 | m_findWidest = true; | |
a340b80d | 576 | |
fa3ebb12 RR |
577 | int sel = GetSelection(); |
578 | ||
a340b80d VZ |
579 | if ( IsCreated() ) |
580 | wxVListBox::SetItemCount( wxVListBox::GetItemCount()-1 ); | |
fa3ebb12 RR |
581 | |
582 | // Fix selection | |
583 | if ( (int)item < sel ) | |
584 | SetSelection(sel-1); | |
585 | else if ( (int)item == sel ) | |
586 | SetSelection(wxNOT_FOUND); | |
a340b80d VZ |
587 | } |
588 | ||
9e6aca68 | 589 | int wxVListBoxComboPopup::FindString(const wxString& s, bool bCase) const |
a340b80d | 590 | { |
9e6aca68 | 591 | return m_strings.Index(s, bCase); |
a340b80d VZ |
592 | } |
593 | ||
594 | unsigned int wxVListBoxComboPopup::GetCount() const | |
595 | { | |
596 | return m_strings.GetCount(); | |
597 | } | |
598 | ||
599 | wxString wxVListBoxComboPopup::GetString( int item ) const | |
600 | { | |
601 | return m_strings[item]; | |
602 | } | |
603 | ||
604 | void wxVListBoxComboPopup::SetString( int item, const wxString& str ) | |
605 | { | |
606 | m_strings[item] = str; | |
e5d63342 | 607 | ItemWidthChanged(item); |
a340b80d VZ |
608 | } |
609 | ||
610 | wxString wxVListBoxComboPopup::GetStringValue() const | |
611 | { | |
612 | if ( m_value >= 0 ) | |
613 | return m_strings[m_value]; | |
614 | return wxEmptyString; | |
615 | } | |
616 | ||
617 | void wxVListBoxComboPopup::SetSelection( int item ) | |
618 | { | |
85fed18c WS |
619 | wxCHECK_RET( item == wxNOT_FOUND || ((unsigned int)item < GetCount()), |
620 | wxT("invalid index in wxVListBoxComboPopup::SetSelection") ); | |
a340b80d VZ |
621 | |
622 | m_value = item; | |
623 | ||
624 | if ( IsCreated() ) | |
625 | wxVListBox::SetSelection(item); | |
626 | } | |
627 | ||
6d0ce565 VZ |
628 | int wxVListBoxComboPopup::GetSelection() const |
629 | { | |
630 | return m_value; | |
631 | } | |
632 | ||
a340b80d VZ |
633 | void wxVListBoxComboPopup::SetStringValue( const wxString& value ) |
634 | { | |
635 | int index = m_strings.Index(value); | |
636 | ||
637 | m_value = index; | |
638 | ||
639 | if ( index >= -1 && index < (int)wxVListBox::GetItemCount() ) | |
640 | wxVListBox::SetSelection(index); | |
641 | } | |
642 | ||
d9e0958c | 643 | void wxVListBoxComboPopup::CalcWidths() |
a340b80d | 644 | { |
e5d63342 WS |
645 | bool doFindWidest = m_findWidest; |
646 | ||
647 | // Measure items with dirty width. | |
648 | if ( m_widthsDirty ) | |
649 | { | |
650 | unsigned int i; | |
651 | unsigned int n = m_widths.GetCount(); | |
652 | int dirtyHandled = 0; | |
653 | wxArrayInt& widths = m_widths; | |
654 | ||
655 | // I think using wxDC::GetTextExtent is faster than | |
656 | // wxWindow::GetTextExtent (assuming same dc is used | |
657 | // for all calls, as we do here). | |
658 | wxClientDC dc(m_combo); | |
659 | dc.SetFont(m_useFont); | |
660 | ||
661 | for ( i=0; i<n; i++ ) | |
662 | { | |
663 | if ( widths[i] < 0 ) | |
664 | { | |
665 | wxCoord x = OnMeasureItemWidth(i); | |
666 | ||
667 | if ( x < 0 ) | |
668 | { | |
669 | const wxString& text = m_strings[i]; | |
670 | ||
671 | // To make sure performance won't suck in extreme scenarios, | |
672 | // we'll estimate length after some arbitrary number of items | |
673 | // have been checked precily. | |
674 | if ( dirtyHandled < 1024 ) | |
675 | { | |
676 | wxCoord y; | |
677 | dc.GetTextExtent(text, &x, &y, 0, 0); | |
678 | x += 4; | |
679 | } | |
680 | else | |
681 | { | |
682 | x = text.length() * (dc.GetCharWidth()+1); | |
683 | } | |
684 | } | |
685 | ||
686 | widths[i] = x; | |
687 | ||
688 | if ( x >= m_widestWidth ) | |
689 | { | |
690 | m_widestWidth = x; | |
691 | m_widestItem = (int)i; | |
692 | } | |
693 | else if ( (int)i == m_widestItem ) | |
694 | { | |
695 | // Width of previously widest item has been decreased, so | |
696 | // we'll have to check all to find current widest item. | |
697 | doFindWidest = true; | |
698 | } | |
699 | ||
700 | dirtyHandled++; | |
701 | } | |
702 | } | |
703 | ||
704 | m_widthsDirty = false; | |
705 | } | |
706 | ||
707 | if ( doFindWidest ) | |
708 | { | |
709 | unsigned int i; | |
710 | unsigned int n = m_widths.GetCount(); | |
711 | ||
712 | int bestWidth = -1; | |
713 | int bestIndex = -1; | |
714 | ||
715 | for ( i=0; i<n; i++ ) | |
716 | { | |
717 | int w = m_widths[i]; | |
718 | if ( w > bestWidth ) | |
719 | { | |
720 | bestIndex = (int)i; | |
721 | bestWidth = w; | |
722 | } | |
723 | } | |
724 | ||
725 | m_widestWidth = bestWidth; | |
726 | m_widestItem = bestIndex; | |
727 | ||
728 | m_findWidest = false; | |
729 | } | |
d9e0958c AB |
730 | } |
731 | ||
732 | wxSize wxVListBoxComboPopup::GetAdjustedSize( int minWidth, int prefHeight, int maxHeight ) | |
733 | { | |
734 | int height = 250; | |
735 | ||
7962f85a RR |
736 | maxHeight -= 2; // Must take borders into account |
737 | ||
d9e0958c AB |
738 | if ( m_strings.GetCount() ) |
739 | { | |
740 | if ( prefHeight > 0 ) | |
741 | height = prefHeight; | |
742 | ||
743 | if ( height > maxHeight ) | |
744 | height = maxHeight; | |
745 | ||
746 | int totalHeight = GetTotalHeight(); // + 3; | |
747 | if ( height >= totalHeight ) | |
748 | { | |
749 | height = totalHeight; | |
750 | } | |
751 | else | |
752 | { | |
753 | // Adjust height to a multiple of the height of the first item | |
754 | // NB: Calculations that take variable height into account | |
755 | // are unnecessary. | |
756 | int fih = GetLineHeight(0); | |
7962f85a | 757 | height -= height % fih; |
d9e0958c AB |
758 | } |
759 | } | |
760 | else | |
761 | height = 50; | |
762 | ||
763 | CalcWidths(); | |
e5d63342 | 764 | |
a340b80d VZ |
765 | // Take scrollbar into account in width calculations |
766 | int widestWidth = m_widestWidth + wxSystemSettings::GetMetric(wxSYS_VSCROLL_X); | |
767 | return wxSize(minWidth > widestWidth ? minWidth : widestWidth, | |
768 | height+2); | |
769 | } | |
770 | ||
6d0ce565 VZ |
771 | //void wxVListBoxComboPopup::Populate( int n, const wxString choices[] ) |
772 | void wxVListBoxComboPopup::Populate( const wxArrayString& choices ) | |
a340b80d VZ |
773 | { |
774 | int i; | |
775 | ||
6d0ce565 VZ |
776 | int n = choices.GetCount(); |
777 | ||
a340b80d VZ |
778 | for ( i=0; i<n; i++ ) |
779 | { | |
6d0ce565 | 780 | const wxString& item = choices.Item(i); |
a340b80d | 781 | m_strings.Add(item); |
a340b80d VZ |
782 | } |
783 | ||
e5d63342 WS |
784 | m_widths.SetCount(n,-1); |
785 | m_widthsDirty = true; | |
786 | ||
a340b80d VZ |
787 | if ( IsCreated() ) |
788 | wxVListBox::SetItemCount(n); | |
789 | ||
790 | // Sort the initial choices | |
791 | if ( m_combo->GetWindowStyle() & wxCB_SORT ) | |
792 | m_strings.Sort(); | |
793 | ||
794 | // Find initial selection | |
795 | wxString strValue = m_combo->GetValue(); | |
40b26d75 | 796 | if ( strValue.length() ) |
a340b80d VZ |
797 | m_value = m_strings.Index(strValue); |
798 | } | |
799 | ||
800 | // ---------------------------------------------------------------------------- | |
801 | // wxOwnerDrawnComboBox | |
802 | // ---------------------------------------------------------------------------- | |
803 | ||
804 | ||
a57d600f | 805 | BEGIN_EVENT_TABLE(wxOwnerDrawnComboBox, wxComboCtrl) |
a340b80d VZ |
806 | END_EVENT_TABLE() |
807 | ||
808 | ||
afc89ff4 MB |
809 | #if wxUSE_EXTENDED_RTTI |
810 | IMPLEMENT_DYNAMIC_CLASS2_XTI(wxOwnerDrawnComboBox, wxComboCtrl, wxControlWithItems, "wx/odcombo.h") | |
811 | ||
812 | wxBEGIN_PROPERTIES_TABLE(wxOwnerDrawnComboBox) | |
813 | wxEND_PROPERTIES_TABLE() | |
814 | ||
815 | wxBEGIN_HANDLERS_TABLE(wxOwnerDrawnComboBox) | |
816 | wxEND_HANDLERS_TABLE() | |
817 | ||
818 | wxCONSTRUCTOR_5( wxOwnerDrawnComboBox , wxWindow* , Parent , wxWindowID , Id , wxString , Value , wxPoint , Position , wxSize , Size ) | |
819 | #else | |
a57d600f | 820 | IMPLEMENT_DYNAMIC_CLASS2(wxOwnerDrawnComboBox, wxComboCtrl, wxControlWithItems) |
afc89ff4 | 821 | #endif |
a340b80d VZ |
822 | |
823 | void wxOwnerDrawnComboBox::Init() | |
824 | { | |
825 | } | |
826 | ||
827 | bool wxOwnerDrawnComboBox::Create(wxWindow *parent, | |
828 | wxWindowID id, | |
829 | const wxString& value, | |
830 | const wxPoint& pos, | |
831 | const wxSize& size, | |
832 | long style, | |
833 | const wxValidator& validator, | |
834 | const wxString& name) | |
835 | { | |
a57d600f | 836 | return wxComboCtrl::Create(parent,id,value,pos,size,style,validator,name); |
a340b80d VZ |
837 | } |
838 | ||
839 | wxOwnerDrawnComboBox::wxOwnerDrawnComboBox(wxWindow *parent, | |
840 | wxWindowID id, | |
841 | const wxString& value, | |
842 | const wxPoint& pos, | |
843 | const wxSize& size, | |
844 | const wxArrayString& choices, | |
845 | long style, | |
846 | const wxValidator& validator, | |
847 | const wxString& name) | |
a57d600f | 848 | : wxComboCtrl() |
a340b80d VZ |
849 | { |
850 | Init(); | |
851 | ||
852 | Create(parent,id,value,pos,size,choices,style, validator, name); | |
853 | } | |
854 | ||
855 | bool wxOwnerDrawnComboBox::Create(wxWindow *parent, | |
856 | wxWindowID id, | |
857 | const wxString& value, | |
858 | const wxPoint& pos, | |
859 | const wxSize& size, | |
860 | const wxArrayString& choices, | |
861 | long style, | |
862 | const wxValidator& validator, | |
863 | const wxString& name) | |
864 | { | |
6d0ce565 VZ |
865 | m_initChs = choices; |
866 | //wxCArrayString chs(choices); | |
a340b80d | 867 | |
6d0ce565 VZ |
868 | //return Create(parent, id, value, pos, size, chs.GetCount(), |
869 | // chs.GetStrings(), style, validator, name); | |
870 | return Create(parent, id, value, pos, size, 0, | |
871 | NULL, style, validator, name); | |
a340b80d VZ |
872 | } |
873 | ||
874 | bool wxOwnerDrawnComboBox::Create(wxWindow *parent, | |
875 | wxWindowID id, | |
876 | const wxString& value, | |
877 | const wxPoint& pos, | |
878 | const wxSize& size, | |
879 | int n, | |
880 | const wxString choices[], | |
881 | long style, | |
882 | const wxValidator& validator, | |
883 | const wxString& name) | |
884 | { | |
885 | ||
886 | if ( !Create(parent, id, value, pos, size, style, | |
887 | validator, name) ) | |
888 | { | |
889 | return false; | |
890 | } | |
891 | ||
6d0ce565 VZ |
892 | int i; |
893 | for ( i=0; i<n; i++ ) | |
894 | m_initChs.Add(choices[i]); | |
a340b80d VZ |
895 | |
896 | return true; | |
897 | } | |
898 | ||
899 | wxOwnerDrawnComboBox::~wxOwnerDrawnComboBox() | |
900 | { | |
901 | if ( m_popupInterface ) | |
57693473 | 902 | GetVListBoxComboPopup()->ClearClientDatas(); |
a340b80d VZ |
903 | } |
904 | ||
db53c6ea | 905 | void wxOwnerDrawnComboBox::DoSetPopupControl(wxComboPopup* popup) |
6d0ce565 VZ |
906 | { |
907 | if ( !popup ) | |
908 | { | |
909 | popup = new wxVListBoxComboPopup(); | |
910 | } | |
911 | ||
db53c6ea | 912 | wxComboCtrl::DoSetPopupControl(popup); |
6d0ce565 VZ |
913 | |
914 | wxASSERT(popup); | |
6d0ce565 VZ |
915 | |
916 | // Add initial choices to the wxVListBox | |
57693473 | 917 | if ( !GetVListBoxComboPopup()->GetCount() ) |
6d0ce565 | 918 | { |
57693473 | 919 | GetVListBoxComboPopup()->Populate(m_initChs); |
6d0ce565 VZ |
920 | m_initChs.Clear(); |
921 | } | |
922 | } | |
923 | ||
a340b80d VZ |
924 | // ---------------------------------------------------------------------------- |
925 | // wxOwnerDrawnComboBox item manipulation methods | |
926 | // ---------------------------------------------------------------------------- | |
927 | ||
a236aa20 | 928 | void wxOwnerDrawnComboBox::DoClear() |
a340b80d | 929 | { |
6d0ce565 | 930 | EnsurePopupControl(); |
a340b80d | 931 | |
57693473 | 932 | GetVListBoxComboPopup()->Clear(); |
a340b80d | 933 | |
cdc99ddd | 934 | SetValue(wxEmptyString); |
a340b80d VZ |
935 | } |
936 | ||
a236aa20 | 937 | void wxOwnerDrawnComboBox::DoDeleteOneItem(unsigned int n) |
a340b80d | 938 | { |
85fed18c | 939 | wxCHECK_RET( IsValid(n), _T("invalid index in wxOwnerDrawnComboBox::Delete") ); |
a340b80d VZ |
940 | |
941 | if ( GetSelection() == (int) n ) | |
942 | SetValue(wxEmptyString); | |
943 | ||
57693473 | 944 | GetVListBoxComboPopup()->Delete(n); |
a340b80d VZ |
945 | } |
946 | ||
947 | unsigned int wxOwnerDrawnComboBox::GetCount() const | |
948 | { | |
54a61762 WS |
949 | if ( !m_popupInterface ) |
950 | return m_initChs.GetCount(); | |
951 | ||
57693473 | 952 | return GetVListBoxComboPopup()->GetCount(); |
a340b80d VZ |
953 | } |
954 | ||
955 | wxString wxOwnerDrawnComboBox::GetString(unsigned int n) const | |
956 | { | |
85fed18c | 957 | wxCHECK_MSG( IsValid(n), wxEmptyString, _T("invalid index in wxOwnerDrawnComboBox::GetString") ); |
54a61762 WS |
958 | |
959 | if ( !m_popupInterface ) | |
960 | return m_initChs.Item(n); | |
961 | ||
57693473 | 962 | return GetVListBoxComboPopup()->GetString(n); |
a340b80d VZ |
963 | } |
964 | ||
965 | void wxOwnerDrawnComboBox::SetString(unsigned int n, const wxString& s) | |
966 | { | |
54a61762 WS |
967 | EnsurePopupControl(); |
968 | ||
85fed18c | 969 | wxCHECK_RET( IsValid(n), _T("invalid index in wxOwnerDrawnComboBox::SetString") ); |
54a61762 | 970 | |
57693473 | 971 | GetVListBoxComboPopup()->SetString(n,s); |
a340b80d VZ |
972 | } |
973 | ||
9e6aca68 | 974 | int wxOwnerDrawnComboBox::FindString(const wxString& s, bool bCase) const |
a340b80d | 975 | { |
54a61762 WS |
976 | if ( !m_popupInterface ) |
977 | return m_initChs.Index(s, bCase); | |
978 | ||
57693473 | 979 | return GetVListBoxComboPopup()->FindString(s, bCase); |
a340b80d VZ |
980 | } |
981 | ||
982 | void wxOwnerDrawnComboBox::Select(int n) | |
983 | { | |
6d0ce565 | 984 | EnsurePopupControl(); |
a340b80d | 985 | |
40b26d75 WS |
986 | wxCHECK_RET( (n == wxNOT_FOUND) || IsValid(n), _T("invalid index in wxOwnerDrawnComboBox::Select") ); |
987 | ||
57693473 | 988 | GetVListBoxComboPopup()->SetSelection(n); |
a340b80d VZ |
989 | |
990 | wxString str; | |
991 | if ( n >= 0 ) | |
57693473 | 992 | str = GetVListBoxComboPopup()->GetString(n); |
a340b80d VZ |
993 | |
994 | // Refresh text portion in control | |
995 | if ( m_text ) | |
996 | m_text->SetValue( str ); | |
997 | else | |
998 | m_valueString = str; | |
999 | ||
1000 | Refresh(); | |
1001 | } | |
1002 | ||
1003 | int wxOwnerDrawnComboBox::GetSelection() const | |
1004 | { | |
54a61762 WS |
1005 | if ( !m_popupInterface ) |
1006 | return m_initChs.Index(m_valueString); | |
1007 | ||
57693473 | 1008 | return GetVListBoxComboPopup()->GetSelection(); |
a340b80d VZ |
1009 | } |
1010 | ||
a236aa20 VZ |
1011 | int wxOwnerDrawnComboBox::DoInsertItems(const wxArrayStringsAdapter& items, |
1012 | unsigned int pos, | |
1013 | void **clientData, | |
1014 | wxClientDataType type) | |
a340b80d | 1015 | { |
40b26d75 WS |
1016 | EnsurePopupControl(); |
1017 | ||
a236aa20 VZ |
1018 | const unsigned int count = items.GetCount(); |
1019 | for( unsigned int i = 0; i < count; ++i, ++pos ) | |
1020 | { | |
1021 | GetVListBoxComboPopup()->Insert(items[i], pos); | |
1022 | AssignNewItemClientData(pos, clientData, i, type); | |
1023 | } | |
a340b80d | 1024 | |
a236aa20 | 1025 | return pos - 1; |
a340b80d VZ |
1026 | } |
1027 | ||
1028 | void wxOwnerDrawnComboBox::DoSetItemClientData(unsigned int n, void* clientData) | |
1029 | { | |
6d0ce565 | 1030 | EnsurePopupControl(); |
57693473 | 1031 | |
131b1fba VZ |
1032 | GetVListBoxComboPopup()->SetItemClientData(n, clientData, |
1033 | GetClientDataType()); | |
a340b80d VZ |
1034 | } |
1035 | ||
1036 | void* wxOwnerDrawnComboBox::DoGetItemClientData(unsigned int n) const | |
1037 | { | |
54a61762 WS |
1038 | if ( !m_popupInterface ) |
1039 | return NULL; | |
1040 | ||
57693473 | 1041 | return GetVListBoxComboPopup()->GetItemClientData(n); |
a340b80d VZ |
1042 | } |
1043 | ||
40b26d75 WS |
1044 | // ---------------------------------------------------------------------------- |
1045 | // wxOwnerDrawnComboBox item drawing and measuring default implementations | |
1046 | // ---------------------------------------------------------------------------- | |
1047 | ||
1048 | void wxOwnerDrawnComboBox::OnDrawItem( wxDC& dc, | |
1049 | const wxRect& rect, | |
1050 | int item, | |
1051 | int flags ) const | |
1052 | { | |
1053 | if ( flags & wxODCB_PAINTING_CONTROL ) | |
1054 | { | |
1055 | dc.DrawText( GetValue(), | |
1056 | rect.x + GetTextIndent(), | |
1057 | (rect.height-dc.GetCharHeight())/2 + rect.y ); | |
1058 | } | |
1059 | else | |
1060 | { | |
57693473 | 1061 | dc.DrawText( GetVListBoxComboPopup()->GetString(item), rect.x + 2, rect.y ); |
40b26d75 WS |
1062 | } |
1063 | } | |
1064 | ||
1065 | wxCoord wxOwnerDrawnComboBox::OnMeasureItem( size_t WXUNUSED(item) ) const | |
1066 | { | |
1067 | return -1; | |
1068 | } | |
1069 | ||
1070 | wxCoord wxOwnerDrawnComboBox::OnMeasureItemWidth( size_t WXUNUSED(item) ) const | |
1071 | { | |
1072 | return -1; | |
1073 | } | |
1074 | ||
ce22ac45 RR |
1075 | void wxOwnerDrawnComboBox::OnDrawBackground(wxDC& dc, |
1076 | const wxRect& rect, | |
1077 | int WXUNUSED(item), | |
1078 | int flags) const | |
40b26d75 | 1079 | { |
ce22ac45 RR |
1080 | // We need only to explicitly draw background for items |
1081 | // that should have selected background. Also, call PrepareBackground | |
1082 | // always when painting the control so that clipping is done properly. | |
1083 | ||
1084 | if ( (flags & wxODCB_PAINTING_SELECTED) || | |
1085 | ((flags & wxODCB_PAINTING_CONTROL) && HasFlag(wxCB_READONLY)) ) | |
40b26d75 | 1086 | { |
118f5fbd | 1087 | int bgFlags = wxCONTROL_SELECTED; |
4dd31ff5 | 1088 | |
ce22ac45 | 1089 | if ( !(flags & wxODCB_PAINTING_CONTROL) ) |
118f5fbd | 1090 | bgFlags |= wxCONTROL_ISSUBMENU; |
ce22ac45 RR |
1091 | |
1092 | PrepareBackground(dc, rect, bgFlags); | |
40b26d75 | 1093 | } |
40b26d75 WS |
1094 | } |
1095 | ||
a57d600f | 1096 | #endif // wxUSE_ODCOMBOBOX |