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