]>
Commit | Line | Data |
---|---|---|
e9576ca5 SC |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: listbox.cpp | |
3 | // Purpose: wxListBox | |
a31a5f85 | 4 | // Author: Stefan Csomor |
e9576ca5 | 5 | // Modified by: |
a31a5f85 | 6 | // Created: 1998-01-01 |
e9576ca5 | 7 | // RCS-ID: $Id$ |
a31a5f85 | 8 | // Copyright: (c) Stefan Csomor |
77ffb593 | 9 | // Licence: wxWidgets licence |
e9576ca5 SC |
10 | /////////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | #ifdef __GNUG__ | |
13 | #pragma implementation "listbox.h" | |
14 | #endif | |
15 | ||
03e11df5 | 16 | #include "wx/app.h" |
e9576ca5 | 17 | #include "wx/listbox.h" |
dc0ace7c | 18 | #include "wx/button.h" |
e9576ca5 | 19 | #include "wx/settings.h" |
422644a3 | 20 | #include "wx/toplevel.h" |
e9576ca5 SC |
21 | #include "wx/dynarray.h" |
22 | #include "wx/log.h" | |
23 | ||
519cb848 | 24 | #include "wx/utils.h" |
519cb848 | 25 | |
2f1ae414 | 26 | #if !USE_SHARED_LIBRARY |
e40298d5 | 27 | IMPLEMENT_DYNAMIC_CLASS(wxListBox, wxControl) |
519cb848 SC |
28 | |
29 | BEGIN_EVENT_TABLE(wxListBox, wxControl) | |
36ba5f7c | 30 | #if !__WXMAC_OSX__ |
e40298d5 JS |
31 | EVT_SIZE( wxListBox::OnSize ) |
32 | EVT_CHAR( wxListBox::OnChar ) | |
facd6764 | 33 | #endif |
519cb848 | 34 | END_EVENT_TABLE() |
2f1ae414 | 35 | #endif |
e9576ca5 | 36 | |
facd6764 SC |
37 | #include "wx/mac/uma.h" |
38 | ||
a9fc5eec SC |
39 | const short kTextColumnId = 1024 ; |
40 | ||
facd6764 SC |
41 | // new databrowserbased version |
42 | ||
43 | // Listbox item | |
44 | wxListBox::wxListBox() | |
45 | { | |
46 | m_noItems = 0; | |
47 | m_selected = 0; | |
48 | m_macList = NULL ; | |
49 | } | |
50 | ||
51 | bool wxListBox::Create(wxWindow *parent, wxWindowID id, | |
52 | const wxPoint& pos, | |
53 | const wxSize& size, | |
54 | const wxArrayString& choices, | |
55 | long style, | |
56 | const wxValidator& validator, | |
57 | const wxString& name) | |
58 | { | |
59 | wxCArrayString chs(choices); | |
60 | ||
61 | return Create(parent, id, pos, size, chs.GetCount(), chs.GetStrings(), | |
62 | style, validator, name); | |
63 | } | |
64 | ||
83ce5634 SC |
65 | #if TARGET_API_MAC_OSX |
66 | static pascal void DataBrowserItemNotificationProc(ControlRef browser, DataBrowserItemID itemID, | |
67 | DataBrowserItemNotification message, DataBrowserItemDataRef itemData) | |
68 | #else | |
69 | static pascal void DataBrowserItemNotificationProc(ControlRef browser, DataBrowserItemID itemID, | |
70 | DataBrowserItemNotification message) | |
71 | #endif | |
72 | { | |
73 | long ref = GetControlReference( browser ) ; | |
74 | if ( ref ) | |
75 | { | |
469d8d5d | 76 | wxListBox* list = wxDynamicCast( (wxObject*) ref , wxListBox ) ; |
83ce5634 SC |
77 | for ( size_t i = 0 ; i < list->m_idArray.GetCount() ; ++i ) |
78 | if ( list->m_idArray[i] == (long) itemID ) | |
79 | { | |
80 | bool trigger = false ; | |
81 | wxCommandEvent event( | |
82 | wxEVT_COMMAND_LISTBOX_SELECTED, list->GetId() ); | |
83 | switch( message ) | |
84 | { | |
85 | case kDataBrowserItemDeselected : | |
86 | if ( list->HasMultipleSelection() ) | |
87 | trigger = true ; | |
88 | break ; | |
89 | case kDataBrowserItemSelected : | |
90 | trigger = true ; | |
91 | break ; | |
92 | case kDataBrowserItemDoubleClicked : | |
93 | event.SetEventType(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED) ; | |
94 | trigger = true ; | |
95 | break ; | |
96 | default : | |
97 | break ; | |
98 | } | |
99 | if ( trigger ) | |
100 | { | |
101 | event.SetEventObject( list ); | |
102 | if ( list->HasClientObjectData() ) | |
103 | event.SetClientObject( list->GetClientObject(i) ); | |
104 | else if ( list->HasClientUntypedData() ) | |
105 | event.SetClientData( list->GetClientData(i) ); | |
106 | event.SetString( list->GetString(i) ); | |
107 | event.SetInt(i) ; | |
108 | event.SetExtraLong( list->HasMultipleSelection() ? message == kDataBrowserItemSelected : TRUE ); | |
109 | list->GetEventHandler()->ProcessEvent(event) ; | |
110 | } | |
111 | break ; | |
112 | } | |
113 | } | |
114 | } | |
115 | ||
116 | ||
facd6764 SC |
117 | static pascal OSStatus ListBoxGetSetItemData(ControlRef browser, |
118 | DataBrowserItemID itemID, DataBrowserPropertyID property, | |
119 | DataBrowserItemDataRef itemData, Boolean changeValue) | |
120 | { | |
121 | OSStatus err = errDataBrowserPropertyNotSupported; | |
122 | ||
123 | if ( ! changeValue ) | |
124 | { | |
125 | switch (property) | |
126 | { | |
127 | ||
a9fc5eec | 128 | case kTextColumnId: |
facd6764 SC |
129 | { |
130 | long ref = GetControlReference( browser ) ; | |
131 | if ( ref ) | |
132 | { | |
469d8d5d | 133 | wxListBox* list = wxDynamicCast( (wxObject*) ref , wxListBox ) ; |
facd6764 SC |
134 | for ( size_t i = 0 ; i < list->m_idArray.GetCount() ; ++i ) |
135 | if ( list->m_idArray[i] == (long) itemID ) | |
136 | { | |
137 | wxMacCFStringHolder cf( list->GetString(i) , list->GetFont().GetEncoding() ) ; | |
138 | verify_noerr( ::SetDataBrowserItemDataText( itemData , cf ) ) ; | |
139 | err = noErr ; | |
140 | break ; | |
141 | } | |
142 | } | |
143 | } | |
144 | break; | |
145 | ||
146 | default: | |
147 | ||
148 | break; | |
149 | } | |
150 | } | |
151 | ||
152 | return err; | |
153 | } | |
154 | bool wxListBox::Create(wxWindow *parent, wxWindowID id, | |
155 | const wxPoint& pos, | |
156 | const wxSize& size, | |
157 | int n, const wxString choices[], | |
158 | long style, | |
159 | const wxValidator& validator, | |
160 | const wxString& name) | |
161 | { | |
162 | m_macIsUserPane = FALSE ; | |
5e6f42cd SC |
163 | |
164 | wxASSERT_MSG( !(style & wxLB_MULTIPLE) || !(style & wxLB_EXTENDED), | |
165 | _T("only one of listbox selection modes can be specified") ); | |
facd6764 SC |
166 | |
167 | if ( !wxListBoxBase::Create(parent, id, pos, size, style & ~(wxHSCROLL|wxVSCROLL), validator, name) ) | |
168 | return false; | |
169 | ||
170 | m_noItems = 0 ; // this will be increased by our append command | |
171 | m_selected = 0; | |
172 | m_nextId = 1 ; | |
173 | ||
174 | ||
175 | Rect bounds = wxMacGetBoundsForControl( this , pos , size ) ; | |
176 | ControlRef browser ; | |
177 | ||
178 | verify_noerr( ::CreateDataBrowserControl( MAC_WXHWND(parent->MacGetTopLevelWindowRef()), &bounds, kDataBrowserListView , (ControlRef *)&m_macControl ) ); | |
179 | browser = (ControlRef) m_macControl ; | |
180 | ||
181 | DataBrowserSelectionFlags options = kDataBrowserDragSelect ; | |
182 | if ( style & wxLB_MULTIPLE ) | |
183 | { | |
184 | options += kDataBrowserAlwaysExtendSelection + kDataBrowserCmdTogglesSelection ; | |
185 | } | |
186 | else if ( style & wxLB_EXTENDED ) | |
187 | { | |
188 | // default behaviour | |
189 | } | |
190 | else | |
191 | { | |
192 | options += kDataBrowserSelectOnlyOne ; | |
193 | } | |
194 | verify_noerr(SetDataBrowserSelectionFlags (browser, options ) ); | |
195 | ||
196 | DataBrowserListViewColumnDesc columnDesc ; | |
197 | columnDesc.headerBtnDesc.titleOffset = 0; | |
198 | columnDesc.headerBtnDesc.version = kDataBrowserListViewLatestHeaderDesc; | |
199 | ||
200 | columnDesc.headerBtnDesc.btnFontStyle.flags = | |
201 | kControlUseFontMask | kControlUseJustMask; | |
202 | ||
203 | columnDesc.headerBtnDesc.btnContentInfo.contentType = kControlNoContent; | |
204 | columnDesc.propertyDesc.propertyType = kDataBrowserTextType; | |
205 | columnDesc.headerBtnDesc.btnFontStyle.just = teFlushDefault; | |
206 | columnDesc.headerBtnDesc.minimumWidth = 0; | |
207 | columnDesc.headerBtnDesc.maximumWidth = 10000; | |
208 | ||
209 | columnDesc.headerBtnDesc.btnFontStyle.font = kControlFontViewSystemFont; | |
210 | columnDesc.headerBtnDesc.btnFontStyle.style = normal; | |
211 | columnDesc.headerBtnDesc.titleString = NULL ; // CFSTR( "" ); | |
212 | ||
a9fc5eec | 213 | columnDesc.propertyDesc.propertyID = kTextColumnId; |
facd6764 | 214 | columnDesc.propertyDesc.propertyType = kDataBrowserTextType; |
c2697b87 | 215 | columnDesc.propertyDesc.propertyFlags = |
9bd2d050 | 216 | #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2 |
c2697b87 SC |
217 | kDataBrowserListViewTypeSelectColumn | |
218 | #endif | |
219 | kDataBrowserTableViewSelectionColumn ; | |
facd6764 SC |
220 | |
221 | ||
222 | verify_noerr(::AddDataBrowserListViewColumn(browser, &columnDesc, kDataBrowserListViewAppendColumn) ) ; | |
223 | verify_noerr(::AutoSizeDataBrowserListViewColumns( browser ) ) ; | |
224 | verify_noerr(::SetDataBrowserHasScrollBars( browser , false , true ) ) ; | |
225 | verify_noerr(::SetDataBrowserTableViewHiliteStyle( browser, kDataBrowserTableViewFillHilite ) ) ; | |
226 | verify_noerr(::SetDataBrowserListViewHeaderBtnHeight( browser , 0 ) ) ; | |
227 | DataBrowserCallbacks callbacks ; | |
228 | ||
229 | callbacks.version = kDataBrowserLatestCallbacks; | |
230 | ||
231 | InitDataBrowserCallbacks(&callbacks); | |
232 | ||
233 | callbacks.u.v1.itemDataCallback = | |
234 | NewDataBrowserItemDataUPP(ListBoxGetSetItemData); | |
235 | ||
83ce5634 SC |
236 | callbacks.u.v1.itemNotificationCallback = |
237 | #if TARGET_API_MAC_OSX | |
238 | (DataBrowserItemNotificationUPP) NewDataBrowserItemNotificationWithItemUPP(DataBrowserItemNotificationProc) ; | |
239 | #else | |
240 | NewDataBrowserItemNotificationUPP(DataBrowserItemNotificationProc) ; | |
241 | #endif | |
facd6764 SC |
242 | SetDataBrowserCallbacks(browser, &callbacks); |
243 | ||
244 | MacPostControlCreate(pos,size) ; | |
245 | ||
246 | for ( int i = 0 ; i < n ; i++ ) | |
247 | { | |
248 | Append( choices[i] ) ; | |
249 | } | |
250 | ||
d3b5db4b RD |
251 | SetBestSize(size); // Needed because it is a wxControlWithItems |
252 | ||
facd6764 SC |
253 | return TRUE; |
254 | } | |
255 | ||
256 | wxListBox::~wxListBox() | |
257 | { | |
258 | SetControlReference( (ControlRef) m_macControl , NULL ) ; | |
259 | FreeData() ; | |
260 | // avoid access during destruction | |
261 | if ( m_macList ) | |
262 | { | |
263 | m_macList = NULL ; | |
264 | } | |
265 | } | |
266 | ||
267 | void wxListBox::FreeData() | |
268 | { | |
269 | #if wxUSE_OWNER_DRAWN | |
270 | if ( m_windowStyle & wxLB_OWNERDRAW ) | |
271 | { | |
272 | size_t uiCount = m_aItems.Count(); | |
273 | while ( uiCount-- != 0 ) { | |
274 | delete m_aItems[uiCount]; | |
275 | m_aItems[uiCount] = NULL; | |
276 | } | |
277 | ||
278 | m_aItems.Clear(); | |
279 | } | |
280 | else | |
281 | #endif // wxUSE_OWNER_DRAWN | |
282 | if ( HasClientObjectData() ) | |
283 | { | |
284 | for ( size_t n = 0; n < (size_t)m_noItems; n++ ) | |
285 | { | |
286 | delete GetClientObject(n); | |
287 | } | |
288 | } | |
289 | } | |
290 | ||
291 | void wxListBox::DoSetSize(int x, int y, | |
292 | int width, int height, | |
293 | int sizeFlags ) | |
294 | { | |
295 | wxControl::DoSetSize( x , y , width , height , sizeFlags ) ; | |
296 | } | |
297 | ||
298 | void wxListBox::DoSetFirstItem(int N) | |
299 | { | |
300 | MacScrollTo( N ) ; | |
301 | } | |
302 | ||
303 | void wxListBox::Delete(int N) | |
304 | { | |
305 | wxCHECK_RET( N >= 0 && N < m_noItems, | |
306 | wxT("invalid index in wxListBox::Delete") ); | |
307 | ||
308 | #if wxUSE_OWNER_DRAWN | |
309 | delete m_aItems[N]; | |
310 | m_aItems.RemoveAt(N); | |
311 | #else // !wxUSE_OWNER_DRAWN | |
312 | if ( HasClientObjectData() ) | |
313 | { | |
314 | delete GetClientObject(N); | |
315 | } | |
316 | #endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN | |
317 | m_stringArray.RemoveAt( N ) ; | |
318 | m_dataArray.RemoveAt( N ) ; | |
319 | m_noItems --; | |
320 | ||
321 | MacDelete( N ) ; | |
322 | } | |
323 | ||
324 | int wxListBox::DoAppend(const wxString& item) | |
325 | { | |
326 | int index = m_noItems ; | |
327 | m_stringArray.Add( item ) ; | |
328 | m_dataArray.Add( NULL ); | |
329 | m_noItems ++; | |
330 | DoSetItemClientData( index , NULL ) ; | |
331 | MacAppend( item ) ; | |
332 | ||
333 | return index ; | |
334 | } | |
335 | ||
336 | void wxListBox::DoSetItems(const wxArrayString& choices, void** clientData) | |
337 | { | |
338 | Clear() ; | |
339 | int n = choices.GetCount(); | |
340 | ||
341 | for( int i = 0 ; i < n ; ++i ) | |
342 | { | |
343 | if ( clientData ) | |
344 | { | |
345 | #if wxUSE_OWNER_DRAWN | |
346 | wxASSERT_MSG(clientData[i] == NULL, | |
347 | wxT("Can't use client data with owner-drawn listboxes")); | |
348 | #else // !wxUSE_OWNER_DRAWN | |
349 | Append( choices[i] , clientData[i] ) ; | |
350 | #endif | |
351 | } | |
352 | else | |
353 | Append( choices[i] ) ; | |
354 | } | |
355 | ||
356 | #if wxUSE_OWNER_DRAWN | |
357 | if ( m_windowStyle & wxLB_OWNERDRAW ) { | |
358 | // first delete old items | |
359 | size_t ui = m_aItems.Count(); | |
360 | while ( ui-- != 0 ) { | |
361 | delete m_aItems[ui]; | |
362 | m_aItems[ui] = NULL; | |
363 | } | |
364 | m_aItems.Empty(); | |
365 | ||
366 | // then create new ones | |
367 | for ( ui = 0; ui < (size_t)m_noItems; ui++ ) { | |
368 | wxOwnerDrawn *pNewItem = CreateItem(ui); | |
369 | pNewItem->SetName(choices[ui]); | |
370 | m_aItems.Add(pNewItem); | |
371 | } | |
372 | } | |
373 | #endif // wxUSE_OWNER_DRAWN | |
374 | } | |
375 | ||
facd6764 SC |
376 | int wxListBox::FindString(const wxString& s) const |
377 | { | |
378 | ||
379 | if ( s.Right(1) == wxT("*") ) | |
380 | { | |
381 | wxString search = s.Left( s.Length() - 1 ) ; | |
382 | int len = search.Length() ; | |
383 | Str255 s1 , s2 ; | |
384 | wxMacStringToPascal( search , s2 ) ; | |
385 | ||
386 | for ( int i = 0 ; i < m_noItems ; ++ i ) | |
387 | { | |
388 | wxMacStringToPascal( m_stringArray[i].Left( len ) , s1 ) ; | |
389 | ||
390 | if ( EqualString( s1 , s2 , false , false ) ) | |
391 | return i ; | |
392 | } | |
393 | if ( s.Left(1) == wxT("*") && s.Length() > 1 ) | |
394 | { | |
395 | wxString st = s ; | |
396 | st.MakeLower() ; | |
397 | for ( int i = 0 ; i < m_noItems ; ++i ) | |
398 | { | |
399 | if ( GetString(i).Lower().Matches(st) ) | |
400 | return i ; | |
401 | } | |
402 | } | |
403 | ||
404 | } | |
405 | else | |
406 | { | |
407 | Str255 s1 , s2 ; | |
408 | ||
409 | wxMacStringToPascal( s , s2 ) ; | |
410 | ||
411 | for ( int i = 0 ; i < m_noItems ; ++ i ) | |
412 | { | |
413 | wxMacStringToPascal( m_stringArray[i] , s1 ) ; | |
414 | ||
415 | if ( EqualString( s1 , s2 , false , false ) ) | |
416 | return i ; | |
417 | } | |
418 | } | |
419 | return -1; | |
420 | } | |
421 | ||
422 | void wxListBox::Clear() | |
423 | { | |
424 | FreeData(); | |
425 | m_noItems = 0; | |
426 | m_stringArray.Empty() ; | |
427 | m_dataArray.Empty() ; | |
428 | MacClear() ; | |
429 | } | |
430 | ||
431 | void wxListBox::SetSelection(int N, bool select) | |
432 | { | |
433 | wxCHECK_RET( N >= 0 && N < m_noItems, | |
434 | wxT("invalid index in wxListBox::SetSelection") ); | |
435 | MacSetSelection( N , select ) ; | |
436 | GetSelections( m_selectionPreImage ) ; | |
437 | } | |
438 | ||
439 | bool wxListBox::IsSelected(int N) const | |
440 | { | |
441 | wxCHECK_MSG( N >= 0 && N < m_noItems, FALSE, | |
442 | wxT("invalid index in wxListBox::Selected") ); | |
443 | ||
444 | return MacIsSelected( N ) ; | |
445 | } | |
446 | ||
447 | void *wxListBox::DoGetItemClientData(int N) const | |
448 | { | |
449 | wxCHECK_MSG( N >= 0 && N < m_noItems, NULL, | |
450 | wxT("invalid index in wxListBox::GetClientData")); | |
451 | ||
452 | return (void *)m_dataArray[N]; | |
453 | } | |
454 | ||
455 | wxClientData *wxListBox::DoGetItemClientObject(int N) const | |
456 | { | |
457 | return (wxClientData *) DoGetItemClientData( N ) ; | |
458 | } | |
459 | ||
460 | void wxListBox::DoSetItemClientData(int N, void *Client_data) | |
461 | { | |
462 | wxCHECK_RET( N >= 0 && N < m_noItems, | |
463 | wxT("invalid index in wxListBox::SetClientData") ); | |
464 | ||
465 | #if wxUSE_OWNER_DRAWN | |
466 | if ( m_windowStyle & wxLB_OWNERDRAW ) | |
467 | { | |
468 | // client data must be pointer to wxOwnerDrawn, otherwise we would crash | |
469 | // in OnMeasure/OnDraw. | |
470 | wxFAIL_MSG(wxT("Can't use client data with owner-drawn listboxes")); | |
471 | } | |
472 | #endif // wxUSE_OWNER_DRAWN | |
473 | wxASSERT_MSG( m_dataArray.GetCount() >= (size_t) N , wxT("invalid client_data array") ) ; | |
474 | ||
475 | if ( m_dataArray.GetCount() > (size_t) N ) | |
476 | { | |
477 | m_dataArray[N] = (char*) Client_data ; | |
478 | } | |
479 | else | |
480 | { | |
481 | m_dataArray.Add( (char*) Client_data ) ; | |
482 | } | |
483 | } | |
484 | ||
485 | void wxListBox::DoSetItemClientObject(int n, wxClientData* clientData) | |
486 | { | |
487 | DoSetItemClientData(n, clientData); | |
488 | } | |
489 | ||
490 | // Return number of selections and an array of selected integers | |
491 | int wxListBox::GetSelections(wxArrayInt& aSelections) const | |
492 | { | |
493 | return MacGetSelections( aSelections ) ; | |
494 | } | |
495 | ||
496 | // Get single selection, for single choice list items | |
497 | int wxListBox::GetSelection() const | |
498 | { | |
499 | return MacGetSelection() ; | |
500 | } | |
501 | ||
502 | // Find string for position | |
503 | wxString wxListBox::GetString(int N) const | |
504 | { | |
505 | return m_stringArray[N] ; | |
506 | } | |
507 | ||
508 | void wxListBox::DoInsertItems(const wxArrayString& items, int pos) | |
509 | { | |
510 | wxCHECK_RET( pos >= 0 && pos <= m_noItems, | |
511 | wxT("invalid index in wxListBox::InsertItems") ); | |
512 | ||
513 | int nItems = items.GetCount(); | |
514 | ||
515 | for ( int i = 0 ; i < nItems ; i++ ) | |
516 | { | |
517 | m_stringArray.Insert( items[i] , pos + i ) ; | |
518 | m_dataArray.Insert( NULL , pos + i ) ; | |
519 | MacInsert( pos + i , items[i] ) ; | |
520 | } | |
521 | ||
522 | m_noItems += nItems; | |
523 | } | |
524 | ||
525 | void wxListBox::SetString(int N, const wxString& s) | |
526 | { | |
527 | m_stringArray[N] = s ; | |
528 | MacSet( N , s ) ; | |
529 | } | |
530 | ||
531 | wxSize wxListBox::DoGetBestSize() const | |
532 | { | |
533 | int lbWidth = 100; // some defaults | |
534 | int lbHeight = 110; | |
535 | int wLine; | |
536 | ||
537 | { | |
538 | wxMacPortStateHelper st( UMAGetWindowPort( (WindowRef) MacGetTopLevelWindowRef() ) ) ; | |
539 | ||
540 | if ( m_font.Ok() ) | |
541 | { | |
542 | ::TextFont( m_font.MacGetFontNum() ) ; | |
543 | ::TextSize( m_font.MacGetFontSize() ) ; | |
544 | ::TextFace( m_font.MacGetFontStyle() ) ; | |
545 | } | |
546 | else | |
547 | { | |
548 | ::TextFont( kFontIDMonaco ) ; | |
549 | ::TextSize( 9 ); | |
550 | ::TextFace( 0 ) ; | |
551 | } | |
552 | ||
553 | // Find the widest line | |
554 | for(int i = 0; i < GetCount(); i++) { | |
555 | wxString str(GetString(i)); | |
556 | #if wxUSE_UNICODE | |
557 | Point bounds={0,0} ; | |
558 | SInt16 baseline ; | |
559 | ::GetThemeTextDimensions( wxMacCFStringHolder( str , m_font.GetEncoding() ) , | |
560 | kThemeCurrentPortFont, | |
561 | kThemeStateActive, | |
562 | false, | |
563 | &bounds, | |
564 | &baseline ); | |
565 | wLine = bounds.h ; | |
566 | #else | |
567 | wLine = ::TextWidth( str.c_str() , 0 , str.Length() ) ; | |
568 | #endif | |
569 | lbWidth = wxMax(lbWidth, wLine); | |
570 | } | |
571 | ||
572 | // Add room for the scrollbar | |
573 | lbWidth += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X); | |
574 | ||
575 | // And just a bit more | |
576 | int cy = 12 ; | |
577 | int cx = ::TextWidth( "X" , 0 , 1 ) ; | |
578 | lbWidth += cx ; | |
579 | ||
580 | // don't make the listbox too tall (limit height to around 10 items) but don't | |
581 | // make it too small neither | |
582 | lbHeight = (cy+4) * wxMin(wxMax(GetCount(), 3), 10); | |
583 | } | |
584 | ||
585 | return wxSize(lbWidth, lbHeight); | |
586 | } | |
587 | ||
588 | int wxListBox::GetCount() const | |
589 | { | |
590 | return m_noItems; | |
591 | } | |
592 | ||
593 | void wxListBox::Refresh(bool eraseBack, const wxRect *rect) | |
594 | { | |
595 | wxControl::Refresh( eraseBack , rect ) ; | |
596 | // MacRedrawControl() ; | |
597 | } | |
598 | ||
599 | #if wxUSE_OWNER_DRAWN | |
600 | ||
601 | class wxListBoxItem : public wxOwnerDrawn | |
602 | { | |
603 | public: | |
604 | wxListBoxItem(const wxString& str = ""); | |
605 | }; | |
606 | ||
607 | wxListBoxItem::wxListBoxItem(const wxString& str) : wxOwnerDrawn(str, FALSE) | |
608 | { | |
609 | // no bitmaps/checkmarks | |
610 | SetMarginWidth(0); | |
611 | } | |
612 | ||
613 | wxOwnerDrawn *wxListBox::CreateItem(size_t n) | |
614 | { | |
615 | return new wxListBoxItem(); | |
616 | } | |
617 | ||
618 | #endif //USE_OWNER_DRAWN | |
619 | ||
620 | // ============================================================================ | |
621 | // list box control implementation | |
622 | // ============================================================================ | |
623 | ||
624 | void wxListBox::MacDelete( int N ) | |
625 | { | |
626 | UInt32 id = m_idArray[N] ; | |
627 | verify_noerr(::RemoveDataBrowserItems((ControlRef) m_macControl , kDataBrowserNoItem , 1 , (UInt32*) &id , kDataBrowserItemNoProperty ) ) ; | |
628 | m_idArray.RemoveAt( N ) ; | |
629 | } | |
630 | ||
631 | void wxListBox::MacInsert( int n , const wxString& text) | |
632 | { | |
633 | verify_noerr(::AddDataBrowserItems( (ControlRef) m_macControl , kDataBrowserNoItem , 1 , (UInt32*) &m_nextId , kDataBrowserItemNoProperty ) ) ; | |
634 | m_idArray.Insert( m_nextId , n ) ; | |
635 | ++m_nextId ; | |
636 | } | |
637 | ||
638 | void wxListBox::MacAppend( const wxString& text) | |
639 | { | |
640 | verify_noerr(::AddDataBrowserItems( (ControlRef) m_macControl , kDataBrowserNoItem , 1 , (UInt32*) &m_nextId , kDataBrowserItemNoProperty ) ) ; | |
641 | m_idArray.Add( m_nextId ) ; | |
642 | ++m_nextId ; | |
643 | } | |
644 | ||
645 | void wxListBox::MacClear() | |
646 | { | |
647 | verify_noerr(::RemoveDataBrowserItems((ControlRef) m_macControl , kDataBrowserNoItem , 0 , NULL , kDataBrowserItemNoProperty ) ) ; | |
83ce5634 | 648 | m_idArray.Empty() ; |
facd6764 SC |
649 | } |
650 | ||
651 | void wxListBox::MacSetSelection( int n , bool select ) | |
652 | { | |
653 | UInt32 id = m_idArray[n] ; | |
5e6f42cd SC |
654 | if ( !(GetWindowStyle() & (wxLB_MULTIPLE|wxLB_EXTENDED) ) ) |
655 | { | |
656 | int n = MacGetSelection() ; | |
657 | if ( n >= 0 ) | |
658 | { | |
659 | UInt32 idOld = m_idArray[n] ; | |
660 | SetDataBrowserSelectedItems((ControlRef) m_macControl , 1 , & idOld , kDataBrowserItemsRemove ) ; | |
661 | } | |
662 | } | |
facd6764 SC |
663 | if ( ::IsDataBrowserItemSelected( (ControlRef) m_macControl , id ) != select ) |
664 | { | |
665 | verify_noerr(::SetDataBrowserSelectedItems((ControlRef) m_macControl , 1 , & id , kDataBrowserItemsToggle ) ) ; | |
666 | } | |
667 | MacScrollTo( n ) ; | |
668 | } | |
669 | ||
670 | bool wxListBox::MacIsSelected( int n ) const | |
671 | { | |
672 | return ::IsDataBrowserItemSelected( (ControlRef) m_macControl , m_idArray[n] ) ; | |
673 | } | |
674 | ||
675 | int wxListBox::MacGetSelection() const | |
676 | { | |
677 | for ( size_t i = 0 ; i < m_idArray.GetCount() ; ++i ) | |
678 | { | |
679 | if ( ::IsDataBrowserItemSelected((ControlRef) m_macControl , m_idArray[i] ) ) | |
680 | { | |
681 | return i ; | |
682 | } | |
683 | } | |
684 | return -1 ; | |
685 | } | |
686 | ||
687 | int wxListBox::MacGetSelections( wxArrayInt& aSelections ) const | |
688 | { | |
689 | int no_sel = 0 ; | |
690 | ||
691 | aSelections.Empty(); | |
692 | for ( size_t i = 0 ; i < m_idArray.GetCount() ; ++i ) | |
693 | { | |
694 | if ( ::IsDataBrowserItemSelected((ControlRef) m_macControl , m_idArray[i] ) ) | |
695 | { | |
696 | aSelections.Add( i ) ; | |
697 | no_sel++ ; | |
698 | } | |
699 | } | |
700 | return no_sel ; | |
701 | } | |
519cb848 | 702 | |
facd6764 SC |
703 | void wxListBox::MacSet( int n , const wxString& text ) |
704 | { | |
705 | // as we don't store the strings we only have to issue a redraw | |
706 | UInt32 id = m_idArray[n] ; | |
707 | verify_noerr( ::UpdateDataBrowserItems( (ControlRef) m_macControl , kDataBrowserNoItem , 1 , &id , kDataBrowserItemNoProperty , kDataBrowserItemNoProperty ) ) ; | |
708 | } | |
e42e45a9 | 709 | |
facd6764 SC |
710 | void wxListBox::MacScrollTo( int n ) |
711 | { | |
a9fc5eec SC |
712 | UInt32 id = m_idArray[n] ; |
713 | verify_noerr( ::RevealDataBrowserItem((ControlRef) m_macControl , id , kTextColumnId , kDataBrowserRevealWithoutSelecting ) ) ; | |
facd6764 SC |
714 | } |
715 | ||
5ecae0b7 | 716 | #if !TARGET_API_MAC_OSX |
facd6764 SC |
717 | void wxListBox::OnSize( wxSizeEvent &event) |
718 | { | |
719 | } | |
5ecae0b7 | 720 | #endif |
facd6764 | 721 | |
facd6764 SC |
722 | void wxListBox::MacSetRedraw( bool doDraw ) |
723 | { | |
724 | // nothing to do in compositing mode | |
725 | } | |
726 | ||
727 | void wxListBox::MacDoClick() | |
83ce5634 | 728 | {/* |
facd6764 SC |
729 | wxArrayInt aSelections; |
730 | int n ; | |
731 | size_t count = GetSelections(aSelections); | |
732 | ||
733 | if ( count == m_selectionPreImage.GetCount() ) | |
734 | { | |
735 | bool hasChanged = false ; | |
736 | for ( size_t i = 0 ; i < count ; ++i ) | |
737 | { | |
738 | if ( aSelections[i] != m_selectionPreImage[i] ) | |
739 | { | |
740 | hasChanged = true ; | |
741 | break ; | |
742 | } | |
743 | } | |
744 | if ( !hasChanged ) | |
745 | { | |
746 | return ; | |
747 | } | |
748 | } | |
749 | ||
750 | m_selectionPreImage = aSelections; | |
751 | ||
752 | wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, m_windowId); | |
753 | event.SetEventObject( this ); | |
754 | ||
755 | if ( count > 0 ) | |
756 | { | |
757 | n = aSelections[0]; | |
758 | if ( HasClientObjectData() ) | |
759 | event.SetClientObject( GetClientObject(n) ); | |
760 | else if ( HasClientUntypedData() ) | |
761 | event.SetClientData( GetClientData(n) ); | |
762 | event.SetString( GetString(n) ); | |
763 | } | |
764 | else | |
765 | { | |
766 | n = -1; | |
767 | } | |
768 | ||
769 | event.m_commandInt = n; | |
770 | ||
771 | GetEventHandler()->ProcessEvent(event); | |
83ce5634 | 772 | */ |
facd6764 SC |
773 | } |
774 | ||
775 | void wxListBox::MacDoDoubleClick() | |
776 | { | |
83ce5634 | 777 | /* |
facd6764 SC |
778 | wxCommandEvent event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED, m_windowId); |
779 | event.SetEventObject( this ); | |
780 | GetEventHandler()->ProcessEvent(event) ; | |
83ce5634 | 781 | */ |
facd6764 SC |
782 | } |
783 | ||
5ecae0b7 SC |
784 | #if !TARGET_API_MAC_OSX |
785 | ||
facd6764 SC |
786 | void wxListBox::OnChar(wxKeyEvent& event) |
787 | { | |
788 | // todo trigger proper events here | |
789 | event.Skip() ; | |
790 | return ; | |
791 | ||
792 | if ( event.GetKeyCode() == WXK_RETURN || event.GetKeyCode() == WXK_NUMPAD_ENTER) | |
793 | { | |
794 | wxWindow* parent = GetParent() ; | |
795 | while( parent && !parent->IsTopLevel() && parent->GetDefaultItem() == NULL ) | |
796 | parent = parent->GetParent() ; | |
797 | ||
798 | if ( parent && parent->GetDefaultItem() ) | |
799 | { | |
800 | wxButton *def = wxDynamicCast(parent->GetDefaultItem(), | |
801 | wxButton); | |
802 | if ( def && def->IsEnabled() ) | |
803 | { | |
804 | wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() ); | |
805 | event.SetEventObject(def); | |
806 | def->Command(event); | |
807 | return ; | |
808 | } | |
809 | } | |
810 | event.Skip() ; | |
811 | } | |
812 | /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */ | |
813 | else if (event.GetKeyCode() == WXK_ESCAPE || (event.GetKeyCode() == '.' && event.MetaDown() ) ) | |
814 | { | |
815 | // FIXME: look in ancestors, not just parent. | |
816 | wxWindow* win = GetParent()->FindWindow( wxID_CANCEL ) ; | |
817 | if (win) | |
818 | { | |
819 | wxCommandEvent new_event(wxEVT_COMMAND_BUTTON_CLICKED,wxID_CANCEL); | |
820 | new_event.SetEventObject( win ); | |
821 | win->GetEventHandler()->ProcessEvent( new_event ); | |
822 | } | |
823 | } | |
824 | else if ( event.GetKeyCode() == WXK_TAB ) | |
825 | { | |
826 | wxNavigationKeyEvent new_event; | |
827 | new_event.SetEventObject( this ); | |
828 | new_event.SetDirection( !event.ShiftDown() ); | |
829 | /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */ | |
830 | new_event.SetWindowChange( event.ControlDown() ); | |
831 | new_event.SetCurrentFocus( this ); | |
832 | if ( !GetEventHandler()->ProcessEvent( new_event ) ) | |
833 | event.Skip() ; | |
834 | } | |
835 | else if ( event.GetKeyCode() == WXK_DOWN || event.GetKeyCode() == WXK_UP ) | |
836 | { | |
837 | // perform the default key handling first | |
838 | wxControl::OnKeyDown( event ) ; | |
839 | ||
840 | wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, m_windowId); | |
841 | event.SetEventObject( this ); | |
842 | ||
843 | wxArrayInt aSelections; | |
844 | int n, count = GetSelections(aSelections); | |
845 | if ( count > 0 ) | |
846 | { | |
847 | n = aSelections[0]; | |
848 | if ( HasClientObjectData() ) | |
849 | event.SetClientObject( GetClientObject(n) ); | |
850 | else if ( HasClientUntypedData() ) | |
851 | event.SetClientData( GetClientData(n) ); | |
852 | event.SetString( GetString(n) ); | |
853 | } | |
854 | else | |
855 | { | |
856 | n = -1; | |
857 | } | |
858 | ||
859 | event.m_commandInt = n; | |
860 | ||
861 | GetEventHandler()->ProcessEvent(event); | |
862 | } | |
863 | else | |
864 | { | |
865 | if ( event.GetTimestamp() > m_lastTypeIn + 60 ) | |
866 | { | |
867 | m_typeIn = wxEmptyString ; | |
868 | } | |
869 | m_lastTypeIn = event.GetTimestamp() ; | |
870 | m_typeIn += (char) event.GetKeyCode() ; | |
871 | int line = FindString(wxT("*")+m_typeIn+wxT("*")) ; | |
872 | if ( line >= 0 ) | |
873 | { | |
874 | if ( GetSelection() != line ) | |
875 | { | |
876 | SetSelection(line) ; | |
877 | wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, m_windowId); | |
878 | event.SetEventObject( this ); | |
879 | ||
880 | if ( HasClientObjectData() ) | |
881 | event.SetClientObject( GetClientObject( line ) ); | |
882 | else if ( HasClientUntypedData() ) | |
883 | event.SetClientData( GetClientData(line) ); | |
884 | event.SetString( GetString(line) ); | |
885 | ||
886 | event.m_commandInt = line ; | |
887 | ||
888 | GetEventHandler()->ProcessEvent(event); | |
889 | } | |
890 | } | |
891 | } | |
892 | } | |
573ac9dc | 893 | |
5ecae0b7 SC |
894 | #endif |
895 |