]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/mac/listbox.cpp
make --static flag act on --cppflags too.
[wxWidgets.git] / src / mac / listbox.cpp
... / ...
CommitLineData
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
16#include "wx/app.h"
17#include "wx/listbox.h"
18#include "wx/settings.h"
19#include "wx/dynarray.h"
20#include "wx/log.h"
21
22#include "wx/utils.h"
23#ifndef __DARWIN__
24// #include "extldef.h"
25#endif
26
27#if !USE_SHARED_LIBRARY
28 IMPLEMENT_DYNAMIC_CLASS(wxListBox, wxControl)
29
30BEGIN_EVENT_TABLE(wxListBox, wxControl)
31 EVT_SIZE( wxListBox::OnSize )
32END_EVENT_TABLE()
33#endif
34
35#include "wx/mac/uma.h"
36
37
38typedef struct {
39 unsigned short instruction;
40 void (*function)();
41} ldefRec, *ldefPtr, **ldefHandle;
42
43extern "C"
44{
45static pascal void wxMacListDefinition( short message, Boolean isSelected, Rect *drawRect,
46 Cell cell, short dataOffset, short dataLength,
47 ListHandle listHandle ) ;
48}
49
50static pascal void wxMacListDefinition( short message, Boolean isSelected, Rect *drawRect,
51 Cell cell, short dataOffset, short dataLength,
52 ListHandle listHandle )
53{
54 FontInfo fontInfo;
55 GrafPtr savePort;
56 GrafPtr grafPtr;
57 RgnHandle savedClipRegion;
58 SInt32 savedPenMode;
59 wxListBox* list;
60 GetPort(&savePort);
61 SetPort((**listHandle).port);
62 grafPtr = (**listHandle).port ;
63 // typecast our refCon
64 list = (wxListBox*) GetControlReference( (ControlHandle) GetListRefCon(listHandle) );
65
66 // Calculate the cell rect.
67
68 switch( message ) {
69 case lInitMsg:
70 break;
71
72 case lCloseMsg:
73 break;
74
75 case lDrawMsg:
76 {
77 const wxString text = list->m_stringArray[cell.v] ;
78
79 // Save the current clip region, and set the clip region to the area we are about
80 // to draw.
81
82 savedClipRegion = NewRgn();
83 GetClip( savedClipRegion );
84 ClipRect( drawRect );
85 EraseRect( drawRect );
86
87
88 MoveTo(drawRect->left + 4 , drawRect->top + 10 );
89 ::TextFont( kFontIDMonaco ) ;
90 ::TextSize( 9 );
91 ::TextFace( 0 ) ;
92
93 DrawText(text, 0 , text.Length());
94 // If the cell is hilited, do the hilite now. Paint the cell contents with the
95 // appropriate QuickDraw transform mode.
96
97 if( isSelected ) {
98 savedPenMode = GetPortPenMode( grafPtr );
99 SetPortPenMode( grafPtr, hilitetransfermode );
100 PaintRect( drawRect );
101 SetPortPenMode( grafPtr, savedPenMode );
102 }
103
104 // Restore the saved clip region.
105
106 SetClip( savedClipRegion );
107 DisposeRgn( savedClipRegion );
108 }
109 break;
110 case lHiliteMsg:
111
112 // Hilite or unhilite the cell. Paint the cell contents with the
113 // appropriate QuickDraw transform mode.
114
115 GetPort( &grafPtr );
116 savedPenMode = GetPortPenMode( grafPtr );
117 SetPortPenMode( grafPtr, hilitetransfermode );
118 PaintRect( drawRect );
119 SetPortPenMode( grafPtr, savedPenMode );
120 break;
121 default :
122 break ;
123 }
124 SetPort(savePort);
125}
126
127extern "C" void MacDrawStringCell(Rect *cellRect, Cell lCell, ListHandle theList, long refCon) ;
128const short kwxMacListWithVerticalScrollbar = 128 ;
129
130// ============================================================================
131// list box control implementation
132// ============================================================================
133
134// Listbox item
135wxListBox::wxListBox()
136{
137 m_noItems = 0;
138 m_selected = 0;
139 m_macList = NULL ;
140}
141
142static ListDefUPP macListDefUPP = NULL ;
143
144bool wxListBox::Create(wxWindow *parent, wxWindowID id,
145 const wxPoint& pos,
146 const wxSize& size,
147 int n, const wxString choices[],
148 long style,
149 const wxValidator& validator,
150 const wxString& name)
151{
152 m_noItems = 0 ; // this will be increased by our append command
153 m_selected = 0;
154
155 Rect bounds ;
156 Str255 title ;
157
158 MacPreControlCreate( parent , id , "" , pos , size ,style, validator , name , &bounds , title ) ;
159
160 ListDefSpec listDef;
161 listDef.defType = kListDefUserProcType;
162 if ( macListDefUPP == NULL )
163 {
164 macListDefUPP = NewListDefUPP( wxMacListDefinition );
165 }
166 listDef.u.userProc = macListDefUPP ;
167#if TARGET_CARBON
168 Size asize;
169
170
171 CreateListBoxControl( parent->GetMacRootWindow(), &bounds, false, 0, 1, false, true,
172 14, 14, false, &listDef, &m_macControl );
173
174 GetControlData(m_macControl, kControlNoPart, kControlListBoxListHandleTag,
175 sizeof(ListHandle), (Ptr) &m_macList, &asize);
176
177 SetControlReference(m_macControl, (long) this);
178 SetControlVisibility(m_macControl, false, false);
179
180#else
181 long result ;
182
183 m_macControl = ::NewControl( parent->GetMacRootWindow() , &bounds , title , false ,
184 kwxMacListWithVerticalScrollbar , 0 , 0,
185 kControlListBoxProc , (long) this ) ;
186 ::GetControlData( m_macControl , kControlNoPart , kControlListBoxListHandleTag ,
187 sizeof( ListHandle ) , (char*) &m_macList , &result ) ;
188
189 HLock( (Handle) m_macList ) ;
190 ldefHandle ldef ;
191 ldef = (ldefHandle) NewHandle( sizeof(ldefRec) ) ;
192 if ( (**m_macList).listDefProc != NULL )
193 {
194 (**ldef).instruction = 0x4EF9; /* JMP instruction */
195 (**ldef).function = (void(*)()) listDef.u.userProc;
196 (**m_macList).listDefProc = (Handle) ldef ;
197 }
198
199 Point pt = (**m_macList).cellSize ;
200 pt.v = 14 ;
201 LCellSize( pt , m_macList ) ;
202
203 LAddColumn( 1 , 0 , m_macList ) ;
204#endif
205 OptionBits options = 0;
206 if ( style & wxLB_MULTIPLE )
207 {
208 options += lNoExtend ;
209 }
210 else if ( style & wxLB_EXTENDED )
211 {
212 options += lExtendDrag ;
213 }
214 else
215 {
216 options = lOnlyOne ;
217 }
218 SetListSelectionFlags(m_macList, options);
219
220 MacPostControlCreate() ;
221
222 for ( int i = 0 ; i < n ; i++ )
223 {
224 Append( choices[i] ) ;
225 }
226
227 LSetDrawingMode( true , m_macList ) ;
228
229 return TRUE;
230}
231
232wxListBox::~wxListBox()
233{
234 Free() ;
235 if ( m_macList )
236 {
237#if !TARGET_CARBON
238 DisposeHandle( (**m_macList).listDefProc ) ;
239 (**m_macList).listDefProc = NULL ;
240#endif
241 m_macList = NULL ;
242 }
243}
244
245void wxListBox::Free()
246{
247#if wxUSE_OWNER_DRAWN
248 if ( m_windowStyle & wxLB_OWNERDRAW )
249 {
250 size_t uiCount = m_aItems.Count();
251 while ( uiCount-- != 0 ) {
252 delete m_aItems[uiCount];
253 }
254
255 m_aItems.Clear();
256 }
257 else
258#endif // wxUSE_OWNER_DRAWN
259 if ( HasClientObjectData() )
260 {
261 for ( size_t n = 0; n < (size_t)m_noItems; n++ )
262 {
263 delete GetClientObject(n);
264 }
265 }
266}
267
268void wxListBox::DoSetFirstItem(int N)
269{
270 MacScrollTo( N ) ;
271}
272
273void wxListBox::Delete(int N)
274{
275 wxCHECK_RET( N >= 0 && N < m_noItems,
276 wxT("invalid index in wxListBox::Delete") );
277
278#if wxUSE_OWNER_DRAWN
279 delete m_aItems[N];
280 m_aItems.RemoveAt(N);
281#else // !wxUSE_OWNER_DRAWN
282 if ( HasClientObjectData() )
283 {
284 delete GetClientObject(N);
285 }
286#endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
287 m_stringArray.Remove( N ) ;
288 m_dataArray.RemoveAt( N ) ;
289 m_noItems --;
290
291 MacDelete( N ) ;
292}
293
294int wxListBox::DoAppend(const wxString& item)
295{
296 int index = m_noItems ;
297 if( wxApp::s_macDefaultEncodingIsPC )
298 {
299 m_stringArray.Add( wxMacMakeMacStringFromPC( item ) ) ;
300 m_dataArray.Add( NULL );
301 }
302 else {
303 m_stringArray.Add( item ) ;
304 m_dataArray.Add( NULL );
305 }
306 m_noItems ++;
307 DoSetItemClientData( index , NULL ) ;
308 MacAppend( item ) ;
309
310 return index ;
311}
312
313void wxListBox::DoSetItems(const wxArrayString& choices, void** clientData)
314{
315 MacSetRedraw( false ) ;
316 Clear() ;
317 int n = choices.GetCount();
318
319 for( int i = 0 ; i < n ; ++i )
320 {
321 if ( clientData )
322 {
323#if wxUSE_OWNER_DRAWN
324 wxASSERT_MSG(clientData[i] == NULL,
325 wxT("Can't use client data with owner-drawn listboxes"));
326#else // !wxUSE_OWNER_DRAWN
327 Append( choices[i] , clientData[i] ) ;
328 #endif
329 }
330 else
331 Append( choices[i] ) ;
332 }
333
334#if wxUSE_OWNER_DRAWN
335 if ( m_windowStyle & wxLB_OWNERDRAW ) {
336 // first delete old items
337 size_t ui = m_aItems.Count();
338 while ( ui-- != 0 ) {
339 delete m_aItems[ui];
340 }
341 m_aItems.Empty();
342
343 // then create new ones
344 for ( ui = 0; ui < (size_t)m_noItems; ui++ ) {
345 wxOwnerDrawn *pNewItem = CreateItem(ui);
346 pNewItem->SetName(choices[ui]);
347 m_aItems.Add(pNewItem);
348 }
349 }
350#endif // wxUSE_OWNER_DRAWN
351 MacSetRedraw( true ) ;
352}
353
354bool wxListBox::HasMultipleSelection() const
355{
356 return (m_windowStyle & wxLB_MULTIPLE) || (m_windowStyle & wxLB_EXTENDED);
357}
358
359int wxListBox::FindString(const wxString& st) const
360{
361 wxString s ;
362 if( wxApp::s_macDefaultEncodingIsPC )
363 {
364 s = wxMacMakeMacStringFromPC( st ) ;
365 }
366 else
367 s = st ;
368
369 if ( s.Right(1) == "*" )
370 {
371 wxString search = s.Left( s.Length() - 1 ) ;
372 int len = search.Length() ;
373 Str255 s1 , s2 ;
374
375#if TARGET_CARBON
376 c2pstrcpy( (StringPtr) s2 , search.c_str() ) ;
377#else
378 strcpy( (char *) s2 , search.c_str() ) ;
379 c2pstr( (char *) s2 ) ;
380#endif
381
382 for ( int i = 0 ; i < m_noItems ; ++ i )
383 {
384#if TARGET_CARBON
385 c2pstrcpy( (StringPtr) s1 , m_stringArray[i].Left( len ).c_str() ) ;
386#else
387 strcpy( (char *) s1 , m_stringArray[i].Left( len ).c_str() ) ;
388 c2pstr( (char *) s1 ) ;
389#endif
390 if ( EqualString( s1 , s2 , false , false ) )
391 return i ;
392 }
393 if ( s.Left(1) == "*" && s.Length() > 1 )
394 {
395 s.MakeLower() ;
396 for ( int i = 0 ; i < m_noItems ; ++i )
397 {
398 if ( GetString(i).Lower().Matches(s) )
399 return i ;
400 }
401 }
402
403 }
404 else
405 {
406 Str255 s1 , s2 ;
407
408#if TARGET_CARBON
409 c2pstrcpy( (StringPtr) s2 , s.c_str() ) ;
410#else
411 strcpy( (char *) s2 , s.c_str() ) ;
412 c2pstr( (char *) s2 ) ;
413#endif
414
415 for ( int i = 0 ; i < m_noItems ; ++ i )
416 {
417#if TARGET_CARBON
418 c2pstrcpy( (StringPtr) s1 , m_stringArray[i].c_str() ) ;
419#else
420 strcpy( (char *) s1 , m_stringArray[i].c_str() ) ;
421 c2pstr( (char *) s1 ) ;
422#endif
423 if ( EqualString( s1 , s2 , false , false ) )
424 return i ;
425 }
426 }
427 return -1;
428}
429
430void wxListBox::Clear()
431{
432 Free();
433 m_noItems = 0;
434 m_stringArray.Empty() ;
435 m_dataArray.Empty() ;
436 MacClear() ;
437}
438
439void wxListBox::SetSelection(int N, bool select)
440{
441 wxCHECK_RET( N >= 0 && N < m_noItems,
442 "invalid index in wxListBox::SetSelection" );
443 MacSetSelection( N , select ) ;
444}
445
446bool wxListBox::IsSelected(int N) const
447{
448 wxCHECK_MSG( N >= 0 && N < m_noItems, FALSE,
449 "invalid index in wxListBox::Selected" );
450
451 return MacIsSelected( N ) ;
452}
453
454void *wxListBox::DoGetItemClientData(int N) const
455{
456 wxCHECK_MSG( N >= 0 && N < m_noItems, NULL,
457 wxT("invalid index in wxListBox::GetClientData"));
458
459 return (void *)m_dataArray[N];
460}
461
462wxClientData *wxListBox::DoGetItemClientObject(int N) const
463{
464 return (wxClientData *) DoGetItemClientData( N ) ;
465}
466
467void wxListBox::DoSetItemClientData(int N, void *Client_data)
468{
469 wxCHECK_RET( N >= 0 && N < m_noItems,
470 "invalid index in wxListBox::SetClientData" );
471
472#if wxUSE_OWNER_DRAWN
473 if ( m_windowStyle & wxLB_OWNERDRAW )
474 {
475 // client data must be pointer to wxOwnerDrawn, otherwise we would crash
476 // in OnMeasure/OnDraw.
477 wxFAIL_MSG(wxT("Can't use client data with owner-drawn listboxes"));
478 }
479#endif // wxUSE_OWNER_DRAWN
480 wxASSERT_MSG( m_dataArray.GetCount() >= N , "invalid client_data array" ) ;
481
482 if ( m_dataArray.GetCount() > N )
483 {
484 m_dataArray[N] = (char*) Client_data ;
485 }
486 else
487 {
488 m_dataArray.Add( (char*) Client_data ) ;
489 }
490}
491
492void wxListBox::DoSetItemClientObject(int n, wxClientData* clientData)
493{
494 DoSetItemClientData(n, clientData);
495}
496
497// Return number of selections and an array of selected integers
498int wxListBox::GetSelections(wxArrayInt& aSelections) const
499{
500 return MacGetSelections( aSelections ) ;
501
502/* TODO
503 if ((m_windowStyle & wxLB_MULTIMacE) || (m_windowStyle & wxLB_EXTENDED))
504 {
505 int no_sel = ??
506 for ( int n = 0; n < no_sel; n++ )
507 aSelections.Add(??);
508
509 return no_sel;
510 }
511 else // single-selection listbox
512 {
513 aSelections.Add(??);
514
515 return 1;
516 }
517*/
518}
519
520// Get single selection, for single choice list items
521int wxListBox::GetSelection() const
522{
523 return MacGetSelection() ;
524}
525
526// Find string for position
527wxString wxListBox::GetString(int N) const
528{
529 if( wxApp::s_macDefaultEncodingIsPC )
530 {
531 return wxMacMakePCStringFromMac( m_stringArray[N] ) ;
532 }
533 else
534 return m_stringArray[N] ;
535}
536
537void wxListBox::DoInsertItems(const wxArrayString& items, int pos)
538{
539 wxCHECK_RET( pos >= 0 && pos <= m_noItems,
540 wxT("invalid index in wxListBox::InsertItems") );
541
542 int nItems = items.GetCount();
543
544 for ( int i = 0 ; i < nItems ; i++ )
545 {
546 m_stringArray.Insert( items[i] , pos + i ) ;
547 m_dataArray.Insert( NULL , pos + i ) ;
548 MacInsert( pos + i , items[i] ) ;
549 }
550
551 m_noItems += nItems;
552}
553
554void wxListBox::SetString(int N, const wxString& s)
555{
556 wxString str ;
557 if( wxApp::s_macDefaultEncodingIsPC )
558 {
559 str = wxMacMakeMacStringFromPC( s ) ;
560 }
561 else
562 str = s ;
563 m_stringArray[N] = str ;
564 MacSet( N , s ) ;
565}
566
567wxSize wxListBox::DoGetBestSize() const
568{
569 return wxSize(100, 100);
570}
571
572int wxListBox::GetCount() const
573{
574 return m_noItems;
575}
576
577void wxListBox::SetupColours()
578{
579 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW));
580 SetForegroundColour(GetParent()->GetForegroundColour());
581}
582
583void wxListBox::Refresh(bool eraseBack, const wxRect *rect)
584{
585 // Set up port
586 WindowRef rootwindow = GetMacRootWindow() ;
587 wxWindow* wxrootwindow = wxFindWinFromMacWindow( rootwindow ) ;
588 wxMacDrawingHelper focus( wxrootwindow );
589
590 UMADrawControl(m_macControl);
591}
592
593#if wxUSE_OWNER_DRAWN
594
595class wxListBoxItem : public wxOwnerDrawn
596{
597public:
598 wxListBoxItem(const wxString& str = "");
599};
600
601wxListBoxItem::wxListBoxItem(const wxString& str) : wxOwnerDrawn(str, FALSE)
602{
603 // no bitmaps/checkmarks
604 SetMarginWidth(0);
605}
606
607wxOwnerDrawn *wxListBox::CreateItem(size_t n)
608{
609 return new wxListBoxItem();
610}
611
612#endif //USE_OWNER_DRAWN
613
614// ============================================================================
615// list box control implementation
616// ============================================================================
617
618void MacDrawStringCell(Rect *cellRect, Cell lCell, ListHandle theList, long refCon)
619{
620 wxListBox* list;
621 // typecast our refCon
622 list = (wxListBox*)refCon;
623
624 MoveTo(cellRect->left + 4 , cellRect->top + 10 );
625 const wxString text = list->m_stringArray[lCell.v] ;
626 ::TextFont( kFontIDMonaco ) ;
627 ::TextSize( 9 );
628 ::TextFace( 0 ) ;
629 DrawText(text, 0 , text.Length());
630
631}
632
633void wxListBox::MacDelete( int N )
634{
635 LDelRow( 1 , N , m_macList) ;
636 Refresh();
637}
638
639void wxListBox::MacInsert( int n , const char * text)
640{
641 Cell cell = { 0 , 0 } ;
642 cell.v = n ;
643 LAddRow( 1 , cell.v , m_macList ) ;
644// LSetCell(text, strlen(text), cell, m_macList);
645 Refresh();
646}
647
648void wxListBox::MacAppend( const char * text)
649{
650 Cell cell = { 0 , 0 } ;
651 cell.v = (**m_macList).dataBounds.bottom ;
652 LAddRow( 1 , cell.v , m_macList ) ;
653 // LSetCell(text, strlen(text), cell, m_macList);
654 Refresh();
655}
656
657void wxListBox::MacClear()
658{
659 LDelRow( (**m_macList).dataBounds.bottom , 0 , m_macList ) ;
660 Refresh();
661}
662
663void wxListBox::MacSetSelection( int n , bool select )
664{
665 Cell cell = { 0 , 0 } ;
666 if ( LGetSelect( true , &cell , m_macList ) )
667 {
668 LSetSelect( false , cell , m_macList ) ;
669 }
670
671 cell.v = n ;
672 LSetSelect( select , cell , m_macList ) ;
673 LAutoScroll( m_macList ) ;
674 Refresh();
675}
676
677bool wxListBox::MacIsSelected( int n ) const
678{
679 Cell cell = { 0 , 0 } ;
680 cell.v = n ;
681 return LGetSelect( false , &cell , m_macList ) ;
682}
683
684void wxListBox::MacDestroy()
685{
686// DisposeExtLDEFInfo( m_macList ) ;
687}
688
689int wxListBox::MacGetSelection() const
690{
691 Cell cell = { 0 , 0 } ;
692 if ( LGetSelect( true , &cell , m_macList ) )
693 return cell.v ;
694 else
695 return -1 ;
696}
697
698int wxListBox::MacGetSelections( wxArrayInt& aSelections ) const
699{
700 int no_sel = 0 ;
701
702 aSelections.Empty();
703
704 Cell cell = { 0 , 0 } ;
705 cell.v = 0 ;
706
707 while ( LGetSelect( true , &cell , m_macList ) )
708 {
709 aSelections.Add( cell.v ) ;
710 no_sel++ ;
711 cell.v++ ;
712 }
713 return no_sel ;
714}
715
716void wxListBox::MacSet( int n , const char * text )
717{
718 // our implementation does not store anything in the list
719 // so we just have to redraw
720 Cell cell = { 0 , 0 } ;
721 cell.v = n ;
722// LSetCell(text, strlen(text), cell, m_macList);
723 Refresh();
724}
725
726void wxListBox::MacScrollTo( int n )
727{
728 // TODO implement scrolling
729}
730
731void wxListBox::OnSize( const wxSizeEvent &event)
732{
733 Point pt;
734
735#if TARGET_CARBON
736 GetListCellSize(m_macList, &pt);
737#else
738 pt = (**m_macList).cellSize ;
739#endif
740 pt.h = m_width - 15 ;
741 LCellSize( pt , m_macList ) ;
742}
743
744void wxListBox::MacHandleControlClick( ControlHandle control , SInt16 controlpart )
745{
746 Boolean wasDoubleClick = false ;
747 long result ;
748
749 ::GetControlData( m_macControl , kControlNoPart , kControlListBoxDoubleClickTag , sizeof( wasDoubleClick ) , (char*) &wasDoubleClick , &result ) ;
750 if ( !wasDoubleClick )
751 {
752 MacDoClick() ;
753 }
754 else
755 {
756 MacDoDoubleClick() ;
757 }
758}
759
760void wxListBox::MacSetRedraw( bool doDraw )
761{
762 LSetDrawingMode( doDraw , m_macList ) ;
763
764}
765
766void wxListBox::MacDoClick()
767{
768 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, m_windowId);
769 event.SetEventObject( this );
770
771 wxArrayInt aSelections;
772 int n, count = GetSelections(aSelections);
773 if ( count > 0 )
774 {
775 n = aSelections[0];
776 if ( HasClientObjectData() )
777 event.SetClientObject( GetClientObject(n) );
778 else if ( HasClientUntypedData() )
779 event.SetClientData( GetClientData(n) );
780 event.SetString( GetString(n) );
781 }
782 else
783 {
784 n = -1;
785 }
786
787 event.m_commandInt = n;
788
789 GetEventHandler()->ProcessEvent(event);
790}
791
792void wxListBox::MacDoDoubleClick()
793{
794 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED, m_windowId);
795 event.SetEventObject( this );
796 GetEventHandler()->ProcessEvent(event) ;
797}