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