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