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