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