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"
37 unsigned short instruction
;
39 } ldefRec
, *ldefPtr
, **ldefHandle
;
43 static pascal void wxMacListDefinition( short message
, Boolean isSelected
, Rect
*drawRect
,
44 Cell cell
, short dataOffset
, short dataLength
,
45 ListHandle listHandle
) ;
48 static pascal void wxMacListDefinition( short message
, Boolean isSelected
, Rect
*drawRect
,
49 Cell cell
, short dataOffset
, short dataLength
,
50 ListHandle listHandle
)
55 RgnHandle savedClipRegion
;
59 SetPort((**listHandle
).port
);
60 grafPtr
= (**listHandle
).port
;
61 // typecast our refCon
62 list
= (wxListBox
*) GetControlReference( (ControlHandle
) GetListRefCon(listHandle
) );
64 // Calculate the cell rect.
75 const wxString text
= list
->m_stringArray
[cell
.v
] ;
77 // Save the current clip region, and set the clip region to the area we are about
80 savedClipRegion
= NewRgn();
81 GetClip( savedClipRegion
);
84 EraseRect( drawRect
);
86 MoveTo(drawRect
->left
+ 4 , drawRect
->top
+ 10 );
87 ::TextFont( kFontIDMonaco
) ;
91 DrawText(text
, 0 , text
.Length());
92 // If the cell is hilited, do the hilite now. Paint the cell contents with the
93 // appropriate QuickDraw transform mode.
96 savedPenMode
= GetPortPenMode( grafPtr
);
97 SetPortPenMode( grafPtr
, hilitetransfermode
);
98 PaintRect( drawRect
);
99 SetPortPenMode( grafPtr
, savedPenMode
);
102 // Restore the saved clip region.
104 SetClip( savedClipRegion
);
105 DisposeRgn( savedClipRegion
);
110 // Hilite or unhilite the cell. Paint the cell contents with the
111 // appropriate QuickDraw transform mode.
114 savedPenMode
= GetPortPenMode( grafPtr
);
115 SetPortPenMode( grafPtr
, hilitetransfermode
);
116 PaintRect( drawRect
);
117 SetPortPenMode( grafPtr
, savedPenMode
);
125 extern "C" void MacDrawStringCell(Rect
*cellRect
, Cell lCell
, ListHandle theList
, long refCon
) ;
126 const short kwxMacListWithVerticalScrollbar
= 128 ;
128 // ============================================================================
129 // list box control implementation
130 // ============================================================================
133 wxListBox::wxListBox()
140 static ListDefUPP macListDefUPP
= NULL
;
142 bool wxListBox::Create(wxWindow
*parent
, wxWindowID id
,
145 int n
, const wxString choices
[],
147 const wxValidator
& validator
,
148 const wxString
& name
)
150 m_noItems
= 0 ; // this will be increased by our append command
156 MacPreControlCreate( parent
, id
, "" , pos
, size
,style
, validator
, name
, &bounds
, title
) ;
159 listDef
.defType
= kListDefUserProcType
;
160 if ( macListDefUPP
== NULL
)
162 macListDefUPP
= NewListDefUPP( wxMacListDefinition
);
164 listDef
.u
.userProc
= macListDefUPP
;
169 CreateListBoxControl( parent
->MacGetRootWindow(), &bounds
, false, 0, 1, false, true,
170 14, 14, false, &listDef
, &m_macControl
);
172 GetControlData(m_macControl
, kControlNoPart
, kControlListBoxListHandleTag
,
173 sizeof(ListHandle
), (Ptr
) &m_macList
, &asize
);
175 SetControlReference(m_macControl
, (long) this);
176 SetControlVisibility(m_macControl
, false, false);
181 m_macControl
= ::NewControl( parent
->MacGetRootWindow() , &bounds
, title
, false ,
182 kwxMacListWithVerticalScrollbar
, 0 , 0,
183 kControlListBoxProc
, (long) this ) ;
184 ::GetControlData( m_macControl
, kControlNoPart
, kControlListBoxListHandleTag
,
185 sizeof( ListHandle
) , (char*) &m_macList
, &result
) ;
187 HLock( (Handle
) m_macList
) ;
189 ldef
= (ldefHandle
) NewHandle( sizeof(ldefRec
) ) ;
190 if ( (**m_macList
).listDefProc
!= NULL
)
192 (**ldef
).instruction
= 0x4EF9; /* JMP instruction */
193 (**ldef
).function
= (void(*)()) listDef
.u
.userProc
;
194 (**m_macList
).listDefProc
= (Handle
) ldef
;
197 Point pt
= (**m_macList
).cellSize
;
199 LCellSize( pt
, m_macList
) ;
201 LAddColumn( 1 , 0 , m_macList
) ;
203 OptionBits options
= 0;
204 if ( style
& wxLB_MULTIPLE
)
206 options
+= lNoExtend
;
208 else if ( style
& wxLB_EXTENDED
)
210 options
+= lExtendDrag
;
216 SetListSelectionFlags(m_macList
, options
);
218 MacPostControlCreate() ;
220 for ( int i
= 0 ; i
< n
; i
++ )
222 Append( choices
[i
] ) ;
225 LSetDrawingMode( true , m_macList
) ;
230 wxListBox::~wxListBox()
236 DisposeHandle( (**m_macList
).listDefProc
) ;
237 (**m_macList
).listDefProc
= NULL
;
243 void wxListBox::Free()
245 #if wxUSE_OWNER_DRAWN
246 if ( m_windowStyle
& wxLB_OWNERDRAW
)
248 size_t uiCount
= m_aItems
.Count();
249 while ( uiCount
-- != 0 ) {
250 delete m_aItems
[uiCount
];
256 #endif // wxUSE_OWNER_DRAWN
257 if ( HasClientObjectData() )
259 for ( size_t n
= 0; n
< (size_t)m_noItems
; n
++ )
261 delete GetClientObject(n
);
266 void wxListBox::DoSetSize(int x
, int y
,
267 int width
, int height
,
270 wxControl::DoSetSize( x
, y
, width
, height
, sizeFlags
) ;
273 GetControlBounds( m_macControl
, &bounds
) ;
274 ControlRef control
= GetListVerticalScrollBar( m_macList
) ;
278 GetControlBounds( control
, &scrollbounds
) ;
279 if( scrollbounds
.right
!= bounds
.right
+ 1 )
281 UMAMoveControl( control
, bounds
.right
- (scrollbounds
.right
- scrollbounds
.left
) + 1 ,
287 void wxListBox::DoSetFirstItem(int N
)
292 void wxListBox::Delete(int N
)
294 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
295 wxT("invalid index in wxListBox::Delete") );
297 #if wxUSE_OWNER_DRAWN
299 m_aItems
.RemoveAt(N
);
300 #else // !wxUSE_OWNER_DRAWN
301 if ( HasClientObjectData() )
303 delete GetClientObject(N
);
305 #endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
306 m_stringArray
.Remove( N
) ;
307 m_dataArray
.RemoveAt( N
) ;
313 int wxListBox::DoAppend(const wxString
& item
)
315 int index
= m_noItems
;
316 if( wxApp::s_macDefaultEncodingIsPC
)
318 m_stringArray
.Add( wxMacMakeMacStringFromPC( item
) ) ;
319 m_dataArray
.Add( NULL
);
322 m_stringArray
.Add( item
) ;
323 m_dataArray
.Add( NULL
);
326 DoSetItemClientData( index
, NULL
) ;
332 void wxListBox::DoSetItems(const wxArrayString
& choices
, void** clientData
)
334 MacSetRedraw( false ) ;
336 int n
= choices
.GetCount();
338 for( int i
= 0 ; i
< n
; ++i
)
342 #if wxUSE_OWNER_DRAWN
343 wxASSERT_MSG(clientData
[i
] == NULL
,
344 wxT("Can't use client data with owner-drawn listboxes"));
345 #else // !wxUSE_OWNER_DRAWN
346 Append( choices
[i
] , clientData
[i
] ) ;
350 Append( choices
[i
] ) ;
353 #if wxUSE_OWNER_DRAWN
354 if ( m_windowStyle
& wxLB_OWNERDRAW
) {
355 // first delete old items
356 size_t ui
= m_aItems
.Count();
357 while ( ui
-- != 0 ) {
362 // then create new ones
363 for ( ui
= 0; ui
< (size_t)m_noItems
; ui
++ ) {
364 wxOwnerDrawn
*pNewItem
= CreateItem(ui
);
365 pNewItem
->SetName(choices
[ui
]);
366 m_aItems
.Add(pNewItem
);
369 #endif // wxUSE_OWNER_DRAWN
370 MacSetRedraw( true ) ;
373 bool wxListBox::HasMultipleSelection() const
375 return (m_windowStyle
& wxLB_MULTIPLE
) || (m_windowStyle
& wxLB_EXTENDED
);
378 int wxListBox::FindString(const wxString
& st
) const
381 if( wxApp::s_macDefaultEncodingIsPC
)
383 s
= wxMacMakeMacStringFromPC( st
) ;
388 if ( s
.Right(1) == "*" )
390 wxString search
= s
.Left( s
.Length() - 1 ) ;
391 int len
= search
.Length() ;
395 c2pstrcpy( (StringPtr
) s2
, search
.c_str() ) ;
397 strcpy( (char *) s2
, search
.c_str() ) ;
398 c2pstr( (char *) s2
) ;
401 for ( int i
= 0 ; i
< m_noItems
; ++ i
)
404 c2pstrcpy( (StringPtr
) s1
, m_stringArray
[i
].Left( len
).c_str() ) ;
406 strcpy( (char *) s1
, m_stringArray
[i
].Left( len
).c_str() ) ;
407 c2pstr( (char *) s1
) ;
409 if ( EqualString( s1
, s2
, false , false ) )
412 if ( s
.Left(1) == "*" && s
.Length() > 1 )
415 for ( int i
= 0 ; i
< m_noItems
; ++i
)
417 if ( GetString(i
).Lower().Matches(s
) )
428 c2pstrcpy( (StringPtr
) s2
, s
.c_str() ) ;
430 strcpy( (char *) s2
, s
.c_str() ) ;
431 c2pstr( (char *) s2
) ;
434 for ( int i
= 0 ; i
< m_noItems
; ++ i
)
437 c2pstrcpy( (StringPtr
) s1
, m_stringArray
[i
].c_str() ) ;
439 strcpy( (char *) s1
, m_stringArray
[i
].c_str() ) ;
440 c2pstr( (char *) s1
) ;
442 if ( EqualString( s1
, s2
, false , false ) )
449 void wxListBox::Clear()
453 m_stringArray
.Empty() ;
454 m_dataArray
.Empty() ;
458 void wxListBox::SetSelection(int N
, bool select
)
460 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
461 "invalid index in wxListBox::SetSelection" );
462 MacSetSelection( N
, select
) ;
463 GetSelections( m_selectionPreImage
) ;
466 bool wxListBox::IsSelected(int N
) const
468 wxCHECK_MSG( N
>= 0 && N
< m_noItems
, FALSE
,
469 "invalid index in wxListBox::Selected" );
471 return MacIsSelected( N
) ;
474 void *wxListBox::DoGetItemClientData(int N
) const
476 wxCHECK_MSG( N
>= 0 && N
< m_noItems
, NULL
,
477 wxT("invalid index in wxListBox::GetClientData"));
479 return (void *)m_dataArray
[N
];
482 wxClientData
*wxListBox::DoGetItemClientObject(int N
) const
484 return (wxClientData
*) DoGetItemClientData( N
) ;
487 void wxListBox::DoSetItemClientData(int N
, void *Client_data
)
489 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
490 "invalid index in wxListBox::SetClientData" );
492 #if wxUSE_OWNER_DRAWN
493 if ( m_windowStyle
& wxLB_OWNERDRAW
)
495 // client data must be pointer to wxOwnerDrawn, otherwise we would crash
496 // in OnMeasure/OnDraw.
497 wxFAIL_MSG(wxT("Can't use client data with owner-drawn listboxes"));
499 #endif // wxUSE_OWNER_DRAWN
500 wxASSERT_MSG( m_dataArray
.GetCount() >= N
, "invalid client_data array" ) ;
502 if ( m_dataArray
.GetCount() > N
)
504 m_dataArray
[N
] = (char*) Client_data
;
508 m_dataArray
.Add( (char*) Client_data
) ;
512 void wxListBox::DoSetItemClientObject(int n
, wxClientData
* clientData
)
514 DoSetItemClientData(n
, clientData
);
517 // Return number of selections and an array of selected integers
518 int wxListBox::GetSelections(wxArrayInt
& aSelections
) const
520 return MacGetSelections( aSelections
) ;
523 // Get single selection, for single choice list items
524 int wxListBox::GetSelection() const
526 return MacGetSelection() ;
529 // Find string for position
530 wxString
wxListBox::GetString(int N
) const
532 if( wxApp::s_macDefaultEncodingIsPC
)
534 return wxMacMakePCStringFromMac( m_stringArray
[N
] ) ;
537 return m_stringArray
[N
] ;
540 void wxListBox::DoInsertItems(const wxArrayString
& items
, int pos
)
542 wxCHECK_RET( pos
>= 0 && pos
<= m_noItems
,
543 wxT("invalid index in wxListBox::InsertItems") );
545 int nItems
= items
.GetCount();
547 for ( int i
= 0 ; i
< nItems
; i
++ )
549 m_stringArray
.Insert( items
[i
] , pos
+ i
) ;
550 m_dataArray
.Insert( NULL
, pos
+ i
) ;
551 MacInsert( pos
+ i
, items
[i
] ) ;
557 void wxListBox::SetString(int N
, const wxString
& s
)
560 if( wxApp::s_macDefaultEncodingIsPC
)
562 str
= wxMacMakeMacStringFromPC( s
) ;
566 m_stringArray
[N
] = str
;
570 wxSize
wxListBox::DoGetBestSize() const
572 return wxSize(100, 100);
575 int wxListBox::GetCount() const
580 void wxListBox::SetupColours()
582 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
583 SetForegroundColour(GetParent()->GetForegroundColour());
586 void wxListBox::Refresh(bool eraseBack
, const wxRect
*rect
)
589 WindowRef rootwindow
= MacGetRootWindow() ;
590 wxWindow
* wxrootwindow
= wxFindWinFromMacWindow( rootwindow
) ;
591 wxMacDrawingHelper
focus( wxrootwindow
);
593 UMADrawControl(m_macControl
);
596 #if wxUSE_OWNER_DRAWN
598 class wxListBoxItem
: public wxOwnerDrawn
601 wxListBoxItem(const wxString
& str
= "");
604 wxListBoxItem::wxListBoxItem(const wxString
& str
) : wxOwnerDrawn(str
, FALSE
)
606 // no bitmaps/checkmarks
610 wxOwnerDrawn
*wxListBox::CreateItem(size_t n
)
612 return new wxListBoxItem();
615 #endif //USE_OWNER_DRAWN
617 // ============================================================================
618 // list box control implementation
619 // ============================================================================
621 void MacDrawStringCell(Rect
*cellRect
, Cell lCell
, ListHandle theList
, long refCon
)
624 // typecast our refCon
625 list
= (wxListBox
*)refCon
;
627 MoveTo(cellRect
->left
+ 4 , cellRect
->top
+ 10 );
628 const wxString text
= list
->m_stringArray
[lCell
.v
] ;
629 ::TextFont( kFontIDMonaco
) ;
632 DrawText(text
, 0 , text
.Length());
636 void wxListBox::MacDelete( int N
)
638 LDelRow( 1 , N
, m_macList
) ;
642 void wxListBox::MacInsert( int n
, const char * text
)
644 Cell cell
= { 0 , 0 } ;
646 LAddRow( 1 , cell
.v
, m_macList
) ;
647 // LSetCell(text, strlen(text), cell, m_macList);
651 void wxListBox::MacAppend( const char * text
)
653 Cell cell
= { 0 , 0 } ;
654 cell
.v
= (**m_macList
).dataBounds
.bottom
;
655 LAddRow( 1 , cell
.v
, m_macList
) ;
656 // LSetCell(text, strlen(text), cell, m_macList);
660 void wxListBox::MacClear()
662 LDelRow( (**m_macList
).dataBounds
.bottom
, 0 , m_macList
) ;
666 void wxListBox::MacSetSelection( int n
, bool select
)
668 Cell cell
= { 0 , 0 } ;
669 if ( ! (m_windowStyle
& wxLB_MULTIPLE
) )
671 if ( LGetSelect( true , &cell
, m_macList
) )
673 LSetSelect( false , cell
, m_macList
) ;
678 LSetSelect( select
, cell
, m_macList
) ;
679 LAutoScroll( m_macList
) ;
683 bool wxListBox::MacIsSelected( int n
) const
685 Cell cell
= { 0 , 0 } ;
687 return LGetSelect( false , &cell
, m_macList
) ;
690 void wxListBox::MacDestroy()
692 // DisposeExtLDEFInfo( m_macList ) ;
695 int wxListBox::MacGetSelection() const
697 Cell cell
= { 0 , 0 } ;
698 if ( LGetSelect( true , &cell
, m_macList
) )
704 int wxListBox::MacGetSelections( wxArrayInt
& aSelections
) const
710 Cell cell
= { 0 , 0 } ;
713 while ( LGetSelect( true , &cell
, m_macList
) )
715 aSelections
.Add( cell
.v
) ;
722 void wxListBox::MacSet( int n
, const char * text
)
724 // our implementation does not store anything in the list
725 // so we just have to redraw
726 Cell cell
= { 0 , 0 } ;
728 // LSetCell(text, strlen(text), cell, m_macList);
732 void wxListBox::MacScrollTo( int n
)
734 // TODO implement scrolling
737 void wxListBox::OnSize( const wxSizeEvent
&event
)
742 GetListCellSize(m_macList
, &pt
);
744 pt
= (**m_macList
).cellSize
;
746 pt
.h
= m_width
- 15 ;
747 LCellSize( pt
, m_macList
) ;
750 void wxListBox::MacHandleControlClick( ControlHandle control
, SInt16 controlpart
)
752 Boolean wasDoubleClick
= false ;
755 ::GetControlData( m_macControl
, kControlNoPart
, kControlListBoxDoubleClickTag
, sizeof( wasDoubleClick
) , (char*) &wasDoubleClick
, &result
) ;
756 if ( !wasDoubleClick
)
766 void wxListBox::MacSetRedraw( bool doDraw
)
768 LSetDrawingMode( doDraw
, m_macList
) ;
772 void wxListBox::MacDoClick()
774 wxArrayInt aSelections
;
775 int n
, count
= GetSelections(aSelections
);
777 if ( count
== m_selectionPreImage
.GetCount() )
779 bool hasChanged
= false ;
780 for ( int i
= 0 ; i
< count
; ++i
)
782 if ( aSelections
[i
] != m_selectionPreImage
[i
] )
794 m_selectionPreImage
= aSelections
;
796 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_SELECTED
, m_windowId
);
797 event
.SetEventObject( this );
802 if ( HasClientObjectData() )
803 event
.SetClientObject( GetClientObject(n
) );
804 else if ( HasClientUntypedData() )
805 event
.SetClientData( GetClientData(n
) );
806 event
.SetString( GetString(n
) );
813 event
.m_commandInt
= n
;
815 GetEventHandler()->ProcessEvent(event
);
818 void wxListBox::MacDoDoubleClick()
820 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
, m_windowId
);
821 event
.SetEventObject( this );
822 GetEventHandler()->ProcessEvent(event
) ;