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
;
170 CreateListBoxControl( parent
->MacGetRootWindow(), &bounds
, false, 0, 1, false, true,
171 14, 14, false, &listDef
, &m_macControl
);
173 GetControlData(m_macControl
, kControlNoPart
, kControlListBoxListHandleTag
,
174 sizeof(ListHandle
), (Ptr
) &m_macList
, &asize
);
176 SetControlReference(m_macControl
, (long) this);
177 SetControlVisibility(m_macControl
, false, false);
183 m_macControl
= ::NewControl( parent
->MacGetRootWindow() , &bounds
, title
, false ,
184 kwxMacListWithVerticalScrollbar
, 0 , 0,
185 kControlListBoxProc
, (long) this ) ;
186 ::GetControlData( m_macControl
, kControlNoPart
, kControlListBoxListHandleTag
,
187 sizeof( ListHandle
) , (char*) &m_macList
, &result
) ;
189 HLock( (Handle
) m_macList
) ;
191 ldef
= (ldefHandle
) NewHandle( sizeof(ldefRec
) ) ;
192 if ( (**m_macList
).listDefProc
!= NULL
)
194 (**ldef
).instruction
= 0x4EF9; /* JMP instruction */
195 (**ldef
).function
= (void(*)()) listDef
.u
.userProc
;
196 (**m_macList
).listDefProc
= (Handle
) ldef
;
199 Point pt
= (**m_macList
).cellSize
;
201 LCellSize( pt
, m_macList
) ;
202 LAddColumn( 1 , 0 , m_macList
) ;
204 OptionBits options
= 0;
205 if ( style
& wxLB_MULTIPLE
)
207 options
+= lNoExtend
;
209 else if ( style
& wxLB_EXTENDED
)
211 options
+= lExtendDrag
;
217 SetListSelectionFlags(m_macList
, options
);
219 MacPostControlCreate() ;
221 for ( int i
= 0 ; i
< n
; i
++ )
223 Append( choices
[i
] ) ;
226 LSetDrawingMode( true , m_macList
) ;
231 wxListBox::~wxListBox()
237 DisposeHandle( (**m_macList
).listDefProc
) ;
238 (**m_macList
).listDefProc
= NULL
;
244 void wxListBox::Free()
246 #if wxUSE_OWNER_DRAWN
247 if ( m_windowStyle
& wxLB_OWNERDRAW
)
249 size_t uiCount
= m_aItems
.Count();
250 while ( uiCount
-- != 0 ) {
251 delete m_aItems
[uiCount
];
257 #endif // wxUSE_OWNER_DRAWN
258 if ( HasClientObjectData() )
260 for ( size_t n
= 0; n
< (size_t)m_noItems
; n
++ )
262 delete GetClientObject(n
);
267 void wxListBox::DoSetSize(int x
, int y
,
268 int width
, int height
,
271 wxControl::DoSetSize( x
, y
, width
, height
, sizeFlags
) ;
274 GetControlBounds( m_macControl
, &bounds
) ;
275 ControlRef control
= GetListVerticalScrollBar( m_macList
) ;
279 GetControlBounds( control
, &scrollbounds
) ;
280 if( scrollbounds
.right
!= bounds
.right
+ 1 )
282 UMAMoveControl( control
, bounds
.right
- (scrollbounds
.right
- scrollbounds
.left
) + 1 ,
288 void wxListBox::DoSetFirstItem(int N
)
293 void wxListBox::Delete(int N
)
295 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
296 wxT("invalid index in wxListBox::Delete") );
298 #if wxUSE_OWNER_DRAWN
300 m_aItems
.RemoveAt(N
);
301 #else // !wxUSE_OWNER_DRAWN
302 if ( HasClientObjectData() )
304 delete GetClientObject(N
);
306 #endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
307 m_stringArray
.Remove( N
) ;
308 m_dataArray
.RemoveAt( N
) ;
314 int wxListBox::DoAppend(const wxString
& item
)
316 int index
= m_noItems
;
317 if( wxApp::s_macDefaultEncodingIsPC
)
319 m_stringArray
.Add( wxMacMakeMacStringFromPC( item
) ) ;
320 m_dataArray
.Add( NULL
);
323 m_stringArray
.Add( item
) ;
324 m_dataArray
.Add( NULL
);
327 DoSetItemClientData( index
, NULL
) ;
333 void wxListBox::DoSetItems(const wxArrayString
& choices
, void** clientData
)
335 MacSetRedraw( false ) ;
337 int n
= choices
.GetCount();
339 for( int i
= 0 ; i
< n
; ++i
)
343 #if wxUSE_OWNER_DRAWN
344 wxASSERT_MSG(clientData
[i
] == NULL
,
345 wxT("Can't use client data with owner-drawn listboxes"));
346 #else // !wxUSE_OWNER_DRAWN
347 Append( choices
[i
] , clientData
[i
] ) ;
351 Append( choices
[i
] ) ;
354 #if wxUSE_OWNER_DRAWN
355 if ( m_windowStyle
& wxLB_OWNERDRAW
) {
356 // first delete old items
357 size_t ui
= m_aItems
.Count();
358 while ( ui
-- != 0 ) {
363 // then create new ones
364 for ( ui
= 0; ui
< (size_t)m_noItems
; ui
++ ) {
365 wxOwnerDrawn
*pNewItem
= CreateItem(ui
);
366 pNewItem
->SetName(choices
[ui
]);
367 m_aItems
.Add(pNewItem
);
370 #endif // wxUSE_OWNER_DRAWN
371 MacSetRedraw( true ) ;
374 bool wxListBox::HasMultipleSelection() const
376 return (m_windowStyle
& wxLB_MULTIPLE
) || (m_windowStyle
& wxLB_EXTENDED
);
379 int wxListBox::FindString(const wxString
& st
) const
382 if( wxApp::s_macDefaultEncodingIsPC
)
384 s
= wxMacMakeMacStringFromPC( st
) ;
389 if ( s
.Right(1) == "*" )
391 wxString search
= s
.Left( s
.Length() - 1 ) ;
392 int len
= search
.Length() ;
396 c2pstrcpy( (StringPtr
) s2
, search
.c_str() ) ;
398 strcpy( (char *) s2
, search
.c_str() ) ;
399 c2pstr( (char *) s2
) ;
402 for ( int i
= 0 ; i
< m_noItems
; ++ i
)
405 c2pstrcpy( (StringPtr
) s1
, m_stringArray
[i
].Left( len
).c_str() ) ;
407 strcpy( (char *) s1
, m_stringArray
[i
].Left( len
).c_str() ) ;
408 c2pstr( (char *) s1
) ;
410 if ( EqualString( s1
, s2
, false , false ) )
413 if ( s
.Left(1) == "*" && s
.Length() > 1 )
416 for ( int i
= 0 ; i
< m_noItems
; ++i
)
418 if ( GetString(i
).Lower().Matches(s
) )
429 c2pstrcpy( (StringPtr
) s2
, s
.c_str() ) ;
431 strcpy( (char *) s2
, s
.c_str() ) ;
432 c2pstr( (char *) s2
) ;
435 for ( int i
= 0 ; i
< m_noItems
; ++ i
)
438 c2pstrcpy( (StringPtr
) s1
, m_stringArray
[i
].c_str() ) ;
440 strcpy( (char *) s1
, m_stringArray
[i
].c_str() ) ;
441 c2pstr( (char *) s1
) ;
443 if ( EqualString( s1
, s2
, false , false ) )
450 void wxListBox::Clear()
454 m_stringArray
.Empty() ;
455 m_dataArray
.Empty() ;
459 void wxListBox::SetSelection(int N
, bool select
)
461 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
462 "invalid index in wxListBox::SetSelection" );
463 MacSetSelection( N
, select
) ;
464 GetSelections( m_selectionPreImage
) ;
467 bool wxListBox::IsSelected(int N
) const
469 wxCHECK_MSG( N
>= 0 && N
< m_noItems
, FALSE
,
470 "invalid index in wxListBox::Selected" );
472 return MacIsSelected( N
) ;
475 void *wxListBox::DoGetItemClientData(int N
) const
477 wxCHECK_MSG( N
>= 0 && N
< m_noItems
, NULL
,
478 wxT("invalid index in wxListBox::GetClientData"));
480 return (void *)m_dataArray
[N
];
483 wxClientData
*wxListBox::DoGetItemClientObject(int N
) const
485 return (wxClientData
*) DoGetItemClientData( N
) ;
488 void wxListBox::DoSetItemClientData(int N
, void *Client_data
)
490 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
491 "invalid index in wxListBox::SetClientData" );
493 #if wxUSE_OWNER_DRAWN
494 if ( m_windowStyle
& wxLB_OWNERDRAW
)
496 // client data must be pointer to wxOwnerDrawn, otherwise we would crash
497 // in OnMeasure/OnDraw.
498 wxFAIL_MSG(wxT("Can't use client data with owner-drawn listboxes"));
500 #endif // wxUSE_OWNER_DRAWN
501 wxASSERT_MSG( m_dataArray
.GetCount() >= N
, "invalid client_data array" ) ;
503 if ( m_dataArray
.GetCount() > N
)
505 m_dataArray
[N
] = (char*) Client_data
;
509 m_dataArray
.Add( (char*) Client_data
) ;
513 void wxListBox::DoSetItemClientObject(int n
, wxClientData
* clientData
)
515 DoSetItemClientData(n
, clientData
);
518 // Return number of selections and an array of selected integers
519 int wxListBox::GetSelections(wxArrayInt
& aSelections
) const
521 return MacGetSelections( aSelections
) ;
524 // Get single selection, for single choice list items
525 int wxListBox::GetSelection() const
527 return MacGetSelection() ;
530 // Find string for position
531 wxString
wxListBox::GetString(int N
) const
533 if( wxApp::s_macDefaultEncodingIsPC
)
535 return wxMacMakePCStringFromMac( m_stringArray
[N
] ) ;
538 return m_stringArray
[N
] ;
541 void wxListBox::DoInsertItems(const wxArrayString
& items
, int pos
)
543 wxCHECK_RET( pos
>= 0 && pos
<= m_noItems
,
544 wxT("invalid index in wxListBox::InsertItems") );
546 int nItems
= items
.GetCount();
548 for ( int i
= 0 ; i
< nItems
; i
++ )
550 m_stringArray
.Insert( items
[i
] , pos
+ i
) ;
551 m_dataArray
.Insert( NULL
, pos
+ i
) ;
552 MacInsert( pos
+ i
, items
[i
] ) ;
558 void wxListBox::SetString(int N
, const wxString
& s
)
561 if( wxApp::s_macDefaultEncodingIsPC
)
563 str
= wxMacMakeMacStringFromPC( s
) ;
567 m_stringArray
[N
] = str
;
571 wxSize
wxListBox::DoGetBestSize() const
573 return wxSize(100, 100);
576 int wxListBox::GetCount() const
581 void wxListBox::SetupColours()
583 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
584 SetForegroundColour(GetParent()->GetForegroundColour());
587 void wxListBox::Refresh(bool eraseBack
, const wxRect
*rect
)
589 wxControl::Refresh( eraseBack
, rect
) ;
590 // MacRedrawControl() ;
593 #if wxUSE_OWNER_DRAWN
595 class wxListBoxItem
: public wxOwnerDrawn
598 wxListBoxItem(const wxString
& str
= "");
601 wxListBoxItem::wxListBoxItem(const wxString
& str
) : wxOwnerDrawn(str
, FALSE
)
603 // no bitmaps/checkmarks
607 wxOwnerDrawn
*wxListBox::CreateItem(size_t n
)
609 return new wxListBoxItem();
612 #endif //USE_OWNER_DRAWN
614 // ============================================================================
615 // list box control implementation
616 // ============================================================================
618 void MacDrawStringCell(Rect
*cellRect
, Cell lCell
, ListHandle theList
, long refCon
)
621 // typecast our refCon
622 list
= (wxListBox
*)refCon
;
624 MoveTo(cellRect
->left
+ 4 , cellRect
->top
+ 10 );
625 const wxString text
= list
->m_stringArray
[lCell
.v
] ;
626 ::TextFont( kFontIDMonaco
) ;
629 DrawText(text
, 0 , text
.Length());
633 void wxListBox::MacDelete( int N
)
635 LDelRow( 1 , N
, m_macList
) ;
639 void wxListBox::MacInsert( int n
, const char * text
)
641 Cell cell
= { 0 , 0 } ;
643 LAddRow( 1 , cell
.v
, m_macList
) ;
644 // LSetCell(text, strlen(text), cell, m_macList);
648 void wxListBox::MacAppend( const char * text
)
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);
657 void wxListBox::MacClear()
659 LDelRow( (**m_macList
).dataBounds
.bottom
, 0 , m_macList
) ;
663 void wxListBox::MacSetSelection( int n
, bool select
)
665 Cell cell
= { 0 , 0 } ;
666 if ( ! (m_windowStyle
& wxLB_MULTIPLE
) )
668 if ( LGetSelect( true , &cell
, m_macList
) )
670 LSetSelect( false , cell
, m_macList
) ;
675 LSetSelect( select
, cell
, m_macList
) ;
676 LAutoScroll( m_macList
) ;
680 bool wxListBox::MacIsSelected( int n
) const
682 Cell cell
= { 0 , 0 } ;
684 return LGetSelect( false , &cell
, m_macList
) ;
687 void wxListBox::MacDestroy()
689 // DisposeExtLDEFInfo( m_macList ) ;
692 int wxListBox::MacGetSelection() const
694 Cell cell
= { 0 , 0 } ;
695 if ( LGetSelect( true , &cell
, m_macList
) )
701 int wxListBox::MacGetSelections( wxArrayInt
& aSelections
) const
707 Cell cell
= { 0 , 0 } ;
710 while ( LGetSelect( true , &cell
, m_macList
) )
712 aSelections
.Add( cell
.v
) ;
719 void wxListBox::MacSet( int n
, const char * text
)
721 // our implementation does not store anything in the list
722 // so we just have to redraw
723 Cell cell
= { 0 , 0 } ;
725 // LSetCell(text, strlen(text), cell, m_macList);
729 void wxListBox::MacScrollTo( int n
)
731 // TODO implement scrolling
734 void wxListBox::OnSize( const wxSizeEvent
&event
)
739 GetListCellSize(m_macList
, &pt
);
741 pt
= (**m_macList
).cellSize
;
743 pt
.h
= m_width
- 15 ;
744 LCellSize( pt
, m_macList
) ;
747 void wxListBox::MacHandleControlClick( ControlHandle control
, SInt16 controlpart
)
749 Boolean wasDoubleClick
= false ;
752 ::GetControlData( m_macControl
, kControlNoPart
, kControlListBoxDoubleClickTag
, sizeof( wasDoubleClick
) , (char*) &wasDoubleClick
, &result
) ;
753 if ( !wasDoubleClick
)
763 void wxListBox::MacSetRedraw( bool doDraw
)
765 LSetDrawingMode( doDraw
, m_macList
) ;
769 void wxListBox::MacDoClick()
771 wxArrayInt aSelections
;
772 int n
, count
= GetSelections(aSelections
);
774 if ( count
== m_selectionPreImage
.GetCount() )
776 bool hasChanged
= false ;
777 for ( int i
= 0 ; i
< count
; ++i
)
779 if ( aSelections
[i
] != m_selectionPreImage
[i
] )
791 m_selectionPreImage
= aSelections
;
793 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_SELECTED
, m_windowId
);
794 event
.SetEventObject( this );
799 if ( HasClientObjectData() )
800 event
.SetClientObject( GetClientObject(n
) );
801 else if ( HasClientUntypedData() )
802 event
.SetClientData( GetClientData(n
) );
803 event
.SetString( GetString(n
) );
810 event
.m_commandInt
= n
;
812 GetEventHandler()->ProcessEvent(event
);
815 void wxListBox::MacDoDoubleClick()
817 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
, m_windowId
);
818 event
.SetEventObject( this );
819 GetEventHandler()->ProcessEvent(event
) ;