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