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