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