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