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