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