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