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