]> git.saurik.com Git - wxWidgets.git/blame - src/generic/odcombo.cpp
[ 1509599 ] 'Split pickers page in widgets sample' with more icons and rebaking.
[wxWidgets.git] / src / generic / odcombo.cpp
CommitLineData
a340b80d 1/////////////////////////////////////////////////////////////////////////////
85fed18c 2// Name: src/generic/odcombo.cpp
a340b80d
VZ
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
a57d600f 26#if wxUSE_ODCOMBOBOX
a340b80d 27
85fed18c
WS
28#include "wx/odcombo.h"
29
a340b80d
VZ
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"
a340b80d
VZ
39
40// ============================================================================
41// implementation
42// ============================================================================
43
44
45// ----------------------------------------------------------------------------
46// wxVListBoxComboPopup is a wxVListBox customized to act as a popup control
47//
48// ----------------------------------------------------------------------------
49
50
51BEGIN_EVENT_TABLE(wxVListBoxComboPopup, wxVListBox)
52 EVT_MOTION(wxVListBoxComboPopup::OnMouseMove)
53 EVT_KEY_DOWN(wxVListBoxComboPopup::OnKey)
54 EVT_LEFT_UP(wxVListBoxComboPopup::OnLeftClick)
55END_EVENT_TABLE()
56
57
6d0ce565 58void wxVListBoxComboPopup::Init()
a340b80d
VZ
59{
60 m_widestWidth = 0;
e5d63342
WS
61 m_widestItem = -1;
62 m_widthsDirty = false;
63 m_findWidest = false;
a340b80d
VZ
64 m_itemHeight = 0;
65 m_value = -1;
66 m_itemHover = -1;
67 m_clientDataItemsType = wxClientData_None;
68}
69
70bool wxVListBoxComboPopup::Create(wxWindow* parent)
71{
72 if ( !wxVListBox::Create(parent,
73 wxID_ANY,
74 wxDefaultPosition,
75 wxDefaultSize,
76 wxBORDER_SIMPLE | wxLB_INT_HEIGHT | wxWANTS_CHARS) )
77 return false;
78
6d0ce565 79 m_useFont = m_combo->GetFont();
a340b80d
VZ
80
81 wxVListBox::SetItemCount(m_strings.GetCount());
82
83 // TODO: Move this to SetFont
84 m_itemHeight = GetCharHeight() + 0;
85
86 return true;
87}
88
89wxVListBoxComboPopup::~wxVListBoxComboPopup()
90{
91 Clear();
92}
93
94bool wxVListBoxComboPopup::LazyCreate()
95{
96 // NB: There is a bug with wxVListBox that can be avoided by creating
97 // it later (bug causes empty space to be shown if initial selection
98 // is at the end of a list longer than the control can show at once).
99 return true;
100}
101
102// paint the control itself
103void wxVListBoxComboPopup::PaintComboControl( wxDC& dc, const wxRect& rect )
104{
105 if ( !(m_combo->GetWindowStyle() & wxODCB_STD_CONTROL_PAINT) )
106 {
40b26d75 107 OnDrawBg(dc,rect,m_value,wxODCB_PAINTING_CONTROL);
a340b80d
VZ
108 if ( m_value >= 0 )
109 {
40b26d75 110 OnDrawItem(dc,rect,m_value,wxODCB_PAINTING_CONTROL);
6d0ce565 111 return;
a340b80d
VZ
112 }
113 }
114
115 wxComboPopup::PaintComboControl(dc,rect);
116}
117
118void wxVListBoxComboPopup::OnDrawItem(wxDC& dc, const wxRect& rect, size_t n) const
119{
6d0ce565
VZ
120 // TODO: Maybe this code could be moved to wxVListBox::OnPaint?
121 dc.SetFont(m_useFont);
a340b80d
VZ
122
123 // Set correct text colour for selected items
6d0ce565 124 if ( wxVListBox::GetSelection() == (int) n )
a340b80d
VZ
125 dc.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT) );
126 else
127 dc.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT) );
128
6d0ce565 129 OnDrawItem(dc,rect,(int)n,0);
a340b80d
VZ
130}
131
40b26d75 132wxCoord wxVListBoxComboPopup::OnMeasureItem(size_t n) const
a340b80d 133{
40b26d75
WS
134 wxOwnerDrawnComboBox* combo = (wxOwnerDrawnComboBox*) m_combo;
135
136 wxASSERT_MSG( combo->IsKindOf(CLASSINFO(wxOwnerDrawnComboBox)),
137 wxT("you must subclass wxVListBoxComboPopup for drawing and measuring methods") );
138
139 wxCoord h = combo->OnMeasureItem(n);
140 if ( h < 0 )
141 h = m_itemHeight;
142 return h;
6d0ce565 143}
a340b80d 144
40b26d75 145wxCoord wxVListBoxComboPopup::OnMeasureItemWidth(size_t n) const
6d0ce565 146{
40b26d75
WS
147 wxOwnerDrawnComboBox* combo = (wxOwnerDrawnComboBox*) m_combo;
148
149 wxASSERT_MSG( combo->IsKindOf(CLASSINFO(wxOwnerDrawnComboBox)),
150 wxT("you must subclass wxVListBoxComboPopup for drawing and measuring methods") );
151
152 return combo->OnMeasureItemWidth(n);
153}
154
155void wxVListBoxComboPopup::OnDrawBg( wxDC& dc,
156 const wxRect& rect,
157 int item,
158 int flags ) const
159{
160 wxOwnerDrawnComboBox* combo = (wxOwnerDrawnComboBox*) m_combo;
161
162 wxASSERT_MSG( combo->IsKindOf(CLASSINFO(wxOwnerDrawnComboBox)),
163 wxT("you must subclass wxVListBoxComboPopup for drawing and measuring methods") );
164
165 combo->OnDrawBackground(dc,rect,item,flags);
a340b80d
VZ
166}
167
168void wxVListBoxComboPopup::OnDrawBackground(wxDC& dc, const wxRect& rect, size_t n) const
169{
40b26d75 170 OnDrawBg(dc,rect,(int)n,0);
a340b80d
VZ
171}
172
6d0ce565
VZ
173// This is called from wxVListBoxComboPopup::OnDrawItem, with text colour and font prepared
174void wxVListBoxComboPopup::OnDrawItem( wxDC& dc, const wxRect& rect, int item, int flags ) const
175{
40b26d75
WS
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 combo->OnDrawItem(dc,rect,item,flags);
6d0ce565
VZ
182}
183
184void wxVListBoxComboPopup::DismissWithEvent()
185{
186 int selection = wxVListBox::GetSelection();
187
188 Dismiss();
189
190 wxString valStr;
191 if ( selection != wxNOT_FOUND )
192 valStr = m_strings[selection];
193 else
194 valStr = wxEmptyString;
195
196 m_value = selection;
197
198 if ( valStr != m_combo->GetValue() )
199 m_combo->SetValue(valStr);
200
201 SendComboBoxEvent(selection);
202}
203
204void wxVListBoxComboPopup::SendComboBoxEvent( int selection )
a340b80d
VZ
205{
206 wxCommandEvent evt(wxEVT_COMMAND_COMBOBOX_SELECTED,m_combo->GetId());
a340b80d
VZ
207
208 evt.SetEventObject(m_combo);
6d0ce565 209
a340b80d
VZ
210 evt.SetInt(selection);
211
212 // Set client data, if any
213 if ( selection >= 0 && (int)m_clientDatas.GetCount() > selection )
214 {
215 void* clientData = m_clientDatas[selection];
216 if ( m_clientDataItemsType == wxClientData_Object )
217 evt.SetClientObject((wxClientData*)clientData);
218 else
219 evt.SetClientData(clientData);
220 }
221
222 m_combo->GetEventHandler()->AddPendingEvent(evt);
223}
224
225// returns true if key was consumed
226bool wxVListBoxComboPopup::HandleKey( int keycode, bool saturate )
227{
228 int value = m_value;
229 int itemCount = GetCount();
230
231 if ( keycode == WXK_DOWN || keycode == WXK_RIGHT )
232 {
233 value++;
234 }
235 else if ( keycode == WXK_UP || keycode == WXK_LEFT )
236 {
237 value--;
238 }
239 else if ( keycode == WXK_PAGEDOWN )
240 {
241 value+=10;
242 }
243 else if ( keycode == WXK_PAGEUP )
244 {
245 value-=10;
246 }
a340b80d
VZ
247 else
248 return false;
249
250 if ( saturate )
251 {
252 if ( value >= itemCount )
253 value = itemCount - 1;
254 else if ( value < 0 )
255 value = 0;
256 }
257 else
258 {
259 if ( value >= itemCount )
260 value -= itemCount;
261 else if ( value < 0 )
262 value += itemCount;
263 }
264
265 if ( value == m_value )
266 // Even if value was same, don't skip the event
267 // (good for consistency)
268 return true;
269
270 m_value = value;
271
a340b80d
VZ
272 if ( value >= 0 )
273 m_combo->SetValue(m_strings[value]);
274
6d0ce565 275 SendComboBoxEvent(m_value);
a340b80d
VZ
276
277 return true;
278}
279
280void wxVListBoxComboPopup::OnComboDoubleClick()
281{
282 // Cycle on dclick (disable saturation to allow true cycling).
283 if ( !::wxGetKeyState(WXK_SHIFT) )
284 HandleKey(WXK_DOWN,false);
285 else
286 HandleKey(WXK_UP,false);
287}
288
289void wxVListBoxComboPopup::OnComboKeyEvent( wxKeyEvent& event )
290{
291 // Saturated key movement on
292 if ( !HandleKey(event.GetKeyCode(),true) )
293 event.Skip();
294}
295
296void wxVListBoxComboPopup::OnPopup()
297{
298 // *must* set value after size is set (this is because of a vlbox bug)
299 wxVListBox::SetSelection(m_value);
300}
301
302void wxVListBoxComboPopup::OnMouseMove(wxMouseEvent& event)
303{
304 // Move selection to cursor if it is inside the popup
305 int itemHere = GetItemAtPosition(event.GetPosition());
306 if ( itemHere >= 0 )
307 wxVListBox::SetSelection(itemHere);
308
309 event.Skip();
310}
311
312void wxVListBoxComboPopup::OnLeftClick(wxMouseEvent& WXUNUSED(event))
313{
6d0ce565 314 DismissWithEvent();
a340b80d
VZ
315}
316
317void wxVListBoxComboPopup::OnKey(wxKeyEvent& event)
318{
319 // Select item if ENTER is pressed
320 if ( event.GetKeyCode() == WXK_RETURN || event.GetKeyCode() == WXK_NUMPAD_ENTER )
321 {
6d0ce565 322 DismissWithEvent();
a340b80d
VZ
323 }
324 // Hide popup if ESC is pressed
325 else if ( event.GetKeyCode() == WXK_ESCAPE )
326 Dismiss();
327 else
328 event.Skip();
329}
330
a340b80d
VZ
331void wxVListBoxComboPopup::Insert( const wxString& item, int pos )
332{
333 // Need to change selection?
334 wxString strValue;
335 if ( !(m_combo->GetWindowStyle() & wxCB_READONLY) &&
336 m_combo->GetValue() == item )
6d0ce565 337 {
a340b80d 338 m_value = pos;
6d0ce565 339 }
a340b80d
VZ
340
341 m_strings.Insert(item,pos);
e5d63342
WS
342 m_widths.Insert(-1,pos);
343 m_widthsDirty = true;
a340b80d
VZ
344
345 if ( IsCreated() )
346 wxVListBox::SetItemCount( wxVListBox::GetItemCount()+1 );
a340b80d
VZ
347}
348
349int wxVListBoxComboPopup::Append(const wxString& item)
350{
351 int pos = (int)m_strings.GetCount();
352
353 if ( m_combo->GetWindowStyle() & wxCB_SORT )
354 {
355 // Find position
356 // TODO: Could be optimized with binary search
357 wxArrayString strings = m_strings;
358 unsigned int i;
359
360 for ( i=0; i<strings.GetCount(); i++ )
361 {
362 if ( item.Cmp(strings.Item(i)) < 0 )
363 {
364 pos = (int)i;
365 break;
366 }
367 }
368 }
369
370 Insert(item,pos);
371
372 return pos;
373}
374
375void wxVListBoxComboPopup::Clear()
376{
377 wxASSERT(m_combo);
378
379 m_strings.Empty();
e5d63342
WS
380 m_widths.Empty();
381
382 m_widestWidth = 0;
383 m_widestItem = -1;
a340b80d
VZ
384
385 ClearClientDatas();
386
cdc99ddd
WS
387 m_value = wxNOT_FOUND;
388
a340b80d
VZ
389 if ( IsCreated() )
390 wxVListBox::SetItemCount(0);
391}
392
393void wxVListBoxComboPopup::ClearClientDatas()
394{
395 if ( m_clientDataItemsType == wxClientData_Object )
396 {
397 size_t i;
398 for ( i=0; i<m_clientDatas.GetCount(); i++ )
399 delete (wxClientData*) m_clientDatas[i];
400 }
401
402 m_clientDatas.Empty();
403}
404
405void wxVListBoxComboPopup::SetItemClientData( unsigned int n,
406 void* clientData,
407 wxClientDataType clientDataItemsType )
408{
409 // It should be sufficient to update this variable only here
410 m_clientDataItemsType = clientDataItemsType;
411
412 m_clientDatas.SetCount(n+1,NULL);
413 m_clientDatas[n] = clientData;
e5d63342
WS
414
415 ItemWidthChanged(n);
a340b80d
VZ
416}
417
418void* wxVListBoxComboPopup::GetItemClientData(unsigned int n) const
419{
420 if ( m_clientDatas.GetCount() > n )
421 return m_clientDatas[n];
422
423 return NULL;
424}
425
426void wxVListBoxComboPopup::Delete( unsigned int item )
427{
428 // Remove client data, if set
429 if ( m_clientDatas.GetCount() )
430 {
431 if ( m_clientDataItemsType == wxClientData_Object )
432 delete (wxClientData*) m_clientDatas[item];
433
434 m_clientDatas.RemoveAt(item);
435 }
436
437 m_strings.RemoveAt(item);
e5d63342
WS
438 m_widths.RemoveAt(item);
439
440 if ( (int)item == m_widestItem )
441 m_findWidest = true;
a340b80d
VZ
442
443 if ( IsCreated() )
444 wxVListBox::SetItemCount( wxVListBox::GetItemCount()-1 );
445}
446
9e6aca68 447int wxVListBoxComboPopup::FindString(const wxString& s, bool bCase) const
a340b80d 448{
9e6aca68 449 return m_strings.Index(s, bCase);
a340b80d
VZ
450}
451
452unsigned int wxVListBoxComboPopup::GetCount() const
453{
454 return m_strings.GetCount();
455}
456
457wxString wxVListBoxComboPopup::GetString( int item ) const
458{
459 return m_strings[item];
460}
461
462void wxVListBoxComboPopup::SetString( int item, const wxString& str )
463{
464 m_strings[item] = str;
e5d63342 465 ItemWidthChanged(item);
a340b80d
VZ
466}
467
468wxString wxVListBoxComboPopup::GetStringValue() const
469{
470 if ( m_value >= 0 )
471 return m_strings[m_value];
472 return wxEmptyString;
473}
474
475void wxVListBoxComboPopup::SetSelection( int item )
476{
85fed18c
WS
477 wxCHECK_RET( item == wxNOT_FOUND || ((unsigned int)item < GetCount()),
478 wxT("invalid index in wxVListBoxComboPopup::SetSelection") );
a340b80d
VZ
479
480 m_value = item;
481
482 if ( IsCreated() )
483 wxVListBox::SetSelection(item);
484}
485
6d0ce565
VZ
486int wxVListBoxComboPopup::GetSelection() const
487{
488 return m_value;
489}
490
a340b80d
VZ
491void wxVListBoxComboPopup::SetStringValue( const wxString& value )
492{
493 int index = m_strings.Index(value);
494
495 m_value = index;
496
497 if ( index >= -1 && index < (int)wxVListBox::GetItemCount() )
498 wxVListBox::SetSelection(index);
499}
500
501wxSize wxVListBoxComboPopup::GetAdjustedSize( int minWidth, int prefHeight, int maxHeight )
502{
503 int height = 250;
504
505 if ( m_strings.GetCount() )
506 {
507 if ( prefHeight > 0 )
508 height = prefHeight;
509
510 if ( height > maxHeight )
511 height = maxHeight;
512
513 int totalHeight = GetTotalHeight(); // + 3;
514 if ( height >= totalHeight )
515 {
516 height = totalHeight;
517 }
518 else
519 {
520 // Adjust height to a multiple of the height of the first item
521 // NB: Calculations that take variable height into account
522 // are unnecessary.
523 int fih = GetLineHeight(0);
524 int shown = height/fih;
525 height = shown * fih;
526 }
527 }
528 else
529 height = 50;
530
e5d63342
WS
531 bool doFindWidest = m_findWidest;
532
533 // Measure items with dirty width.
534 if ( m_widthsDirty )
535 {
536 unsigned int i;
537 unsigned int n = m_widths.GetCount();
538 int dirtyHandled = 0;
539 wxArrayInt& widths = m_widths;
540
541 // I think using wxDC::GetTextExtent is faster than
542 // wxWindow::GetTextExtent (assuming same dc is used
543 // for all calls, as we do here).
544 wxClientDC dc(m_combo);
545 dc.SetFont(m_useFont);
546
547 for ( i=0; i<n; i++ )
548 {
549 if ( widths[i] < 0 )
550 {
551 wxCoord x = OnMeasureItemWidth(i);
552
553 if ( x < 0 )
554 {
555 const wxString& text = m_strings[i];
556
557 // To make sure performance won't suck in extreme scenarios,
558 // we'll estimate length after some arbitrary number of items
559 // have been checked precily.
560 if ( dirtyHandled < 1024 )
561 {
562 wxCoord y;
563 dc.GetTextExtent(text, &x, &y, 0, 0);
564 x += 4;
565 }
566 else
567 {
568 x = text.length() * (dc.GetCharWidth()+1);
569 }
570 }
571
572 widths[i] = x;
573
574 if ( x >= m_widestWidth )
575 {
576 m_widestWidth = x;
577 m_widestItem = (int)i;
578 }
579 else if ( (int)i == m_widestItem )
580 {
581 // Width of previously widest item has been decreased, so
582 // we'll have to check all to find current widest item.
583 doFindWidest = true;
584 }
585
586 dirtyHandled++;
587 }
588 }
589
590 m_widthsDirty = false;
591 }
592
593 if ( doFindWidest )
594 {
595 unsigned int i;
596 unsigned int n = m_widths.GetCount();
597
598 int bestWidth = -1;
599 int bestIndex = -1;
600
601 for ( i=0; i<n; i++ )
602 {
603 int w = m_widths[i];
604 if ( w > bestWidth )
605 {
606 bestIndex = (int)i;
607 bestWidth = w;
608 }
609 }
610
611 m_widestWidth = bestWidth;
612 m_widestItem = bestIndex;
613
614 m_findWidest = false;
615 }
616
a340b80d
VZ
617 // Take scrollbar into account in width calculations
618 int widestWidth = m_widestWidth + wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
619 return wxSize(minWidth > widestWidth ? minWidth : widestWidth,
620 height+2);
621}
622
6d0ce565
VZ
623//void wxVListBoxComboPopup::Populate( int n, const wxString choices[] )
624void wxVListBoxComboPopup::Populate( const wxArrayString& choices )
a340b80d
VZ
625{
626 int i;
627
6d0ce565
VZ
628 int n = choices.GetCount();
629
a340b80d
VZ
630 for ( i=0; i<n; i++ )
631 {
6d0ce565 632 const wxString& item = choices.Item(i);
a340b80d 633 m_strings.Add(item);
a340b80d
VZ
634 }
635
e5d63342
WS
636 m_widths.SetCount(n,-1);
637 m_widthsDirty = true;
638
a340b80d
VZ
639 if ( IsCreated() )
640 wxVListBox::SetItemCount(n);
641
642 // Sort the initial choices
643 if ( m_combo->GetWindowStyle() & wxCB_SORT )
644 m_strings.Sort();
645
646 // Find initial selection
647 wxString strValue = m_combo->GetValue();
40b26d75 648 if ( strValue.length() )
a340b80d
VZ
649 m_value = m_strings.Index(strValue);
650}
651
652// ----------------------------------------------------------------------------
653// wxOwnerDrawnComboBox
654// ----------------------------------------------------------------------------
655
656
a57d600f 657BEGIN_EVENT_TABLE(wxOwnerDrawnComboBox, wxComboCtrl)
a340b80d
VZ
658END_EVENT_TABLE()
659
660
a57d600f 661IMPLEMENT_DYNAMIC_CLASS2(wxOwnerDrawnComboBox, wxComboCtrl, wxControlWithItems)
a340b80d
VZ
662
663void wxOwnerDrawnComboBox::Init()
664{
6d0ce565 665 m_popupInterface = NULL;
a340b80d
VZ
666}
667
668bool wxOwnerDrawnComboBox::Create(wxWindow *parent,
669 wxWindowID id,
670 const wxString& value,
671 const wxPoint& pos,
672 const wxSize& size,
673 long style,
674 const wxValidator& validator,
675 const wxString& name)
676{
a57d600f 677 return wxComboCtrl::Create(parent,id,value,pos,size,style,validator,name);
a340b80d
VZ
678}
679
680wxOwnerDrawnComboBox::wxOwnerDrawnComboBox(wxWindow *parent,
681 wxWindowID id,
682 const wxString& value,
683 const wxPoint& pos,
684 const wxSize& size,
685 const wxArrayString& choices,
686 long style,
687 const wxValidator& validator,
688 const wxString& name)
a57d600f 689 : wxComboCtrl()
a340b80d
VZ
690{
691 Init();
692
693 Create(parent,id,value,pos,size,choices,style, validator, name);
694}
695
696bool wxOwnerDrawnComboBox::Create(wxWindow *parent,
697 wxWindowID id,
698 const wxString& value,
699 const wxPoint& pos,
700 const wxSize& size,
701 const wxArrayString& choices,
702 long style,
703 const wxValidator& validator,
704 const wxString& name)
705{
6d0ce565
VZ
706 m_initChs = choices;
707 //wxCArrayString chs(choices);
a340b80d 708
6d0ce565
VZ
709 //return Create(parent, id, value, pos, size, chs.GetCount(),
710 // chs.GetStrings(), style, validator, name);
711 return Create(parent, id, value, pos, size, 0,
712 NULL, style, validator, name);
a340b80d
VZ
713}
714
715bool wxOwnerDrawnComboBox::Create(wxWindow *parent,
716 wxWindowID id,
717 const wxString& value,
718 const wxPoint& pos,
719 const wxSize& size,
720 int n,
721 const wxString choices[],
722 long style,
723 const wxValidator& validator,
724 const wxString& name)
725{
726
727 if ( !Create(parent, id, value, pos, size, style,
728 validator, name) )
729 {
730 return false;
731 }
732
6d0ce565
VZ
733 int i;
734 for ( i=0; i<n; i++ )
735 m_initChs.Add(choices[i]);
a340b80d
VZ
736
737 return true;
738}
739
740wxOwnerDrawnComboBox::~wxOwnerDrawnComboBox()
741{
742 if ( m_popupInterface )
743 m_popupInterface->ClearClientDatas();
744}
745
db53c6ea 746void wxOwnerDrawnComboBox::DoSetPopupControl(wxComboPopup* popup)
6d0ce565
VZ
747{
748 if ( !popup )
749 {
750 popup = new wxVListBoxComboPopup();
751 }
752
db53c6ea 753 wxComboCtrl::DoSetPopupControl(popup);
6d0ce565
VZ
754
755 wxASSERT(popup);
756 m_popupInterface = (wxVListBoxComboPopup*) popup;
757
758 // Add initial choices to the wxVListBox
759 if ( !m_popupInterface->GetCount() )
760 {
761 //m_popupInterface->Populate(m_initChs.GetCount(),m_initChs.GetStrings());
762 m_popupInterface->Populate(m_initChs);
763 m_initChs.Clear();
764 }
765}
766
a340b80d
VZ
767// ----------------------------------------------------------------------------
768// wxOwnerDrawnComboBox item manipulation methods
769// ----------------------------------------------------------------------------
770
771void wxOwnerDrawnComboBox::Clear()
772{
6d0ce565 773 EnsurePopupControl();
a340b80d
VZ
774
775 m_popupInterface->Clear();
776
cdc99ddd 777 SetValue(wxEmptyString);
a340b80d
VZ
778}
779
780void wxOwnerDrawnComboBox::Delete(unsigned int n)
781{
85fed18c 782 wxCHECK_RET( IsValid(n), _T("invalid index in wxOwnerDrawnComboBox::Delete") );
a340b80d
VZ
783
784 if ( GetSelection() == (int) n )
785 SetValue(wxEmptyString);
786
787 m_popupInterface->Delete(n);
788}
789
790unsigned int wxOwnerDrawnComboBox::GetCount() const
791{
54a61762
WS
792 if ( !m_popupInterface )
793 return m_initChs.GetCount();
794
a340b80d
VZ
795 return m_popupInterface->GetCount();
796}
797
798wxString wxOwnerDrawnComboBox::GetString(unsigned int n) const
799{
85fed18c 800 wxCHECK_MSG( IsValid(n), wxEmptyString, _T("invalid index in wxOwnerDrawnComboBox::GetString") );
54a61762
WS
801
802 if ( !m_popupInterface )
803 return m_initChs.Item(n);
804
a340b80d
VZ
805 return m_popupInterface->GetString(n);
806}
807
808void wxOwnerDrawnComboBox::SetString(unsigned int n, const wxString& s)
809{
54a61762
WS
810 EnsurePopupControl();
811
85fed18c 812 wxCHECK_RET( IsValid(n), _T("invalid index in wxOwnerDrawnComboBox::SetString") );
54a61762 813
a340b80d
VZ
814 m_popupInterface->SetString(n,s);
815}
816
9e6aca68 817int wxOwnerDrawnComboBox::FindString(const wxString& s, bool bCase) const
a340b80d 818{
54a61762
WS
819 if ( !m_popupInterface )
820 return m_initChs.Index(s, bCase);
821
9e6aca68 822 return m_popupInterface->FindString(s, bCase);
a340b80d
VZ
823}
824
825void wxOwnerDrawnComboBox::Select(int n)
826{
6d0ce565 827 EnsurePopupControl();
a340b80d 828
40b26d75
WS
829 wxCHECK_RET( (n == wxNOT_FOUND) || IsValid(n), _T("invalid index in wxOwnerDrawnComboBox::Select") );
830
a340b80d
VZ
831 m_popupInterface->SetSelection(n);
832
833 wxString str;
834 if ( n >= 0 )
835 str = m_popupInterface->GetString(n);
836
837 // Refresh text portion in control
838 if ( m_text )
839 m_text->SetValue( str );
840 else
841 m_valueString = str;
842
843 Refresh();
844}
845
846int wxOwnerDrawnComboBox::GetSelection() const
847{
54a61762
WS
848 if ( !m_popupInterface )
849 return m_initChs.Index(m_valueString);
850
a340b80d
VZ
851 return m_popupInterface->GetSelection();
852}
853
854int wxOwnerDrawnComboBox::DoAppend(const wxString& item)
855{
6d0ce565
VZ
856 EnsurePopupControl();
857 wxASSERT(m_popupInterface);
a340b80d
VZ
858 return m_popupInterface->Append(item);
859}
860
861int wxOwnerDrawnComboBox::DoInsert(const wxString& item, unsigned int pos)
862{
40b26d75
WS
863 EnsurePopupControl();
864
a340b80d 865 wxCHECK_MSG(!(GetWindowStyle() & wxCB_SORT), -1, wxT("can't insert into sorted list"));
85fed18c 866 wxCHECK_MSG(IsValidInsert(pos), -1, wxT("invalid index"));
a340b80d
VZ
867
868 m_popupInterface->Insert(item,pos);
869
870 return pos;
871}
872
873void wxOwnerDrawnComboBox::DoSetItemClientData(unsigned int n, void* clientData)
874{
6d0ce565 875 EnsurePopupControl();
a340b80d
VZ
876 m_popupInterface->SetItemClientData(n,clientData,m_clientDataItemsType);
877}
878
879void* wxOwnerDrawnComboBox::DoGetItemClientData(unsigned int n) const
880{
54a61762
WS
881 if ( !m_popupInterface )
882 return NULL;
883
a340b80d
VZ
884 return m_popupInterface->GetItemClientData(n);
885}
886
887void wxOwnerDrawnComboBox::DoSetItemClientObject(unsigned int n, wxClientData* clientData)
888{
889 DoSetItemClientData(n, (void*) clientData);
890}
891
892wxClientData* wxOwnerDrawnComboBox::DoGetItemClientObject(unsigned int n) const
893{
894 return (wxClientData*) DoGetItemClientData(n);
895}
896
40b26d75
WS
897// ----------------------------------------------------------------------------
898// wxOwnerDrawnComboBox item drawing and measuring default implementations
899// ----------------------------------------------------------------------------
900
901void wxOwnerDrawnComboBox::OnDrawItem( wxDC& dc,
902 const wxRect& rect,
903 int item,
904 int flags ) const
905{
906 if ( flags & wxODCB_PAINTING_CONTROL )
907 {
908 dc.DrawText( GetValue(),
909 rect.x + GetTextIndent(),
910 (rect.height-dc.GetCharHeight())/2 + rect.y );
911 }
912 else
913 {
914 dc.DrawText( m_popupInterface->GetString(item), rect.x + 2, rect.y );
915 }
916}
917
918wxCoord wxOwnerDrawnComboBox::OnMeasureItem( size_t WXUNUSED(item) ) const
919{
920 return -1;
921}
922
923wxCoord wxOwnerDrawnComboBox::OnMeasureItemWidth( size_t WXUNUSED(item) ) const
924{
925 return -1;
926}
927
928void wxOwnerDrawnComboBox::OnDrawBackground(wxDC& dc, const wxRect& rect, int item, int flags) const
929{
930 // we need to render selected and current items differently
931 if ( m_popupInterface->IsCurrent((size_t)item) )
932 {
933 DrawFocusBackground(dc,
934 rect,
935 (flags&wxODCB_PAINTING_CONTROL?0:wxCONTROL_ISSUBMENU) |
936 wxCONTROL_SELECTED);
937 }
938 //else: do nothing for the normal items
939}
940
a57d600f 941#endif // wxUSE_ODCOMBOBOX