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