fix OpenWatcom warning (patch from Jaakko Salli)
[wxWidgets.git] / src / generic / odcombo.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: odcombo.cpp
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
26 #if wxUSE_OWNERDRAWNCOMBOBOX
27
28 #ifndef WX_PRECOMP
29 #include "wx/log.h"
30 #include "wx/combobox.h"
31 #include "wx/dcclient.h"
32 #include "wx/settings.h"
33 #include "wx/dialog.h"
34 #endif
35
36 #include "wx/combo.h"
37 #include "wx/odcombo.h"
38
39
40 // ============================================================================
41 // implementation
42 // ============================================================================
43
44
45 // ----------------------------------------------------------------------------
46 // wxVListBoxComboPopup is a wxVListBox customized to act as a popup control
47 //
48 // ----------------------------------------------------------------------------
49
50
51 BEGIN_EVENT_TABLE(wxVListBoxComboPopup, wxVListBox)
52 EVT_MOTION(wxVListBoxComboPopup::OnMouseMove)
53 EVT_KEY_DOWN(wxVListBoxComboPopup::OnKey)
54 EVT_LEFT_UP(wxVListBoxComboPopup::OnLeftClick)
55 END_EVENT_TABLE()
56
57
58 void wxVListBoxComboPopup::Init()
59 {
60 m_widestWidth = 0;
61 m_avgCharWidth = 0;
62 m_baseImageWidth = 0;
63 m_itemHeight = 0;
64 m_value = -1;
65 m_itemHover = -1;
66 m_clientDataItemsType = wxClientData_None;
67 }
68
69 bool wxVListBoxComboPopup::Create(wxWindow* parent)
70 {
71 if ( !wxVListBox::Create(parent,
72 wxID_ANY,
73 wxDefaultPosition,
74 wxDefaultSize,
75 wxBORDER_SIMPLE | wxLB_INT_HEIGHT | wxWANTS_CHARS) )
76 return false;
77
78 m_useFont = m_combo->GetFont();
79
80 wxVListBox::SetItemCount(m_strings.GetCount());
81
82 // TODO: Move this to SetFont
83 m_itemHeight = GetCharHeight() + 0;
84
85 return true;
86 }
87
88 wxVListBoxComboPopup::~wxVListBoxComboPopup()
89 {
90 Clear();
91 }
92
93 bool wxVListBoxComboPopup::LazyCreate()
94 {
95 // NB: There is a bug with wxVListBox that can be avoided by creating
96 // it later (bug causes empty space to be shown if initial selection
97 // is at the end of a list longer than the control can show at once).
98 return true;
99 }
100
101 // paint the control itself
102 void wxVListBoxComboPopup::PaintComboControl( wxDC& dc, const wxRect& rect )
103 {
104 if ( !(m_combo->GetWindowStyle() & wxODCB_STD_CONTROL_PAINT) )
105 {
106 m_combo->DrawFocusBackground(dc,rect,0);
107 if ( m_value >= 0 )
108 {
109 OnDrawItem(dc,rect,m_value,wxCP_PAINTING_CONTROL);
110 return;
111 }
112 }
113
114 wxComboPopup::PaintComboControl(dc,rect);
115 }
116
117 void wxVListBoxComboPopup::OnDrawItem(wxDC& dc, const wxRect& rect, size_t n) const
118 {
119 // TODO: Maybe this code could be moved to wxVListBox::OnPaint?
120 dc.SetFont(m_useFont);
121
122 // Set correct text colour for selected items
123 if ( wxVListBox::GetSelection() == (int) n )
124 dc.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT) );
125 else
126 dc.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT) );
127
128 OnDrawItem(dc,rect,(int)n,0);
129 }
130
131 wxCoord wxVListBoxComboPopup::OnMeasureItem(size_t WXUNUSED(n)) const
132 {
133 return m_itemHeight;
134 }
135
136 wxCoord wxVListBoxComboPopup::OnMeasureItemWidth(size_t WXUNUSED(n)) const
137 {
138 //return OnMeasureListItemWidth(n);
139 return -1;
140 }
141
142 void wxVListBoxComboPopup::OnDrawBackground(wxDC& dc, const wxRect& rect, size_t n) const
143 {
144 // we need to render selected and current items differently
145 if ( IsCurrent(n) )
146 {
147 m_combo->DrawFocusBackground( dc, rect, wxCONTROL_ISSUBMENU|wxCONTROL_SELECTED );
148 }
149 //else: do nothing for the normal items
150 }
151
152 // This is called from wxVListBoxComboPopup::OnDrawItem, with text colour and font prepared
153 void wxVListBoxComboPopup::OnDrawItem( wxDC& dc, const wxRect& rect, int item, int flags ) const
154 {
155 if ( flags & wxCP_PAINTING_CONTROL )
156 {
157 dc.DrawText( m_combo->GetValue(),
158 rect.x + m_combo->GetTextIndent(),
159 (rect.height-dc.GetCharHeight())/2 + rect.y );
160 }
161 else
162 {
163 dc.DrawText( GetString(item), rect.x + 2, rect.y );
164 }
165 }
166
167 void wxVListBoxComboPopup::DismissWithEvent()
168 {
169 int selection = wxVListBox::GetSelection();
170
171 Dismiss();
172
173 wxString valStr;
174 if ( selection != wxNOT_FOUND )
175 valStr = m_strings[selection];
176 else
177 valStr = wxEmptyString;
178
179 m_value = selection;
180
181 if ( valStr != m_combo->GetValue() )
182 m_combo->SetValue(valStr);
183
184 SendComboBoxEvent(selection);
185 }
186
187 void wxVListBoxComboPopup::SendComboBoxEvent( int selection )
188 {
189 wxCommandEvent evt(wxEVT_COMMAND_COMBOBOX_SELECTED,m_combo->GetId());
190
191 evt.SetEventObject(m_combo);
192
193 evt.SetInt(selection);
194
195 // Set client data, if any
196 if ( selection >= 0 && (int)m_clientDatas.GetCount() > selection )
197 {
198 void* clientData = m_clientDatas[selection];
199 if ( m_clientDataItemsType == wxClientData_Object )
200 evt.SetClientObject((wxClientData*)clientData);
201 else
202 evt.SetClientData(clientData);
203 }
204
205 m_combo->GetEventHandler()->AddPendingEvent(evt);
206 }
207
208 // returns true if key was consumed
209 bool wxVListBoxComboPopup::HandleKey( int keycode, bool saturate )
210 {
211 int value = m_value;
212 int itemCount = GetCount();
213
214 if ( keycode == WXK_DOWN || keycode == WXK_RIGHT )
215 {
216 value++;
217 }
218 else if ( keycode == WXK_UP || keycode == WXK_LEFT )
219 {
220 value--;
221 }
222 else if ( keycode == WXK_PAGEDOWN )
223 {
224 value+=10;
225 }
226 else if ( keycode == WXK_PAGEUP )
227 {
228 value-=10;
229 }
230 else
231 return false;
232
233 if ( saturate )
234 {
235 if ( value >= itemCount )
236 value = itemCount - 1;
237 else if ( value < 0 )
238 value = 0;
239 }
240 else
241 {
242 if ( value >= itemCount )
243 value -= itemCount;
244 else if ( value < 0 )
245 value += itemCount;
246 }
247
248 if ( value == m_value )
249 // Even if value was same, don't skip the event
250 // (good for consistency)
251 return true;
252
253 m_value = value;
254
255 if ( value >= 0 )
256 m_combo->SetValue(m_strings[value]);
257
258 SendComboBoxEvent(m_value);
259
260 return true;
261 }
262
263 void wxVListBoxComboPopup::OnComboDoubleClick()
264 {
265 // Cycle on dclick (disable saturation to allow true cycling).
266 if ( !::wxGetKeyState(WXK_SHIFT) )
267 HandleKey(WXK_DOWN,false);
268 else
269 HandleKey(WXK_UP,false);
270 }
271
272 void wxVListBoxComboPopup::OnComboKeyEvent( wxKeyEvent& event )
273 {
274 // Saturated key movement on
275 if ( !HandleKey(event.GetKeyCode(),true) )
276 event.Skip();
277 }
278
279 void wxVListBoxComboPopup::OnPopup()
280 {
281 // *must* set value after size is set (this is because of a vlbox bug)
282 wxVListBox::SetSelection(m_value);
283 }
284
285 void wxVListBoxComboPopup::OnMouseMove(wxMouseEvent& event)
286 {
287 // Move selection to cursor if it is inside the popup
288 int itemHere = GetItemAtPosition(event.GetPosition());
289 if ( itemHere >= 0 )
290 wxVListBox::SetSelection(itemHere);
291
292 event.Skip();
293 }
294
295 void wxVListBoxComboPopup::OnLeftClick(wxMouseEvent& WXUNUSED(event))
296 {
297 DismissWithEvent();
298 }
299
300 void wxVListBoxComboPopup::OnKey(wxKeyEvent& event)
301 {
302 // Select item if ENTER is pressed
303 if ( event.GetKeyCode() == WXK_RETURN || event.GetKeyCode() == WXK_NUMPAD_ENTER )
304 {
305 DismissWithEvent();
306 }
307 // Hide popup if ESC is pressed
308 else if ( event.GetKeyCode() == WXK_ESCAPE )
309 Dismiss();
310 else
311 event.Skip();
312 }
313
314 void wxVListBoxComboPopup::CheckWidth( int pos )
315 {
316 wxCoord x = OnMeasureItemWidth(pos);
317
318 if ( x < 0 )
319 {
320 if ( !m_useFont.Ok() )
321 m_useFont = m_combo->GetFont();
322
323 wxCoord y;
324 m_combo->GetTextExtent(m_strings[pos], &x, &y, 0, 0, &m_useFont);
325 x += 4;
326 }
327
328 if ( m_widestWidth < x )
329 {
330 m_widestWidth = x;
331 }
332 }
333
334 void wxVListBoxComboPopup::Insert( const wxString& item, int pos )
335 {
336 // Need to change selection?
337 wxString strValue;
338 if ( !(m_combo->GetWindowStyle() & wxCB_READONLY) &&
339 m_combo->GetValue() == item )
340 {
341 m_value = pos;
342 }
343
344 m_strings.Insert(item,pos);
345
346 if ( IsCreated() )
347 wxVListBox::SetItemCount( wxVListBox::GetItemCount()+1 );
348
349 // Calculate width
350 CheckWidth(pos);
351 }
352
353 int wxVListBoxComboPopup::Append(const wxString& item)
354 {
355 int pos = (int)m_strings.GetCount();
356
357 if ( m_combo->GetWindowStyle() & wxCB_SORT )
358 {
359 // Find position
360 // TODO: Could be optimized with binary search
361 wxArrayString strings = m_strings;
362 unsigned int i;
363
364 for ( i=0; i<strings.GetCount(); i++ )
365 {
366 if ( item.Cmp(strings.Item(i)) < 0 )
367 {
368 pos = (int)i;
369 break;
370 }
371 }
372 }
373
374 Insert(item,pos);
375
376 return pos;
377 }
378
379 void wxVListBoxComboPopup::Clear()
380 {
381 wxASSERT(m_combo);
382
383 m_strings.Empty();
384
385 ClearClientDatas();
386
387 if ( IsCreated() )
388 wxVListBox::SetItemCount(0);
389 }
390
391 void wxVListBoxComboPopup::ClearClientDatas()
392 {
393 if ( m_clientDataItemsType == wxClientData_Object )
394 {
395 size_t i;
396 for ( i=0; i<m_clientDatas.GetCount(); i++ )
397 delete (wxClientData*) m_clientDatas[i];
398 }
399
400 m_clientDatas.Empty();
401 }
402
403 void wxVListBoxComboPopup::SetItemClientData( unsigned int n,
404 void* clientData,
405 wxClientDataType clientDataItemsType )
406 {
407 // It should be sufficient to update this variable only here
408 m_clientDataItemsType = clientDataItemsType;
409
410 m_clientDatas.SetCount(n+1,NULL);
411 m_clientDatas[n] = clientData;
412 }
413
414 void* wxVListBoxComboPopup::GetItemClientData(unsigned int n) const
415 {
416 if ( m_clientDatas.GetCount() > n )
417 return m_clientDatas[n];
418
419 return NULL;
420 }
421
422 void wxVListBoxComboPopup::Delete( unsigned int item )
423 {
424 // Remove client data, if set
425 if ( m_clientDatas.GetCount() )
426 {
427 if ( m_clientDataItemsType == wxClientData_Object )
428 delete (wxClientData*) m_clientDatas[item];
429
430 m_clientDatas.RemoveAt(item);
431 }
432
433 m_strings.RemoveAt(item);
434
435 if ( IsCreated() )
436 wxVListBox::SetItemCount( wxVListBox::GetItemCount()-1 );
437 }
438
439 int wxVListBoxComboPopup::FindString(const wxString& s) const
440 {
441 return m_strings.Index(s);
442 }
443
444 unsigned int wxVListBoxComboPopup::GetCount() const
445 {
446 return m_strings.GetCount();
447 }
448
449 wxString wxVListBoxComboPopup::GetString( int item ) const
450 {
451 return m_strings[item];
452 }
453
454 void wxVListBoxComboPopup::SetString( int item, const wxString& str )
455 {
456 m_strings[item] = str;
457 }
458
459 wxString wxVListBoxComboPopup::GetStringValue() const
460 {
461 if ( m_value >= 0 )
462 return m_strings[m_value];
463 return wxEmptyString;
464 }
465
466 void wxVListBoxComboPopup::SetSelection( int item )
467 {
468 // This seems to be necessary (2.5.3 w/ MingW atleast)
469 if ( item < -1 || item >= (int)m_strings.GetCount() )
470 item = -1;
471
472 m_value = item;
473
474 if ( IsCreated() )
475 wxVListBox::SetSelection(item);
476 }
477
478 int wxVListBoxComboPopup::GetSelection() const
479 {
480 return m_value;
481 }
482
483 void wxVListBoxComboPopup::SetStringValue( const wxString& value )
484 {
485 int index = m_strings.Index(value);
486
487 m_value = index;
488
489 if ( index >= -1 && index < (int)wxVListBox::GetItemCount() )
490 wxVListBox::SetSelection(index);
491 }
492
493 wxSize wxVListBoxComboPopup::GetAdjustedSize( int minWidth, int prefHeight, int maxHeight )
494 {
495 int height = 250;
496
497 if ( m_strings.GetCount() )
498 {
499 if ( prefHeight > 0 )
500 height = prefHeight;
501
502 if ( height > maxHeight )
503 height = maxHeight;
504
505 int totalHeight = GetTotalHeight(); // + 3;
506 if ( height >= totalHeight )
507 {
508 height = totalHeight;
509 }
510 else
511 {
512 // Adjust height to a multiple of the height of the first item
513 // NB: Calculations that take variable height into account
514 // are unnecessary.
515 int fih = GetLineHeight(0);
516 int shown = height/fih;
517 height = shown * fih;
518 }
519 }
520 else
521 height = 50;
522
523 // Take scrollbar into account in width calculations
524 int widestWidth = m_widestWidth + wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
525 return wxSize(minWidth > widestWidth ? minWidth : widestWidth,
526 height+2);
527 }
528
529 //void wxVListBoxComboPopup::Populate( int n, const wxString choices[] )
530 void wxVListBoxComboPopup::Populate( const wxArrayString& choices )
531 {
532 int i;
533
534 int n = choices.GetCount();
535
536 for ( i=0; i<n; i++ )
537 {
538 const wxString& item = choices.Item(i);
539 m_strings.Add(item);
540 CheckWidth(i);
541 }
542
543 if ( IsCreated() )
544 wxVListBox::SetItemCount(n);
545
546 // Sort the initial choices
547 if ( m_combo->GetWindowStyle() & wxCB_SORT )
548 m_strings.Sort();
549
550 // Find initial selection
551 wxString strValue = m_combo->GetValue();
552 if ( strValue.Length() )
553 m_value = m_strings.Index(strValue);
554 }
555
556 // ----------------------------------------------------------------------------
557 // wxOwnerDrawnComboBox
558 // ----------------------------------------------------------------------------
559
560
561 BEGIN_EVENT_TABLE(wxOwnerDrawnComboBox, wxComboControl)
562 END_EVENT_TABLE()
563
564
565 IMPLEMENT_DYNAMIC_CLASS2(wxOwnerDrawnComboBox, wxComboControl, wxControlWithItems)
566
567 void wxOwnerDrawnComboBox::Init()
568 {
569 m_popupInterface = NULL;
570 }
571
572 bool wxOwnerDrawnComboBox::Create(wxWindow *parent,
573 wxWindowID id,
574 const wxString& value,
575 const wxPoint& pos,
576 const wxSize& size,
577 long style,
578 const wxValidator& validator,
579 const wxString& name)
580 {
581 return wxComboControl::Create(parent,id,value,pos,size,style,validator,name);
582 }
583
584 wxOwnerDrawnComboBox::wxOwnerDrawnComboBox(wxWindow *parent,
585 wxWindowID id,
586 const wxString& value,
587 const wxPoint& pos,
588 const wxSize& size,
589 const wxArrayString& choices,
590 long style,
591 const wxValidator& validator,
592 const wxString& name)
593 : wxComboControl()
594 {
595 Init();
596
597 Create(parent,id,value,pos,size,choices,style, validator, name);
598 }
599
600 bool wxOwnerDrawnComboBox::Create(wxWindow *parent,
601 wxWindowID id,
602 const wxString& value,
603 const wxPoint& pos,
604 const wxSize& size,
605 const wxArrayString& choices,
606 long style,
607 const wxValidator& validator,
608 const wxString& name)
609 {
610 m_initChs = choices;
611 //wxCArrayString chs(choices);
612
613 //return Create(parent, id, value, pos, size, chs.GetCount(),
614 // chs.GetStrings(), style, validator, name);
615 return Create(parent, id, value, pos, size, 0,
616 NULL, style, validator, name);
617 }
618
619 bool wxOwnerDrawnComboBox::Create(wxWindow *parent,
620 wxWindowID id,
621 const wxString& value,
622 const wxPoint& pos,
623 const wxSize& size,
624 int n,
625 const wxString choices[],
626 long style,
627 const wxValidator& validator,
628 const wxString& name)
629 {
630
631 if ( !Create(parent, id, value, pos, size, style,
632 validator, name) )
633 {
634 return false;
635 }
636
637 int i;
638 for ( i=0; i<n; i++ )
639 m_initChs.Add(choices[i]);
640
641 return true;
642 }
643
644 wxOwnerDrawnComboBox::~wxOwnerDrawnComboBox()
645 {
646 if ( m_popupInterface )
647 m_popupInterface->ClearClientDatas();
648 }
649
650 void wxOwnerDrawnComboBox::SetPopupControl( wxComboPopup* popup )
651 {
652 if ( !popup )
653 {
654 popup = new wxVListBoxComboPopup();
655 }
656
657 wxComboControl::SetPopupControl(popup);
658
659 wxASSERT(popup);
660 m_popupInterface = (wxVListBoxComboPopup*) popup;
661
662 // Add initial choices to the wxVListBox
663 if ( !m_popupInterface->GetCount() )
664 {
665 //m_popupInterface->Populate(m_initChs.GetCount(),m_initChs.GetStrings());
666 m_popupInterface->Populate(m_initChs);
667 m_initChs.Clear();
668 }
669 }
670
671 // ----------------------------------------------------------------------------
672 // wxOwnerDrawnComboBox item manipulation methods
673 // ----------------------------------------------------------------------------
674
675 void wxOwnerDrawnComboBox::Clear()
676 {
677 EnsurePopupControl();
678
679 m_popupInterface->Clear();
680
681 GetTextCtrl()->SetValue(wxEmptyString);
682 }
683
684 void wxOwnerDrawnComboBox::Delete(unsigned int n)
685 {
686 wxCHECK_RET( n < GetCount(), _T("invalid index in wxOwnerDrawnComboBox::Delete") );
687
688 if ( GetSelection() == (int) n )
689 SetValue(wxEmptyString);
690
691 m_popupInterface->Delete(n);
692 }
693
694 unsigned int wxOwnerDrawnComboBox::GetCount() const
695 {
696 wxASSERT_MSG( m_popupInterface, wxT("no popup interface") );
697 return m_popupInterface->GetCount();
698 }
699
700 wxString wxOwnerDrawnComboBox::GetString(unsigned int n) const
701 {
702 wxCHECK_MSG( n < GetCount(), wxEmptyString, _T("invalid index in wxOwnerDrawnComboBox::GetString") );
703 return m_popupInterface->GetString(n);
704 }
705
706 void wxOwnerDrawnComboBox::SetString(unsigned int n, const wxString& s)
707 {
708 wxCHECK_RET( n < GetCount(), _T("invalid index in wxOwnerDrawnComboBox::SetString") );
709 m_popupInterface->SetString(n,s);
710 }
711
712 int wxOwnerDrawnComboBox::FindString(const wxString& s) const
713 {
714 wxASSERT_MSG( m_popupInterface, wxT("no popup interface") );
715 return m_popupInterface->FindString(s);
716 }
717
718 void wxOwnerDrawnComboBox::Select(int n)
719 {
720 wxCHECK_RET( (n >= -1) && (n < (int)GetCount()), _T("invalid index in wxOwnerDrawnComboBox::Select") );
721 EnsurePopupControl();
722
723 m_popupInterface->SetSelection(n);
724
725 wxString str;
726 if ( n >= 0 )
727 str = m_popupInterface->GetString(n);
728
729 // Refresh text portion in control
730 if ( m_text )
731 m_text->SetValue( str );
732 else
733 m_valueString = str;
734
735 Refresh();
736 }
737
738 int wxOwnerDrawnComboBox::GetSelection() const
739 {
740 wxASSERT_MSG( m_popupInterface, wxT("no popup interface") );
741 return m_popupInterface->GetSelection();
742 }
743
744 int wxOwnerDrawnComboBox::DoAppend(const wxString& item)
745 {
746 EnsurePopupControl();
747 wxASSERT(m_popupInterface);
748 return m_popupInterface->Append(item);
749 }
750
751 int wxOwnerDrawnComboBox::DoInsert(const wxString& item, unsigned int pos)
752 {
753 wxCHECK_MSG(!(GetWindowStyle() & wxCB_SORT), -1, wxT("can't insert into sorted list"));
754 wxCHECK_MSG(pos<=GetCount(), -1, wxT("invalid index"));
755
756 EnsurePopupControl();
757 m_popupInterface->Insert(item,pos);
758
759 return pos;
760 }
761
762 void wxOwnerDrawnComboBox::DoSetItemClientData(unsigned int n, void* clientData)
763 {
764 EnsurePopupControl();
765 m_popupInterface->SetItemClientData(n,clientData,m_clientDataItemsType);
766 }
767
768 void* wxOwnerDrawnComboBox::DoGetItemClientData(unsigned int n) const
769 {
770 wxASSERT_MSG( m_popupInterface, wxT("no popup interface") );
771 return m_popupInterface->GetItemClientData(n);
772 }
773
774 void wxOwnerDrawnComboBox::DoSetItemClientObject(unsigned int n, wxClientData* clientData)
775 {
776 DoSetItemClientData(n, (void*) clientData);
777 }
778
779 wxClientData* wxOwnerDrawnComboBox::DoGetItemClientObject(unsigned int n) const
780 {
781 return (wxClientData*) DoGetItemClientData(n);
782 }
783
784 #endif // wxUSE_OWNERDRAWNCOMBOBOX