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