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