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