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