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