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