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