]> git.saurik.com Git - wxWidgets.git/blame - src/generic/odcombo.cpp
wxAUI: Made fixed size floating panes work
[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{
276ebe79
WS
304 event.Skip();
305
a340b80d 306 // Move selection to cursor if it is inside the popup
a340b80d 307
276ebe79
WS
308 int y = event.GetPosition().y;
309 int fromBottom = GetClientSize().y - y;
310
311 // Since in any case we need to find out if the last item is only
312 // partially visible, we might just as well replicate the HitTest
313 // loop here.
314 const size_t lineMax = GetVisibleEnd();
315 for ( size_t line = GetVisibleBegin(); line < lineMax; line++ )
316 {
317 y -= OnGetLineHeight(line);
318 if ( y < 0 )
319 {
320 // Only change selection if item is fully visible
321 if ( (y + fromBottom) >= 0 )
322 {
323 wxVListBox::SetSelection((int)line);
324 return;
325 }
326 }
327 }
a340b80d
VZ
328}
329
330void wxVListBoxComboPopup::OnLeftClick(wxMouseEvent& WXUNUSED(event))
331{
6d0ce565 332 DismissWithEvent();
a340b80d
VZ
333}
334
335void wxVListBoxComboPopup::OnKey(wxKeyEvent& event)
336{
337 // Select item if ENTER is pressed
338 if ( event.GetKeyCode() == WXK_RETURN || event.GetKeyCode() == WXK_NUMPAD_ENTER )
339 {
6d0ce565 340 DismissWithEvent();
a340b80d
VZ
341 }
342 // Hide popup if ESC is pressed
343 else if ( event.GetKeyCode() == WXK_ESCAPE )
344 Dismiss();
345 else
346 event.Skip();
347}
348
a340b80d
VZ
349void wxVListBoxComboPopup::Insert( const wxString& item, int pos )
350{
351 // Need to change selection?
352 wxString strValue;
353 if ( !(m_combo->GetWindowStyle() & wxCB_READONLY) &&
354 m_combo->GetValue() == item )
6d0ce565 355 {
a340b80d 356 m_value = pos;
6d0ce565 357 }
a340b80d
VZ
358
359 m_strings.Insert(item,pos);
e5d63342
WS
360 m_widths.Insert(-1,pos);
361 m_widthsDirty = true;
a340b80d
VZ
362
363 if ( IsCreated() )
364 wxVListBox::SetItemCount( wxVListBox::GetItemCount()+1 );
a340b80d
VZ
365}
366
367int wxVListBoxComboPopup::Append(const wxString& item)
368{
369 int pos = (int)m_strings.GetCount();
370
371 if ( m_combo->GetWindowStyle() & wxCB_SORT )
372 {
373 // Find position
374 // TODO: Could be optimized with binary search
375 wxArrayString strings = m_strings;
376 unsigned int i;
377
378 for ( i=0; i<strings.GetCount(); i++ )
379 {
380 if ( item.Cmp(strings.Item(i)) < 0 )
381 {
382 pos = (int)i;
383 break;
384 }
385 }
386 }
387
388 Insert(item,pos);
389
390 return pos;
391}
392
393void wxVListBoxComboPopup::Clear()
394{
395 wxASSERT(m_combo);
396
397 m_strings.Empty();
e5d63342
WS
398 m_widths.Empty();
399
400 m_widestWidth = 0;
401 m_widestItem = -1;
a340b80d
VZ
402
403 ClearClientDatas();
404
cdc99ddd
WS
405 m_value = wxNOT_FOUND;
406
a340b80d
VZ
407 if ( IsCreated() )
408 wxVListBox::SetItemCount(0);
409}
410
411void wxVListBoxComboPopup::ClearClientDatas()
412{
413 if ( m_clientDataItemsType == wxClientData_Object )
414 {
415 size_t i;
416 for ( i=0; i<m_clientDatas.GetCount(); i++ )
417 delete (wxClientData*) m_clientDatas[i];
418 }
419
420 m_clientDatas.Empty();
421}
422
423void wxVListBoxComboPopup::SetItemClientData( unsigned int n,
424 void* clientData,
425 wxClientDataType clientDataItemsType )
426{
427 // It should be sufficient to update this variable only here
428 m_clientDataItemsType = clientDataItemsType;
429
430 m_clientDatas.SetCount(n+1,NULL);
431 m_clientDatas[n] = clientData;
e5d63342
WS
432
433 ItemWidthChanged(n);
a340b80d
VZ
434}
435
436void* wxVListBoxComboPopup::GetItemClientData(unsigned int n) const
437{
438 if ( m_clientDatas.GetCount() > n )
439 return m_clientDatas[n];
440
441 return NULL;
442}
443
444void wxVListBoxComboPopup::Delete( unsigned int item )
445{
446 // Remove client data, if set
447 if ( m_clientDatas.GetCount() )
448 {
449 if ( m_clientDataItemsType == wxClientData_Object )
450 delete (wxClientData*) m_clientDatas[item];
451
452 m_clientDatas.RemoveAt(item);
453 }
454
455 m_strings.RemoveAt(item);
e5d63342
WS
456 m_widths.RemoveAt(item);
457
458 if ( (int)item == m_widestItem )
459 m_findWidest = true;
a340b80d
VZ
460
461 if ( IsCreated() )
462 wxVListBox::SetItemCount( wxVListBox::GetItemCount()-1 );
463}
464
9e6aca68 465int wxVListBoxComboPopup::FindString(const wxString& s, bool bCase) const
a340b80d 466{
9e6aca68 467 return m_strings.Index(s, bCase);
a340b80d
VZ
468}
469
470unsigned int wxVListBoxComboPopup::GetCount() const
471{
472 return m_strings.GetCount();
473}
474
475wxString wxVListBoxComboPopup::GetString( int item ) const
476{
477 return m_strings[item];
478}
479
480void wxVListBoxComboPopup::SetString( int item, const wxString& str )
481{
482 m_strings[item] = str;
e5d63342 483 ItemWidthChanged(item);
a340b80d
VZ
484}
485
486wxString wxVListBoxComboPopup::GetStringValue() const
487{
488 if ( m_value >= 0 )
489 return m_strings[m_value];
490 return wxEmptyString;
491}
492
493void wxVListBoxComboPopup::SetSelection( int item )
494{
85fed18c
WS
495 wxCHECK_RET( item == wxNOT_FOUND || ((unsigned int)item < GetCount()),
496 wxT("invalid index in wxVListBoxComboPopup::SetSelection") );
a340b80d
VZ
497
498 m_value = item;
499
500 if ( IsCreated() )
501 wxVListBox::SetSelection(item);
502}
503
6d0ce565
VZ
504int wxVListBoxComboPopup::GetSelection() const
505{
506 return m_value;
507}
508
a340b80d
VZ
509void wxVListBoxComboPopup::SetStringValue( const wxString& value )
510{
511 int index = m_strings.Index(value);
512
513 m_value = index;
514
515 if ( index >= -1 && index < (int)wxVListBox::GetItemCount() )
516 wxVListBox::SetSelection(index);
517}
518
519wxSize wxVListBoxComboPopup::GetAdjustedSize( int minWidth, int prefHeight, int maxHeight )
520{
521 int height = 250;
522
523 if ( m_strings.GetCount() )
524 {
525 if ( prefHeight > 0 )
526 height = prefHeight;
527
528 if ( height > maxHeight )
529 height = maxHeight;
530
531 int totalHeight = GetTotalHeight(); // + 3;
532 if ( height >= totalHeight )
533 {
534 height = totalHeight;
535 }
536 else
537 {
538 // Adjust height to a multiple of the height of the first item
539 // NB: Calculations that take variable height into account
540 // are unnecessary.
541 int fih = GetLineHeight(0);
542 int shown = height/fih;
543 height = shown * fih;
544 }
545 }
546 else
547 height = 50;
548
e5d63342
WS
549 bool doFindWidest = m_findWidest;
550
551 // Measure items with dirty width.
552 if ( m_widthsDirty )
553 {
554 unsigned int i;
555 unsigned int n = m_widths.GetCount();
556 int dirtyHandled = 0;
557 wxArrayInt& widths = m_widths;
558
559 // I think using wxDC::GetTextExtent is faster than
560 // wxWindow::GetTextExtent (assuming same dc is used
561 // for all calls, as we do here).
562 wxClientDC dc(m_combo);
563 dc.SetFont(m_useFont);
564
565 for ( i=0; i<n; i++ )
566 {
567 if ( widths[i] < 0 )
568 {
569 wxCoord x = OnMeasureItemWidth(i);
570
571 if ( x < 0 )
572 {
573 const wxString& text = m_strings[i];
574
575 // To make sure performance won't suck in extreme scenarios,
576 // we'll estimate length after some arbitrary number of items
577 // have been checked precily.
578 if ( dirtyHandled < 1024 )
579 {
580 wxCoord y;
581 dc.GetTextExtent(text, &x, &y, 0, 0);
582 x += 4;
583 }
584 else
585 {
586 x = text.length() * (dc.GetCharWidth()+1);
587 }
588 }
589
590 widths[i] = x;
591
592 if ( x >= m_widestWidth )
593 {
594 m_widestWidth = x;
595 m_widestItem = (int)i;
596 }
597 else if ( (int)i == m_widestItem )
598 {
599 // Width of previously widest item has been decreased, so
600 // we'll have to check all to find current widest item.
601 doFindWidest = true;
602 }
603
604 dirtyHandled++;
605 }
606 }
607
608 m_widthsDirty = false;
609 }
610
611 if ( doFindWidest )
612 {
613 unsigned int i;
614 unsigned int n = m_widths.GetCount();
615
616 int bestWidth = -1;
617 int bestIndex = -1;
618
619 for ( i=0; i<n; i++ )
620 {
621 int w = m_widths[i];
622 if ( w > bestWidth )
623 {
624 bestIndex = (int)i;
625 bestWidth = w;
626 }
627 }
628
629 m_widestWidth = bestWidth;
630 m_widestItem = bestIndex;
631
632 m_findWidest = false;
633 }
634
a340b80d
VZ
635 // Take scrollbar into account in width calculations
636 int widestWidth = m_widestWidth + wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
637 return wxSize(minWidth > widestWidth ? minWidth : widestWidth,
638 height+2);
639}
640
6d0ce565
VZ
641//void wxVListBoxComboPopup::Populate( int n, const wxString choices[] )
642void wxVListBoxComboPopup::Populate( const wxArrayString& choices )
a340b80d
VZ
643{
644 int i;
645
6d0ce565
VZ
646 int n = choices.GetCount();
647
a340b80d
VZ
648 for ( i=0; i<n; i++ )
649 {
6d0ce565 650 const wxString& item = choices.Item(i);
a340b80d 651 m_strings.Add(item);
a340b80d
VZ
652 }
653
e5d63342
WS
654 m_widths.SetCount(n,-1);
655 m_widthsDirty = true;
656
a340b80d
VZ
657 if ( IsCreated() )
658 wxVListBox::SetItemCount(n);
659
660 // Sort the initial choices
661 if ( m_combo->GetWindowStyle() & wxCB_SORT )
662 m_strings.Sort();
663
664 // Find initial selection
665 wxString strValue = m_combo->GetValue();
40b26d75 666 if ( strValue.length() )
a340b80d
VZ
667 m_value = m_strings.Index(strValue);
668}
669
670// ----------------------------------------------------------------------------
671// wxOwnerDrawnComboBox
672// ----------------------------------------------------------------------------
673
674
a57d600f 675BEGIN_EVENT_TABLE(wxOwnerDrawnComboBox, wxComboCtrl)
a340b80d
VZ
676END_EVENT_TABLE()
677
678
a57d600f 679IMPLEMENT_DYNAMIC_CLASS2(wxOwnerDrawnComboBox, wxComboCtrl, wxControlWithItems)
a340b80d
VZ
680
681void wxOwnerDrawnComboBox::Init()
682{
683}
684
685bool wxOwnerDrawnComboBox::Create(wxWindow *parent,
686 wxWindowID id,
687 const wxString& value,
688 const wxPoint& pos,
689 const wxSize& size,
690 long style,
691 const wxValidator& validator,
692 const wxString& name)
693{
a57d600f 694 return wxComboCtrl::Create(parent,id,value,pos,size,style,validator,name);
a340b80d
VZ
695}
696
697wxOwnerDrawnComboBox::wxOwnerDrawnComboBox(wxWindow *parent,
698 wxWindowID id,
699 const wxString& value,
700 const wxPoint& pos,
701 const wxSize& size,
702 const wxArrayString& choices,
703 long style,
704 const wxValidator& validator,
705 const wxString& name)
a57d600f 706 : wxComboCtrl()
a340b80d
VZ
707{
708 Init();
709
710 Create(parent,id,value,pos,size,choices,style, validator, name);
711}
712
713bool wxOwnerDrawnComboBox::Create(wxWindow *parent,
714 wxWindowID id,
715 const wxString& value,
716 const wxPoint& pos,
717 const wxSize& size,
718 const wxArrayString& choices,
719 long style,
720 const wxValidator& validator,
721 const wxString& name)
722{
6d0ce565
VZ
723 m_initChs = choices;
724 //wxCArrayString chs(choices);
a340b80d 725
6d0ce565
VZ
726 //return Create(parent, id, value, pos, size, chs.GetCount(),
727 // chs.GetStrings(), style, validator, name);
728 return Create(parent, id, value, pos, size, 0,
729 NULL, style, validator, name);
a340b80d
VZ
730}
731
732bool wxOwnerDrawnComboBox::Create(wxWindow *parent,
733 wxWindowID id,
734 const wxString& value,
735 const wxPoint& pos,
736 const wxSize& size,
737 int n,
738 const wxString choices[],
739 long style,
740 const wxValidator& validator,
741 const wxString& name)
742{
743
744 if ( !Create(parent, id, value, pos, size, style,
745 validator, name) )
746 {
747 return false;
748 }
749
6d0ce565
VZ
750 int i;
751 for ( i=0; i<n; i++ )
752 m_initChs.Add(choices[i]);
a340b80d
VZ
753
754 return true;
755}
756
757wxOwnerDrawnComboBox::~wxOwnerDrawnComboBox()
758{
759 if ( m_popupInterface )
57693473 760 GetVListBoxComboPopup()->ClearClientDatas();
a340b80d
VZ
761}
762
db53c6ea 763void wxOwnerDrawnComboBox::DoSetPopupControl(wxComboPopup* popup)
6d0ce565
VZ
764{
765 if ( !popup )
766 {
767 popup = new wxVListBoxComboPopup();
768 }
769
db53c6ea 770 wxComboCtrl::DoSetPopupControl(popup);
6d0ce565
VZ
771
772 wxASSERT(popup);
6d0ce565
VZ
773
774 // Add initial choices to the wxVListBox
57693473 775 if ( !GetVListBoxComboPopup()->GetCount() )
6d0ce565 776 {
57693473 777 GetVListBoxComboPopup()->Populate(m_initChs);
6d0ce565
VZ
778 m_initChs.Clear();
779 }
780}
781
a340b80d
VZ
782// ----------------------------------------------------------------------------
783// wxOwnerDrawnComboBox item manipulation methods
784// ----------------------------------------------------------------------------
785
786void wxOwnerDrawnComboBox::Clear()
787{
6d0ce565 788 EnsurePopupControl();
a340b80d 789
57693473 790 GetVListBoxComboPopup()->Clear();
a340b80d 791
cdc99ddd 792 SetValue(wxEmptyString);
a340b80d
VZ
793}
794
795void wxOwnerDrawnComboBox::Delete(unsigned int n)
796{
85fed18c 797 wxCHECK_RET( IsValid(n), _T("invalid index in wxOwnerDrawnComboBox::Delete") );
a340b80d
VZ
798
799 if ( GetSelection() == (int) n )
800 SetValue(wxEmptyString);
801
57693473 802 GetVListBoxComboPopup()->Delete(n);
a340b80d
VZ
803}
804
805unsigned int wxOwnerDrawnComboBox::GetCount() const
806{
54a61762
WS
807 if ( !m_popupInterface )
808 return m_initChs.GetCount();
809
57693473 810 return GetVListBoxComboPopup()->GetCount();
a340b80d
VZ
811}
812
813wxString wxOwnerDrawnComboBox::GetString(unsigned int n) const
814{
85fed18c 815 wxCHECK_MSG( IsValid(n), wxEmptyString, _T("invalid index in wxOwnerDrawnComboBox::GetString") );
54a61762
WS
816
817 if ( !m_popupInterface )
818 return m_initChs.Item(n);
819
57693473 820 return GetVListBoxComboPopup()->GetString(n);
a340b80d
VZ
821}
822
823void wxOwnerDrawnComboBox::SetString(unsigned int n, const wxString& s)
824{
54a61762
WS
825 EnsurePopupControl();
826
85fed18c 827 wxCHECK_RET( IsValid(n), _T("invalid index in wxOwnerDrawnComboBox::SetString") );
54a61762 828
57693473 829 GetVListBoxComboPopup()->SetString(n,s);
a340b80d
VZ
830}
831
9e6aca68 832int wxOwnerDrawnComboBox::FindString(const wxString& s, bool bCase) const
a340b80d 833{
54a61762
WS
834 if ( !m_popupInterface )
835 return m_initChs.Index(s, bCase);
836
57693473 837 return GetVListBoxComboPopup()->FindString(s, bCase);
a340b80d
VZ
838}
839
840void wxOwnerDrawnComboBox::Select(int n)
841{
6d0ce565 842 EnsurePopupControl();
a340b80d 843
40b26d75
WS
844 wxCHECK_RET( (n == wxNOT_FOUND) || IsValid(n), _T("invalid index in wxOwnerDrawnComboBox::Select") );
845
57693473 846 GetVListBoxComboPopup()->SetSelection(n);
a340b80d
VZ
847
848 wxString str;
849 if ( n >= 0 )
57693473 850 str = GetVListBoxComboPopup()->GetString(n);
a340b80d
VZ
851
852 // Refresh text portion in control
853 if ( m_text )
854 m_text->SetValue( str );
855 else
856 m_valueString = str;
857
858 Refresh();
859}
860
861int wxOwnerDrawnComboBox::GetSelection() const
862{
54a61762
WS
863 if ( !m_popupInterface )
864 return m_initChs.Index(m_valueString);
865
57693473 866 return GetVListBoxComboPopup()->GetSelection();
a340b80d
VZ
867}
868
869int wxOwnerDrawnComboBox::DoAppend(const wxString& item)
870{
6d0ce565
VZ
871 EnsurePopupControl();
872 wxASSERT(m_popupInterface);
57693473
VZ
873
874 return GetVListBoxComboPopup()->Append(item);
a340b80d
VZ
875}
876
877int wxOwnerDrawnComboBox::DoInsert(const wxString& item, unsigned int pos)
878{
40b26d75
WS
879 EnsurePopupControl();
880
a340b80d 881 wxCHECK_MSG(!(GetWindowStyle() & wxCB_SORT), -1, wxT("can't insert into sorted list"));
85fed18c 882 wxCHECK_MSG(IsValidInsert(pos), -1, wxT("invalid index"));
a340b80d 883
57693473 884 GetVListBoxComboPopup()->Insert(item,pos);
a340b80d
VZ
885
886 return pos;
887}
888
889void wxOwnerDrawnComboBox::DoSetItemClientData(unsigned int n, void* clientData)
890{
6d0ce565 891 EnsurePopupControl();
57693473
VZ
892
893 GetVListBoxComboPopup()->SetItemClientData(n,clientData,m_clientDataItemsType);
a340b80d
VZ
894}
895
896void* wxOwnerDrawnComboBox::DoGetItemClientData(unsigned int n) const
897{
54a61762
WS
898 if ( !m_popupInterface )
899 return NULL;
900
57693473 901 return GetVListBoxComboPopup()->GetItemClientData(n);
a340b80d
VZ
902}
903
904void wxOwnerDrawnComboBox::DoSetItemClientObject(unsigned int n, wxClientData* clientData)
905{
906 DoSetItemClientData(n, (void*) clientData);
907}
908
909wxClientData* wxOwnerDrawnComboBox::DoGetItemClientObject(unsigned int n) const
910{
911 return (wxClientData*) DoGetItemClientData(n);
912}
913
40b26d75
WS
914// ----------------------------------------------------------------------------
915// wxOwnerDrawnComboBox item drawing and measuring default implementations
916// ----------------------------------------------------------------------------
917
918void wxOwnerDrawnComboBox::OnDrawItem( wxDC& dc,
919 const wxRect& rect,
920 int item,
921 int flags ) const
922{
923 if ( flags & wxODCB_PAINTING_CONTROL )
924 {
925 dc.DrawText( GetValue(),
926 rect.x + GetTextIndent(),
927 (rect.height-dc.GetCharHeight())/2 + rect.y );
928 }
929 else
930 {
57693473 931 dc.DrawText( GetVListBoxComboPopup()->GetString(item), rect.x + 2, rect.y );
40b26d75
WS
932 }
933}
934
935wxCoord wxOwnerDrawnComboBox::OnMeasureItem( size_t WXUNUSED(item) ) const
936{
937 return -1;
938}
939
940wxCoord wxOwnerDrawnComboBox::OnMeasureItemWidth( size_t WXUNUSED(item) ) const
941{
942 return -1;
943}
944
945void wxOwnerDrawnComboBox::OnDrawBackground(wxDC& dc, const wxRect& rect, int item, int flags) const
946{
947 // we need to render selected and current items differently
57693473 948 if ( GetVListBoxComboPopup()->IsCurrent((size_t)item) )
40b26d75
WS
949 {
950 DrawFocusBackground(dc,
951 rect,
952 (flags&wxODCB_PAINTING_CONTROL?0:wxCONTROL_ISSUBMENU) |
953 wxCONTROL_SELECTED);
954 }
955 //else: do nothing for the normal items
956}
957
a57d600f 958#endif // wxUSE_ODCOMBOBOX