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