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