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