]> git.saurik.com Git - wxWidgets.git/blame - src/generic/odcombo.cpp
use wxStockCursor (as documented and as already implemented in wxMotif) instead of...
[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);
697c314b 480 if ( (int)m_clientDatas.size() >= pos )
57306cd4 481 m_clientDatas.Insert(NULL, pos);
a236aa20 482
e5d63342
WS
483 m_widths.Insert(-1,pos);
484 m_widthsDirty = true;
a340b80d
VZ
485
486 if ( IsCreated() )
487 wxVListBox::SetItemCount( wxVListBox::GetItemCount()+1 );
a340b80d
VZ
488}
489
490int wxVListBoxComboPopup::Append(const wxString& item)
491{
492 int pos = (int)m_strings.GetCount();
493
494 if ( m_combo->GetWindowStyle() & wxCB_SORT )
495 {
496 // Find position
497 // TODO: Could be optimized with binary search
498 wxArrayString strings = m_strings;
499 unsigned int i;
500
501 for ( i=0; i<strings.GetCount(); i++ )
502 {
503 if ( item.Cmp(strings.Item(i)) < 0 )
504 {
505 pos = (int)i;
506 break;
507 }
508 }
509 }
510
511 Insert(item,pos);
512
513 return pos;
514}
515
516void wxVListBoxComboPopup::Clear()
517{
518 wxASSERT(m_combo);
519
520 m_strings.Empty();
e5d63342
WS
521 m_widths.Empty();
522
523 m_widestWidth = 0;
524 m_widestItem = -1;
a340b80d
VZ
525
526 ClearClientDatas();
527
cdc99ddd
WS
528 m_value = wxNOT_FOUND;
529
a340b80d
VZ
530 if ( IsCreated() )
531 wxVListBox::SetItemCount(0);
532}
533
534void wxVListBoxComboPopup::ClearClientDatas()
535{
536 if ( m_clientDataItemsType == wxClientData_Object )
537 {
538 size_t i;
539 for ( i=0; i<m_clientDatas.GetCount(); i++ )
540 delete (wxClientData*) m_clientDatas[i];
541 }
542
543 m_clientDatas.Empty();
544}
545
546void wxVListBoxComboPopup::SetItemClientData( unsigned int n,
547 void* clientData,
548 wxClientDataType clientDataItemsType )
549{
550 // It should be sufficient to update this variable only here
551 m_clientDataItemsType = clientDataItemsType;
552
a340b80d 553 m_clientDatas[n] = clientData;
e5d63342
WS
554
555 ItemWidthChanged(n);
a340b80d
VZ
556}
557
558void* wxVListBoxComboPopup::GetItemClientData(unsigned int n) const
559{
560 if ( m_clientDatas.GetCount() > n )
561 return m_clientDatas[n];
562
563 return NULL;
564}
565
566void wxVListBoxComboPopup::Delete( unsigned int item )
567{
568 // Remove client data, if set
569 if ( m_clientDatas.GetCount() )
570 {
571 if ( m_clientDataItemsType == wxClientData_Object )
572 delete (wxClientData*) m_clientDatas[item];
573
574 m_clientDatas.RemoveAt(item);
575 }
576
577 m_strings.RemoveAt(item);
e5d63342
WS
578 m_widths.RemoveAt(item);
579
580 if ( (int)item == m_widestItem )
581 m_findWidest = true;
a340b80d 582
fa3ebb12
RR
583 int sel = GetSelection();
584
a340b80d
VZ
585 if ( IsCreated() )
586 wxVListBox::SetItemCount( wxVListBox::GetItemCount()-1 );
fa3ebb12
RR
587
588 // Fix selection
589 if ( (int)item < sel )
590 SetSelection(sel-1);
591 else if ( (int)item == sel )
592 SetSelection(wxNOT_FOUND);
a340b80d
VZ
593}
594
9e6aca68 595int wxVListBoxComboPopup::FindString(const wxString& s, bool bCase) const
a340b80d 596{
9e6aca68 597 return m_strings.Index(s, bCase);
a340b80d
VZ
598}
599
600unsigned int wxVListBoxComboPopup::GetCount() const
601{
602 return m_strings.GetCount();
603}
604
605wxString wxVListBoxComboPopup::GetString( int item ) const
606{
607 return m_strings[item];
608}
609
610void wxVListBoxComboPopup::SetString( int item, const wxString& str )
611{
612 m_strings[item] = str;
e5d63342 613 ItemWidthChanged(item);
a340b80d
VZ
614}
615
616wxString wxVListBoxComboPopup::GetStringValue() const
617{
fdb47e62 618 return m_stringValue;
a340b80d
VZ
619}
620
621void wxVListBoxComboPopup::SetSelection( int item )
622{
85fed18c
WS
623 wxCHECK_RET( item == wxNOT_FOUND || ((unsigned int)item < GetCount()),
624 wxT("invalid index in wxVListBoxComboPopup::SetSelection") );
a340b80d
VZ
625
626 m_value = item;
627
fdb47e62
VZ
628 if ( item >= 0 )
629 m_stringValue = m_strings[item];
630 else
631 m_stringValue = wxEmptyString;
632
a340b80d
VZ
633 if ( IsCreated() )
634 wxVListBox::SetSelection(item);
635}
636
6d0ce565
VZ
637int wxVListBoxComboPopup::GetSelection() const
638{
639 return m_value;
640}
641
a340b80d
VZ
642void wxVListBoxComboPopup::SetStringValue( const wxString& value )
643{
644 int index = m_strings.Index(value);
645
fdb47e62 646 m_stringValue = value;
a340b80d 647
fdb47e62
VZ
648 if ( index >= 0 && index < (int)wxVListBox::GetItemCount() )
649 {
a340b80d 650 wxVListBox::SetSelection(index);
fdb47e62
VZ
651 m_value = index;
652 }
a340b80d
VZ
653}
654
d9e0958c 655void wxVListBoxComboPopup::CalcWidths()
a340b80d 656{
e5d63342
WS
657 bool doFindWidest = m_findWidest;
658
659 // Measure items with dirty width.
660 if ( m_widthsDirty )
661 {
662 unsigned int i;
663 unsigned int n = m_widths.GetCount();
664 int dirtyHandled = 0;
665 wxArrayInt& widths = m_widths;
666
667 // I think using wxDC::GetTextExtent is faster than
668 // wxWindow::GetTextExtent (assuming same dc is used
669 // for all calls, as we do here).
670 wxClientDC dc(m_combo);
671 dc.SetFont(m_useFont);
672
673 for ( i=0; i<n; i++ )
674 {
675 if ( widths[i] < 0 )
676 {
677 wxCoord x = OnMeasureItemWidth(i);
678
679 if ( x < 0 )
680 {
681 const wxString& text = m_strings[i];
682
683 // To make sure performance won't suck in extreme scenarios,
684 // we'll estimate length after some arbitrary number of items
685 // have been checked precily.
686 if ( dirtyHandled < 1024 )
687 {
688 wxCoord y;
689 dc.GetTextExtent(text, &x, &y, 0, 0);
690 x += 4;
691 }
692 else
693 {
694 x = text.length() * (dc.GetCharWidth()+1);
695 }
696 }
697
698 widths[i] = x;
699
700 if ( x >= m_widestWidth )
701 {
702 m_widestWidth = x;
703 m_widestItem = (int)i;
704 }
705 else if ( (int)i == m_widestItem )
706 {
707 // Width of previously widest item has been decreased, so
708 // we'll have to check all to find current widest item.
709 doFindWidest = true;
710 }
711
712 dirtyHandled++;
713 }
714 }
715
716 m_widthsDirty = false;
717 }
718
719 if ( doFindWidest )
720 {
721 unsigned int i;
722 unsigned int n = m_widths.GetCount();
723
724 int bestWidth = -1;
725 int bestIndex = -1;
726
727 for ( i=0; i<n; i++ )
728 {
729 int w = m_widths[i];
730 if ( w > bestWidth )
731 {
732 bestIndex = (int)i;
733 bestWidth = w;
734 }
735 }
736
737 m_widestWidth = bestWidth;
738 m_widestItem = bestIndex;
739
740 m_findWidest = false;
741 }
d9e0958c
AB
742}
743
744wxSize wxVListBoxComboPopup::GetAdjustedSize( int minWidth, int prefHeight, int maxHeight )
745{
746 int height = 250;
747
7962f85a
RR
748 maxHeight -= 2; // Must take borders into account
749
d9e0958c
AB
750 if ( m_strings.GetCount() )
751 {
752 if ( prefHeight > 0 )
753 height = prefHeight;
754
755 if ( height > maxHeight )
756 height = maxHeight;
757
758 int totalHeight = GetTotalHeight(); // + 3;
759 if ( height >= totalHeight )
760 {
761 height = totalHeight;
762 }
763 else
764 {
765 // Adjust height to a multiple of the height of the first item
766 // NB: Calculations that take variable height into account
767 // are unnecessary.
768 int fih = GetLineHeight(0);
7962f85a 769 height -= height % fih;
d9e0958c
AB
770 }
771 }
772 else
773 height = 50;
774
775 CalcWidths();
e5d63342 776
a340b80d
VZ
777 // Take scrollbar into account in width calculations
778 int widestWidth = m_widestWidth + wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
779 return wxSize(minWidth > widestWidth ? minWidth : widestWidth,
780 height+2);
781}
782
6d0ce565
VZ
783//void wxVListBoxComboPopup::Populate( int n, const wxString choices[] )
784void wxVListBoxComboPopup::Populate( const wxArrayString& choices )
a340b80d
VZ
785{
786 int i;
787
6d0ce565
VZ
788 int n = choices.GetCount();
789
a340b80d
VZ
790 for ( i=0; i<n; i++ )
791 {
6d0ce565 792 const wxString& item = choices.Item(i);
a340b80d 793 m_strings.Add(item);
a340b80d
VZ
794 }
795
e5d63342
WS
796 m_widths.SetCount(n,-1);
797 m_widthsDirty = true;
798
a340b80d
VZ
799 if ( IsCreated() )
800 wxVListBox::SetItemCount(n);
801
802 // Sort the initial choices
803 if ( m_combo->GetWindowStyle() & wxCB_SORT )
804 m_strings.Sort();
805
806 // Find initial selection
807 wxString strValue = m_combo->GetValue();
40b26d75 808 if ( strValue.length() )
a340b80d
VZ
809 m_value = m_strings.Index(strValue);
810}
811
812// ----------------------------------------------------------------------------
813// wxOwnerDrawnComboBox
814// ----------------------------------------------------------------------------
815
816
a57d600f 817BEGIN_EVENT_TABLE(wxOwnerDrawnComboBox, wxComboCtrl)
a340b80d
VZ
818END_EVENT_TABLE()
819
820
afc89ff4
MB
821#if wxUSE_EXTENDED_RTTI
822IMPLEMENT_DYNAMIC_CLASS2_XTI(wxOwnerDrawnComboBox, wxComboCtrl, wxControlWithItems, "wx/odcombo.h")
823
824wxBEGIN_PROPERTIES_TABLE(wxOwnerDrawnComboBox)
825wxEND_PROPERTIES_TABLE()
826
827wxBEGIN_HANDLERS_TABLE(wxOwnerDrawnComboBox)
828wxEND_HANDLERS_TABLE()
829
830wxCONSTRUCTOR_5( wxOwnerDrawnComboBox , wxWindow* , Parent , wxWindowID , Id , wxString , Value , wxPoint , Position , wxSize , Size )
831#else
a57d600f 832IMPLEMENT_DYNAMIC_CLASS2(wxOwnerDrawnComboBox, wxComboCtrl, wxControlWithItems)
afc89ff4 833#endif
a340b80d
VZ
834
835void wxOwnerDrawnComboBox::Init()
836{
837}
838
839bool wxOwnerDrawnComboBox::Create(wxWindow *parent,
840 wxWindowID id,
841 const wxString& value,
842 const wxPoint& pos,
843 const wxSize& size,
844 long style,
845 const wxValidator& validator,
846 const wxString& name)
847{
a57d600f 848 return wxComboCtrl::Create(parent,id,value,pos,size,style,validator,name);
a340b80d
VZ
849}
850
851wxOwnerDrawnComboBox::wxOwnerDrawnComboBox(wxWindow *parent,
852 wxWindowID id,
853 const wxString& value,
854 const wxPoint& pos,
855 const wxSize& size,
856 const wxArrayString& choices,
857 long style,
858 const wxValidator& validator,
859 const wxString& name)
a57d600f 860 : wxComboCtrl()
a340b80d
VZ
861{
862 Init();
863
864 Create(parent,id,value,pos,size,choices,style, validator, name);
865}
866
867bool wxOwnerDrawnComboBox::Create(wxWindow *parent,
868 wxWindowID id,
869 const wxString& value,
870 const wxPoint& pos,
871 const wxSize& size,
872 const wxArrayString& choices,
873 long style,
874 const wxValidator& validator,
875 const wxString& name)
876{
6d0ce565
VZ
877 m_initChs = choices;
878 //wxCArrayString chs(choices);
a340b80d 879
6d0ce565
VZ
880 //return Create(parent, id, value, pos, size, chs.GetCount(),
881 // chs.GetStrings(), style, validator, name);
882 return Create(parent, id, value, pos, size, 0,
883 NULL, style, validator, name);
a340b80d
VZ
884}
885
886bool wxOwnerDrawnComboBox::Create(wxWindow *parent,
887 wxWindowID id,
888 const wxString& value,
889 const wxPoint& pos,
890 const wxSize& size,
891 int n,
892 const wxString choices[],
893 long style,
894 const wxValidator& validator,
895 const wxString& name)
896{
897
898 if ( !Create(parent, id, value, pos, size, style,
899 validator, name) )
900 {
901 return false;
902 }
903
6d0ce565
VZ
904 int i;
905 for ( i=0; i<n; i++ )
906 m_initChs.Add(choices[i]);
a340b80d
VZ
907
908 return true;
909}
910
911wxOwnerDrawnComboBox::~wxOwnerDrawnComboBox()
912{
913 if ( m_popupInterface )
57693473 914 GetVListBoxComboPopup()->ClearClientDatas();
a340b80d
VZ
915}
916
db53c6ea 917void wxOwnerDrawnComboBox::DoSetPopupControl(wxComboPopup* popup)
6d0ce565
VZ
918{
919 if ( !popup )
920 {
921 popup = new wxVListBoxComboPopup();
922 }
923
db53c6ea 924 wxComboCtrl::DoSetPopupControl(popup);
6d0ce565
VZ
925
926 wxASSERT(popup);
6d0ce565
VZ
927
928 // Add initial choices to the wxVListBox
57693473 929 if ( !GetVListBoxComboPopup()->GetCount() )
6d0ce565 930 {
57693473 931 GetVListBoxComboPopup()->Populate(m_initChs);
6d0ce565
VZ
932 m_initChs.Clear();
933 }
934}
935
a340b80d
VZ
936// ----------------------------------------------------------------------------
937// wxOwnerDrawnComboBox item manipulation methods
938// ----------------------------------------------------------------------------
939
a236aa20 940void wxOwnerDrawnComboBox::DoClear()
a340b80d 941{
6d0ce565 942 EnsurePopupControl();
a340b80d 943
57693473 944 GetVListBoxComboPopup()->Clear();
a340b80d 945
cdc99ddd 946 SetValue(wxEmptyString);
a340b80d
VZ
947}
948
a236aa20 949void wxOwnerDrawnComboBox::DoDeleteOneItem(unsigned int n)
a340b80d 950{
85fed18c 951 wxCHECK_RET( IsValid(n), _T("invalid index in wxOwnerDrawnComboBox::Delete") );
a340b80d
VZ
952
953 if ( GetSelection() == (int) n )
954 SetValue(wxEmptyString);
955
57693473 956 GetVListBoxComboPopup()->Delete(n);
a340b80d
VZ
957}
958
959unsigned int wxOwnerDrawnComboBox::GetCount() const
960{
54a61762
WS
961 if ( !m_popupInterface )
962 return m_initChs.GetCount();
963
57693473 964 return GetVListBoxComboPopup()->GetCount();
a340b80d
VZ
965}
966
967wxString wxOwnerDrawnComboBox::GetString(unsigned int n) const
968{
85fed18c 969 wxCHECK_MSG( IsValid(n), wxEmptyString, _T("invalid index in wxOwnerDrawnComboBox::GetString") );
54a61762
WS
970
971 if ( !m_popupInterface )
972 return m_initChs.Item(n);
973
57693473 974 return GetVListBoxComboPopup()->GetString(n);
a340b80d
VZ
975}
976
977void wxOwnerDrawnComboBox::SetString(unsigned int n, const wxString& s)
978{
54a61762
WS
979 EnsurePopupControl();
980
85fed18c 981 wxCHECK_RET( IsValid(n), _T("invalid index in wxOwnerDrawnComboBox::SetString") );
54a61762 982
57693473 983 GetVListBoxComboPopup()->SetString(n,s);
a340b80d
VZ
984}
985
9e6aca68 986int wxOwnerDrawnComboBox::FindString(const wxString& s, bool bCase) const
a340b80d 987{
54a61762
WS
988 if ( !m_popupInterface )
989 return m_initChs.Index(s, bCase);
990
57693473 991 return GetVListBoxComboPopup()->FindString(s, bCase);
a340b80d
VZ
992}
993
994void wxOwnerDrawnComboBox::Select(int n)
995{
6d0ce565 996 EnsurePopupControl();
a340b80d 997
40b26d75
WS
998 wxCHECK_RET( (n == wxNOT_FOUND) || IsValid(n), _T("invalid index in wxOwnerDrawnComboBox::Select") );
999
57693473 1000 GetVListBoxComboPopup()->SetSelection(n);
a340b80d
VZ
1001
1002 wxString str;
1003 if ( n >= 0 )
57693473 1004 str = GetVListBoxComboPopup()->GetString(n);
a340b80d
VZ
1005
1006 // Refresh text portion in control
1007 if ( m_text )
1008 m_text->SetValue( str );
1009 else
1010 m_valueString = str;
1011
1012 Refresh();
1013}
1014
1015int wxOwnerDrawnComboBox::GetSelection() const
1016{
54a61762
WS
1017 if ( !m_popupInterface )
1018 return m_initChs.Index(m_valueString);
1019
57693473 1020 return GetVListBoxComboPopup()->GetSelection();
a340b80d
VZ
1021}
1022
a236aa20
VZ
1023int wxOwnerDrawnComboBox::DoInsertItems(const wxArrayStringsAdapter& items,
1024 unsigned int pos,
1025 void **clientData,
1026 wxClientDataType type)
a340b80d 1027{
40b26d75
WS
1028 EnsurePopupControl();
1029
a236aa20 1030 const unsigned int count = items.GetCount();
fd11f777
VZ
1031
1032 if ( HasFlag(wxCB_SORT) )
a236aa20 1033 {
fd11f777
VZ
1034 int n = pos;
1035
1036 for ( unsigned int i = 0; i < count; ++i )
1037 {
1038 int n = GetVListBoxComboPopup()->Append(items[i]);
1039 AssignNewItemClientData(n, clientData, i, type);
1040 }
1041
1042 return n;
a236aa20 1043 }
fd11f777
VZ
1044 else
1045 {
1046 for ( unsigned int i = 0; i < count; ++i, ++pos )
1047 {
1048 GetVListBoxComboPopup()->Insert(items[i], pos);
1049 AssignNewItemClientData(pos, clientData, i, type);
1050 }
a340b80d 1051
fd11f777
VZ
1052 return pos - 1;
1053 }
a340b80d
VZ
1054}
1055
1056void wxOwnerDrawnComboBox::DoSetItemClientData(unsigned int n, void* clientData)
1057{
6d0ce565 1058 EnsurePopupControl();
57693473 1059
131b1fba
VZ
1060 GetVListBoxComboPopup()->SetItemClientData(n, clientData,
1061 GetClientDataType());
a340b80d
VZ
1062}
1063
1064void* wxOwnerDrawnComboBox::DoGetItemClientData(unsigned int n) const
1065{
54a61762
WS
1066 if ( !m_popupInterface )
1067 return NULL;
1068
57693473 1069 return GetVListBoxComboPopup()->GetItemClientData(n);
a340b80d
VZ
1070}
1071
40b26d75
WS
1072// ----------------------------------------------------------------------------
1073// wxOwnerDrawnComboBox item drawing and measuring default implementations
1074// ----------------------------------------------------------------------------
1075
1076void wxOwnerDrawnComboBox::OnDrawItem( wxDC& dc,
1077 const wxRect& rect,
1078 int item,
1079 int flags ) const
1080{
1081 if ( flags & wxODCB_PAINTING_CONTROL )
1082 {
1083 dc.DrawText( GetValue(),
1084 rect.x + GetTextIndent(),
1085 (rect.height-dc.GetCharHeight())/2 + rect.y );
1086 }
1087 else
1088 {
57693473 1089 dc.DrawText( GetVListBoxComboPopup()->GetString(item), rect.x + 2, rect.y );
40b26d75
WS
1090 }
1091}
1092
1093wxCoord wxOwnerDrawnComboBox::OnMeasureItem( size_t WXUNUSED(item) ) const
1094{
1095 return -1;
1096}
1097
1098wxCoord wxOwnerDrawnComboBox::OnMeasureItemWidth( size_t WXUNUSED(item) ) const
1099{
1100 return -1;
1101}
1102
ce22ac45
RR
1103void wxOwnerDrawnComboBox::OnDrawBackground(wxDC& dc,
1104 const wxRect& rect,
1105 int WXUNUSED(item),
1106 int flags) const
40b26d75 1107{
ce22ac45
RR
1108 // We need only to explicitly draw background for items
1109 // that should have selected background. Also, call PrepareBackground
1110 // always when painting the control so that clipping is done properly.
1111
1112 if ( (flags & wxODCB_PAINTING_SELECTED) ||
1113 ((flags & wxODCB_PAINTING_CONTROL) && HasFlag(wxCB_READONLY)) )
40b26d75 1114 {
118f5fbd 1115 int bgFlags = wxCONTROL_SELECTED;
4dd31ff5 1116
ce22ac45 1117 if ( !(flags & wxODCB_PAINTING_CONTROL) )
118f5fbd 1118 bgFlags |= wxCONTROL_ISSUBMENU;
ce22ac45
RR
1119
1120 PrepareBackground(dc, rect, bgFlags);
40b26d75 1121 }
40b26d75
WS
1122}
1123
a57d600f 1124#endif // wxUSE_ODCOMBOBOX