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