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