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