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