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/dynarray.h"
24 // #include "extldef.h"
27 #if !USE_SHARED_LIBRARY
28 IMPLEMENT_DYNAMIC_CLASS(wxListBox
, wxControl
)
30 BEGIN_EVENT_TABLE(wxListBox
, wxControl
)
31 EVT_SIZE( wxListBox::OnSize
)
35 #include "wx/mac/uma.h"
39 unsigned short instruction
;
41 } ldefRec
, *ldefPtr
, **ldefHandle
;
45 static pascal void wxMacListDefinition( short message
, Boolean isSelected
, Rect
*drawRect
,
46 Cell cell
, short dataOffset
, short dataLength
,
47 ListHandle listHandle
) ;
50 static pascal void wxMacListDefinition( short message
, Boolean isSelected
, Rect
*drawRect
,
51 Cell cell
, short dataOffset
, short dataLength
,
52 ListHandle listHandle
)
57 RgnHandle savedClipRegion
;
61 SetPort((**listHandle
).port
);
62 grafPtr
= (**listHandle
).port
;
63 // typecast our refCon
64 list
= (wxListBox
*) GetControlReference( (ControlHandle
) GetListRefCon(listHandle
) );
66 // Calculate the cell rect.
77 const wxString text
= list
->m_stringArray
[cell
.v
] ;
79 // Save the current clip region, and set the clip region to the area we are about
82 savedClipRegion
= NewRgn();
83 GetClip( savedClipRegion
);
85 EraseRect( drawRect
);
88 MoveTo(drawRect
->left
+ 4 , drawRect
->top
+ 10 );
89 ::TextFont( kFontIDMonaco
) ;
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.
98 savedPenMode
= GetPortPenMode( grafPtr
);
99 SetPortPenMode( grafPtr
, hilitetransfermode
);
100 PaintRect( drawRect
);
101 SetPortPenMode( grafPtr
, savedPenMode
);
104 // Restore the saved clip region.
106 SetClip( savedClipRegion
);
107 DisposeRgn( savedClipRegion
);
112 // Hilite or unhilite the cell. Paint the cell contents with the
113 // appropriate QuickDraw transform mode.
116 savedPenMode
= GetPortPenMode( grafPtr
);
117 SetPortPenMode( grafPtr
, hilitetransfermode
);
118 PaintRect( drawRect
);
119 SetPortPenMode( grafPtr
, savedPenMode
);
127 extern "C" void MacDrawStringCell(Rect
*cellRect
, Cell lCell
, ListHandle theList
, long refCon
) ;
128 const short kwxMacListWithVerticalScrollbar
= 128 ;
130 // ============================================================================
131 // list box control implementation
132 // ============================================================================
135 wxListBox::wxListBox()
142 static ListDefUPP macListDefUPP
= NULL
;
144 bool wxListBox::Create(wxWindow
*parent
, wxWindowID id
,
147 int n
, const wxString choices
[],
149 const wxValidator
& validator
,
150 const wxString
& name
)
152 m_noItems
= 0 ; // this will be increased by our append command
158 MacPreControlCreate( parent
, id
, "" , pos
, size
,style
, validator
, name
, &bounds
, title
) ;
161 listDef
.defType
= kListDefUserProcType
;
162 if ( macListDefUPP
== NULL
)
164 macListDefUPP
= NewListDefUPP( wxMacListDefinition
);
166 listDef
.u
.userProc
= macListDefUPP
;
171 CreateListBoxControl( parent
->GetMacRootWindow(), &bounds
, false, 0, 1, false, true,
172 14, 14, false, &listDef
, &m_macControl
);
174 GetControlData(m_macControl
, kControlNoPart
, kControlListBoxListHandleTag
,
175 sizeof(ListHandle
), (Ptr
) &m_macList
, &asize
);
177 SetControlReference(m_macControl
, (long) this);
178 SetControlVisibility(m_macControl
, false, false);
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
) ;
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
) ;
203 LAddColumn( 1 , 0 , m_macList
) ;
205 OptionBits options
= 0;
206 if ( style
& wxLB_MULTIPLE
)
208 options
+= lNoExtend
;
210 else if ( style
& wxLB_EXTENDED
)
212 options
+= lExtendDrag
;
218 SetListSelectionFlags(m_macList
, options
);
220 MacPostControlCreate() ;
222 for ( int i
= 0 ; i
< n
; i
++ )
224 Append( choices
[i
] ) ;
227 LSetDrawingMode( true , m_macList
) ;
232 wxListBox::~wxListBox()
238 DisposeHandle( (**m_macList
).listDefProc
) ;
239 (**m_macList
).listDefProc
= NULL
;
245 void wxListBox::Free()
247 #if wxUSE_OWNER_DRAWN
248 if ( m_windowStyle
& wxLB_OWNERDRAW
)
250 size_t uiCount
= m_aItems
.Count();
251 while ( uiCount
-- != 0 ) {
252 delete m_aItems
[uiCount
];
258 #endif // wxUSE_OWNER_DRAWN
259 if ( HasClientObjectData() )
261 for ( size_t n
= 0; n
< (size_t)m_noItems
; n
++ )
263 delete GetClientObject(n
);
268 void wxListBox::DoSetFirstItem(int N
)
273 void wxListBox::Delete(int N
)
275 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
276 wxT("invalid index in wxListBox::Delete") );
278 #if wxUSE_OWNER_DRAWN
280 m_aItems
.RemoveAt(N
);
281 #else // !wxUSE_OWNER_DRAWN
282 if ( HasClientObjectData() )
284 delete GetClientObject(N
);
286 #endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
287 m_stringArray
.Remove( N
) ;
288 m_dataArray
.RemoveAt( N
) ;
294 int wxListBox::DoAppend(const wxString
& item
)
296 int index
= m_noItems
;
297 if( wxApp::s_macDefaultEncodingIsPC
)
299 m_stringArray
.Add( wxMacMakeMacStringFromPC( item
) ) ;
300 m_dataArray
.Add( NULL
);
303 m_stringArray
.Add( item
) ;
304 m_dataArray
.Add( NULL
);
307 DoSetItemClientData( index
, NULL
) ;
313 void wxListBox::DoSetItems(const wxArrayString
& choices
, void** clientData
)
315 MacSetRedraw( false ) ;
317 int n
= choices
.GetCount();
319 for( int i
= 0 ; i
< n
; ++i
)
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
] ) ;
331 Append( choices
[i
] ) ;
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 ) {
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
);
350 #endif // wxUSE_OWNER_DRAWN
351 MacSetRedraw( true ) ;
354 bool wxListBox::HasMultipleSelection() const
356 return (m_windowStyle
& wxLB_MULTIPLE
) || (m_windowStyle
& wxLB_EXTENDED
);
359 int wxListBox::FindString(const wxString
& st
) const
362 if( wxApp::s_macDefaultEncodingIsPC
)
364 s
= wxMacMakeMacStringFromPC( st
) ;
369 if ( s
.Right(1) == "*" )
371 wxString search
= s
.Left( s
.Length() - 1 ) ;
372 int len
= search
.Length() ;
376 c2pstrcpy( (StringPtr
) s2
, search
.c_str() ) ;
378 strcpy( (char *) s2
, search
.c_str() ) ;
379 c2pstr( (char *) s2
) ;
382 for ( int i
= 0 ; i
< m_noItems
; ++ i
)
385 c2pstrcpy( (StringPtr
) s1
, m_stringArray
[i
].Left( len
).c_str() ) ;
387 strcpy( (char *) s1
, m_stringArray
[i
].Left( len
).c_str() ) ;
388 c2pstr( (char *) s1
) ;
390 if ( EqualString( s1
, s2
, false , false ) )
393 if ( s
.Left(1) == "*" && s
.Length() > 1 )
396 for ( int i
= 0 ; i
< m_noItems
; ++i
)
398 if ( GetString(i
).Lower().Matches(s
) )
409 c2pstrcpy( (StringPtr
) s2
, s
.c_str() ) ;
411 strcpy( (char *) s2
, s
.c_str() ) ;
412 c2pstr( (char *) s2
) ;
415 for ( int i
= 0 ; i
< m_noItems
; ++ i
)
418 c2pstrcpy( (StringPtr
) s1
, m_stringArray
[i
].c_str() ) ;
420 strcpy( (char *) s1
, m_stringArray
[i
].c_str() ) ;
421 c2pstr( (char *) s1
) ;
423 if ( EqualString( s1
, s2
, false , false ) )
430 void wxListBox::Clear()
434 m_stringArray
.Empty() ;
435 m_dataArray
.Empty() ;
439 void wxListBox::SetSelection(int N
, bool select
)
441 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
442 "invalid index in wxListBox::SetSelection" );
443 MacSetSelection( N
, select
) ;
444 GetSelections( m_selectionPreImage
) ;
447 bool wxListBox::IsSelected(int N
) const
449 wxCHECK_MSG( N
>= 0 && N
< m_noItems
, FALSE
,
450 "invalid index in wxListBox::Selected" );
452 return MacIsSelected( N
) ;
455 void *wxListBox::DoGetItemClientData(int N
) const
457 wxCHECK_MSG( N
>= 0 && N
< m_noItems
, NULL
,
458 wxT("invalid index in wxListBox::GetClientData"));
460 return (void *)m_dataArray
[N
];
463 wxClientData
*wxListBox::DoGetItemClientObject(int N
) const
465 return (wxClientData
*) DoGetItemClientData( N
) ;
468 void wxListBox::DoSetItemClientData(int N
, void *Client_data
)
470 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
471 "invalid index in wxListBox::SetClientData" );
473 #if wxUSE_OWNER_DRAWN
474 if ( m_windowStyle
& wxLB_OWNERDRAW
)
476 // client data must be pointer to wxOwnerDrawn, otherwise we would crash
477 // in OnMeasure/OnDraw.
478 wxFAIL_MSG(wxT("Can't use client data with owner-drawn listboxes"));
480 #endif // wxUSE_OWNER_DRAWN
481 wxASSERT_MSG( m_dataArray
.GetCount() >= N
, "invalid client_data array" ) ;
483 if ( m_dataArray
.GetCount() > N
)
485 m_dataArray
[N
] = (char*) Client_data
;
489 m_dataArray
.Add( (char*) Client_data
) ;
493 void wxListBox::DoSetItemClientObject(int n
, wxClientData
* clientData
)
495 DoSetItemClientData(n
, clientData
);
498 // Return number of selections and an array of selected integers
499 int wxListBox::GetSelections(wxArrayInt
& aSelections
) const
501 return MacGetSelections( aSelections
) ;
504 // Get single selection, for single choice list items
505 int wxListBox::GetSelection() const
507 return MacGetSelection() ;
510 // Find string for position
511 wxString
wxListBox::GetString(int N
) const
513 if( wxApp::s_macDefaultEncodingIsPC
)
515 return wxMacMakePCStringFromMac( m_stringArray
[N
] ) ;
518 return m_stringArray
[N
] ;
521 void wxListBox::DoInsertItems(const wxArrayString
& items
, int pos
)
523 wxCHECK_RET( pos
>= 0 && pos
<= m_noItems
,
524 wxT("invalid index in wxListBox::InsertItems") );
526 int nItems
= items
.GetCount();
528 for ( int i
= 0 ; i
< nItems
; i
++ )
530 m_stringArray
.Insert( items
[i
] , pos
+ i
) ;
531 m_dataArray
.Insert( NULL
, pos
+ i
) ;
532 MacInsert( pos
+ i
, items
[i
] ) ;
538 void wxListBox::SetString(int N
, const wxString
& s
)
541 if( wxApp::s_macDefaultEncodingIsPC
)
543 str
= wxMacMakeMacStringFromPC( s
) ;
547 m_stringArray
[N
] = str
;
551 wxSize
wxListBox::DoGetBestSize() const
553 return wxSize(100, 100);
556 int wxListBox::GetCount() const
561 void wxListBox::SetupColours()
563 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
564 SetForegroundColour(GetParent()->GetForegroundColour());
567 void wxListBox::Refresh(bool eraseBack
, const wxRect
*rect
)
570 WindowRef rootwindow
= GetMacRootWindow() ;
571 wxWindow
* wxrootwindow
= wxFindWinFromMacWindow( rootwindow
) ;
572 wxMacDrawingHelper
focus( wxrootwindow
);
574 UMADrawControl(m_macControl
);
577 #if wxUSE_OWNER_DRAWN
579 class wxListBoxItem
: public wxOwnerDrawn
582 wxListBoxItem(const wxString
& str
= "");
585 wxListBoxItem::wxListBoxItem(const wxString
& str
) : wxOwnerDrawn(str
, FALSE
)
587 // no bitmaps/checkmarks
591 wxOwnerDrawn
*wxListBox::CreateItem(size_t n
)
593 return new wxListBoxItem();
596 #endif //USE_OWNER_DRAWN
598 // ============================================================================
599 // list box control implementation
600 // ============================================================================
602 void MacDrawStringCell(Rect
*cellRect
, Cell lCell
, ListHandle theList
, long refCon
)
605 // typecast our refCon
606 list
= (wxListBox
*)refCon
;
608 MoveTo(cellRect
->left
+ 4 , cellRect
->top
+ 10 );
609 const wxString text
= list
->m_stringArray
[lCell
.v
] ;
610 ::TextFont( kFontIDMonaco
) ;
613 DrawText(text
, 0 , text
.Length());
617 void wxListBox::MacDelete( int N
)
619 LDelRow( 1 , N
, m_macList
) ;
623 void wxListBox::MacInsert( int n
, const char * text
)
625 Cell cell
= { 0 , 0 } ;
627 LAddRow( 1 , cell
.v
, m_macList
) ;
628 // LSetCell(text, strlen(text), cell, m_macList);
632 void wxListBox::MacAppend( const char * text
)
634 Cell cell
= { 0 , 0 } ;
635 cell
.v
= (**m_macList
).dataBounds
.bottom
;
636 LAddRow( 1 , cell
.v
, m_macList
) ;
637 // LSetCell(text, strlen(text), cell, m_macList);
641 void wxListBox::MacClear()
643 LDelRow( (**m_macList
).dataBounds
.bottom
, 0 , m_macList
) ;
647 void wxListBox::MacSetSelection( int n
, bool select
)
649 Cell cell
= { 0 , 0 } ;
650 if ( ! (m_windowStyle
& wxLB_MULTIPLE
) )
652 if ( LGetSelect( true , &cell
, m_macList
) )
654 LSetSelect( false , cell
, m_macList
) ;
659 LSetSelect( select
, cell
, m_macList
) ;
660 LAutoScroll( m_macList
) ;
664 bool wxListBox::MacIsSelected( int n
) const
666 Cell cell
= { 0 , 0 } ;
668 return LGetSelect( false , &cell
, m_macList
) ;
671 void wxListBox::MacDestroy()
673 // DisposeExtLDEFInfo( m_macList ) ;
676 int wxListBox::MacGetSelection() const
678 Cell cell
= { 0 , 0 } ;
679 if ( LGetSelect( true , &cell
, m_macList
) )
685 int wxListBox::MacGetSelections( wxArrayInt
& aSelections
) const
691 Cell cell
= { 0 , 0 } ;
694 while ( LGetSelect( true , &cell
, m_macList
) )
696 aSelections
.Add( cell
.v
) ;
703 void wxListBox::MacSet( int n
, const char * text
)
705 // our implementation does not store anything in the list
706 // so we just have to redraw
707 Cell cell
= { 0 , 0 } ;
709 // LSetCell(text, strlen(text), cell, m_macList);
713 void wxListBox::MacScrollTo( int n
)
715 // TODO implement scrolling
718 void wxListBox::OnSize( const wxSizeEvent
&event
)
723 GetListCellSize(m_macList
, &pt
);
725 pt
= (**m_macList
).cellSize
;
727 pt
.h
= m_width
- 15 ;
728 LCellSize( pt
, m_macList
) ;
731 void wxListBox::MacHandleControlClick( ControlHandle control
, SInt16 controlpart
)
733 Boolean wasDoubleClick
= false ;
736 ::GetControlData( m_macControl
, kControlNoPart
, kControlListBoxDoubleClickTag
, sizeof( wasDoubleClick
) , (char*) &wasDoubleClick
, &result
) ;
737 if ( !wasDoubleClick
)
747 void wxListBox::MacSetRedraw( bool doDraw
)
749 LSetDrawingMode( doDraw
, m_macList
) ;
753 void wxListBox::MacDoClick()
755 wxArrayInt aSelections
;
756 int n
, count
= GetSelections(aSelections
);
758 if ( count
== m_selectionPreImage
.GetCount() )
760 bool hasChanged
= false ;
761 for ( int i
= 0 ; i
< count
; ++i
)
763 if ( aSelections
[i
] != m_selectionPreImage
[i
] )
773 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_SELECTED
, m_windowId
);
774 event
.SetEventObject( this );
779 if ( HasClientObjectData() )
780 event
.SetClientObject( GetClientObject(n
) );
781 else if ( HasClientUntypedData() )
782 event
.SetClientData( GetClientData(n
) );
783 event
.SetString( GetString(n
) );
790 event
.m_commandInt
= n
;
792 GetEventHandler()->ProcessEvent(event
);
795 void wxListBox::MacDoDoubleClick()
797 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
, m_windowId
);
798 event
.SetEventObject( this );
799 GetEventHandler()->ProcessEvent(event
) ;