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