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