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