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