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
)
69 RgnHandle savedClipRegion
;
73 SetPort((**listHandle
).port
);
74 grafPtr
= (**listHandle
).port
;
75 // typecast our refCon
76 list
= (wxListBox
*) GetControlReference( (ControlHandle
) GetListRefCon(listHandle
) );
78 // Calculate the cell rect.
89 const wxString text
= list
->m_stringArray
[cell
.v
] ;
91 // Save the current clip region, and set the clip region to the area we are about
94 savedClipRegion
= NewRgn();
95 GetClip( savedClipRegion
);
98 EraseRect( drawRect
);
100 MoveTo(drawRect
->left
+ 4 , drawRect
->top
+ 10 );
101 ::TextFont( kFontIDMonaco
) ;
105 DrawText(text
, 0 , text
.Length());
106 // If the cell is hilited, do the hilite now. Paint the cell contents with the
107 // appropriate QuickDraw transform mode.
110 savedPenMode
= GetPortPenMode( (CGrafPtr
) grafPtr
);
111 SetPortPenMode( (CGrafPtr
)grafPtr
, hilitetransfermode
);
112 PaintRect( drawRect
);
113 SetPortPenMode( (CGrafPtr
)grafPtr
, savedPenMode
);
116 // Restore the saved clip region.
118 SetClip( savedClipRegion
);
119 DisposeRgn( savedClipRegion
);
124 // Hilite or unhilite the cell. Paint the cell contents with the
125 // appropriate QuickDraw transform mode.
128 savedPenMode
= GetPortPenMode( (CGrafPtr
)grafPtr
);
129 SetPortPenMode( (CGrafPtr
)grafPtr
, hilitetransfermode
);
130 PaintRect( drawRect
);
131 SetPortPenMode( (CGrafPtr
)grafPtr
, savedPenMode
);
139 extern "C" void MacDrawStringCell(Rect
*cellRect
, Cell lCell
, ListHandle theList
, long refCon
) ;
140 const short kwxMacListWithVerticalScrollbar
= 128 ;
141 const short kwxMacListItemHeight
= 14 ;
143 // ============================================================================
144 // list box control implementation
145 // ============================================================================
148 wxListBox::wxListBox()
155 static ListDefUPP macListDefUPP
= NULL
;
157 bool wxListBox::Create(wxWindow
*parent
, wxWindowID id
,
160 int n
, const wxString choices
[],
162 const wxValidator
& validator
,
163 const wxString
& name
)
165 m_noItems
= 0 ; // this will be increased by our append command
171 MacPreControlCreate( parent
, id
, "" , pos
, size
,style
, validator
, name
, &bounds
, title
) ;
174 listDef
.defType
= kListDefUserProcType
;
175 if ( macListDefUPP
== NULL
)
177 macListDefUPP
= NewListDefUPP( wxMacListDefinition
);
179 listDef
.u
.userProc
= macListDefUPP
;
185 CreateListBoxControl( MAC_WXHWND(parent
->MacGetRootWindow()), &bounds
, false, 0, 1, false, true,
186 kwxMacListItemHeight
, kwxMacListItemHeight
, false, &listDef
, (ControlRef
*)&m_macControl
);
188 GetControlData( (ControlHandle
) m_macControl
, kControlNoPart
, kControlListBoxListHandleTag
,
189 sizeof(ListHandle
), (Ptr
) &m_macList
, &asize
);
191 SetControlReference( (ControlHandle
) m_macControl
, (long) this);
192 SetControlVisibility( (ControlHandle
) m_macControl
, false, false);
197 wxStAppResource resload
;
198 m_macControl
= ::NewControl( MAC_WXHWND(parent
->MacGetRootWindow()) , &bounds
, title
, false ,
199 kwxMacListWithVerticalScrollbar
, 0 , 0,
200 kControlListBoxProc
, (long) this ) ;
201 ::GetControlData( (ControlHandle
) m_macControl
, kControlNoPart
, kControlListBoxListHandleTag
,
202 sizeof( ListHandle
) , (char*) &m_macList
, &result
) ;
204 HLock( (Handle
) m_macList
) ;
206 ldef
= (ldefHandle
) NewHandle( sizeof(ldefRec
) ) ;
207 if ( (**(ListHandle
)m_macList
).listDefProc
!= NULL
)
209 (**ldef
).instruction
= 0x4EF9; /* JMP instruction */
210 (**ldef
).function
= (void(*)()) listDef
.u
.userProc
;
211 (**(ListHandle
)m_macList
).listDefProc
= (Handle
) ldef
;
214 Point pt
= (**(ListHandle
)m_macList
).cellSize
;
215 pt
.v
= kwxMacListItemHeight
;
216 LCellSize( pt
, (ListHandle
)m_macList
) ;
217 LAddColumn( 1 , 0 , (ListHandle
)m_macList
) ;
219 OptionBits options
= 0;
220 if ( style
& wxLB_MULTIPLE
)
222 options
+= lNoExtend
;
224 else if ( style
& wxLB_EXTENDED
)
226 options
+= lExtendDrag
;
232 SetListSelectionFlags((ListHandle
)m_macList
, options
);
234 MacPostControlCreate() ;
236 for ( int i
= 0 ; i
< n
; i
++ )
238 Append( choices
[i
] ) ;
241 LSetDrawingMode( true , (ListHandle
)m_macList
) ;
246 wxListBox::~wxListBox()
252 DisposeHandle( (**(ListHandle
)m_macList
).listDefProc
) ;
253 (**(ListHandle
)m_macList
).listDefProc
= NULL
;
259 void wxListBox::Free()
261 #if wxUSE_OWNER_DRAWN
262 if ( m_windowStyle
& wxLB_OWNERDRAW
)
264 size_t uiCount
= m_aItems
.Count();
265 while ( uiCount
-- != 0 ) {
266 delete m_aItems
[uiCount
];
272 #endif // wxUSE_OWNER_DRAWN
273 if ( HasClientObjectData() )
275 for ( size_t n
= 0; n
< (size_t)m_noItems
; n
++ )
277 delete GetClientObject(n
);
282 void wxListBox::DoSetSize(int x
, int y
,
283 int width
, int height
,
286 wxControl::DoSetSize( x
, y
, width
, height
, sizeFlags
) ;
289 GetControlBounds( (ControlHandle
) m_macControl
, &bounds
) ;
290 ControlRef control
= GetListVerticalScrollBar( (ListHandle
)m_macList
) ;
294 GetControlBounds( control
, &scrollbounds
) ;
295 if( scrollbounds
.right
!= bounds
.right
+ 1 )
297 UMAMoveControl( control
, bounds
.right
- (scrollbounds
.right
- scrollbounds
.left
) + 1 ,
303 void wxListBox::DoSetFirstItem(int N
)
308 void wxListBox::Delete(int N
)
310 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
311 wxT("invalid index in wxListBox::Delete") );
313 #if wxUSE_OWNER_DRAWN
315 m_aItems
.RemoveAt(N
);
316 #else // !wxUSE_OWNER_DRAWN
317 if ( HasClientObjectData() )
319 delete GetClientObject(N
);
321 #endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
322 m_stringArray
.Remove( N
) ;
323 m_dataArray
.RemoveAt( N
) ;
329 int wxListBox::DoAppend(const wxString
& item
)
331 int index
= m_noItems
;
332 if( wxApp::s_macDefaultEncodingIsPC
)
334 m_stringArray
.Add( wxMacMakeMacStringFromPC( item
) ) ;
335 m_dataArray
.Add( NULL
);
338 m_stringArray
.Add( item
) ;
339 m_dataArray
.Add( NULL
);
342 DoSetItemClientData( index
, NULL
) ;
348 void wxListBox::DoSetItems(const wxArrayString
& choices
, void** clientData
)
350 MacSetRedraw( false ) ;
352 int n
= choices
.GetCount();
354 for( int i
= 0 ; i
< n
; ++i
)
358 #if wxUSE_OWNER_DRAWN
359 wxASSERT_MSG(clientData
[i
] == NULL
,
360 wxT("Can't use client data with owner-drawn listboxes"));
361 #else // !wxUSE_OWNER_DRAWN
362 Append( choices
[i
] , clientData
[i
] ) ;
366 Append( choices
[i
] ) ;
369 #if wxUSE_OWNER_DRAWN
370 if ( m_windowStyle
& wxLB_OWNERDRAW
) {
371 // first delete old items
372 size_t ui
= m_aItems
.Count();
373 while ( ui
-- != 0 ) {
378 // then create new ones
379 for ( ui
= 0; ui
< (size_t)m_noItems
; ui
++ ) {
380 wxOwnerDrawn
*pNewItem
= CreateItem(ui
);
381 pNewItem
->SetName(choices
[ui
]);
382 m_aItems
.Add(pNewItem
);
385 #endif // wxUSE_OWNER_DRAWN
386 MacSetRedraw( true ) ;
389 bool wxListBox::HasMultipleSelection() const
391 return (m_windowStyle
& wxLB_MULTIPLE
) || (m_windowStyle
& wxLB_EXTENDED
);
394 int wxListBox::FindString(const wxString
& st
) const
397 if( wxApp::s_macDefaultEncodingIsPC
)
399 s
= wxMacMakeMacStringFromPC( st
) ;
404 if ( s
.Right(1) == "*" )
406 wxString search
= s
.Left( s
.Length() - 1 ) ;
407 int len
= search
.Length() ;
411 c2pstrcpy( (StringPtr
) s2
, search
.c_str() ) ;
413 strcpy( (char *) s2
, search
.c_str() ) ;
414 c2pstr( (char *) s2
) ;
417 for ( int i
= 0 ; i
< m_noItems
; ++ i
)
420 c2pstrcpy( (StringPtr
) s1
, m_stringArray
[i
].Left( len
).c_str() ) ;
422 strcpy( (char *) s1
, m_stringArray
[i
].Left( len
).c_str() ) ;
423 c2pstr( (char *) s1
) ;
425 if ( EqualString( s1
, s2
, false , false ) )
428 if ( s
.Left(1) == "*" && s
.Length() > 1 )
431 for ( int i
= 0 ; i
< m_noItems
; ++i
)
433 if ( GetString(i
).Lower().Matches(s
) )
444 c2pstrcpy( (StringPtr
) s2
, s
.c_str() ) ;
446 strcpy( (char *) s2
, s
.c_str() ) ;
447 c2pstr( (char *) s2
) ;
450 for ( int i
= 0 ; i
< m_noItems
; ++ i
)
453 c2pstrcpy( (StringPtr
) s1
, m_stringArray
[i
].c_str() ) ;
455 strcpy( (char *) s1
, m_stringArray
[i
].c_str() ) ;
456 c2pstr( (char *) s1
) ;
458 if ( EqualString( s1
, s2
, false , false ) )
465 void wxListBox::Clear()
469 m_stringArray
.Empty() ;
470 m_dataArray
.Empty() ;
474 void wxListBox::SetSelection(int N
, bool select
)
476 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
477 "invalid index in wxListBox::SetSelection" );
478 MacSetSelection( N
, select
) ;
479 GetSelections( m_selectionPreImage
) ;
482 bool wxListBox::IsSelected(int N
) const
484 wxCHECK_MSG( N
>= 0 && N
< m_noItems
, FALSE
,
485 "invalid index in wxListBox::Selected" );
487 return MacIsSelected( N
) ;
490 void *wxListBox::DoGetItemClientData(int N
) const
492 wxCHECK_MSG( N
>= 0 && N
< m_noItems
, NULL
,
493 wxT("invalid index in wxListBox::GetClientData"));
495 return (void *)m_dataArray
[N
];
498 wxClientData
*wxListBox::DoGetItemClientObject(int N
) const
500 return (wxClientData
*) DoGetItemClientData( N
) ;
503 void wxListBox::DoSetItemClientData(int N
, void *Client_data
)
505 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
506 "invalid index in wxListBox::SetClientData" );
508 #if wxUSE_OWNER_DRAWN
509 if ( m_windowStyle
& wxLB_OWNERDRAW
)
511 // client data must be pointer to wxOwnerDrawn, otherwise we would crash
512 // in OnMeasure/OnDraw.
513 wxFAIL_MSG(wxT("Can't use client data with owner-drawn listboxes"));
515 #endif // wxUSE_OWNER_DRAWN
516 wxASSERT_MSG( m_dataArray
.GetCount() >= N
, "invalid client_data array" ) ;
518 if ( m_dataArray
.GetCount() > N
)
520 m_dataArray
[N
] = (char*) Client_data
;
524 m_dataArray
.Add( (char*) Client_data
) ;
528 void wxListBox::DoSetItemClientObject(int n
, wxClientData
* clientData
)
530 DoSetItemClientData(n
, clientData
);
533 // Return number of selections and an array of selected integers
534 int wxListBox::GetSelections(wxArrayInt
& aSelections
) const
536 return MacGetSelections( aSelections
) ;
539 // Get single selection, for single choice list items
540 int wxListBox::GetSelection() const
542 return MacGetSelection() ;
545 // Find string for position
546 wxString
wxListBox::GetString(int N
) const
548 if( wxApp::s_macDefaultEncodingIsPC
)
550 return wxMacMakePCStringFromMac( m_stringArray
[N
] ) ;
553 return m_stringArray
[N
] ;
556 void wxListBox::DoInsertItems(const wxArrayString
& items
, int pos
)
558 wxCHECK_RET( pos
>= 0 && pos
<= m_noItems
,
559 wxT("invalid index in wxListBox::InsertItems") );
561 int nItems
= items
.GetCount();
563 for ( int i
= 0 ; i
< nItems
; i
++ )
565 m_stringArray
.Insert( items
[i
] , pos
+ i
) ;
566 m_dataArray
.Insert( NULL
, pos
+ i
) ;
567 MacInsert( pos
+ i
, items
[i
] ) ;
573 void wxListBox::SetString(int N
, const wxString
& s
)
576 if( wxApp::s_macDefaultEncodingIsPC
)
578 str
= wxMacMakeMacStringFromPC( s
) ;
582 m_stringArray
[N
] = str
;
586 wxSize
wxListBox::DoGetBestSize() const
588 return wxSize(100, 100);
591 int wxListBox::GetCount() const
596 void wxListBox::SetupColours()
598 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
599 SetForegroundColour(GetParent()->GetForegroundColour());
602 void wxListBox::Refresh(bool eraseBack
, const wxRect
*rect
)
604 wxControl::Refresh( eraseBack
, rect
) ;
605 // MacRedrawControl() ;
608 #if wxUSE_OWNER_DRAWN
610 class wxListBoxItem
: public wxOwnerDrawn
613 wxListBoxItem(const wxString
& str
= "");
616 wxListBoxItem::wxListBoxItem(const wxString
& str
) : wxOwnerDrawn(str
, FALSE
)
618 // no bitmaps/checkmarks
622 wxOwnerDrawn
*wxListBox::CreateItem(size_t n
)
624 return new wxListBoxItem();
627 #endif //USE_OWNER_DRAWN
629 // ============================================================================
630 // list box control implementation
631 // ============================================================================
633 void MacDrawStringCell(Rect
*cellRect
, Cell lCell
, ListHandle theList
, long refCon
)
636 // typecast our refCon
637 list
= (wxListBox
*)refCon
;
639 MoveTo(cellRect
->left
+ 4 , cellRect
->top
+ 10 );
640 const wxString text
= list
->m_stringArray
[lCell
.v
] ;
641 ::TextFont( kFontIDMonaco
) ;
644 DrawText(text
, 0 , text
.Length());
648 void wxListBox::MacDelete( int N
)
650 LDelRow( 1 , N
, (ListHandle
)m_macList
) ;
654 void wxListBox::MacInsert( int n
, const char * text
)
656 Cell cell
= { 0 , 0 } ;
658 LAddRow( 1 , cell
.v
, (ListHandle
)m_macList
) ;
659 // LSetCell(text, strlen(text), cell, m_macList);
663 void wxListBox::MacAppend( const char * text
)
665 Cell cell
= { 0 , 0 } ;
666 cell
.v
= (**(ListHandle
)m_macList
).dataBounds
.bottom
;
667 LAddRow( 1 , cell
.v
, (ListHandle
)m_macList
) ;
668 // LSetCell(text, strlen(text), cell, m_macList);
672 void wxListBox::MacClear()
674 LDelRow( (**(ListHandle
)m_macList
).dataBounds
.bottom
, 0 ,(ListHandle
) m_macList
) ;
678 void wxListBox::MacSetSelection( int n
, bool select
)
680 Cell cell
= { 0 , 0 } ;
681 if ( ! (m_windowStyle
& wxLB_MULTIPLE
) )
683 if ( LGetSelect( true , &cell
, (ListHandle
)m_macList
) )
685 LSetSelect( false , cell
, (ListHandle
)m_macList
) ;
690 LSetSelect( select
, cell
, (ListHandle
)m_macList
) ;
691 LAutoScroll( (ListHandle
)m_macList
) ;
695 bool wxListBox::MacIsSelected( int n
) const
697 Cell cell
= { 0 , 0 } ;
699 return LGetSelect( false , &cell
, (ListHandle
)m_macList
) ;
702 void wxListBox::MacDestroy()
704 // DisposeExtLDEFInfo( m_macList ) ;
707 int wxListBox::MacGetSelection() const
709 Cell cell
= { 0 , 0 } ;
710 if ( LGetSelect( true , &cell
, (ListHandle
)m_macList
) )
716 int wxListBox::MacGetSelections( wxArrayInt
& aSelections
) const
722 Cell cell
= { 0 , 0 } ;
725 while ( LGetSelect( true , &cell
,(ListHandle
) m_macList
) )
727 aSelections
.Add( cell
.v
) ;
734 void wxListBox::MacSet( int n
, const char * text
)
736 // our implementation does not store anything in the list
737 // so we just have to redraw
738 Cell cell
= { 0 , 0 } ;
740 // LSetCell(text, strlen(text), cell, m_macList);
744 void wxListBox::MacScrollTo( int n
)
746 // TODO implement scrolling
749 void wxListBox::OnSize( const wxSizeEvent
&event
)
754 GetListCellSize((ListHandle
)m_macList
, &pt
);
756 pt
= (**(ListHandle
)m_macList
).cellSize
;
758 pt
.h
= m_width
- 15 ;
759 LCellSize( pt
, (ListHandle
)m_macList
) ;
762 void wxListBox::MacHandleControlClick( WXWidget control
, wxInt16 controlpart
)
764 Boolean wasDoubleClick
= false ;
767 ::GetControlData( (ControlHandle
) m_macControl
, kControlNoPart
, kControlListBoxDoubleClickTag
, sizeof( wasDoubleClick
) , (char*) &wasDoubleClick
, &result
) ;
768 if ( !wasDoubleClick
)
778 void wxListBox::MacSetRedraw( bool doDraw
)
780 LSetDrawingMode( doDraw
, (ListHandle
)m_macList
) ;
784 void wxListBox::MacDoClick()
786 wxArrayInt aSelections
;
787 int n
, count
= GetSelections(aSelections
);
789 if ( count
== m_selectionPreImage
.GetCount() )
791 bool hasChanged
= false ;
792 for ( int i
= 0 ; i
< count
; ++i
)
794 if ( aSelections
[i
] != m_selectionPreImage
[i
] )
806 m_selectionPreImage
= aSelections
;
808 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_SELECTED
, m_windowId
);
809 event
.SetEventObject( this );
814 if ( HasClientObjectData() )
815 event
.SetClientObject( GetClientObject(n
) );
816 else if ( HasClientUntypedData() )
817 event
.SetClientData( GetClientData(n
) );
818 event
.SetString( GetString(n
) );
825 event
.m_commandInt
= n
;
827 GetEventHandler()->ProcessEvent(event
);
830 void wxListBox::MacDoDoubleClick()
832 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
, m_windowId
);
833 event
.SetEventObject( this );
834 GetEventHandler()->ProcessEvent(event
) ;