]>
Commit | Line | Data |
---|---|---|
2646f485 SC |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: listbox.cpp | |
3 | // Purpose: wxListBox | |
4 | // Author: Stefan Csomor | |
5 | // Modified by: | |
6 | // Created: 1998-01-01 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Stefan Csomor | |
65571936 | 9 | // Licence: wxWindows licence |
2646f485 SC |
10 | /////////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | #ifdef __GNUG__ | |
13 | #pragma implementation "listbox.h" | |
14 | #endif | |
15 | ||
16 | #include "wx/app.h" | |
17 | #include "wx/listbox.h" | |
18 | #include "wx/button.h" | |
19 | #include "wx/settings.h" | |
20 | #include "wx/toplevel.h" | |
21 | #include "wx/dynarray.h" | |
22 | #include "wx/log.h" | |
23 | ||
24 | #include "wx/utils.h" | |
25 | ||
2646f485 SC |
26 | IMPLEMENT_DYNAMIC_CLASS(wxListBox, wxControl) |
27 | ||
28 | BEGIN_EVENT_TABLE(wxListBox, wxControl) | |
29 | EVT_SIZE( wxListBox::OnSize ) | |
30 | EVT_CHAR( wxListBox::OnChar ) | |
31 | END_EVENT_TABLE() | |
2646f485 SC |
32 | |
33 | #include "wx/mac/uma.h" | |
34 | ||
35 | #if PRAGMA_STRUCT_ALIGN | |
36 | #pragma options align=mac68k | |
37 | #elif PRAGMA_STRUCT_PACKPUSH | |
38 | #pragma pack(push, 2) | |
39 | #elif PRAGMA_STRUCT_PACK | |
40 | #pragma pack(2) | |
41 | #endif | |
42 | ||
43 | typedef struct { | |
44 | unsigned short instruction; | |
45 | void (*function)(); | |
46 | } ldefRec, *ldefPtr, **ldefHandle; | |
47 | ||
48 | #if PRAGMA_STRUCT_ALIGN | |
49 | #pragma options align=reset | |
50 | #elif PRAGMA_STRUCT_PACKPUSH | |
51 | #pragma pack(pop) | |
52 | #elif PRAGMA_STRUCT_PACK | |
53 | #pragma pack() | |
54 | #endif | |
55 | ||
56 | #if TARGET_CARBON | |
57 | const short kwxMacListItemHeight = 19 ; | |
58 | #else | |
59 | const short kwxMacListItemHeight = 14 ; | |
60 | #endif | |
61 | ||
62 | extern "C" | |
63 | { | |
64 | static pascal void wxMacListDefinition( short message, Boolean isSelected, Rect *drawRect, | |
65 | Cell cell, short dataOffset, short dataLength, | |
66 | ListHandle listHandle ) ; | |
67 | } | |
68 | ||
69 | static pascal void wxMacListDefinition( short message, Boolean isSelected, Rect *drawRect, | |
70 | Cell cell, short dataOffset, short dataLength, | |
71 | ListHandle listHandle ) | |
72 | { | |
73 | wxListBox* list; | |
74 | list = (wxListBox*) GetControlReference( (ControlHandle) GetListRefCon(listHandle) ); | |
75 | if ( list == NULL ) | |
76 | return ; | |
77 | ||
78 | GrafPtr savePort; | |
79 | GrafPtr grafPtr; | |
80 | RgnHandle savedClipRegion; | |
81 | SInt32 savedPenMode; | |
82 | GetPort(&savePort); | |
83 | SetPort((**listHandle).port); | |
84 | grafPtr = (**listHandle).port ; | |
85 | // typecast our refCon | |
86 | ||
87 | // Calculate the cell rect. | |
88 | ||
89 | switch( message ) { | |
90 | case lInitMsg: | |
91 | break; | |
92 | ||
93 | case lCloseMsg: | |
94 | break; | |
95 | ||
96 | case lDrawMsg: | |
97 | { | |
98 | const wxString linetext = list->m_stringArray[cell.v] ; | |
99 | ||
100 | // Save the current clip region, and set the clip region to the area we are about | |
101 | // to draw. | |
102 | ||
103 | savedClipRegion = NewRgn(); | |
104 | GetClip( savedClipRegion ); | |
105 | ||
106 | ClipRect( drawRect ); | |
107 | EraseRect( drawRect ); | |
108 | ||
109 | const wxFont& font = list->GetFont(); | |
110 | if ( font.Ok() ) | |
111 | { | |
112 | ::TextFont( font.GetMacFontNum() ) ; | |
113 | ::TextSize( font.GetMacFontSize() ) ; | |
114 | ::TextFace( font.GetMacFontStyle() ) ; | |
115 | } | |
116 | else | |
117 | { | |
118 | ::TextFont( kFontIDMonaco ) ; | |
119 | ::TextSize( 9 ); | |
120 | ::TextFace( 0 ) ; | |
121 | } | |
122 | ||
123 | #if TARGET_CARBON | |
124 | { | |
125 | Rect frame = { drawRect->top, drawRect->left + 4, | |
126 | drawRect->top + kwxMacListItemHeight, drawRect->right + 10000 } ; | |
127 | CFMutableStringRef mString = CFStringCreateMutableCopy( NULL , 0 , wxMacCFStringHolder(linetext , list->GetFont().GetEncoding()) ) ; | |
128 | ::TruncateThemeText( mString , kThemeCurrentPortFont, kThemeStateActive, drawRect->right - drawRect->left , truncEnd , NULL ) ; | |
129 | ::DrawThemeTextBox( mString, | |
130 | kThemeCurrentPortFont, | |
131 | kThemeStateActive, | |
132 | false, | |
133 | &frame, | |
134 | teJustLeft, | |
135 | nil ); | |
136 | CFRelease( mString ) ; | |
137 | } | |
138 | #else | |
139 | { | |
140 | wxCharBuffer text = linetext.mb_str( wxConvLocal) ; | |
141 | MoveTo(drawRect->left + 4 , drawRect->top + 10 ); | |
142 | DrawText(text, 0 , strlen(text) ); | |
143 | } | |
144 | #endif | |
145 | // If the cell is hilited, do the hilite now. Paint the cell contents with the | |
146 | // appropriate QuickDraw transform mode. | |
147 | ||
148 | if( isSelected ) { | |
149 | savedPenMode = GetPortPenMode( (CGrafPtr) grafPtr ); | |
150 | SetPortPenMode( (CGrafPtr)grafPtr, hilitetransfermode ); | |
151 | PaintRect( drawRect ); | |
152 | SetPortPenMode( (CGrafPtr)grafPtr, savedPenMode ); | |
153 | } | |
154 | ||
155 | // Restore the saved clip region. | |
156 | ||
157 | SetClip( savedClipRegion ); | |
158 | DisposeRgn( savedClipRegion ); | |
159 | } | |
160 | break; | |
161 | case lHiliteMsg: | |
162 | ||
163 | // Hilite or unhilite the cell. Paint the cell contents with the | |
164 | // appropriate QuickDraw transform mode. | |
165 | ||
166 | GetPort( &grafPtr ); | |
167 | savedPenMode = GetPortPenMode( (CGrafPtr)grafPtr ); | |
168 | SetPortPenMode( (CGrafPtr)grafPtr, hilitetransfermode ); | |
169 | PaintRect( drawRect ); | |
170 | SetPortPenMode( (CGrafPtr)grafPtr, savedPenMode ); | |
171 | break; | |
172 | default : | |
173 | break ; | |
174 | } | |
175 | SetPort(savePort); | |
176 | } | |
177 | ||
178 | extern "C" void MacDrawStringCell(Rect *cellRect, Cell lCell, ListHandle theList, long refCon) ; | |
179 | // resources ldef ids | |
180 | const short kwxMacListWithVerticalScrollbar = 128 ; | |
181 | const short kwxMacListWithVerticalAndHorizontalScrollbar = 129 ; | |
182 | ||
183 | // ============================================================================ | |
184 | // list box control implementation | |
185 | // ============================================================================ | |
186 | ||
187 | // Listbox item | |
188 | wxListBox::wxListBox() | |
189 | { | |
190 | m_noItems = 0; | |
191 | m_selected = 0; | |
192 | m_macList = NULL ; | |
193 | } | |
194 | ||
195 | static ListDefUPP macListDefUPP = NULL ; | |
196 | ||
197 | bool wxListBox::Create(wxWindow *parent, wxWindowID id, | |
198 | const wxPoint& pos, | |
199 | const wxSize& size, | |
200 | const wxArrayString& choices, | |
201 | long style, | |
202 | const wxValidator& validator, | |
203 | const wxString& name) | |
204 | { | |
205 | wxCArrayString chs(choices); | |
206 | ||
207 | return Create(parent, id, pos, size, chs.GetCount(), chs.GetStrings(), | |
208 | style, validator, name); | |
209 | } | |
210 | ||
211 | bool wxListBox::Create(wxWindow *parent, wxWindowID id, | |
212 | const wxPoint& pos, | |
213 | const wxSize& size, | |
214 | int n, const wxString choices[], | |
215 | long style, | |
216 | const wxValidator& validator, | |
217 | const wxString& name) | |
218 | { | |
219 | if ( !wxListBoxBase::Create(parent, id, pos, size, style & ~(wxHSCROLL|wxVSCROLL), validator, name) ) | |
220 | return false; | |
221 | ||
222 | m_noItems = 0 ; // this will be increased by our append command | |
223 | m_selected = 0; | |
224 | ||
225 | Rect bounds ; | |
226 | Str255 title ; | |
227 | ||
228 | MacPreControlCreate( parent , id , wxEmptyString , pos , size ,style, validator , name , &bounds , title ) ; | |
229 | ||
230 | ListDefSpec listDef; | |
231 | listDef.defType = kListDefUserProcType; | |
232 | if ( macListDefUPP == NULL ) | |
233 | { | |
234 | macListDefUPP = NewListDefUPP( wxMacListDefinition ); | |
235 | } | |
236 | listDef.u.userProc = macListDefUPP ; | |
237 | ||
238 | Str255 fontName ; | |
239 | SInt16 fontSize ; | |
240 | Style fontStyle ; | |
241 | #if TARGET_CARBON | |
242 | GetThemeFont(kThemeViewsFont , GetApplicationScript() , fontName , &fontSize , &fontStyle ) ; | |
243 | #else | |
244 | GetFontName( kFontIDMonaco , fontName ) ; | |
245 | fontSize = 9 ; | |
246 | fontStyle = normal ; | |
247 | #endif | |
248 | SetFont( wxFont (fontSize, wxSWISS, wxNORMAL, wxNORMAL , false , wxMacMakeStringFromPascal( fontName ) ) ) ; | |
249 | #if TARGET_CARBON | |
250 | Size asize; | |
251 | ||
252 | ||
253 | CreateListBoxControl( MAC_WXHWND(parent->MacGetRootWindow()), &bounds, false, 0, 1, (style & wxLB_HSCROLL), true, | |
254 | kwxMacListItemHeight, kwxMacListItemHeight, false, &listDef, (ControlRef *)&m_macControl ); | |
255 | ||
256 | GetControlData( (ControlHandle) m_macControl, kControlNoPart, kControlListBoxListHandleTag, | |
257 | sizeof(ListHandle), (Ptr) &m_macList, &asize); | |
258 | ||
259 | SetControlReference( (ControlHandle) m_macControl, (long) this); | |
260 | SetControlVisibility( (ControlHandle) m_macControl, false, false); | |
261 | ||
262 | #else | |
263 | ||
264 | long result ; | |
265 | wxStAppResource resload ; | |
6bc3b8e9 | 266 | m_macControl = (WXWidget) ::NewControl( MAC_WXHWND(parent->MacGetRootWindow()) , &bounds , title , false , |
2646f485 SC |
267 | (style & wxLB_HSCROLL) ? kwxMacListWithVerticalAndHorizontalScrollbar : kwxMacListWithVerticalScrollbar , |
268 | 0 , 0, kControlListBoxProc , (long) this ) ; | |
269 | ::GetControlData( (ControlHandle) m_macControl , kControlNoPart , kControlListBoxListHandleTag , | |
270 | sizeof( ListHandle ) , (char*) &m_macList , &result ) ; | |
271 | ||
272 | HLock( (Handle) m_macList ) ; | |
273 | ldefHandle ldef ; | |
274 | ldef = (ldefHandle) NewHandle( sizeof(ldefRec) ) ; | |
275 | if ( (**(ListHandle)m_macList).listDefProc != NULL ) | |
276 | { | |
277 | (**ldef).instruction = 0x4EF9; /* JMP instruction */ | |
278 | (**ldef).function = (void(*)()) listDef.u.userProc; | |
279 | (**(ListHandle)m_macList).listDefProc = (Handle) ldef ; | |
280 | } | |
281 | ||
282 | Point pt = (**(ListHandle)m_macList).cellSize ; | |
283 | pt.v = kwxMacListItemHeight ; | |
284 | LCellSize( pt , (ListHandle)m_macList ) ; | |
285 | LAddColumn( 1 , 0 , (ListHandle)m_macList ) ; | |
286 | #endif | |
287 | OptionBits options = 0; | |
288 | if ( style & wxLB_MULTIPLE ) | |
289 | { | |
290 | options += lExtendDrag + lUseSense ; | |
291 | } | |
292 | else if ( style & wxLB_EXTENDED ) | |
293 | { | |
294 | // default behaviour | |
295 | } | |
296 | else | |
297 | { | |
298 | options = (OptionBits) lOnlyOne ; | |
299 | } | |
300 | SetListSelectionFlags((ListHandle)m_macList, options); | |
301 | ||
302 | for ( int i = 0 ; i < n ; i++ ) | |
303 | { | |
304 | Append( choices[i] ) ; | |
305 | } | |
306 | ||
307 | MacPostControlCreate() ; | |
308 | ||
309 | LSetDrawingMode( true , (ListHandle)m_macList ) ; | |
310 | ||
311 | return TRUE; | |
312 | } | |
313 | ||
314 | wxListBox::~wxListBox() | |
315 | { | |
316 | FreeData() ; | |
317 | // avoid access during destruction | |
318 | SetControlReference( (ControlHandle) m_macControl , NULL ) ; | |
319 | if ( m_macList ) | |
320 | { | |
321 | #if !TARGET_CARBON | |
322 | DisposeHandle( (**(ListHandle)m_macList).listDefProc ) ; | |
323 | (**(ListHandle)m_macList).listDefProc = NULL ; | |
324 | #endif | |
325 | m_macList = NULL ; | |
326 | } | |
327 | } | |
328 | ||
329 | void wxListBox::FreeData() | |
330 | { | |
331 | #if wxUSE_OWNER_DRAWN | |
332 | if ( m_windowStyle & wxLB_OWNERDRAW ) | |
333 | { | |
334 | size_t uiCount = m_aItems.Count(); | |
335 | while ( uiCount-- != 0 ) { | |
336 | delete m_aItems[uiCount]; | |
337 | m_aItems[uiCount] = NULL; | |
338 | } | |
339 | ||
340 | m_aItems.Clear(); | |
341 | } | |
342 | else | |
343 | #endif // wxUSE_OWNER_DRAWN | |
344 | if ( HasClientObjectData() ) | |
345 | { | |
346 | for ( size_t n = 0; n < (size_t)m_noItems; n++ ) | |
347 | { | |
348 | delete GetClientObject(n); | |
349 | } | |
350 | } | |
351 | } | |
352 | ||
353 | void wxListBox::DoSetSize(int x, int y, | |
354 | int width, int height, | |
355 | int sizeFlags ) | |
356 | { | |
357 | wxControl::DoSetSize( x , y , width , height , sizeFlags ) ; | |
358 | #if TARGET_CARBON | |
359 | Rect bounds ; | |
360 | GetControlBounds( (ControlHandle) m_macControl , &bounds ) ; | |
361 | ControlRef control = GetListVerticalScrollBar( (ListHandle)m_macList ) ; | |
362 | if ( control ) | |
363 | { | |
364 | Rect scrollbounds ; | |
365 | GetControlBounds( control , &scrollbounds ) ; | |
366 | if( scrollbounds.right != bounds.right + 1 ) | |
367 | { | |
368 | UMAMoveControl( control , bounds.right - (scrollbounds.right - scrollbounds.left) + 1 , | |
369 | scrollbounds.top ) ; | |
370 | } | |
371 | } | |
372 | #endif | |
373 | } | |
374 | void wxListBox::DoSetFirstItem(int N) | |
375 | { | |
376 | MacScrollTo( N ) ; | |
377 | } | |
378 | ||
379 | void wxListBox::Delete(int N) | |
380 | { | |
381 | wxCHECK_RET( N >= 0 && N < m_noItems, | |
382 | wxT("invalid index in wxListBox::Delete") ); | |
383 | ||
384 | #if wxUSE_OWNER_DRAWN | |
385 | delete m_aItems[N]; | |
386 | m_aItems.RemoveAt(N); | |
387 | #else // !wxUSE_OWNER_DRAWN | |
388 | if ( HasClientObjectData() ) | |
389 | { | |
390 | delete GetClientObject(N); | |
391 | } | |
392 | #endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN | |
393 | m_stringArray.RemoveAt( N ) ; | |
394 | m_dataArray.RemoveAt( N ) ; | |
395 | m_noItems --; | |
396 | ||
397 | MacDelete( N ) ; | |
398 | } | |
399 | ||
400 | int wxListBox::DoAppend(const wxString& item) | |
401 | { | |
9f884528 RD |
402 | InvalidateBestSize(); |
403 | ||
2646f485 SC |
404 | int index = m_noItems ; |
405 | m_stringArray.Add( item ) ; | |
406 | m_dataArray.Add( NULL ); | |
407 | m_noItems ++; | |
408 | DoSetItemClientData( index , NULL ) ; | |
409 | MacAppend( item ) ; | |
410 | ||
411 | return index ; | |
412 | } | |
413 | ||
414 | void wxListBox::DoSetItems(const wxArrayString& choices, void** clientData) | |
415 | { | |
416 | MacSetRedraw( false ) ; | |
417 | Clear() ; | |
418 | int n = choices.GetCount(); | |
419 | ||
420 | for( int i = 0 ; i < n ; ++i ) | |
421 | { | |
422 | if ( clientData ) | |
423 | { | |
424 | #if wxUSE_OWNER_DRAWN | |
425 | wxASSERT_MSG(clientData[i] == NULL, | |
426 | wxT("Can't use client data with owner-drawn listboxes")); | |
427 | #else // !wxUSE_OWNER_DRAWN | |
428 | Append( choices[i] , clientData[i] ) ; | |
429 | #endif | |
430 | } | |
431 | else | |
432 | Append( choices[i] ) ; | |
433 | } | |
434 | ||
435 | #if wxUSE_OWNER_DRAWN | |
436 | if ( m_windowStyle & wxLB_OWNERDRAW ) { | |
437 | // first delete old items | |
438 | size_t ui = m_aItems.Count(); | |
439 | while ( ui-- != 0 ) { | |
440 | delete m_aItems[ui]; | |
441 | m_aItems[ui] = NULL; | |
442 | } | |
443 | m_aItems.Empty(); | |
444 | ||
445 | // then create new ones | |
446 | for ( ui = 0; ui < (size_t)m_noItems; ui++ ) { | |
447 | wxOwnerDrawn *pNewItem = CreateItem(ui); | |
448 | pNewItem->SetName(choices[ui]); | |
449 | m_aItems.Add(pNewItem); | |
450 | } | |
451 | } | |
452 | #endif // wxUSE_OWNER_DRAWN | |
453 | MacSetRedraw( true ) ; | |
454 | } | |
455 | ||
456 | bool wxListBox::HasMultipleSelection() const | |
457 | { | |
458 | return (m_windowStyle & wxLB_MULTIPLE) || (m_windowStyle & wxLB_EXTENDED); | |
459 | } | |
460 | ||
461 | int wxListBox::FindString(const wxString& s) const | |
462 | { | |
463 | ||
464 | if ( s.Right(1) == wxT("*") ) | |
465 | { | |
466 | wxString search = s.Left( s.Length() - 1 ) ; | |
467 | int len = search.Length() ; | |
468 | Str255 s1 , s2 ; | |
469 | wxMacStringToPascal( search , s2 ) ; | |
470 | ||
471 | for ( int i = 0 ; i < m_noItems ; ++ i ) | |
472 | { | |
473 | wxMacStringToPascal( m_stringArray[i].Left( len ) , s1 ) ; | |
474 | ||
475 | if ( EqualString( s1 , s2 , false , false ) ) | |
476 | return i ; | |
477 | } | |
478 | if ( s.Left(1) == wxT("*") && s.Length() > 1 ) | |
479 | { | |
480 | wxString st = s ; | |
481 | st.MakeLower() ; | |
482 | for ( int i = 0 ; i < m_noItems ; ++i ) | |
483 | { | |
484 | if ( GetString(i).Lower().Matches(st) ) | |
485 | return i ; | |
486 | } | |
487 | } | |
488 | ||
489 | } | |
490 | else | |
491 | { | |
492 | Str255 s1 , s2 ; | |
493 | ||
494 | wxMacStringToPascal( s , s2 ) ; | |
495 | ||
496 | for ( int i = 0 ; i < m_noItems ; ++ i ) | |
497 | { | |
498 | wxMacStringToPascal( m_stringArray[i] , s1 ) ; | |
499 | ||
500 | if ( EqualString( s1 , s2 , false , false ) ) | |
501 | return i ; | |
502 | } | |
503 | } | |
504 | return -1; | |
505 | } | |
506 | ||
507 | void wxListBox::Clear() | |
508 | { | |
509 | FreeData(); | |
510 | m_noItems = 0; | |
511 | m_stringArray.Empty() ; | |
512 | m_dataArray.Empty() ; | |
513 | MacClear() ; | |
514 | } | |
515 | ||
c6179a84 | 516 | void wxListBox::DoSetSelection(int N, bool select) |
2646f485 SC |
517 | { |
518 | wxCHECK_RET( N >= 0 && N < m_noItems, | |
519 | wxT("invalid index in wxListBox::SetSelection") ); | |
520 | MacSetSelection( N , select ) ; | |
521 | GetSelections( m_selectionPreImage ) ; | |
522 | } | |
523 | ||
524 | bool wxListBox::IsSelected(int N) const | |
525 | { | |
526 | wxCHECK_MSG( N >= 0 && N < m_noItems, FALSE, | |
527 | wxT("invalid index in wxListBox::Selected") ); | |
528 | ||
529 | return MacIsSelected( N ) ; | |
530 | } | |
531 | ||
532 | void *wxListBox::DoGetItemClientData(int N) const | |
533 | { | |
534 | wxCHECK_MSG( N >= 0 && N < m_noItems, NULL, | |
535 | wxT("invalid index in wxListBox::GetClientData")); | |
536 | ||
537 | return (void *)m_dataArray[N]; | |
538 | } | |
539 | ||
540 | wxClientData *wxListBox::DoGetItemClientObject(int N) const | |
541 | { | |
542 | return (wxClientData *) DoGetItemClientData( N ) ; | |
543 | } | |
544 | ||
545 | void wxListBox::DoSetItemClientData(int N, void *Client_data) | |
546 | { | |
547 | wxCHECK_RET( N >= 0 && N < m_noItems, | |
548 | wxT("invalid index in wxListBox::SetClientData") ); | |
549 | ||
550 | #if wxUSE_OWNER_DRAWN | |
551 | if ( m_windowStyle & wxLB_OWNERDRAW ) | |
552 | { | |
553 | // client data must be pointer to wxOwnerDrawn, otherwise we would crash | |
554 | // in OnMeasure/OnDraw. | |
555 | wxFAIL_MSG(wxT("Can't use client data with owner-drawn listboxes")); | |
556 | } | |
557 | #endif // wxUSE_OWNER_DRAWN | |
558 | wxASSERT_MSG( m_dataArray.GetCount() >= (size_t) N , wxT("invalid client_data array") ) ; | |
559 | ||
560 | if ( m_dataArray.GetCount() > (size_t) N ) | |
561 | { | |
562 | m_dataArray[N] = (char*) Client_data ; | |
563 | } | |
564 | else | |
565 | { | |
566 | m_dataArray.Add( (char*) Client_data ) ; | |
567 | } | |
568 | } | |
569 | ||
570 | void wxListBox::DoSetItemClientObject(int n, wxClientData* clientData) | |
571 | { | |
572 | DoSetItemClientData(n, clientData); | |
573 | } | |
574 | ||
575 | // Return number of selections and an array of selected integers | |
576 | int wxListBox::GetSelections(wxArrayInt& aSelections) const | |
577 | { | |
578 | return MacGetSelections( aSelections ) ; | |
579 | } | |
580 | ||
581 | // Get single selection, for single choice list items | |
582 | int wxListBox::GetSelection() const | |
583 | { | |
584 | return MacGetSelection() ; | |
585 | } | |
586 | ||
587 | // Find string for position | |
588 | wxString wxListBox::GetString(int N) const | |
589 | { | |
590 | return m_stringArray[N] ; | |
591 | } | |
592 | ||
593 | void wxListBox::DoInsertItems(const wxArrayString& items, int pos) | |
594 | { | |
595 | wxCHECK_RET( pos >= 0 && pos <= m_noItems, | |
596 | wxT("invalid index in wxListBox::InsertItems") ); | |
597 | ||
9f884528 RD |
598 | InvalidateBestSize(); |
599 | ||
2646f485 SC |
600 | int nItems = items.GetCount(); |
601 | ||
602 | for ( int i = 0 ; i < nItems ; i++ ) | |
603 | { | |
604 | m_stringArray.Insert( items[i] , pos + i ) ; | |
605 | m_dataArray.Insert( NULL , pos + i ) ; | |
606 | MacInsert( pos + i , items[i] ) ; | |
607 | } | |
608 | ||
609 | m_noItems += nItems; | |
610 | } | |
611 | ||
612 | void wxListBox::SetString(int N, const wxString& s) | |
613 | { | |
614 | m_stringArray[N] = s ; | |
615 | MacSet( N , s ) ; | |
616 | } | |
617 | ||
618 | wxSize wxListBox::DoGetBestSize() const | |
619 | { | |
620 | int lbWidth = 100; // some defaults | |
621 | int lbHeight = 110; | |
622 | int wLine; | |
623 | ||
624 | { | |
625 | wxMacPortStateHelper st( UMAGetWindowPort( (WindowRef) MacGetRootWindow() ) ) ; | |
626 | ||
627 | if ( m_font.Ok() ) | |
628 | { | |
629 | ::TextFont( m_font.GetMacFontNum() ) ; | |
630 | ::TextSize( m_font.GetMacFontSize() ) ; | |
631 | ::TextFace( m_font.GetMacFontStyle() ) ; | |
632 | } | |
633 | else | |
634 | { | |
635 | ::TextFont( kFontIDMonaco ) ; | |
636 | ::TextSize( 9 ); | |
637 | ::TextFace( 0 ) ; | |
638 | } | |
639 | ||
640 | // Find the widest line | |
641 | for(int i = 0; i < GetCount(); i++) { | |
642 | wxString str(GetString(i)); | |
643 | #if wxUSE_UNICODE | |
644 | Point bounds={0,0} ; | |
645 | SInt16 baseline ; | |
646 | ::GetThemeTextDimensions( wxMacCFStringHolder( str , m_font.GetEncoding() ) , | |
647 | kThemeCurrentPortFont, | |
648 | kThemeStateActive, | |
649 | false, | |
650 | &bounds, | |
651 | &baseline ); | |
652 | wLine = bounds.h ; | |
653 | #else | |
654 | wLine = ::TextWidth( str.c_str() , 0 , str.Length() ) ; | |
655 | #endif | |
656 | lbWidth = wxMax(lbWidth, wLine); | |
657 | } | |
658 | ||
659 | // Add room for the scrollbar | |
660 | lbWidth += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X); | |
661 | ||
662 | // And just a bit more | |
663 | int cy = 12 ; | |
664 | int cx = ::TextWidth( "X" , 0 , 1 ) ; | |
665 | lbWidth += cx ; | |
666 | ||
667 | // don't make the listbox too tall (limit height to around 10 items) but don't | |
668 | // make it too small neither | |
669 | lbHeight = (cy+4) * wxMin(wxMax(GetCount(), 3), 10); | |
670 | } | |
671 | return wxSize(lbWidth, lbHeight); | |
672 | } | |
673 | ||
674 | int wxListBox::GetCount() const | |
675 | { | |
676 | return m_noItems; | |
677 | } | |
678 | ||
679 | void wxListBox::SetupColours() | |
680 | { | |
681 | SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW)); | |
682 | SetForegroundColour(GetParent()->GetForegroundColour()); | |
683 | } | |
684 | ||
685 | void wxListBox::Refresh(bool eraseBack, const wxRect *rect) | |
686 | { | |
687 | wxControl::Refresh( eraseBack , rect ) ; | |
688 | // MacRedrawControl() ; | |
689 | } | |
690 | ||
691 | #if wxUSE_OWNER_DRAWN | |
692 | ||
693 | class wxListBoxItem : public wxOwnerDrawn | |
694 | { | |
695 | public: | |
696 | wxListBoxItem(const wxString& str = ""); | |
697 | }; | |
698 | ||
699 | wxListBoxItem::wxListBoxItem(const wxString& str) : wxOwnerDrawn(str, FALSE) | |
700 | { | |
701 | // no bitmaps/checkmarks | |
702 | SetMarginWidth(0); | |
703 | } | |
704 | ||
705 | wxOwnerDrawn *wxListBox::CreateItem(size_t n) | |
706 | { | |
707 | return new wxListBoxItem(); | |
708 | } | |
709 | ||
710 | #endif //USE_OWNER_DRAWN | |
711 | ||
712 | // ============================================================================ | |
713 | // list box control implementation | |
714 | // ============================================================================ | |
715 | ||
716 | /* | |
717 | void MacDrawStringCell(Rect *cellRect, Cell lCell, ListHandle theList, long refCon) | |
718 | { | |
719 | wxListBox* list; | |
720 | // typecast our refCon | |
721 | list = (wxListBox*)refCon; | |
722 | ||
723 | MoveTo(cellRect->left + 4 , cellRect->top + 10 ); | |
724 | const wxString text = list->m_stringArray[lCell.v] ; | |
725 | ::TextFont( kFontIDMonaco ) ; | |
726 | ::TextSize( 9 ); | |
727 | ::TextFace( 0 ) ; | |
728 | DrawText(text, 0 , text.Length()); | |
729 | ||
730 | } | |
731 | */ | |
732 | void wxListBox::MacDelete( int N ) | |
733 | { | |
734 | LDelRow( 1 , N , (ListHandle)m_macList) ; | |
735 | Refresh(); | |
736 | } | |
737 | ||
738 | void wxListBox::MacInsert( int n , const wxString& text) | |
739 | { | |
740 | Cell cell = { 0 , 0 } ; | |
741 | cell.v = n ; | |
742 | LAddRow( 1 , cell.v , (ListHandle)m_macList ) ; | |
743 | // LSetCell(text, strlen(text), cell, m_macList); | |
744 | Refresh(); | |
745 | } | |
746 | ||
747 | void wxListBox::MacAppend( const wxString& text) | |
748 | { | |
749 | Cell cell = { 0 , 0 } ; | |
750 | cell.v = (**(ListHandle)m_macList).dataBounds.bottom ; | |
751 | LAddRow( 1 , cell.v , (ListHandle)m_macList ) ; | |
752 | // LSetCell(text, strlen(text), cell, m_macList); | |
753 | Refresh(); | |
754 | } | |
755 | ||
756 | void wxListBox::MacClear() | |
757 | { | |
758 | LDelRow( (**(ListHandle)m_macList).dataBounds.bottom , 0 ,(ListHandle) m_macList ) ; | |
759 | Refresh(); | |
760 | } | |
761 | ||
762 | void wxListBox::MacSetSelection( int n , bool select ) | |
763 | { | |
764 | Cell cell = { 0 , 0 } ; | |
765 | if ( ! (m_windowStyle & wxLB_MULTIPLE) ) | |
766 | { | |
767 | if ( LGetSelect( true , &cell , (ListHandle)m_macList ) ) | |
768 | { | |
769 | LSetSelect( false , cell , (ListHandle)m_macList ) ; | |
770 | } | |
771 | } | |
772 | ||
773 | cell.v = n ; | |
774 | LSetSelect( select , cell , (ListHandle)m_macList ) ; | |
775 | LAutoScroll( (ListHandle)m_macList ) ; | |
776 | Refresh(); | |
777 | } | |
778 | ||
779 | bool wxListBox::MacIsSelected( int n ) const | |
780 | { | |
781 | Cell cell = { 0 , 0 } ; | |
782 | cell.v = n ; | |
783 | return LGetSelect( false , &cell , (ListHandle)m_macList ) ; | |
784 | } | |
785 | ||
786 | void wxListBox::MacDestroy() | |
787 | { | |
788 | // DisposeExtLDEFInfo( m_macList ) ; | |
789 | } | |
790 | ||
791 | int wxListBox::MacGetSelection() const | |
792 | { | |
793 | Cell cell = { 0 , 0 } ; | |
794 | if ( LGetSelect( true , &cell , (ListHandle)m_macList ) ) | |
795 | return cell.v ; | |
796 | else | |
797 | return -1 ; | |
798 | } | |
799 | ||
800 | int wxListBox::MacGetSelections( wxArrayInt& aSelections ) const | |
801 | { | |
802 | int no_sel = 0 ; | |
803 | ||
804 | aSelections.Empty(); | |
805 | ||
806 | Cell cell = { 0 , 0 } ; | |
807 | cell.v = 0 ; | |
808 | ||
809 | while ( LGetSelect( true , &cell ,(ListHandle) m_macList ) ) | |
810 | { | |
811 | aSelections.Add( cell.v ) ; | |
812 | no_sel++ ; | |
813 | cell.v++ ; | |
814 | } | |
815 | return no_sel ; | |
816 | } | |
817 | ||
818 | void wxListBox::MacSet( int n , const wxString& text ) | |
819 | { | |
820 | // our implementation does not store anything in the list | |
821 | // so we just have to redraw | |
822 | Cell cell = { 0 , 0 } ; | |
823 | cell.v = n ; | |
824 | // LSetCell(text, strlen(text), cell, m_macList); | |
825 | Refresh(); | |
826 | } | |
827 | ||
828 | void wxListBox::MacScrollTo( int n ) | |
829 | { | |
830 | // TODO implement scrolling | |
831 | } | |
832 | ||
833 | void wxListBox::OnSize( wxSizeEvent &event) | |
834 | { | |
835 | Point pt; | |
836 | ||
837 | #if TARGET_CARBON | |
838 | GetListCellSize((ListHandle)m_macList, &pt); | |
839 | #else | |
840 | pt = (**(ListHandle)m_macList).cellSize ; | |
841 | #endif | |
842 | pt.h = m_width - 15 ; | |
843 | LCellSize( pt , (ListHandle)m_macList ) ; | |
844 | } | |
845 | ||
846 | void wxListBox::MacHandleControlClick( WXWidget control , wxInt16 controlpart , bool WXUNUSED(mouseStillDown)) | |
847 | { | |
848 | Boolean wasDoubleClick = false ; | |
849 | long result ; | |
850 | ||
851 | ::GetControlData( (ControlHandle) m_macControl , kControlNoPart , kControlListBoxDoubleClickTag , sizeof( wasDoubleClick ) , (char*) &wasDoubleClick , &result ) ; | |
852 | if ( !wasDoubleClick ) | |
853 | { | |
854 | MacDoClick() ; | |
855 | } | |
856 | else | |
857 | { | |
858 | MacDoDoubleClick() ; | |
859 | } | |
860 | } | |
861 | ||
862 | void wxListBox::MacSetRedraw( bool doDraw ) | |
863 | { | |
864 | LSetDrawingMode( doDraw , (ListHandle)m_macList ) ; | |
865 | ||
866 | } | |
867 | ||
868 | void wxListBox::MacDoClick() | |
869 | { | |
870 | wxArrayInt aSelections; | |
871 | int n ; | |
872 | size_t count = GetSelections(aSelections); | |
873 | ||
874 | if ( count == m_selectionPreImage.GetCount() ) | |
875 | { | |
876 | bool hasChanged = false ; | |
877 | for ( size_t i = 0 ; i < count ; ++i ) | |
878 | { | |
879 | if ( aSelections[i] != m_selectionPreImage[i] ) | |
880 | { | |
881 | hasChanged = true ; | |
882 | break ; | |
883 | } | |
884 | } | |
885 | if ( !hasChanged ) | |
886 | { | |
887 | return ; | |
888 | } | |
889 | } | |
890 | ||
891 | m_selectionPreImage = aSelections; | |
892 | ||
893 | wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, m_windowId); | |
894 | event.SetEventObject( this ); | |
895 | ||
896 | if ( count > 0 ) | |
897 | { | |
898 | n = aSelections[0]; | |
899 | if ( HasClientObjectData() ) | |
900 | event.SetClientObject( GetClientObject(n) ); | |
901 | else if ( HasClientUntypedData() ) | |
902 | event.SetClientData( GetClientData(n) ); | |
903 | event.SetString( GetString(n) ); | |
904 | } | |
905 | else | |
906 | { | |
907 | n = -1; | |
908 | } | |
909 | ||
687706f5 | 910 | event.SetInt(n); |
2646f485 SC |
911 | |
912 | GetEventHandler()->ProcessEvent(event); | |
913 | } | |
914 | ||
915 | void wxListBox::MacDoDoubleClick() | |
916 | { | |
917 | wxCommandEvent event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED, m_windowId); | |
918 | event.SetEventObject( this ); | |
919 | GetEventHandler()->ProcessEvent(event) ; | |
920 | } | |
921 | ||
922 | void wxListBox::OnChar(wxKeyEvent& event) | |
923 | { | |
924 | if ( event.GetKeyCode() == WXK_RETURN || event.GetKeyCode() == WXK_NUMPAD_ENTER) | |
925 | { | |
926 | wxWindow* parent = GetParent() ; | |
927 | while( parent && !parent->IsTopLevel() && parent->GetDefaultItem() == NULL ) | |
928 | parent = parent->GetParent() ; | |
929 | ||
930 | if ( parent && parent->GetDefaultItem() ) | |
931 | { | |
932 | wxButton *def = wxDynamicCast(parent->GetDefaultItem(), | |
933 | wxButton); | |
934 | if ( def && def->IsEnabled() ) | |
935 | { | |
936 | wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() ); | |
937 | event.SetEventObject(def); | |
938 | def->Command(event); | |
939 | return ; | |
940 | } | |
941 | } | |
942 | event.Skip() ; | |
943 | } | |
944 | /* generate wxID_CANCEL if command-. or <esc> has been pressed (typically in dialogs) */ | |
945 | else if (event.GetKeyCode() == WXK_ESCAPE || (event.GetKeyCode() == '.' && event.MetaDown() ) ) | |
946 | { | |
947 | // FIXME: look in ancestors, not just parent. | |
948 | wxWindow* win = GetParent()->FindWindow( wxID_CANCEL ) ; | |
949 | if (win) | |
950 | { | |
951 | wxCommandEvent new_event(wxEVT_COMMAND_BUTTON_CLICKED,wxID_CANCEL); | |
952 | new_event.SetEventObject( win ); | |
953 | win->GetEventHandler()->ProcessEvent( new_event ); | |
954 | } | |
955 | } | |
956 | else if ( event.GetKeyCode() == WXK_TAB ) | |
957 | { | |
958 | wxNavigationKeyEvent new_event; | |
959 | new_event.SetEventObject( this ); | |
960 | new_event.SetDirection( !event.ShiftDown() ); | |
961 | /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */ | |
962 | new_event.SetWindowChange( event.ControlDown() ); | |
963 | new_event.SetCurrentFocus( this ); | |
964 | if ( !GetEventHandler()->ProcessEvent( new_event ) ) | |
965 | event.Skip() ; | |
966 | } | |
967 | else if ( event.GetKeyCode() == WXK_DOWN || event.GetKeyCode() == WXK_UP ) | |
968 | { | |
969 | // perform the default key handling first | |
970 | wxControl::OnKeyDown( event ) ; | |
971 | ||
972 | wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, m_windowId); | |
973 | event.SetEventObject( this ); | |
974 | ||
975 | wxArrayInt aSelections; | |
976 | int n, count = GetSelections(aSelections); | |
977 | if ( count > 0 ) | |
978 | { | |
979 | n = aSelections[0]; | |
980 | if ( HasClientObjectData() ) | |
981 | event.SetClientObject( GetClientObject(n) ); | |
982 | else if ( HasClientUntypedData() ) | |
983 | event.SetClientData( GetClientData(n) ); | |
984 | event.SetString( GetString(n) ); | |
985 | } | |
986 | else | |
987 | { | |
988 | n = -1; | |
989 | } | |
990 | ||
687706f5 | 991 | event.SetInt(n); |
2646f485 SC |
992 | |
993 | GetEventHandler()->ProcessEvent(event); | |
994 | } | |
995 | else | |
996 | { | |
997 | if ( event.GetTimestamp() > m_lastTypeIn + 60 ) | |
998 | { | |
999 | m_typeIn = wxEmptyString ; | |
1000 | } | |
1001 | m_lastTypeIn = event.GetTimestamp() ; | |
1002 | m_typeIn += (char) event.GetKeyCode() ; | |
1003 | int line = FindString(wxT("*")+m_typeIn+wxT("*")) ; | |
1004 | if ( line >= 0 ) | |
1005 | { | |
1006 | if ( GetSelection() != line ) | |
1007 | { | |
1008 | SetSelection(line) ; | |
1009 | wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, m_windowId); | |
1010 | event.SetEventObject( this ); | |
1011 | ||
1012 | if ( HasClientObjectData() ) | |
1013 | event.SetClientObject( GetClientObject( line ) ); | |
1014 | else if ( HasClientUntypedData() ) | |
1015 | event.SetClientData( GetClientData(line) ); | |
1016 | event.SetString( GetString(line) ); | |
1017 | ||
687706f5 | 1018 | event.SetInt(line); |
2646f485 SC |
1019 | |
1020 | GetEventHandler()->ProcessEvent(event); | |
1021 | } | |
1022 | } | |
1023 | } | |
1024 | } | |
1025 |