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