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