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