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