1 ///////////////////////////////////////////////////////////////////////////////
8 // Copyright: (c) AUTHOR
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "listbox.h"
17 #include "wx/listbox.h"
18 #include "wx/settings.h"
19 #include "wx/toplevel.h"
20 #include "wx/dynarray.h"
25 #if !USE_SHARED_LIBRARY
26 IMPLEMENT_DYNAMIC_CLASS(wxListBox
, wxControl
)
28 BEGIN_EVENT_TABLE(wxListBox
, wxControl
)
29 EVT_SIZE( wxListBox::OnSize
)
33 #include "wx/mac/uma.h"
35 #if PRAGMA_STRUCT_ALIGN
36 #pragma options align=mac68k
37 #elif PRAGMA_STRUCT_PACKPUSH
39 #elif PRAGMA_STRUCT_PACK
44 unsigned short instruction
;
46 } ldefRec
, *ldefPtr
, **ldefHandle
;
48 #if PRAGMA_STRUCT_ALIGN
49 #pragma options align=reset
50 #elif PRAGMA_STRUCT_PACKPUSH
52 #elif PRAGMA_STRUCT_PACK
58 static pascal void wxMacListDefinition( short message
, Boolean isSelected
, Rect
*drawRect
,
59 Cell cell
, short dataOffset
, short dataLength
,
60 ListHandle listHandle
) ;
63 static pascal void wxMacListDefinition( short message
, Boolean isSelected
, Rect
*drawRect
,
64 Cell cell
, short dataOffset
, short dataLength
,
65 ListHandle listHandle
)
70 RgnHandle savedClipRegion
;
74 SetPort((**listHandle
).port
);
75 grafPtr
= (**listHandle
).port
;
76 // typecast our refCon
77 list
= (wxListBox
*) GetControlReference( (ControlHandle
) GetListRefCon(listHandle
) );
79 // Calculate the cell rect.
90 const wxString text
= list
->m_stringArray
[cell
.v
] ;
92 // Save the current clip region, and set the clip region to the area we are about
95 savedClipRegion
= NewRgn();
96 GetClip( savedClipRegion
);
99 EraseRect( drawRect
);
101 MoveTo(drawRect
->left
+ 4 , drawRect
->top
+ 10 );
102 ::TextFont( kFontIDMonaco
) ;
106 DrawText(text
, 0 , text
.Length());
107 // If the cell is hilited, do the hilite now. Paint the cell contents with the
108 // appropriate QuickDraw transform mode.
111 savedPenMode
= GetPortPenMode( (CGrafPtr
) grafPtr
);
112 SetPortPenMode( (CGrafPtr
)grafPtr
, hilitetransfermode
);
113 PaintRect( drawRect
);
114 SetPortPenMode( (CGrafPtr
)grafPtr
, savedPenMode
);
117 // Restore the saved clip region.
119 SetClip( savedClipRegion
);
120 DisposeRgn( savedClipRegion
);
125 // Hilite or unhilite the cell. Paint the cell contents with the
126 // appropriate QuickDraw transform mode.
129 savedPenMode
= GetPortPenMode( (CGrafPtr
)grafPtr
);
130 SetPortPenMode( (CGrafPtr
)grafPtr
, hilitetransfermode
);
131 PaintRect( drawRect
);
132 SetPortPenMode( (CGrafPtr
)grafPtr
, savedPenMode
);
140 extern "C" void MacDrawStringCell(Rect
*cellRect
, Cell lCell
, ListHandle theList
, long refCon
) ;
141 const short kwxMacListWithVerticalScrollbar
= 128 ;
142 const short kwxMacListItemHeight
= 14 ;
144 // ============================================================================
145 // list box control implementation
146 // ============================================================================
149 wxListBox::wxListBox()
156 static ListDefUPP macListDefUPP
= NULL
;
158 bool wxListBox::Create(wxWindow
*parent
, wxWindowID id
,
161 int n
, const wxString choices
[],
163 const wxValidator
& validator
,
164 const wxString
& name
)
166 m_noItems
= 0 ; // this will be increased by our append command
172 MacPreControlCreate( parent
, id
, "" , pos
, size
,style
, validator
, name
, &bounds
, title
) ;
175 listDef
.defType
= kListDefUserProcType
;
176 if ( macListDefUPP
== NULL
)
178 macListDefUPP
= NewListDefUPP( wxMacListDefinition
);
180 listDef
.u
.userProc
= macListDefUPP
;
186 CreateListBoxControl( MAC_WXHWND(parent
->MacGetRootWindow()), &bounds
, false, 0, 1, false, true,
187 kwxMacListItemHeight
, kwxMacListItemHeight
, false, &listDef
, (ControlRef
*)&m_macControl
);
189 GetControlData( (ControlHandle
) m_macControl
, kControlNoPart
, kControlListBoxListHandleTag
,
190 sizeof(ListHandle
), (Ptr
) &m_macList
, &asize
);
192 SetControlReference( (ControlHandle
) m_macControl
, (long) this);
193 SetControlVisibility( (ControlHandle
) m_macControl
, false, false);
199 m_macControl
= ::NewControl( MAC_WXHWND(parent
->MacGetRootWindow()) , &bounds
, title
, false ,
200 kwxMacListWithVerticalScrollbar
, 0 , 0,
201 kControlListBoxProc
, (long) this ) ;
202 ::GetControlData( (ControlHandle
) m_macControl
, kControlNoPart
, kControlListBoxListHandleTag
,
203 sizeof( ListHandle
) , (char*) &m_macList
, &result
) ;
205 HLock( (Handle
) m_macList
) ;
207 ldef
= (ldefHandle
) NewHandle( sizeof(ldefRec
) ) ;
208 if ( (**(ListHandle
)m_macList
).listDefProc
!= NULL
)
210 (**ldef
).instruction
= 0x4EF9; /* JMP instruction */
211 (**ldef
).function
= (void(*)()) listDef
.u
.userProc
;
212 (**(ListHandle
)m_macList
).listDefProc
= (Handle
) ldef
;
215 Point pt
= (**(ListHandle
)m_macList
).cellSize
;
216 pt
.v
= kwxMacListItemHeight
;
217 LCellSize( pt
, (ListHandle
)m_macList
) ;
218 LAddColumn( 1 , 0 , (ListHandle
)m_macList
) ;
220 OptionBits options
= 0;
221 if ( style
& wxLB_MULTIPLE
)
223 options
+= lNoExtend
;
225 else if ( style
& wxLB_EXTENDED
)
227 options
+= lExtendDrag
;
233 SetListSelectionFlags((ListHandle
)m_macList
, options
);
235 MacPostControlCreate() ;
237 for ( int i
= 0 ; i
< n
; i
++ )
239 Append( choices
[i
] ) ;
242 LSetDrawingMode( true , (ListHandle
)m_macList
) ;
247 wxListBox::~wxListBox()
253 DisposeHandle( (**(ListHandle
)m_macList
).listDefProc
) ;
254 (**(ListHandle
)m_macList
).listDefProc
= NULL
;
260 void wxListBox::Free()
262 #if wxUSE_OWNER_DRAWN
263 if ( m_windowStyle
& wxLB_OWNERDRAW
)
265 size_t uiCount
= m_aItems
.Count();
266 while ( uiCount
-- != 0 ) {
267 delete m_aItems
[uiCount
];
273 #endif // wxUSE_OWNER_DRAWN
274 if ( HasClientObjectData() )
276 for ( size_t n
= 0; n
< (size_t)m_noItems
; n
++ )
278 delete GetClientObject(n
);
283 void wxListBox::DoSetSize(int x
, int y
,
284 int width
, int height
,
287 wxControl::DoSetSize( x
, y
, width
, height
, sizeFlags
) ;
290 GetControlBounds( (ControlHandle
) m_macControl
, &bounds
) ;
291 ControlRef control
= GetListVerticalScrollBar( (ListHandle
)m_macList
) ;
295 GetControlBounds( control
, &scrollbounds
) ;
296 if( scrollbounds
.right
!= bounds
.right
+ 1 )
298 UMAMoveControl( control
, bounds
.right
- (scrollbounds
.right
- scrollbounds
.left
) + 1 ,
304 void wxListBox::DoSetFirstItem(int N
)
309 void wxListBox::Delete(int N
)
311 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
312 wxT("invalid index in wxListBox::Delete") );
314 #if wxUSE_OWNER_DRAWN
316 m_aItems
.RemoveAt(N
);
317 #else // !wxUSE_OWNER_DRAWN
318 if ( HasClientObjectData() )
320 delete GetClientObject(N
);
322 #endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
323 m_stringArray
.Remove( N
) ;
324 m_dataArray
.RemoveAt( N
) ;
330 int wxListBox::DoAppend(const wxString
& item
)
332 int index
= m_noItems
;
333 if( wxApp::s_macDefaultEncodingIsPC
)
335 m_stringArray
.Add( wxMacMakeMacStringFromPC( item
) ) ;
336 m_dataArray
.Add( NULL
);
339 m_stringArray
.Add( item
) ;
340 m_dataArray
.Add( NULL
);
343 DoSetItemClientData( index
, NULL
) ;
349 void wxListBox::DoSetItems(const wxArrayString
& choices
, void** clientData
)
351 MacSetRedraw( false ) ;
353 int n
= choices
.GetCount();
355 for( int i
= 0 ; i
< n
; ++i
)
359 #if wxUSE_OWNER_DRAWN
360 wxASSERT_MSG(clientData
[i
] == NULL
,
361 wxT("Can't use client data with owner-drawn listboxes"));
362 #else // !wxUSE_OWNER_DRAWN
363 Append( choices
[i
] , clientData
[i
] ) ;
367 Append( choices
[i
] ) ;
370 #if wxUSE_OWNER_DRAWN
371 if ( m_windowStyle
& wxLB_OWNERDRAW
) {
372 // first delete old items
373 size_t ui
= m_aItems
.Count();
374 while ( ui
-- != 0 ) {
379 // then create new ones
380 for ( ui
= 0; ui
< (size_t)m_noItems
; ui
++ ) {
381 wxOwnerDrawn
*pNewItem
= CreateItem(ui
);
382 pNewItem
->SetName(choices
[ui
]);
383 m_aItems
.Add(pNewItem
);
386 #endif // wxUSE_OWNER_DRAWN
387 MacSetRedraw( true ) ;
390 bool wxListBox::HasMultipleSelection() const
392 return (m_windowStyle
& wxLB_MULTIPLE
) || (m_windowStyle
& wxLB_EXTENDED
);
395 int wxListBox::FindString(const wxString
& st
) const
398 if( wxApp::s_macDefaultEncodingIsPC
)
400 s
= wxMacMakeMacStringFromPC( st
) ;
405 if ( s
.Right(1) == "*" )
407 wxString search
= s
.Left( s
.Length() - 1 ) ;
408 int len
= search
.Length() ;
412 c2pstrcpy( (StringPtr
) s2
, search
.c_str() ) ;
414 strcpy( (char *) s2
, search
.c_str() ) ;
415 c2pstr( (char *) s2
) ;
418 for ( int i
= 0 ; i
< m_noItems
; ++ i
)
421 c2pstrcpy( (StringPtr
) s1
, m_stringArray
[i
].Left( len
).c_str() ) ;
423 strcpy( (char *) s1
, m_stringArray
[i
].Left( len
).c_str() ) ;
424 c2pstr( (char *) s1
) ;
426 if ( EqualString( s1
, s2
, false , false ) )
429 if ( s
.Left(1) == "*" && s
.Length() > 1 )
432 for ( int i
= 0 ; i
< m_noItems
; ++i
)
434 if ( GetString(i
).Lower().Matches(s
) )
445 c2pstrcpy( (StringPtr
) s2
, s
.c_str() ) ;
447 strcpy( (char *) s2
, s
.c_str() ) ;
448 c2pstr( (char *) s2
) ;
451 for ( int i
= 0 ; i
< m_noItems
; ++ i
)
454 c2pstrcpy( (StringPtr
) s1
, m_stringArray
[i
].c_str() ) ;
456 strcpy( (char *) s1
, m_stringArray
[i
].c_str() ) ;
457 c2pstr( (char *) s1
) ;
459 if ( EqualString( s1
, s2
, false , false ) )
466 void wxListBox::Clear()
470 m_stringArray
.Empty() ;
471 m_dataArray
.Empty() ;
475 void wxListBox::SetSelection(int N
, bool select
)
477 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
478 "invalid index in wxListBox::SetSelection" );
479 MacSetSelection( N
, select
) ;
480 GetSelections( m_selectionPreImage
) ;
483 bool wxListBox::IsSelected(int N
) const
485 wxCHECK_MSG( N
>= 0 && N
< m_noItems
, FALSE
,
486 "invalid index in wxListBox::Selected" );
488 return MacIsSelected( N
) ;
491 void *wxListBox::DoGetItemClientData(int N
) const
493 wxCHECK_MSG( N
>= 0 && N
< m_noItems
, NULL
,
494 wxT("invalid index in wxListBox::GetClientData"));
496 return (void *)m_dataArray
[N
];
499 wxClientData
*wxListBox::DoGetItemClientObject(int N
) const
501 return (wxClientData
*) DoGetItemClientData( N
) ;
504 void wxListBox::DoSetItemClientData(int N
, void *Client_data
)
506 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
507 "invalid index in wxListBox::SetClientData" );
509 #if wxUSE_OWNER_DRAWN
510 if ( m_windowStyle
& wxLB_OWNERDRAW
)
512 // client data must be pointer to wxOwnerDrawn, otherwise we would crash
513 // in OnMeasure/OnDraw.
514 wxFAIL_MSG(wxT("Can't use client data with owner-drawn listboxes"));
516 #endif // wxUSE_OWNER_DRAWN
517 wxASSERT_MSG( m_dataArray
.GetCount() >= N
, "invalid client_data array" ) ;
519 if ( m_dataArray
.GetCount() > N
)
521 m_dataArray
[N
] = (char*) Client_data
;
525 m_dataArray
.Add( (char*) Client_data
) ;
529 void wxListBox::DoSetItemClientObject(int n
, wxClientData
* clientData
)
531 DoSetItemClientData(n
, clientData
);
534 // Return number of selections and an array of selected integers
535 int wxListBox::GetSelections(wxArrayInt
& aSelections
) const
537 return MacGetSelections( aSelections
) ;
540 // Get single selection, for single choice list items
541 int wxListBox::GetSelection() const
543 return MacGetSelection() ;
546 // Find string for position
547 wxString
wxListBox::GetString(int N
) const
549 if( wxApp::s_macDefaultEncodingIsPC
)
551 return wxMacMakePCStringFromMac( m_stringArray
[N
] ) ;
554 return m_stringArray
[N
] ;
557 void wxListBox::DoInsertItems(const wxArrayString
& items
, int pos
)
559 wxCHECK_RET( pos
>= 0 && pos
<= m_noItems
,
560 wxT("invalid index in wxListBox::InsertItems") );
562 int nItems
= items
.GetCount();
564 for ( int i
= 0 ; i
< nItems
; i
++ )
566 m_stringArray
.Insert( items
[i
] , pos
+ i
) ;
567 m_dataArray
.Insert( NULL
, pos
+ i
) ;
568 MacInsert( pos
+ i
, items
[i
] ) ;
574 void wxListBox::SetString(int N
, const wxString
& s
)
577 if( wxApp::s_macDefaultEncodingIsPC
)
579 str
= wxMacMakeMacStringFromPC( s
) ;
583 m_stringArray
[N
] = str
;
587 wxSize
wxListBox::DoGetBestSize() const
589 return wxSize(100, 100);
592 int wxListBox::GetCount() const
597 void wxListBox::SetupColours()
599 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
600 SetForegroundColour(GetParent()->GetForegroundColour());
603 void wxListBox::Refresh(bool eraseBack
, const wxRect
*rect
)
605 wxControl::Refresh( eraseBack
, rect
) ;
606 // MacRedrawControl() ;
609 #if wxUSE_OWNER_DRAWN
611 class wxListBoxItem
: public wxOwnerDrawn
614 wxListBoxItem(const wxString
& str
= "");
617 wxListBoxItem::wxListBoxItem(const wxString
& str
) : wxOwnerDrawn(str
, FALSE
)
619 // no bitmaps/checkmarks
623 wxOwnerDrawn
*wxListBox::CreateItem(size_t n
)
625 return new wxListBoxItem();
628 #endif //USE_OWNER_DRAWN
630 // ============================================================================
631 // list box control implementation
632 // ============================================================================
634 void MacDrawStringCell(Rect
*cellRect
, Cell lCell
, ListHandle theList
, long refCon
)
637 // typecast our refCon
638 list
= (wxListBox
*)refCon
;
640 MoveTo(cellRect
->left
+ 4 , cellRect
->top
+ 10 );
641 const wxString text
= list
->m_stringArray
[lCell
.v
] ;
642 ::TextFont( kFontIDMonaco
) ;
645 DrawText(text
, 0 , text
.Length());
649 void wxListBox::MacDelete( int N
)
651 LDelRow( 1 , N
, (ListHandle
)m_macList
) ;
655 void wxListBox::MacInsert( int n
, const char * text
)
657 Cell cell
= { 0 , 0 } ;
659 LAddRow( 1 , cell
.v
, (ListHandle
)m_macList
) ;
660 // LSetCell(text, strlen(text), cell, m_macList);
664 void wxListBox::MacAppend( const char * text
)
666 Cell cell
= { 0 , 0 } ;
667 cell
.v
= (**(ListHandle
)m_macList
).dataBounds
.bottom
;
668 LAddRow( 1 , cell
.v
, (ListHandle
)m_macList
) ;
669 // LSetCell(text, strlen(text), cell, m_macList);
673 void wxListBox::MacClear()
675 LDelRow( (**(ListHandle
)m_macList
).dataBounds
.bottom
, 0 ,(ListHandle
) m_macList
) ;
679 void wxListBox::MacSetSelection( int n
, bool select
)
681 Cell cell
= { 0 , 0 } ;
682 if ( ! (m_windowStyle
& wxLB_MULTIPLE
) )
684 if ( LGetSelect( true , &cell
, (ListHandle
)m_macList
) )
686 LSetSelect( false , cell
, (ListHandle
)m_macList
) ;
691 LSetSelect( select
, cell
, (ListHandle
)m_macList
) ;
692 LAutoScroll( (ListHandle
)m_macList
) ;
696 bool wxListBox::MacIsSelected( int n
) const
698 Cell cell
= { 0 , 0 } ;
700 return LGetSelect( false , &cell
, (ListHandle
)m_macList
) ;
703 void wxListBox::MacDestroy()
705 // DisposeExtLDEFInfo( m_macList ) ;
708 int wxListBox::MacGetSelection() const
710 Cell cell
= { 0 , 0 } ;
711 if ( LGetSelect( true , &cell
, (ListHandle
)m_macList
) )
717 int wxListBox::MacGetSelections( wxArrayInt
& aSelections
) const
723 Cell cell
= { 0 , 0 } ;
726 while ( LGetSelect( true , &cell
,(ListHandle
) m_macList
) )
728 aSelections
.Add( cell
.v
) ;
735 void wxListBox::MacSet( int n
, const char * text
)
737 // our implementation does not store anything in the list
738 // so we just have to redraw
739 Cell cell
= { 0 , 0 } ;
741 // LSetCell(text, strlen(text), cell, m_macList);
745 void wxListBox::MacScrollTo( int n
)
747 // TODO implement scrolling
750 void wxListBox::OnSize( const wxSizeEvent
&event
)
755 GetListCellSize((ListHandle
)m_macList
, &pt
);
757 pt
= (**(ListHandle
)m_macList
).cellSize
;
759 pt
.h
= m_width
- 15 ;
760 LCellSize( pt
, (ListHandle
)m_macList
) ;
763 void wxListBox::MacHandleControlClick( WXWidget control
, wxInt16 controlpart
)
765 Boolean wasDoubleClick
= false ;
768 ::GetControlData( (ControlHandle
) m_macControl
, kControlNoPart
, kControlListBoxDoubleClickTag
, sizeof( wasDoubleClick
) , (char*) &wasDoubleClick
, &result
) ;
769 if ( !wasDoubleClick
)
779 void wxListBox::MacSetRedraw( bool doDraw
)
781 LSetDrawingMode( doDraw
, (ListHandle
)m_macList
) ;
785 void wxListBox::MacDoClick()
787 wxArrayInt aSelections
;
788 int n
, count
= GetSelections(aSelections
);
790 if ( count
== m_selectionPreImage
.GetCount() )
792 bool hasChanged
= false ;
793 for ( int i
= 0 ; i
< count
; ++i
)
795 if ( aSelections
[i
] != m_selectionPreImage
[i
] )
807 m_selectionPreImage
= aSelections
;
809 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_SELECTED
, m_windowId
);
810 event
.SetEventObject( this );
815 if ( HasClientObjectData() )
816 event
.SetClientObject( GetClientObject(n
) );
817 else if ( HasClientUntypedData() )
818 event
.SetClientData( GetClientData(n
) );
819 event
.SetString( GetString(n
) );
826 event
.m_commandInt
= n
;
828 GetEventHandler()->ProcessEvent(event
);
831 void wxListBox::MacDoDoubleClick()
833 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
, m_windowId
);
834 event
.SetEventObject( this );
835 GetEventHandler()->ProcessEvent(event
) ;