]> git.saurik.com Git - wxWidgets.git/blame - src/generic/odcombo.cpp
fixing reentrancy which happened in tests, bringing client coordinates origins in...
[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)
c765e6fb 57 EVT_CHAR(wxVListBoxComboPopup::OnChar)
a340b80d
VZ
58 EVT_LEFT_UP(wxVListBoxComboPopup::OnLeftClick)
59END_EVENT_TABLE()
60
61
6d0ce565 62void wxVListBoxComboPopup::Init()
a340b80d
VZ
63{
64 m_widestWidth = 0;
e5d63342
WS
65 m_widestItem = -1;
66 m_widthsDirty = false;
67 m_findWidest = false;
a340b80d
VZ
68 m_itemHeight = 0;
69 m_value = -1;
70 m_itemHover = -1;
71 m_clientDataItemsType = wxClientData_None;
dc8a1aa5 72 m_partialCompletionString = wxEmptyString;
a340b80d
VZ
73}
74
75bool wxVListBoxComboPopup::Create(wxWindow* parent)
76{
77 if ( !wxVListBox::Create(parent,
78 wxID_ANY,
79 wxDefaultPosition,
80 wxDefaultSize,
81 wxBORDER_SIMPLE | wxLB_INT_HEIGHT | wxWANTS_CHARS) )
82 return false;
83
6d0ce565 84 m_useFont = m_combo->GetFont();
a340b80d
VZ
85
86 wxVListBox::SetItemCount(m_strings.GetCount());
87
88 // TODO: Move this to SetFont
89 m_itemHeight = GetCharHeight() + 0;
90
91 return true;
92}
93
94wxVListBoxComboPopup::~wxVListBoxComboPopup()
95{
96 Clear();
97}
98
c0c05e79
JS
99void wxVListBoxComboPopup::SetFocus()
100{
101 // Suppress SetFocus() warning by simply not calling it. This combo popup
102 // has already been designed with the assumption that SetFocus() may not
103 // do anything useful, so it really doesn't need to be called.
104#ifdef __WXMSW__
105 //
106#else
107 wxVListBox::SetFocus();
108#endif
109}
110
a340b80d
VZ
111bool wxVListBoxComboPopup::LazyCreate()
112{
113 // NB: There is a bug with wxVListBox that can be avoided by creating
114 // it later (bug causes empty space to be shown if initial selection
115 // is at the end of a list longer than the control can show at once).
116 return true;
117}
118
119// paint the control itself
120void wxVListBoxComboPopup::PaintComboControl( wxDC& dc, const wxRect& rect )
121{
122 if ( !(m_combo->GetWindowStyle() & wxODCB_STD_CONTROL_PAINT) )
123 {
ce22ac45
RR
124 int flags = wxODCB_PAINTING_CONTROL;
125
126 if ( m_combo->ShouldDrawFocus() )
127 flags |= wxODCB_PAINTING_SELECTED;
128
129 OnDrawBg(dc, rect, m_value, flags);
130
a340b80d
VZ
131 if ( m_value >= 0 )
132 {
b4ff336e 133 OnDrawItem(dc,rect,m_value,flags);
6d0ce565 134 return;
a340b80d
VZ
135 }
136 }
137
138 wxComboPopup::PaintComboControl(dc,rect);
139}
140
141void wxVListBoxComboPopup::OnDrawItem(wxDC& dc, const wxRect& rect, size_t n) const
142{
6d0ce565
VZ
143 // TODO: Maybe this code could be moved to wxVListBox::OnPaint?
144 dc.SetFont(m_useFont);
a340b80d 145
b4ff336e
VZ
146 int flags = 0;
147
a340b80d 148 // Set correct text colour for selected items
6d0ce565 149 if ( wxVListBox::GetSelection() == (int) n )
b4ff336e 150 {
a340b80d 151 dc.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT) );
b4ff336e
VZ
152 flags |= wxODCB_PAINTING_SELECTED;
153 }
a340b80d 154 else
b4ff336e 155 {
a340b80d 156 dc.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT) );
b4ff336e 157 }
a340b80d 158
b4ff336e 159 OnDrawItem(dc,rect,(int)n,flags);
a340b80d
VZ
160}
161
40b26d75 162wxCoord wxVListBoxComboPopup::OnMeasureItem(size_t n) const
a340b80d 163{
40b26d75
WS
164 wxOwnerDrawnComboBox* combo = (wxOwnerDrawnComboBox*) m_combo;
165
345c78ca 166 wxASSERT_MSG( wxDynamicCast(combo, wxOwnerDrawnComboBox),
40b26d75
WS
167 wxT("you must subclass wxVListBoxComboPopup for drawing and measuring methods") );
168
169 wxCoord h = combo->OnMeasureItem(n);
170 if ( h < 0 )
171 h = m_itemHeight;
172 return h;
6d0ce565 173}
a340b80d 174
40b26d75 175wxCoord wxVListBoxComboPopup::OnMeasureItemWidth(size_t n) const
6d0ce565 176{
40b26d75
WS
177 wxOwnerDrawnComboBox* combo = (wxOwnerDrawnComboBox*) m_combo;
178
345c78ca 179 wxASSERT_MSG( wxDynamicCast(combo, wxOwnerDrawnComboBox),
40b26d75
WS
180 wxT("you must subclass wxVListBoxComboPopup for drawing and measuring methods") );
181
182 return combo->OnMeasureItemWidth(n);
183}
184
185void wxVListBoxComboPopup::OnDrawBg( wxDC& dc,
186 const wxRect& rect,
187 int item,
188 int flags ) const
189{
190 wxOwnerDrawnComboBox* combo = (wxOwnerDrawnComboBox*) m_combo;
191
345c78ca 192 wxASSERT_MSG( wxDynamicCast(combo, wxOwnerDrawnComboBox),
40b26d75
WS
193 wxT("you must subclass wxVListBoxComboPopup for drawing and measuring methods") );
194
ce22ac45
RR
195 if ( IsCurrent((size_t)item) && !(flags & wxODCB_PAINTING_CONTROL) )
196 flags |= wxODCB_PAINTING_SELECTED;
197
40b26d75 198 combo->OnDrawBackground(dc,rect,item,flags);
a340b80d
VZ
199}
200
201void wxVListBoxComboPopup::OnDrawBackground(wxDC& dc, const wxRect& rect, size_t n) const
202{
40b26d75 203 OnDrawBg(dc,rect,(int)n,0);
a340b80d
VZ
204}
205
6d0ce565
VZ
206// This is called from wxVListBoxComboPopup::OnDrawItem, with text colour and font prepared
207void wxVListBoxComboPopup::OnDrawItem( wxDC& dc, const wxRect& rect, int item, int flags ) const
208{
40b26d75
WS
209 wxOwnerDrawnComboBox* combo = (wxOwnerDrawnComboBox*) m_combo;
210
345c78ca 211 wxASSERT_MSG( wxDynamicCast(combo, wxOwnerDrawnComboBox),
40b26d75
WS
212 wxT("you must subclass wxVListBoxComboPopup for drawing and measuring methods") );
213
214 combo->OnDrawItem(dc,rect,item,flags);
6d0ce565
VZ
215}
216
217void wxVListBoxComboPopup::DismissWithEvent()
218{
dc8a1aa5
AB
219 StopPartialCompletion();
220
6d0ce565
VZ
221 int selection = wxVListBox::GetSelection();
222
223 Dismiss();
224
6d0ce565 225 if ( selection != wxNOT_FOUND )
fdb47e62 226 m_stringValue = m_strings[selection];
6d0ce565 227 else
fdb47e62 228 m_stringValue = wxEmptyString;
6d0ce565 229
fdb47e62 230 if ( m_stringValue != m_combo->GetValue() )
0306e73e 231 m_combo->SetValueByUser(m_stringValue);
6d0ce565 232
fdb47e62 233 m_value = selection;
6d0ce565
VZ
234
235 SendComboBoxEvent(selection);
236}
237
238void wxVListBoxComboPopup::SendComboBoxEvent( int selection )
a340b80d 239{
ce7fe42e 240 wxCommandEvent evt(wxEVT_COMBOBOX,m_combo->GetId());
a340b80d
VZ
241
242 evt.SetEventObject(m_combo);
6d0ce565 243
a340b80d
VZ
244 evt.SetInt(selection);
245
246 // Set client data, if any
247 if ( selection >= 0 && (int)m_clientDatas.GetCount() > selection )
248 {
249 void* clientData = m_clientDatas[selection];
250 if ( m_clientDataItemsType == wxClientData_Object )
251 evt.SetClientObject((wxClientData*)clientData);
252 else
253 evt.SetClientData(clientData);
254 }
255
256 m_combo->GetEventHandler()->AddPendingEvent(evt);
257}
258
259// returns true if key was consumed
c765e6fb 260bool wxVListBoxComboPopup::HandleKey( int keycode, bool saturate, wxChar keychar )
a340b80d 261{
704bc436
VZ
262 const int itemCount = GetCount();
263
264 // keys do nothing in the empty control and returning immediately avoids
265 // using invalid indices below
266 if ( !itemCount )
267 return false;
268
a340b80d 269 int value = m_value;
dc8a1aa5
AB
270 int comboStyle = m_combo->GetWindowStyle();
271
c765e6fb 272 if ( keychar > 0 )
dc8a1aa5 273 {
c765e6fb
VS
274 // we have character equivalent of the keycode; filter out these that
275 // are not printable characters
276 if ( !wxIsprint(keychar) )
277 keychar = 0;
dc8a1aa5 278 }
a340b80d 279
39191cbf
VZ
280 const bool readOnly = (comboStyle & wxCB_READONLY) != 0;
281
282 if ( keycode == WXK_DOWN || keycode == WXK_NUMPAD_DOWN || ( keycode == WXK_RIGHT && readOnly ) )
a340b80d
VZ
283 {
284 value++;
dc8a1aa5 285 StopPartialCompletion();
a340b80d 286 }
39191cbf 287 else if ( keycode == WXK_UP || keycode == WXK_NUMPAD_UP || ( keycode == WXK_LEFT && readOnly ) )
a340b80d
VZ
288 {
289 value--;
dc8a1aa5 290 StopPartialCompletion();
a340b80d 291 }
293987e8 292 else if ( keycode == WXK_PAGEDOWN || keycode == WXK_NUMPAD_PAGEDOWN )
a340b80d
VZ
293 {
294 value+=10;
dc8a1aa5 295 StopPartialCompletion();
a340b80d 296 }
293987e8 297 else if ( keycode == WXK_PAGEUP || keycode == WXK_NUMPAD_PAGEUP )
a340b80d
VZ
298 {
299 value-=10;
dc8a1aa5
AB
300 StopPartialCompletion();
301 }
39191cbf 302 else if ( ( keycode == WXK_HOME || keycode == WXK_NUMPAD_HOME ) && readOnly )
10265e6c
VS
303 {
304 value=0;
305 StopPartialCompletion();
306 }
39191cbf 307 else if ( ( keycode == WXK_END || keycode == WXK_NUMPAD_END ) && readOnly )
10265e6c
VS
308 {
309 value=itemCount-1;
310 StopPartialCompletion();
311 }
39191cbf 312 else if ( keychar && readOnly )
dc8a1aa5
AB
313 {
314 // Try partial completion
315
316 // find the new partial completion string
f0fa8b47 317#if wxUSE_TIMER
dc8a1aa5
AB
318 if (m_partialCompletionTimer.IsRunning())
319 m_partialCompletionString+=wxString(keychar);
320 else
f0fa8b47 321#endif // wxUSE_TIMER
dc8a1aa5
AB
322 m_partialCompletionString=wxString(keychar);
323
324 // now search through the values to see if this is found
325 int found = -1;
4dd31ff5 326 unsigned int length=m_partialCompletionString.length();
dc8a1aa5
AB
327 int i;
328 for (i=0; i<itemCount; i++)
329 {
330 wxString item=GetString(i);
4dd31ff5 331 if (( item.length() >= length) && (! m_partialCompletionString.CmpNoCase(item.Left(length))))
dc8a1aa5
AB
332 {
333 found=i;
334 break;
335 }
336 }
337
338 if (found<0)
339 {
340 StopPartialCompletion();
341 ::wxBell();
342 return true; // to stop the first value being set
343 }
344 else
345 {
346 value=i;
f0fa8b47 347#if wxUSE_TIMER
dc8a1aa5 348 m_partialCompletionTimer.Start(wxODCB_PARTIAL_COMPLETION_TIME, true);
f0fa8b47 349#endif // wxUSE_TIMER
dc8a1aa5 350 }
a340b80d 351 }
a340b80d
VZ
352 else
353 return false;
354
355 if ( saturate )
356 {
357 if ( value >= itemCount )
358 value = itemCount - 1;
359 else if ( value < 0 )
360 value = 0;
361 }
362 else
363 {
364 if ( value >= itemCount )
365 value -= itemCount;
366 else if ( value < 0 )
367 value += itemCount;
368 }
369
370 if ( value == m_value )
371 // Even if value was same, don't skip the event
372 // (good for consistency)
373 return true;
374
a340b80d 375 if ( value >= 0 )
65d30b79 376 m_combo->ChangeValue(m_strings[value]);
a340b80d 377
71dd05d5
VS
378 // The m_combo->SetValue() call above sets m_value to the index of this
379 // string. But if there are more identical string, the index is of the
e75e8815 380 // first occurrence, which may be wrong, so set the index explicitly here,
71dd05d5
VS
381 // _after_ the SetValue() call.
382 m_value = value;
383
6d0ce565 384 SendComboBoxEvent(m_value);
a340b80d
VZ
385
386 return true;
387}
388
dc8a1aa5
AB
389// stop partial completion
390void wxVListBoxComboPopup::StopPartialCompletion()
391{
392 m_partialCompletionString = wxEmptyString;
f0fa8b47 393#if wxUSE_TIMER
dc8a1aa5 394 m_partialCompletionTimer.Stop();
f0fa8b47 395#endif // wxUSE_TIMER
dc8a1aa5
AB
396}
397
a340b80d
VZ
398void wxVListBoxComboPopup::OnComboDoubleClick()
399{
400 // Cycle on dclick (disable saturation to allow true cycling).
401 if ( !::wxGetKeyState(WXK_SHIFT) )
402 HandleKey(WXK_DOWN,false);
403 else
404 HandleKey(WXK_UP,false);
405}
406
407void wxVListBoxComboPopup::OnComboKeyEvent( wxKeyEvent& event )
408{
409 // Saturated key movement on
c765e6fb
VS
410 if ( !HandleKey(event.GetKeyCode(), true) )
411 event.Skip();
412}
413
414void wxVListBoxComboPopup::OnComboCharEvent( wxKeyEvent& event )
415{
416 // unlike in OnComboKeyEvent, wxEVT_CHAR contains meaningful
417 // printable character information, so pass it
dc8a1aa5 418#if wxUSE_UNICODE
c765e6fb 419 const wxChar charcode = event.GetUnicodeKey();
dc8a1aa5 420#else
c765e6fb 421 const wxChar charcode = (wxChar)event.GetKeyCode();
dc8a1aa5 422#endif
c765e6fb
VS
423
424 if ( !HandleKey(event.GetKeyCode(), true, charcode) )
a340b80d
VZ
425 event.Skip();
426}
427
428void wxVListBoxComboPopup::OnPopup()
429{
430 // *must* set value after size is set (this is because of a vlbox bug)
431 wxVListBox::SetSelection(m_value);
432}
433
434void wxVListBoxComboPopup::OnMouseMove(wxMouseEvent& event)
435{
276ebe79
WS
436 event.Skip();
437
a340b80d 438 // Move selection to cursor if it is inside the popup
a340b80d 439
276ebe79
WS
440 int y = event.GetPosition().y;
441 int fromBottom = GetClientSize().y - y;
442
443 // Since in any case we need to find out if the last item is only
444 // partially visible, we might just as well replicate the HitTest
445 // loop here.
446 const size_t lineMax = GetVisibleEnd();
447 for ( size_t line = GetVisibleBegin(); line < lineMax; line++ )
448 {
e02c72fa 449 y -= OnGetRowHeight(line);
276ebe79
WS
450 if ( y < 0 )
451 {
452 // Only change selection if item is fully visible
453 if ( (y + fromBottom) >= 0 )
454 {
455 wxVListBox::SetSelection((int)line);
456 return;
457 }
458 }
459 }
a340b80d
VZ
460}
461
462void wxVListBoxComboPopup::OnLeftClick(wxMouseEvent& WXUNUSED(event))
463{
6d0ce565 464 DismissWithEvent();
a340b80d
VZ
465}
466
467void wxVListBoxComboPopup::OnKey(wxKeyEvent& event)
468{
b445b6a7
VZ
469 // Hide popup if certain key or key combination was pressed
470 if ( m_combo->IsKeyPopupToggle(event) )
dc8a1aa5
AB
471 {
472 StopPartialCompletion();
a340b80d 473 Dismiss();
dc8a1aa5 474 }
b445b6a7
VZ
475 else if ( event.AltDown() )
476 {
477 // On both wxGTK and wxMSW, pressing Alt down seems to
478 // completely freeze things in popup (ie. arrow keys and
479 // enter won't work).
480 return;
481 }
482 // Select item if ENTER is pressed
483 else if ( event.GetKeyCode() == WXK_RETURN || event.GetKeyCode() == WXK_NUMPAD_ENTER )
484 {
485 DismissWithEvent();
486 }
a340b80d 487 else
dc8a1aa5 488 {
c765e6fb
VS
489 // completion is handled in OnChar() below
490 event.Skip();
491 }
492}
493
494void wxVListBoxComboPopup::OnChar(wxKeyEvent& event)
495{
496 if ( m_combo->GetWindowStyle() & wxCB_READONLY )
497 {
498 // Process partial completion key codes here, but not the arrow keys as
499 // the base class will do that for us
500#if wxUSE_UNICODE
501 const wxChar charcode = event.GetUnicodeKey();
502#else
503 const wxChar charcode = (wxChar)event.GetKeyCode();
504#endif
505 if ( wxIsprint(charcode) )
dc8a1aa5 506 {
c765e6fb 507 OnComboCharEvent(event);
dc8a1aa5 508 SetSelection(m_value); // ensure the highlight bar moves
c765e6fb 509 return; // don't skip the event
dc8a1aa5 510 }
dc8a1aa5 511 }
c765e6fb
VS
512
513 event.Skip();
a340b80d
VZ
514}
515
a340b80d
VZ
516void wxVListBoxComboPopup::Insert( const wxString& item, int pos )
517{
518 // Need to change selection?
519 wxString strValue;
520 if ( !(m_combo->GetWindowStyle() & wxCB_READONLY) &&
521 m_combo->GetValue() == item )
6d0ce565 522 {
a340b80d 523 m_value = pos;
6d0ce565 524 }
a340b80d
VZ
525
526 m_strings.Insert(item,pos);
697c314b 527 if ( (int)m_clientDatas.size() >= pos )
57306cd4 528 m_clientDatas.Insert(NULL, pos);
a236aa20 529
e5d63342
WS
530 m_widths.Insert(-1,pos);
531 m_widthsDirty = true;
a340b80d
VZ
532
533 if ( IsCreated() )
534 wxVListBox::SetItemCount( wxVListBox::GetItemCount()+1 );
a340b80d
VZ
535}
536
537int wxVListBoxComboPopup::Append(const wxString& item)
538{
539 int pos = (int)m_strings.GetCount();
540
541 if ( m_combo->GetWindowStyle() & wxCB_SORT )
542 {
543 // Find position
544 // TODO: Could be optimized with binary search
545 wxArrayString strings = m_strings;
546 unsigned int i;
547
548 for ( i=0; i<strings.GetCount(); i++ )
549 {
f74ab004 550 if ( item.CmpNoCase(strings.Item(i)) <= 0 )
a340b80d
VZ
551 {
552 pos = (int)i;
553 break;
554 }
555 }
556 }
557
558 Insert(item,pos);
559
560 return pos;
561}
562
563void wxVListBoxComboPopup::Clear()
564{
565 wxASSERT(m_combo);
566
567 m_strings.Empty();
e5d63342
WS
568 m_widths.Empty();
569
570 m_widestWidth = 0;
571 m_widestItem = -1;
a340b80d
VZ
572
573 ClearClientDatas();
574
cdc99ddd
WS
575 m_value = wxNOT_FOUND;
576
a340b80d
VZ
577 if ( IsCreated() )
578 wxVListBox::SetItemCount(0);
579}
580
581void wxVListBoxComboPopup::ClearClientDatas()
582{
583 if ( m_clientDataItemsType == wxClientData_Object )
584 {
585 size_t i;
586 for ( i=0; i<m_clientDatas.GetCount(); i++ )
587 delete (wxClientData*) m_clientDatas[i];
588 }
589
590 m_clientDatas.Empty();
591}
592
593void wxVListBoxComboPopup::SetItemClientData( unsigned int n,
594 void* clientData,
595 wxClientDataType clientDataItemsType )
596{
597 // It should be sufficient to update this variable only here
598 m_clientDataItemsType = clientDataItemsType;
599
a340b80d 600 m_clientDatas[n] = clientData;
e5d63342
WS
601
602 ItemWidthChanged(n);
a340b80d
VZ
603}
604
605void* wxVListBoxComboPopup::GetItemClientData(unsigned int n) const
606{
607 if ( m_clientDatas.GetCount() > n )
608 return m_clientDatas[n];
609
610 return NULL;
611}
612
613void wxVListBoxComboPopup::Delete( unsigned int item )
614{
615 // Remove client data, if set
616 if ( m_clientDatas.GetCount() )
617 {
618 if ( m_clientDataItemsType == wxClientData_Object )
619 delete (wxClientData*) m_clientDatas[item];
620
621 m_clientDatas.RemoveAt(item);
622 }
623
624 m_strings.RemoveAt(item);
e5d63342
WS
625 m_widths.RemoveAt(item);
626
627 if ( (int)item == m_widestItem )
628 m_findWidest = true;
a340b80d 629
fa3ebb12
RR
630 int sel = GetSelection();
631
a340b80d
VZ
632 if ( IsCreated() )
633 wxVListBox::SetItemCount( wxVListBox::GetItemCount()-1 );
fa3ebb12
RR
634
635 // Fix selection
636 if ( (int)item < sel )
637 SetSelection(sel-1);
638 else if ( (int)item == sel )
639 SetSelection(wxNOT_FOUND);
a340b80d
VZ
640}
641
9e6aca68 642int wxVListBoxComboPopup::FindString(const wxString& s, bool bCase) const
a340b80d 643{
9e6aca68 644 return m_strings.Index(s, bCase);
a340b80d
VZ
645}
646
238b33ab
JS
647bool wxVListBoxComboPopup::FindItem(const wxString& item, wxString* trueItem)
648{
649 int idx = m_strings.Index(item, false);
650 if ( idx == wxNOT_FOUND )
651 return false;
652 if ( trueItem != NULL )
653 *trueItem = m_strings[idx];
654 return true;
655}
656
a340b80d
VZ
657unsigned int wxVListBoxComboPopup::GetCount() const
658{
659 return m_strings.GetCount();
660}
661
662wxString wxVListBoxComboPopup::GetString( int item ) const
663{
664 return m_strings[item];
665}
666
667void wxVListBoxComboPopup::SetString( int item, const wxString& str )
668{
669 m_strings[item] = str;
e5d63342 670 ItemWidthChanged(item);
a340b80d
VZ
671}
672
673wxString wxVListBoxComboPopup::GetStringValue() const
674{
fdb47e62 675 return m_stringValue;
a340b80d
VZ
676}
677
678void wxVListBoxComboPopup::SetSelection( int item )
679{
85fed18c
WS
680 wxCHECK_RET( item == wxNOT_FOUND || ((unsigned int)item < GetCount()),
681 wxT("invalid index in wxVListBoxComboPopup::SetSelection") );
a340b80d
VZ
682
683 m_value = item;
684
fdb47e62
VZ
685 if ( item >= 0 )
686 m_stringValue = m_strings[item];
687 else
688 m_stringValue = wxEmptyString;
689
a340b80d
VZ
690 if ( IsCreated() )
691 wxVListBox::SetSelection(item);
692}
693
6d0ce565
VZ
694int wxVListBoxComboPopup::GetSelection() const
695{
696 return m_value;
697}
698
a340b80d
VZ
699void wxVListBoxComboPopup::SetStringValue( const wxString& value )
700{
701 int index = m_strings.Index(value);
702
fdb47e62 703 m_stringValue = value;
a340b80d 704
fdb47e62
VZ
705 if ( index >= 0 && index < (int)wxVListBox::GetItemCount() )
706 {
a340b80d 707 wxVListBox::SetSelection(index);
fdb47e62
VZ
708 m_value = index;
709 }
a340b80d
VZ
710}
711
d9e0958c 712void wxVListBoxComboPopup::CalcWidths()
a340b80d 713{
e5d63342
WS
714 bool doFindWidest = m_findWidest;
715
716 // Measure items with dirty width.
717 if ( m_widthsDirty )
718 {
719 unsigned int i;
720 unsigned int n = m_widths.GetCount();
721 int dirtyHandled = 0;
722 wxArrayInt& widths = m_widths;
723
724 // I think using wxDC::GetTextExtent is faster than
725 // wxWindow::GetTextExtent (assuming same dc is used
726 // for all calls, as we do here).
727 wxClientDC dc(m_combo);
e2ac737f
VZ
728 if ( !m_useFont.IsOk() )
729 m_useFont = m_combo->GetFont();
e5d63342
WS
730 dc.SetFont(m_useFont);
731
732 for ( i=0; i<n; i++ )
733 {
734 if ( widths[i] < 0 )
735 {
736 wxCoord x = OnMeasureItemWidth(i);
737
738 if ( x < 0 )
739 {
740 const wxString& text = m_strings[i];
741
742 // To make sure performance won't suck in extreme scenarios,
743 // we'll estimate length after some arbitrary number of items
744 // have been checked precily.
745 if ( dirtyHandled < 1024 )
746 {
747 wxCoord y;
748 dc.GetTextExtent(text, &x, &y, 0, 0);
749 x += 4;
750 }
751 else
752 {
753 x = text.length() * (dc.GetCharWidth()+1);
754 }
755 }
756
757 widths[i] = x;
758
759 if ( x >= m_widestWidth )
760 {
761 m_widestWidth = x;
762 m_widestItem = (int)i;
763 }
764 else if ( (int)i == m_widestItem )
765 {
766 // Width of previously widest item has been decreased, so
767 // we'll have to check all to find current widest item.
768 doFindWidest = true;
769 }
770
771 dirtyHandled++;
772 }
773 }
774
775 m_widthsDirty = false;
776 }
777
778 if ( doFindWidest )
779 {
780 unsigned int i;
781 unsigned int n = m_widths.GetCount();
782
783 int bestWidth = -1;
784 int bestIndex = -1;
785
786 for ( i=0; i<n; i++ )
787 {
788 int w = m_widths[i];
789 if ( w > bestWidth )
790 {
791 bestIndex = (int)i;
792 bestWidth = w;
793 }
794 }
795
796 m_widestWidth = bestWidth;
797 m_widestItem = bestIndex;
798
799 m_findWidest = false;
800 }
d9e0958c
AB
801}
802
803wxSize wxVListBoxComboPopup::GetAdjustedSize( int minWidth, int prefHeight, int maxHeight )
804{
805 int height = 250;
806
7962f85a
RR
807 maxHeight -= 2; // Must take borders into account
808
d9e0958c
AB
809 if ( m_strings.GetCount() )
810 {
811 if ( prefHeight > 0 )
812 height = prefHeight;
813
814 if ( height > maxHeight )
815 height = maxHeight;
816
817 int totalHeight = GetTotalHeight(); // + 3;
a44c4abe
JS
818
819 // Take borders into account on Mac or scrollbars always appear
820#if defined(__WXMAC__)
821 totalHeight += 2;
822#endif
d9e0958c
AB
823 if ( height >= totalHeight )
824 {
825 height = totalHeight;
826 }
827 else
828 {
829 // Adjust height to a multiple of the height of the first item
830 // NB: Calculations that take variable height into account
831 // are unnecessary.
832 int fih = GetLineHeight(0);
7962f85a 833 height -= height % fih;
d9e0958c
AB
834 }
835 }
836 else
837 height = 50;
838
839 CalcWidths();
e5d63342 840
a340b80d
VZ
841 // Take scrollbar into account in width calculations
842 int widestWidth = m_widestWidth + wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
843 return wxSize(minWidth > widestWidth ? minWidth : widestWidth,
844 height+2);
845}
846
6d0ce565
VZ
847//void wxVListBoxComboPopup::Populate( int n, const wxString choices[] )
848void wxVListBoxComboPopup::Populate( const wxArrayString& choices )
a340b80d
VZ
849{
850 int i;
851
6d0ce565
VZ
852 int n = choices.GetCount();
853
a340b80d
VZ
854 for ( i=0; i<n; i++ )
855 {
6d0ce565 856 const wxString& item = choices.Item(i);
a340b80d 857 m_strings.Add(item);
a340b80d
VZ
858 }
859
e5d63342
WS
860 m_widths.SetCount(n,-1);
861 m_widthsDirty = true;
862
a340b80d
VZ
863 if ( IsCreated() )
864 wxVListBox::SetItemCount(n);
865
866 // Sort the initial choices
867 if ( m_combo->GetWindowStyle() & wxCB_SORT )
868 m_strings.Sort();
869
870 // Find initial selection
871 wxString strValue = m_combo->GetValue();
6636ef8d 872 if ( !strValue.empty() )
a340b80d
VZ
873 m_value = m_strings.Index(strValue);
874}
875
876// ----------------------------------------------------------------------------
877// wxOwnerDrawnComboBox
878// ----------------------------------------------------------------------------
879
880
a57d600f 881BEGIN_EVENT_TABLE(wxOwnerDrawnComboBox, wxComboCtrl)
a340b80d
VZ
882END_EVENT_TABLE()
883
a340b80d
VZ
884void wxOwnerDrawnComboBox::Init()
885{
886}
887
888bool wxOwnerDrawnComboBox::Create(wxWindow *parent,
889 wxWindowID id,
890 const wxString& value,
891 const wxPoint& pos,
892 const wxSize& size,
893 long style,
894 const wxValidator& validator,
895 const wxString& name)
896{
a57d600f 897 return wxComboCtrl::Create(parent,id,value,pos,size,style,validator,name);
a340b80d
VZ
898}
899
900wxOwnerDrawnComboBox::wxOwnerDrawnComboBox(wxWindow *parent,
901 wxWindowID id,
902 const wxString& value,
903 const wxPoint& pos,
904 const wxSize& size,
905 const wxArrayString& choices,
906 long style,
907 const wxValidator& validator,
908 const wxString& name)
a340b80d
VZ
909{
910 Init();
911
912 Create(parent,id,value,pos,size,choices,style, validator, name);
913}
914
915bool wxOwnerDrawnComboBox::Create(wxWindow *parent,
916 wxWindowID id,
917 const wxString& value,
918 const wxPoint& pos,
919 const wxSize& size,
920 const wxArrayString& choices,
921 long style,
922 const wxValidator& validator,
923 const wxString& name)
924{
6d0ce565
VZ
925 m_initChs = choices;
926 //wxCArrayString chs(choices);
a340b80d 927
6d0ce565
VZ
928 //return Create(parent, id, value, pos, size, chs.GetCount(),
929 // chs.GetStrings(), style, validator, name);
930 return Create(parent, id, value, pos, size, 0,
931 NULL, style, validator, name);
a340b80d
VZ
932}
933
934bool wxOwnerDrawnComboBox::Create(wxWindow *parent,
935 wxWindowID id,
936 const wxString& value,
937 const wxPoint& pos,
938 const wxSize& size,
939 int n,
940 const wxString choices[],
941 long style,
942 const wxValidator& validator,
943 const wxString& name)
944{
945
946 if ( !Create(parent, id, value, pos, size, style,
947 validator, name) )
948 {
949 return false;
950 }
951
6d0ce565
VZ
952 int i;
953 for ( i=0; i<n; i++ )
954 m_initChs.Add(choices[i]);
a340b80d
VZ
955
956 return true;
957}
958
959wxOwnerDrawnComboBox::~wxOwnerDrawnComboBox()
960{
961 if ( m_popupInterface )
57693473 962 GetVListBoxComboPopup()->ClearClientDatas();
a340b80d
VZ
963}
964
db53c6ea 965void wxOwnerDrawnComboBox::DoSetPopupControl(wxComboPopup* popup)
6d0ce565
VZ
966{
967 if ( !popup )
968 {
969 popup = new wxVListBoxComboPopup();
970 }
971
db53c6ea 972 wxComboCtrl::DoSetPopupControl(popup);
6d0ce565
VZ
973
974 wxASSERT(popup);
6d0ce565
VZ
975
976 // Add initial choices to the wxVListBox
57693473 977 if ( !GetVListBoxComboPopup()->GetCount() )
6d0ce565 978 {
57693473 979 GetVListBoxComboPopup()->Populate(m_initChs);
6d0ce565
VZ
980 m_initChs.Clear();
981 }
982}
983
a340b80d
VZ
984// ----------------------------------------------------------------------------
985// wxOwnerDrawnComboBox item manipulation methods
986// ----------------------------------------------------------------------------
987
a236aa20 988void wxOwnerDrawnComboBox::DoClear()
a340b80d 989{
6d0ce565 990 EnsurePopupControl();
a340b80d 991
57693473 992 GetVListBoxComboPopup()->Clear();
a340b80d 993
65d30b79
JS
994 // NB: This really needs to be SetValue() instead of ChangeValue(),
995 // as wxTextEntry API expects an event to be sent.
cdc99ddd 996 SetValue(wxEmptyString);
a340b80d
VZ
997}
998
eae20c90
JS
999void wxOwnerDrawnComboBox::Clear()
1000{
1001 DoClear();
1002}
1003
a236aa20 1004void wxOwnerDrawnComboBox::DoDeleteOneItem(unsigned int n)
a340b80d 1005{
9a83f860 1006 wxCHECK_RET( IsValid(n), wxT("invalid index in wxOwnerDrawnComboBox::Delete") );
a340b80d
VZ
1007
1008 if ( GetSelection() == (int) n )
65d30b79 1009 ChangeValue(wxEmptyString);
a340b80d 1010
57693473 1011 GetVListBoxComboPopup()->Delete(n);
a340b80d
VZ
1012}
1013
1014unsigned int wxOwnerDrawnComboBox::GetCount() const
1015{
54a61762
WS
1016 if ( !m_popupInterface )
1017 return m_initChs.GetCount();
1018
57693473 1019 return GetVListBoxComboPopup()->GetCount();
a340b80d
VZ
1020}
1021
1022wxString wxOwnerDrawnComboBox::GetString(unsigned int n) const
1023{
9a83f860 1024 wxCHECK_MSG( IsValid(n), wxEmptyString, wxT("invalid index in wxOwnerDrawnComboBox::GetString") );
54a61762
WS
1025
1026 if ( !m_popupInterface )
1027 return m_initChs.Item(n);
1028
57693473 1029 return GetVListBoxComboPopup()->GetString(n);
a340b80d
VZ
1030}
1031
1032void wxOwnerDrawnComboBox::SetString(unsigned int n, const wxString& s)
1033{
54a61762
WS
1034 EnsurePopupControl();
1035
9a83f860 1036 wxCHECK_RET( IsValid(n), wxT("invalid index in wxOwnerDrawnComboBox::SetString") );
54a61762 1037
57693473 1038 GetVListBoxComboPopup()->SetString(n,s);
a340b80d
VZ
1039}
1040
9e6aca68 1041int wxOwnerDrawnComboBox::FindString(const wxString& s, bool bCase) const
a340b80d 1042{
54a61762
WS
1043 if ( !m_popupInterface )
1044 return m_initChs.Index(s, bCase);
1045
57693473 1046 return GetVListBoxComboPopup()->FindString(s, bCase);
a340b80d
VZ
1047}
1048
1049void wxOwnerDrawnComboBox::Select(int n)
1050{
6d0ce565 1051 EnsurePopupControl();
a340b80d 1052
9a83f860 1053 wxCHECK_RET( (n == wxNOT_FOUND) || IsValid(n), wxT("invalid index in wxOwnerDrawnComboBox::Select") );
40b26d75 1054
57693473 1055 GetVListBoxComboPopup()->SetSelection(n);
a340b80d
VZ
1056
1057 wxString str;
1058 if ( n >= 0 )
57693473 1059 str = GetVListBoxComboPopup()->GetString(n);
a340b80d
VZ
1060
1061 // Refresh text portion in control
1062 if ( m_text )
65d30b79 1063 m_text->ChangeValue( str );
a340b80d
VZ
1064 else
1065 m_valueString = str;
1066
1067 Refresh();
1068}
1069
1070int wxOwnerDrawnComboBox::GetSelection() const
1071{
54a61762
WS
1072 if ( !m_popupInterface )
1073 return m_initChs.Index(m_valueString);
1074
57693473 1075 return GetVListBoxComboPopup()->GetSelection();
a340b80d
VZ
1076}
1077
9ff4b6cc
JS
1078void wxOwnerDrawnComboBox::GetSelection(long *from, long *to) const
1079{
2debe2b1 1080 wxComboCtrl::GetSelection(from, to);
9ff4b6cc
JS
1081}
1082
a236aa20
VZ
1083int wxOwnerDrawnComboBox::DoInsertItems(const wxArrayStringsAdapter& items,
1084 unsigned int pos,
1085 void **clientData,
1086 wxClientDataType type)
a340b80d 1087{
40b26d75
WS
1088 EnsurePopupControl();
1089
a236aa20 1090 const unsigned int count = items.GetCount();
fd11f777
VZ
1091
1092 if ( HasFlag(wxCB_SORT) )
a236aa20 1093 {
fd11f777
VZ
1094 int n = pos;
1095
1096 for ( unsigned int i = 0; i < count; ++i )
1097 {
31329795 1098 n = GetVListBoxComboPopup()->Append(items[i]);
fd11f777
VZ
1099 AssignNewItemClientData(n, clientData, i, type);
1100 }
1101
1102 return n;
a236aa20 1103 }
fd11f777
VZ
1104 else
1105 {
1106 for ( unsigned int i = 0; i < count; ++i, ++pos )
1107 {
1108 GetVListBoxComboPopup()->Insert(items[i], pos);
1109 AssignNewItemClientData(pos, clientData, i, type);
1110 }
a340b80d 1111
fd11f777
VZ
1112 return pos - 1;
1113 }
a340b80d
VZ
1114}
1115
1116void wxOwnerDrawnComboBox::DoSetItemClientData(unsigned int n, void* clientData)
1117{
6d0ce565 1118 EnsurePopupControl();
57693473 1119
131b1fba
VZ
1120 GetVListBoxComboPopup()->SetItemClientData(n, clientData,
1121 GetClientDataType());
a340b80d
VZ
1122}
1123
1124void* wxOwnerDrawnComboBox::DoGetItemClientData(unsigned int n) const
1125{
54a61762
WS
1126 if ( !m_popupInterface )
1127 return NULL;
1128
57693473 1129 return GetVListBoxComboPopup()->GetItemClientData(n);
a340b80d
VZ
1130}
1131
40b26d75
WS
1132// ----------------------------------------------------------------------------
1133// wxOwnerDrawnComboBox item drawing and measuring default implementations
1134// ----------------------------------------------------------------------------
1135
1136void wxOwnerDrawnComboBox::OnDrawItem( wxDC& dc,
1137 const wxRect& rect,
1138 int item,
1139 int flags ) const
1140{
1141 if ( flags & wxODCB_PAINTING_CONTROL )
1142 {
107defe3
JS
1143 wxString text;
1144
1145 if ( !ShouldUseHintText() )
1146 {
1147 text = GetValue();
1148 }
1149 else
1150 {
1151 text = GetHint();
1152 wxColour col = wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT);
1153 dc.SetTextForeground(col);
1154 }
1155
1156 dc.DrawText( text,
0847e36e 1157 rect.x + GetMargins().x,
40b26d75
WS
1158 (rect.height-dc.GetCharHeight())/2 + rect.y );
1159 }
1160 else
1161 {
57693473 1162 dc.DrawText( GetVListBoxComboPopup()->GetString(item), rect.x + 2, rect.y );
40b26d75
WS
1163 }
1164}
1165
1166wxCoord wxOwnerDrawnComboBox::OnMeasureItem( size_t WXUNUSED(item) ) const
1167{
1168 return -1;
1169}
1170
1171wxCoord wxOwnerDrawnComboBox::OnMeasureItemWidth( size_t WXUNUSED(item) ) const
1172{
1173 return -1;
1174}
1175
e2ac737f
VZ
1176wxSize wxOwnerDrawnComboBox::DoGetBestSize() const
1177{
1178 wxSize best( wxComboCtrlBase::DoGetBestSize() );
1179
1180 if ( GetCount() > 0 )
1181 {
1182 wxOwnerDrawnComboBox* odc = const_cast<wxOwnerDrawnComboBox*>(this);
1183 best.x = odc->GetWidestItemWidth();
1184 // TODO: this class may also have GetHightestItemHeight() and
1185 // GetHightestItem() methods, and so set the whole (edit part + arrow)
1186 // control's height according with this max height, not only max width.
1187 }
1188
1189 return GetSizeFromTextSize(best.x);
1190}
1191
ce22ac45
RR
1192void wxOwnerDrawnComboBox::OnDrawBackground(wxDC& dc,
1193 const wxRect& rect,
1194 int WXUNUSED(item),
1195 int flags) const
40b26d75 1196{
ce22ac45
RR
1197 // We need only to explicitly draw background for items
1198 // that should have selected background. Also, call PrepareBackground
1199 // always when painting the control so that clipping is done properly.
1200
1201 if ( (flags & wxODCB_PAINTING_SELECTED) ||
1202 ((flags & wxODCB_PAINTING_CONTROL) && HasFlag(wxCB_READONLY)) )
40b26d75 1203 {
118f5fbd 1204 int bgFlags = wxCONTROL_SELECTED;
4dd31ff5 1205
ce22ac45 1206 if ( !(flags & wxODCB_PAINTING_CONTROL) )
118f5fbd 1207 bgFlags |= wxCONTROL_ISSUBMENU;
ce22ac45
RR
1208
1209 PrepareBackground(dc, rect, bgFlags);
40b26d75 1210 }
40b26d75
WS
1211}
1212
a57d600f 1213#endif // wxUSE_ODCOMBOBOX