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