]> git.saurik.com Git - wxWidgets.git/blob - src/generic/odcombo.cpp
made Freeze/Thaw recursively (un)freeze child windows too
[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 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_clientDatas.Insert(NULL, pos);
475
476 m_widths.Insert(-1,pos);
477 m_widthsDirty = true;
478
479 if ( IsCreated() )
480 wxVListBox::SetItemCount( wxVListBox::GetItemCount()+1 );
481 }
482
483 int wxVListBoxComboPopup::Append(const wxString& item)
484 {
485 int pos = (int)m_strings.GetCount();
486
487 if ( m_combo->GetWindowStyle() & wxCB_SORT )
488 {
489 // Find position
490 // TODO: Could be optimized with binary search
491 wxArrayString strings = m_strings;
492 unsigned int i;
493
494 for ( i=0; i<strings.GetCount(); i++ )
495 {
496 if ( item.Cmp(strings.Item(i)) < 0 )
497 {
498 pos = (int)i;
499 break;
500 }
501 }
502 }
503
504 Insert(item,pos);
505
506 return pos;
507 }
508
509 void wxVListBoxComboPopup::Clear()
510 {
511 wxASSERT(m_combo);
512
513 m_strings.Empty();
514 m_widths.Empty();
515
516 m_widestWidth = 0;
517 m_widestItem = -1;
518
519 ClearClientDatas();
520
521 m_value = wxNOT_FOUND;
522
523 if ( IsCreated() )
524 wxVListBox::SetItemCount(0);
525 }
526
527 void wxVListBoxComboPopup::ClearClientDatas()
528 {
529 if ( m_clientDataItemsType == wxClientData_Object )
530 {
531 size_t i;
532 for ( i=0; i<m_clientDatas.GetCount(); i++ )
533 delete (wxClientData*) m_clientDatas[i];
534 }
535
536 m_clientDatas.Empty();
537 }
538
539 void wxVListBoxComboPopup::SetItemClientData( unsigned int n,
540 void* clientData,
541 wxClientDataType clientDataItemsType )
542 {
543 // It should be sufficient to update this variable only here
544 m_clientDataItemsType = clientDataItemsType;
545
546 m_clientDatas[n] = clientData;
547
548 ItemWidthChanged(n);
549 }
550
551 void* wxVListBoxComboPopup::GetItemClientData(unsigned int n) const
552 {
553 if ( m_clientDatas.GetCount() > n )
554 return m_clientDatas[n];
555
556 return NULL;
557 }
558
559 void wxVListBoxComboPopup::Delete( unsigned int item )
560 {
561 // Remove client data, if set
562 if ( m_clientDatas.GetCount() )
563 {
564 if ( m_clientDataItemsType == wxClientData_Object )
565 delete (wxClientData*) m_clientDatas[item];
566
567 m_clientDatas.RemoveAt(item);
568 }
569
570 m_strings.RemoveAt(item);
571 m_widths.RemoveAt(item);
572
573 if ( (int)item == m_widestItem )
574 m_findWidest = true;
575
576 int sel = GetSelection();
577
578 if ( IsCreated() )
579 wxVListBox::SetItemCount( wxVListBox::GetItemCount()-1 );
580
581 // Fix selection
582 if ( (int)item < sel )
583 SetSelection(sel-1);
584 else if ( (int)item == sel )
585 SetSelection(wxNOT_FOUND);
586 }
587
588 int wxVListBoxComboPopup::FindString(const wxString& s, bool bCase) const
589 {
590 return m_strings.Index(s, bCase);
591 }
592
593 unsigned int wxVListBoxComboPopup::GetCount() const
594 {
595 return m_strings.GetCount();
596 }
597
598 wxString wxVListBoxComboPopup::GetString( int item ) const
599 {
600 return m_strings[item];
601 }
602
603 void wxVListBoxComboPopup::SetString( int item, const wxString& str )
604 {
605 m_strings[item] = str;
606 ItemWidthChanged(item);
607 }
608
609 wxString wxVListBoxComboPopup::GetStringValue() const
610 {
611 return m_stringValue;
612 }
613
614 void wxVListBoxComboPopup::SetSelection( int item )
615 {
616 wxCHECK_RET( item == wxNOT_FOUND || ((unsigned int)item < GetCount()),
617 wxT("invalid index in wxVListBoxComboPopup::SetSelection") );
618
619 m_value = item;
620
621 if ( item >= 0 )
622 m_stringValue = m_strings[item];
623 else
624 m_stringValue = wxEmptyString;
625
626 if ( IsCreated() )
627 wxVListBox::SetSelection(item);
628 }
629
630 int wxVListBoxComboPopup::GetSelection() const
631 {
632 return m_value;
633 }
634
635 void wxVListBoxComboPopup::SetStringValue( const wxString& value )
636 {
637 int index = m_strings.Index(value);
638
639 m_stringValue = value;
640
641 if ( index >= 0 && index < (int)wxVListBox::GetItemCount() )
642 {
643 wxVListBox::SetSelection(index);
644 m_value = index;
645 }
646 }
647
648 void wxVListBoxComboPopup::CalcWidths()
649 {
650 bool doFindWidest = m_findWidest;
651
652 // Measure items with dirty width.
653 if ( m_widthsDirty )
654 {
655 unsigned int i;
656 unsigned int n = m_widths.GetCount();
657 int dirtyHandled = 0;
658 wxArrayInt& widths = m_widths;
659
660 // I think using wxDC::GetTextExtent is faster than
661 // wxWindow::GetTextExtent (assuming same dc is used
662 // for all calls, as we do here).
663 wxClientDC dc(m_combo);
664 dc.SetFont(m_useFont);
665
666 for ( i=0; i<n; i++ )
667 {
668 if ( widths[i] < 0 )
669 {
670 wxCoord x = OnMeasureItemWidth(i);
671
672 if ( x < 0 )
673 {
674 const wxString& text = m_strings[i];
675
676 // To make sure performance won't suck in extreme scenarios,
677 // we'll estimate length after some arbitrary number of items
678 // have been checked precily.
679 if ( dirtyHandled < 1024 )
680 {
681 wxCoord y;
682 dc.GetTextExtent(text, &x, &y, 0, 0);
683 x += 4;
684 }
685 else
686 {
687 x = text.length() * (dc.GetCharWidth()+1);
688 }
689 }
690
691 widths[i] = x;
692
693 if ( x >= m_widestWidth )
694 {
695 m_widestWidth = x;
696 m_widestItem = (int)i;
697 }
698 else if ( (int)i == m_widestItem )
699 {
700 // Width of previously widest item has been decreased, so
701 // we'll have to check all to find current widest item.
702 doFindWidest = true;
703 }
704
705 dirtyHandled++;
706 }
707 }
708
709 m_widthsDirty = false;
710 }
711
712 if ( doFindWidest )
713 {
714 unsigned int i;
715 unsigned int n = m_widths.GetCount();
716
717 int bestWidth = -1;
718 int bestIndex = -1;
719
720 for ( i=0; i<n; i++ )
721 {
722 int w = m_widths[i];
723 if ( w > bestWidth )
724 {
725 bestIndex = (int)i;
726 bestWidth = w;
727 }
728 }
729
730 m_widestWidth = bestWidth;
731 m_widestItem = bestIndex;
732
733 m_findWidest = false;
734 }
735 }
736
737 wxSize wxVListBoxComboPopup::GetAdjustedSize( int minWidth, int prefHeight, int maxHeight )
738 {
739 int height = 250;
740
741 maxHeight -= 2; // Must take borders into account
742
743 if ( m_strings.GetCount() )
744 {
745 if ( prefHeight > 0 )
746 height = prefHeight;
747
748 if ( height > maxHeight )
749 height = maxHeight;
750
751 int totalHeight = GetTotalHeight(); // + 3;
752 if ( height >= totalHeight )
753 {
754 height = totalHeight;
755 }
756 else
757 {
758 // Adjust height to a multiple of the height of the first item
759 // NB: Calculations that take variable height into account
760 // are unnecessary.
761 int fih = GetLineHeight(0);
762 height -= height % fih;
763 }
764 }
765 else
766 height = 50;
767
768 CalcWidths();
769
770 // Take scrollbar into account in width calculations
771 int widestWidth = m_widestWidth + wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
772 return wxSize(minWidth > widestWidth ? minWidth : widestWidth,
773 height+2);
774 }
775
776 //void wxVListBoxComboPopup::Populate( int n, const wxString choices[] )
777 void wxVListBoxComboPopup::Populate( const wxArrayString& choices )
778 {
779 int i;
780
781 int n = choices.GetCount();
782
783 for ( i=0; i<n; i++ )
784 {
785 const wxString& item = choices.Item(i);
786 m_strings.Add(item);
787 }
788
789 m_widths.SetCount(n,-1);
790 m_widthsDirty = true;
791
792 if ( IsCreated() )
793 wxVListBox::SetItemCount(n);
794
795 // Sort the initial choices
796 if ( m_combo->GetWindowStyle() & wxCB_SORT )
797 m_strings.Sort();
798
799 // Find initial selection
800 wxString strValue = m_combo->GetValue();
801 if ( strValue.length() )
802 m_value = m_strings.Index(strValue);
803 }
804
805 // ----------------------------------------------------------------------------
806 // wxOwnerDrawnComboBox
807 // ----------------------------------------------------------------------------
808
809
810 BEGIN_EVENT_TABLE(wxOwnerDrawnComboBox, wxComboCtrl)
811 END_EVENT_TABLE()
812
813
814 #if wxUSE_EXTENDED_RTTI
815 IMPLEMENT_DYNAMIC_CLASS2_XTI(wxOwnerDrawnComboBox, wxComboCtrl, wxControlWithItems, "wx/odcombo.h")
816
817 wxBEGIN_PROPERTIES_TABLE(wxOwnerDrawnComboBox)
818 wxEND_PROPERTIES_TABLE()
819
820 wxBEGIN_HANDLERS_TABLE(wxOwnerDrawnComboBox)
821 wxEND_HANDLERS_TABLE()
822
823 wxCONSTRUCTOR_5( wxOwnerDrawnComboBox , wxWindow* , Parent , wxWindowID , Id , wxString , Value , wxPoint , Position , wxSize , Size )
824 #else
825 IMPLEMENT_DYNAMIC_CLASS2(wxOwnerDrawnComboBox, wxComboCtrl, wxControlWithItems)
826 #endif
827
828 void wxOwnerDrawnComboBox::Init()
829 {
830 }
831
832 bool wxOwnerDrawnComboBox::Create(wxWindow *parent,
833 wxWindowID id,
834 const wxString& value,
835 const wxPoint& pos,
836 const wxSize& size,
837 long style,
838 const wxValidator& validator,
839 const wxString& name)
840 {
841 return wxComboCtrl::Create(parent,id,value,pos,size,style,validator,name);
842 }
843
844 wxOwnerDrawnComboBox::wxOwnerDrawnComboBox(wxWindow *parent,
845 wxWindowID id,
846 const wxString& value,
847 const wxPoint& pos,
848 const wxSize& size,
849 const wxArrayString& choices,
850 long style,
851 const wxValidator& validator,
852 const wxString& name)
853 : wxComboCtrl()
854 {
855 Init();
856
857 Create(parent,id,value,pos,size,choices,style, validator, name);
858 }
859
860 bool wxOwnerDrawnComboBox::Create(wxWindow *parent,
861 wxWindowID id,
862 const wxString& value,
863 const wxPoint& pos,
864 const wxSize& size,
865 const wxArrayString& choices,
866 long style,
867 const wxValidator& validator,
868 const wxString& name)
869 {
870 m_initChs = choices;
871 //wxCArrayString chs(choices);
872
873 //return Create(parent, id, value, pos, size, chs.GetCount(),
874 // chs.GetStrings(), style, validator, name);
875 return Create(parent, id, value, pos, size, 0,
876 NULL, style, validator, name);
877 }
878
879 bool wxOwnerDrawnComboBox::Create(wxWindow *parent,
880 wxWindowID id,
881 const wxString& value,
882 const wxPoint& pos,
883 const wxSize& size,
884 int n,
885 const wxString choices[],
886 long style,
887 const wxValidator& validator,
888 const wxString& name)
889 {
890
891 if ( !Create(parent, id, value, pos, size, style,
892 validator, name) )
893 {
894 return false;
895 }
896
897 int i;
898 for ( i=0; i<n; i++ )
899 m_initChs.Add(choices[i]);
900
901 return true;
902 }
903
904 wxOwnerDrawnComboBox::~wxOwnerDrawnComboBox()
905 {
906 if ( m_popupInterface )
907 GetVListBoxComboPopup()->ClearClientDatas();
908 }
909
910 void wxOwnerDrawnComboBox::DoSetPopupControl(wxComboPopup* popup)
911 {
912 if ( !popup )
913 {
914 popup = new wxVListBoxComboPopup();
915 }
916
917 wxComboCtrl::DoSetPopupControl(popup);
918
919 wxASSERT(popup);
920
921 // Add initial choices to the wxVListBox
922 if ( !GetVListBoxComboPopup()->GetCount() )
923 {
924 GetVListBoxComboPopup()->Populate(m_initChs);
925 m_initChs.Clear();
926 }
927 }
928
929 // ----------------------------------------------------------------------------
930 // wxOwnerDrawnComboBox item manipulation methods
931 // ----------------------------------------------------------------------------
932
933 void wxOwnerDrawnComboBox::DoClear()
934 {
935 EnsurePopupControl();
936
937 GetVListBoxComboPopup()->Clear();
938
939 SetValue(wxEmptyString);
940 }
941
942 void wxOwnerDrawnComboBox::DoDeleteOneItem(unsigned int n)
943 {
944 wxCHECK_RET( IsValid(n), _T("invalid index in wxOwnerDrawnComboBox::Delete") );
945
946 if ( GetSelection() == (int) n )
947 SetValue(wxEmptyString);
948
949 GetVListBoxComboPopup()->Delete(n);
950 }
951
952 unsigned int wxOwnerDrawnComboBox::GetCount() const
953 {
954 if ( !m_popupInterface )
955 return m_initChs.GetCount();
956
957 return GetVListBoxComboPopup()->GetCount();
958 }
959
960 wxString wxOwnerDrawnComboBox::GetString(unsigned int n) const
961 {
962 wxCHECK_MSG( IsValid(n), wxEmptyString, _T("invalid index in wxOwnerDrawnComboBox::GetString") );
963
964 if ( !m_popupInterface )
965 return m_initChs.Item(n);
966
967 return GetVListBoxComboPopup()->GetString(n);
968 }
969
970 void wxOwnerDrawnComboBox::SetString(unsigned int n, const wxString& s)
971 {
972 EnsurePopupControl();
973
974 wxCHECK_RET( IsValid(n), _T("invalid index in wxOwnerDrawnComboBox::SetString") );
975
976 GetVListBoxComboPopup()->SetString(n,s);
977 }
978
979 int wxOwnerDrawnComboBox::FindString(const wxString& s, bool bCase) const
980 {
981 if ( !m_popupInterface )
982 return m_initChs.Index(s, bCase);
983
984 return GetVListBoxComboPopup()->FindString(s, bCase);
985 }
986
987 void wxOwnerDrawnComboBox::Select(int n)
988 {
989 EnsurePopupControl();
990
991 wxCHECK_RET( (n == wxNOT_FOUND) || IsValid(n), _T("invalid index in wxOwnerDrawnComboBox::Select") );
992
993 GetVListBoxComboPopup()->SetSelection(n);
994
995 wxString str;
996 if ( n >= 0 )
997 str = GetVListBoxComboPopup()->GetString(n);
998
999 // Refresh text portion in control
1000 if ( m_text )
1001 m_text->SetValue( str );
1002 else
1003 m_valueString = str;
1004
1005 Refresh();
1006 }
1007
1008 int wxOwnerDrawnComboBox::GetSelection() const
1009 {
1010 if ( !m_popupInterface )
1011 return m_initChs.Index(m_valueString);
1012
1013 return GetVListBoxComboPopup()->GetSelection();
1014 }
1015
1016 int wxOwnerDrawnComboBox::DoInsertItems(const wxArrayStringsAdapter& items,
1017 unsigned int pos,
1018 void **clientData,
1019 wxClientDataType type)
1020 {
1021 EnsurePopupControl();
1022
1023 const unsigned int count = items.GetCount();
1024 for( unsigned int i = 0; i < count; ++i, ++pos )
1025 {
1026 GetVListBoxComboPopup()->Insert(items[i], pos);
1027 AssignNewItemClientData(pos, clientData, i, type);
1028 }
1029
1030 return pos - 1;
1031 }
1032
1033 void wxOwnerDrawnComboBox::DoSetItemClientData(unsigned int n, void* clientData)
1034 {
1035 EnsurePopupControl();
1036
1037 GetVListBoxComboPopup()->SetItemClientData(n, clientData,
1038 GetClientDataType());
1039 }
1040
1041 void* wxOwnerDrawnComboBox::DoGetItemClientData(unsigned int n) const
1042 {
1043 if ( !m_popupInterface )
1044 return NULL;
1045
1046 return GetVListBoxComboPopup()->GetItemClientData(n);
1047 }
1048
1049 // ----------------------------------------------------------------------------
1050 // wxOwnerDrawnComboBox item drawing and measuring default implementations
1051 // ----------------------------------------------------------------------------
1052
1053 void wxOwnerDrawnComboBox::OnDrawItem( wxDC& dc,
1054 const wxRect& rect,
1055 int item,
1056 int flags ) const
1057 {
1058 if ( flags & wxODCB_PAINTING_CONTROL )
1059 {
1060 dc.DrawText( GetValue(),
1061 rect.x + GetTextIndent(),
1062 (rect.height-dc.GetCharHeight())/2 + rect.y );
1063 }
1064 else
1065 {
1066 dc.DrawText( GetVListBoxComboPopup()->GetString(item), rect.x + 2, rect.y );
1067 }
1068 }
1069
1070 wxCoord wxOwnerDrawnComboBox::OnMeasureItem( size_t WXUNUSED(item) ) const
1071 {
1072 return -1;
1073 }
1074
1075 wxCoord wxOwnerDrawnComboBox::OnMeasureItemWidth( size_t WXUNUSED(item) ) const
1076 {
1077 return -1;
1078 }
1079
1080 void wxOwnerDrawnComboBox::OnDrawBackground(wxDC& dc,
1081 const wxRect& rect,
1082 int WXUNUSED(item),
1083 int flags) const
1084 {
1085 // We need only to explicitly draw background for items
1086 // that should have selected background. Also, call PrepareBackground
1087 // always when painting the control so that clipping is done properly.
1088
1089 if ( (flags & wxODCB_PAINTING_SELECTED) ||
1090 ((flags & wxODCB_PAINTING_CONTROL) && HasFlag(wxCB_READONLY)) )
1091 {
1092 int bgFlags = wxCONTROL_SELECTED;
1093
1094 if ( !(flags & wxODCB_PAINTING_CONTROL) )
1095 bgFlags |= wxCONTROL_ISSUBMENU;
1096
1097 PrepareBackground(dc, rect, bgFlags);
1098 }
1099 }
1100
1101 #endif // wxUSE_ODCOMBOBOX