]> git.saurik.com Git - wxWidgets.git/blame - src/generic/odcombo.cpp
support for toggle bitmap button
[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"
0ec1179b 36 #include "wx/textctrl.h"
a340b80d
VZ
37#endif
38
39#include "wx/combo.h"
a340b80d
VZ
40
41// ============================================================================
42// implementation
43// ============================================================================
44
dc8a1aa5
AB
45// time in milliseconds before partial completion buffer drops
46#define wxODCB_PARTIAL_COMPLETION_TIME 1000
a340b80d
VZ
47
48// ----------------------------------------------------------------------------
49// wxVListBoxComboPopup is a wxVListBox customized to act as a popup control
50//
51// ----------------------------------------------------------------------------
52
53
54BEGIN_EVENT_TABLE(wxVListBoxComboPopup, wxVListBox)
55 EVT_MOTION(wxVListBoxComboPopup::OnMouseMove)
56 EVT_KEY_DOWN(wxVListBoxComboPopup::OnKey)
57 EVT_LEFT_UP(wxVListBoxComboPopup::OnLeftClick)
58END_EVENT_TABLE()
59
60
6d0ce565 61void wxVListBoxComboPopup::Init()
a340b80d
VZ
62{
63 m_widestWidth = 0;
e5d63342
WS
64 m_widestItem = -1;
65 m_widthsDirty = false;
66 m_findWidest = false;
a340b80d
VZ
67 m_itemHeight = 0;
68 m_value = -1;
69 m_itemHover = -1;
70 m_clientDataItemsType = wxClientData_None;
dc8a1aa5 71 m_partialCompletionString = wxEmptyString;
a340b80d
VZ
72}
73
74bool 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
6d0ce565 83 m_useFont = m_combo->GetFont();
a340b80d
VZ
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
93wxVListBoxComboPopup::~wxVListBoxComboPopup()
94{
95 Clear();
96}
97
98bool 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
107void wxVListBoxComboPopup::PaintComboControl( wxDC& dc, const wxRect& rect )
108{
109 if ( !(m_combo->GetWindowStyle() & wxODCB_STD_CONTROL_PAINT) )
110 {
ce22ac45
RR
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
a340b80d
VZ
118 if ( m_value >= 0 )
119 {
b4ff336e 120 OnDrawItem(dc,rect,m_value,flags);
6d0ce565 121 return;
a340b80d
VZ
122 }
123 }
124
125 wxComboPopup::PaintComboControl(dc,rect);
126}
127
128void wxVListBoxComboPopup::OnDrawItem(wxDC& dc, const wxRect& rect, size_t n) const
129{
6d0ce565
VZ
130 // TODO: Maybe this code could be moved to wxVListBox::OnPaint?
131 dc.SetFont(m_useFont);
a340b80d 132
b4ff336e
VZ
133 int flags = 0;
134
a340b80d 135 // Set correct text colour for selected items
6d0ce565 136 if ( wxVListBox::GetSelection() == (int) n )
b4ff336e 137 {
a340b80d 138 dc.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT) );
b4ff336e
VZ
139 flags |= wxODCB_PAINTING_SELECTED;
140 }
a340b80d 141 else
b4ff336e 142 {
a340b80d 143 dc.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT) );
b4ff336e 144 }
a340b80d 145
b4ff336e 146 OnDrawItem(dc,rect,(int)n,flags);
a340b80d
VZ
147}
148
40b26d75 149wxCoord wxVListBoxComboPopup::OnMeasureItem(size_t n) const
a340b80d 150{
40b26d75
WS
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;
6d0ce565 160}
a340b80d 161
40b26d75 162wxCoord wxVListBoxComboPopup::OnMeasureItemWidth(size_t n) const
6d0ce565 163{
40b26d75
WS
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
172void 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
ce22ac45
RR
182 if ( IsCurrent((size_t)item) && !(flags & wxODCB_PAINTING_CONTROL) )
183 flags |= wxODCB_PAINTING_SELECTED;
184
40b26d75 185 combo->OnDrawBackground(dc,rect,item,flags);
a340b80d
VZ
186}
187
188void wxVListBoxComboPopup::OnDrawBackground(wxDC& dc, const wxRect& rect, size_t n) const
189{
40b26d75 190 OnDrawBg(dc,rect,(int)n,0);
a340b80d
VZ
191}
192
6d0ce565
VZ
193// This is called from wxVListBoxComboPopup::OnDrawItem, with text colour and font prepared
194void wxVListBoxComboPopup::OnDrawItem( wxDC& dc, const wxRect& rect, int item, int flags ) const
195{
40b26d75
WS
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);
6d0ce565
VZ
202}
203
204void wxVListBoxComboPopup::DismissWithEvent()
205{
dc8a1aa5
AB
206 StopPartialCompletion();
207
6d0ce565
VZ
208 int selection = wxVListBox::GetSelection();
209
210 Dismiss();
211
6d0ce565 212 if ( selection != wxNOT_FOUND )
fdb47e62 213 m_stringValue = m_strings[selection];
6d0ce565 214 else
fdb47e62 215 m_stringValue = wxEmptyString;
6d0ce565 216
fdb47e62
VZ
217 if ( m_stringValue != m_combo->GetValue() )
218 m_combo->SetValueWithEvent(m_stringValue);
6d0ce565 219
fdb47e62 220 m_value = selection;
6d0ce565
VZ
221
222 SendComboBoxEvent(selection);
223}
224
225void wxVListBoxComboPopup::SendComboBoxEvent( int selection )
a340b80d
VZ
226{
227 wxCommandEvent evt(wxEVT_COMMAND_COMBOBOX_SELECTED,m_combo->GetId());
a340b80d
VZ
228
229 evt.SetEventObject(m_combo);
6d0ce565 230
a340b80d
VZ
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
dc8a1aa5 247bool wxVListBoxComboPopup::HandleKey( int keycode, bool saturate, wxChar unicode )
a340b80d 248{
704bc436
VZ
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
a340b80d 256 int value = m_value;
dc8a1aa5
AB
257 int comboStyle = m_combo->GetWindowStyle();
258
259 // this is the character equivalent of the code
b5b3c8a0
JS
260 wxChar keychar = 0;
261 if ( keycode < WXK_START )
dc8a1aa5 262 {
b5b3c8a0
JS
263 if ( unicode > 0 )
264 {
265 if ( wxIsprint(unicode) )
266 keychar = unicode;
267 }
268 else if ( wxIsprint(keycode) )
269 {
270 keychar = (wxChar) keycode;
271 }
dc8a1aa5 272 }
a340b80d
VZ
273
274 if ( keycode == WXK_DOWN || keycode == WXK_RIGHT )
275 {
276 value++;
dc8a1aa5 277 StopPartialCompletion();
a340b80d
VZ
278 }
279 else if ( keycode == WXK_UP || keycode == WXK_LEFT )
280 {
281 value--;
dc8a1aa5 282 StopPartialCompletion();
a340b80d
VZ
283 }
284 else if ( keycode == WXK_PAGEDOWN )
285 {
286 value+=10;
dc8a1aa5 287 StopPartialCompletion();
a340b80d
VZ
288 }
289 else if ( keycode == WXK_PAGEUP )
290 {
291 value-=10;
dc8a1aa5
AB
292 StopPartialCompletion();
293 }
b5b3c8a0 294 else if ( keychar && (comboStyle & wxCB_READONLY) )
dc8a1aa5
AB
295 {
296 // Try partial completion
297
298 // find the new partial completion string
f0fa8b47 299#if wxUSE_TIMER
dc8a1aa5
AB
300 if (m_partialCompletionTimer.IsRunning())
301 m_partialCompletionString+=wxString(keychar);
302 else
f0fa8b47 303#endif // wxUSE_TIMER
dc8a1aa5
AB
304 m_partialCompletionString=wxString(keychar);
305
306 // now search through the values to see if this is found
307 int found = -1;
4dd31ff5 308 unsigned int length=m_partialCompletionString.length();
dc8a1aa5
AB
309 int i;
310 for (i=0; i<itemCount; i++)
311 {
312 wxString item=GetString(i);
4dd31ff5 313 if (( item.length() >= length) && (! m_partialCompletionString.CmpNoCase(item.Left(length))))
dc8a1aa5
AB
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;
f0fa8b47 329#if wxUSE_TIMER
dc8a1aa5 330 m_partialCompletionTimer.Start(wxODCB_PARTIAL_COMPLETION_TIME, true);
f0fa8b47 331#endif // wxUSE_TIMER
dc8a1aa5 332 }
a340b80d 333 }
a340b80d
VZ
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
a340b80d
VZ
359 if ( value >= 0 )
360 m_combo->SetValue(m_strings[value]);
361
6d0ce565 362 SendComboBoxEvent(m_value);
a340b80d
VZ
363
364 return true;
365}
366
dc8a1aa5
AB
367// stop partial completion
368void wxVListBoxComboPopup::StopPartialCompletion()
369{
370 m_partialCompletionString = wxEmptyString;
f0fa8b47 371#if wxUSE_TIMER
dc8a1aa5 372 m_partialCompletionTimer.Stop();
f0fa8b47 373#endif // wxUSE_TIMER
dc8a1aa5
AB
374}
375
a340b80d
VZ
376void 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
385void wxVListBoxComboPopup::OnComboKeyEvent( wxKeyEvent& event )
386{
387 // Saturated key movement on
dc8a1aa5
AB
388 if ( !HandleKey(event.GetKeyCode(),true,
389#if wxUSE_UNICODE
390 event.GetUnicodeKey()
391#else
392 0
393#endif
394 ) )
a340b80d
VZ
395 event.Skip();
396}
397
398void 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
404void wxVListBoxComboPopup::OnMouseMove(wxMouseEvent& event)
405{
276ebe79
WS
406 event.Skip();
407
a340b80d 408 // Move selection to cursor if it is inside the popup
a340b80d 409
276ebe79
WS
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 {
e02c72fa 419 y -= OnGetRowHeight(line);
276ebe79
WS
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 }
a340b80d
VZ
430}
431
432void wxVListBoxComboPopup::OnLeftClick(wxMouseEvent& WXUNUSED(event))
433{
6d0ce565 434 DismissWithEvent();
a340b80d
VZ
435}
436
437void wxVListBoxComboPopup::OnKey(wxKeyEvent& event)
438{
b445b6a7
VZ
439 // Hide popup if certain key or key combination was pressed
440 if ( m_combo->IsKeyPopupToggle(event) )
dc8a1aa5
AB
441 {
442 StopPartialCompletion();
a340b80d 443 Dismiss();
dc8a1aa5 444 }
b445b6a7
VZ
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 }
a340b80d 457 else
dc8a1aa5
AB
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
d9b72d25 462 if ((comboStyle & wxCB_READONLY) &&
dc8a1aa5
AB
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 }
a340b80d
VZ
471}
472
a340b80d
VZ
473void 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 )
6d0ce565 479 {
a340b80d 480 m_value = pos;
6d0ce565 481 }
a340b80d
VZ
482
483 m_strings.Insert(item,pos);
697c314b 484 if ( (int)m_clientDatas.size() >= pos )
57306cd4 485 m_clientDatas.Insert(NULL, pos);
a236aa20 486
e5d63342
WS
487 m_widths.Insert(-1,pos);
488 m_widthsDirty = true;
a340b80d
VZ
489
490 if ( IsCreated() )
491 wxVListBox::SetItemCount( wxVListBox::GetItemCount()+1 );
a340b80d
VZ
492}
493
494int 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 {
31329795 507 if ( item.CmpNoCase(strings.Item(i)) < 0 )
a340b80d
VZ
508 {
509 pos = (int)i;
510 break;
511 }
512 }
513 }
514
515 Insert(item,pos);
516
517 return pos;
518}
519
520void wxVListBoxComboPopup::Clear()
521{
522 wxASSERT(m_combo);
523
524 m_strings.Empty();
e5d63342
WS
525 m_widths.Empty();
526
527 m_widestWidth = 0;
528 m_widestItem = -1;
a340b80d
VZ
529
530 ClearClientDatas();
531
cdc99ddd
WS
532 m_value = wxNOT_FOUND;
533
a340b80d
VZ
534 if ( IsCreated() )
535 wxVListBox::SetItemCount(0);
536}
537
538void 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
550void 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
a340b80d 557 m_clientDatas[n] = clientData;
e5d63342
WS
558
559 ItemWidthChanged(n);
a340b80d
VZ
560}
561
562void* wxVListBoxComboPopup::GetItemClientData(unsigned int n) const
563{
564 if ( m_clientDatas.GetCount() > n )
565 return m_clientDatas[n];
566
567 return NULL;
568}
569
570void 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);
e5d63342
WS
582 m_widths.RemoveAt(item);
583
584 if ( (int)item == m_widestItem )
585 m_findWidest = true;
a340b80d 586
fa3ebb12
RR
587 int sel = GetSelection();
588
a340b80d
VZ
589 if ( IsCreated() )
590 wxVListBox::SetItemCount( wxVListBox::GetItemCount()-1 );
fa3ebb12
RR
591
592 // Fix selection
593 if ( (int)item < sel )
594 SetSelection(sel-1);
595 else if ( (int)item == sel )
596 SetSelection(wxNOT_FOUND);
a340b80d
VZ
597}
598
9e6aca68 599int wxVListBoxComboPopup::FindString(const wxString& s, bool bCase) const
a340b80d 600{
9e6aca68 601 return m_strings.Index(s, bCase);
a340b80d
VZ
602}
603
604unsigned int wxVListBoxComboPopup::GetCount() const
605{
606 return m_strings.GetCount();
607}
608
609wxString wxVListBoxComboPopup::GetString( int item ) const
610{
611 return m_strings[item];
612}
613
614void wxVListBoxComboPopup::SetString( int item, const wxString& str )
615{
616 m_strings[item] = str;
e5d63342 617 ItemWidthChanged(item);
a340b80d
VZ
618}
619
620wxString wxVListBoxComboPopup::GetStringValue() const
621{
fdb47e62 622 return m_stringValue;
a340b80d
VZ
623}
624
625void wxVListBoxComboPopup::SetSelection( int item )
626{
85fed18c
WS
627 wxCHECK_RET( item == wxNOT_FOUND || ((unsigned int)item < GetCount()),
628 wxT("invalid index in wxVListBoxComboPopup::SetSelection") );
a340b80d
VZ
629
630 m_value = item;
631
fdb47e62
VZ
632 if ( item >= 0 )
633 m_stringValue = m_strings[item];
634 else
635 m_stringValue = wxEmptyString;
636
a340b80d
VZ
637 if ( IsCreated() )
638 wxVListBox::SetSelection(item);
639}
640
6d0ce565
VZ
641int wxVListBoxComboPopup::GetSelection() const
642{
643 return m_value;
644}
645
a340b80d
VZ
646void wxVListBoxComboPopup::SetStringValue( const wxString& value )
647{
648 int index = m_strings.Index(value);
649
fdb47e62 650 m_stringValue = value;
a340b80d 651
fdb47e62
VZ
652 if ( index >= 0 && index < (int)wxVListBox::GetItemCount() )
653 {
a340b80d 654 wxVListBox::SetSelection(index);
fdb47e62
VZ
655 m_value = index;
656 }
a340b80d
VZ
657}
658
d9e0958c 659void wxVListBoxComboPopup::CalcWidths()
a340b80d 660{
e5d63342
WS
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 }
d9e0958c
AB
746}
747
748wxSize wxVListBoxComboPopup::GetAdjustedSize( int minWidth, int prefHeight, int maxHeight )
749{
750 int height = 250;
751
7962f85a
RR
752 maxHeight -= 2; // Must take borders into account
753
d9e0958c
AB
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;
a44c4abe
JS
763
764 // Take borders into account on Mac or scrollbars always appear
765#if defined(__WXMAC__)
766 totalHeight += 2;
767#endif
d9e0958c
AB
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);
7962f85a 778 height -= height % fih;
d9e0958c
AB
779 }
780 }
781 else
782 height = 50;
783
784 CalcWidths();
e5d63342 785
a340b80d
VZ
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
6d0ce565
VZ
792//void wxVListBoxComboPopup::Populate( int n, const wxString choices[] )
793void wxVListBoxComboPopup::Populate( const wxArrayString& choices )
a340b80d
VZ
794{
795 int i;
796
6d0ce565
VZ
797 int n = choices.GetCount();
798
a340b80d
VZ
799 for ( i=0; i<n; i++ )
800 {
6d0ce565 801 const wxString& item = choices.Item(i);
a340b80d 802 m_strings.Add(item);
a340b80d
VZ
803 }
804
e5d63342
WS
805 m_widths.SetCount(n,-1);
806 m_widthsDirty = true;
807
a340b80d
VZ
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();
40b26d75 817 if ( strValue.length() )
a340b80d
VZ
818 m_value = m_strings.Index(strValue);
819}
820
821// ----------------------------------------------------------------------------
822// wxOwnerDrawnComboBox
823// ----------------------------------------------------------------------------
824
825
a57d600f 826BEGIN_EVENT_TABLE(wxOwnerDrawnComboBox, wxComboCtrl)
a340b80d
VZ
827END_EVENT_TABLE()
828
829
afc89ff4
MB
830#if wxUSE_EXTENDED_RTTI
831IMPLEMENT_DYNAMIC_CLASS2_XTI(wxOwnerDrawnComboBox, wxComboCtrl, wxControlWithItems, "wx/odcombo.h")
832
833wxBEGIN_PROPERTIES_TABLE(wxOwnerDrawnComboBox)
834wxEND_PROPERTIES_TABLE()
835
836wxBEGIN_HANDLERS_TABLE(wxOwnerDrawnComboBox)
837wxEND_HANDLERS_TABLE()
838
839wxCONSTRUCTOR_5( wxOwnerDrawnComboBox , wxWindow* , Parent , wxWindowID , Id , wxString , Value , wxPoint , Position , wxSize , Size )
840#else
a57d600f 841IMPLEMENT_DYNAMIC_CLASS2(wxOwnerDrawnComboBox, wxComboCtrl, wxControlWithItems)
afc89ff4 842#endif
a340b80d
VZ
843
844void wxOwnerDrawnComboBox::Init()
845{
846}
847
848bool 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{
a57d600f 857 return wxComboCtrl::Create(parent,id,value,pos,size,style,validator,name);
a340b80d
VZ
858}
859
860wxOwnerDrawnComboBox::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)
a57d600f 869 : wxComboCtrl()
a340b80d
VZ
870{
871 Init();
872
873 Create(parent,id,value,pos,size,choices,style, validator, name);
874}
875
876bool 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{
6d0ce565
VZ
886 m_initChs = choices;
887 //wxCArrayString chs(choices);
a340b80d 888
6d0ce565
VZ
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);
a340b80d
VZ
893}
894
895bool 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
6d0ce565
VZ
913 int i;
914 for ( i=0; i<n; i++ )
915 m_initChs.Add(choices[i]);
a340b80d
VZ
916
917 return true;
918}
919
920wxOwnerDrawnComboBox::~wxOwnerDrawnComboBox()
921{
922 if ( m_popupInterface )
57693473 923 GetVListBoxComboPopup()->ClearClientDatas();
a340b80d
VZ
924}
925
db53c6ea 926void wxOwnerDrawnComboBox::DoSetPopupControl(wxComboPopup* popup)
6d0ce565
VZ
927{
928 if ( !popup )
929 {
930 popup = new wxVListBoxComboPopup();
931 }
932
db53c6ea 933 wxComboCtrl::DoSetPopupControl(popup);
6d0ce565
VZ
934
935 wxASSERT(popup);
6d0ce565
VZ
936
937 // Add initial choices to the wxVListBox
57693473 938 if ( !GetVListBoxComboPopup()->GetCount() )
6d0ce565 939 {
57693473 940 GetVListBoxComboPopup()->Populate(m_initChs);
6d0ce565
VZ
941 m_initChs.Clear();
942 }
943}
944
a340b80d
VZ
945// ----------------------------------------------------------------------------
946// wxOwnerDrawnComboBox item manipulation methods
947// ----------------------------------------------------------------------------
948
a236aa20 949void wxOwnerDrawnComboBox::DoClear()
a340b80d 950{
6d0ce565 951 EnsurePopupControl();
a340b80d 952
57693473 953 GetVListBoxComboPopup()->Clear();
a340b80d 954
cdc99ddd 955 SetValue(wxEmptyString);
a340b80d
VZ
956}
957
a236aa20 958void wxOwnerDrawnComboBox::DoDeleteOneItem(unsigned int n)
a340b80d 959{
9a83f860 960 wxCHECK_RET( IsValid(n), wxT("invalid index in wxOwnerDrawnComboBox::Delete") );
a340b80d
VZ
961
962 if ( GetSelection() == (int) n )
963 SetValue(wxEmptyString);
964
57693473 965 GetVListBoxComboPopup()->Delete(n);
a340b80d
VZ
966}
967
968unsigned int wxOwnerDrawnComboBox::GetCount() const
969{
54a61762
WS
970 if ( !m_popupInterface )
971 return m_initChs.GetCount();
972
57693473 973 return GetVListBoxComboPopup()->GetCount();
a340b80d
VZ
974}
975
976wxString wxOwnerDrawnComboBox::GetString(unsigned int n) const
977{
9a83f860 978 wxCHECK_MSG( IsValid(n), wxEmptyString, wxT("invalid index in wxOwnerDrawnComboBox::GetString") );
54a61762
WS
979
980 if ( !m_popupInterface )
981 return m_initChs.Item(n);
982
57693473 983 return GetVListBoxComboPopup()->GetString(n);
a340b80d
VZ
984}
985
986void wxOwnerDrawnComboBox::SetString(unsigned int n, const wxString& s)
987{
54a61762
WS
988 EnsurePopupControl();
989
9a83f860 990 wxCHECK_RET( IsValid(n), wxT("invalid index in wxOwnerDrawnComboBox::SetString") );
54a61762 991
57693473 992 GetVListBoxComboPopup()->SetString(n,s);
a340b80d
VZ
993}
994
9e6aca68 995int wxOwnerDrawnComboBox::FindString(const wxString& s, bool bCase) const
a340b80d 996{
54a61762
WS
997 if ( !m_popupInterface )
998 return m_initChs.Index(s, bCase);
999
57693473 1000 return GetVListBoxComboPopup()->FindString(s, bCase);
a340b80d
VZ
1001}
1002
1003void wxOwnerDrawnComboBox::Select(int n)
1004{
6d0ce565 1005 EnsurePopupControl();
a340b80d 1006
9a83f860 1007 wxCHECK_RET( (n == wxNOT_FOUND) || IsValid(n), wxT("invalid index in wxOwnerDrawnComboBox::Select") );
40b26d75 1008
57693473 1009 GetVListBoxComboPopup()->SetSelection(n);
a340b80d
VZ
1010
1011 wxString str;
1012 if ( n >= 0 )
57693473 1013 str = GetVListBoxComboPopup()->GetString(n);
a340b80d
VZ
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
1024int wxOwnerDrawnComboBox::GetSelection() const
1025{
54a61762
WS
1026 if ( !m_popupInterface )
1027 return m_initChs.Index(m_valueString);
1028
57693473 1029 return GetVListBoxComboPopup()->GetSelection();
a340b80d
VZ
1030}
1031
a236aa20
VZ
1032int wxOwnerDrawnComboBox::DoInsertItems(const wxArrayStringsAdapter& items,
1033 unsigned int pos,
1034 void **clientData,
1035 wxClientDataType type)
a340b80d 1036{
40b26d75
WS
1037 EnsurePopupControl();
1038
a236aa20 1039 const unsigned int count = items.GetCount();
fd11f777
VZ
1040
1041 if ( HasFlag(wxCB_SORT) )
a236aa20 1042 {
fd11f777
VZ
1043 int n = pos;
1044
1045 for ( unsigned int i = 0; i < count; ++i )
1046 {
31329795 1047 n = GetVListBoxComboPopup()->Append(items[i]);
fd11f777
VZ
1048 AssignNewItemClientData(n, clientData, i, type);
1049 }
1050
1051 return n;
a236aa20 1052 }
fd11f777
VZ
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 }
a340b80d 1060
fd11f777
VZ
1061 return pos - 1;
1062 }
a340b80d
VZ
1063}
1064
1065void wxOwnerDrawnComboBox::DoSetItemClientData(unsigned int n, void* clientData)
1066{
6d0ce565 1067 EnsurePopupControl();
57693473 1068
131b1fba
VZ
1069 GetVListBoxComboPopup()->SetItemClientData(n, clientData,
1070 GetClientDataType());
a340b80d
VZ
1071}
1072
1073void* wxOwnerDrawnComboBox::DoGetItemClientData(unsigned int n) const
1074{
54a61762
WS
1075 if ( !m_popupInterface )
1076 return NULL;
1077
57693473 1078 return GetVListBoxComboPopup()->GetItemClientData(n);
a340b80d
VZ
1079}
1080
40b26d75
WS
1081// ----------------------------------------------------------------------------
1082// wxOwnerDrawnComboBox item drawing and measuring default implementations
1083// ----------------------------------------------------------------------------
1084
1085void wxOwnerDrawnComboBox::OnDrawItem( wxDC& dc,
1086 const wxRect& rect,
1087 int item,
1088 int flags ) const
1089{
1090 if ( flags & wxODCB_PAINTING_CONTROL )
1091 {
107defe3
JS
1092 wxString text;
1093
1094 if ( !ShouldUseHintText() )
1095 {
1096 text = GetValue();
1097 }
1098 else
1099 {
1100 text = GetHint();
1101 wxColour col = wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT);
1102 dc.SetTextForeground(col);
1103 }
1104
1105 dc.DrawText( text,
0847e36e 1106 rect.x + GetMargins().x,
40b26d75
WS
1107 (rect.height-dc.GetCharHeight())/2 + rect.y );
1108 }
1109 else
1110 {
57693473 1111 dc.DrawText( GetVListBoxComboPopup()->GetString(item), rect.x + 2, rect.y );
40b26d75
WS
1112 }
1113}
1114
1115wxCoord wxOwnerDrawnComboBox::OnMeasureItem( size_t WXUNUSED(item) ) const
1116{
1117 return -1;
1118}
1119
1120wxCoord wxOwnerDrawnComboBox::OnMeasureItemWidth( size_t WXUNUSED(item) ) const
1121{
1122 return -1;
1123}
1124
ce22ac45
RR
1125void wxOwnerDrawnComboBox::OnDrawBackground(wxDC& dc,
1126 const wxRect& rect,
1127 int WXUNUSED(item),
1128 int flags) const
40b26d75 1129{
ce22ac45
RR
1130 // We need only to explicitly draw background for items
1131 // that should have selected background. Also, call PrepareBackground
1132 // always when painting the control so that clipping is done properly.
1133
1134 if ( (flags & wxODCB_PAINTING_SELECTED) ||
1135 ((flags & wxODCB_PAINTING_CONTROL) && HasFlag(wxCB_READONLY)) )
40b26d75 1136 {
118f5fbd 1137 int bgFlags = wxCONTROL_SELECTED;
4dd31ff5 1138
ce22ac45 1139 if ( !(flags & wxODCB_PAINTING_CONTROL) )
118f5fbd 1140 bgFlags |= wxCONTROL_ISSUBMENU;
ce22ac45
RR
1141
1142 PrepareBackground(dc, rect, bgFlags);
40b26d75 1143 }
40b26d75
WS
1144}
1145
a57d600f 1146#endif // wxUSE_ODCOMBOBOX