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