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
) ;
446 bool wxListBox::IsSelected(int N
) const
448 wxCHECK_MSG( N
>= 0 && N
< m_noItems
, FALSE
,
449 "invalid index in wxListBox::Selected" );
451 return MacIsSelected( N
) ;
454 void *wxListBox::DoGetItemClientData(int N
) const
456 wxCHECK_MSG( N
>= 0 && N
< m_noItems
, NULL
,
457 wxT("invalid index in wxListBox::GetClientData"));
459 return (void *)m_dataArray
[N
];
462 wxClientData
*wxListBox::DoGetItemClientObject(int N
) const
464 return (wxClientData
*) DoGetItemClientData( N
) ;
467 void wxListBox::DoSetItemClientData(int N
, void *Client_data
)
469 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
470 "invalid index in wxListBox::SetClientData" );
472 #if wxUSE_OWNER_DRAWN
473 if ( m_windowStyle
& wxLB_OWNERDRAW
)
475 // client data must be pointer to wxOwnerDrawn, otherwise we would crash
476 // in OnMeasure/OnDraw.
477 wxFAIL_MSG(wxT("Can't use client data with owner-drawn listboxes"));
479 #endif // wxUSE_OWNER_DRAWN
480 wxASSERT_MSG( m_dataArray
.GetCount() >= N
, "invalid client_data array" ) ;
482 if ( m_dataArray
.GetCount() > N
)
484 m_dataArray
[N
] = (char*) Client_data
;
488 m_dataArray
.Add( (char*) Client_data
) ;
492 void wxListBox::DoSetItemClientObject(int n
, wxClientData
* clientData
)
494 DoSetItemClientData(n
, clientData
);
497 // Return number of selections and an array of selected integers
498 int wxListBox::GetSelections(wxArrayInt
& aSelections
) const
500 return MacGetSelections( aSelections
) ;
503 if ((m_windowStyle & wxLB_MULTIMacE) || (m_windowStyle & wxLB_EXTENDED))
506 for ( int n = 0; n < no_sel; n++ )
511 else // single-selection listbox
520 // Get single selection, for single choice list items
521 int wxListBox::GetSelection() const
523 return MacGetSelection() ;
526 // Find string for position
527 wxString
wxListBox::GetString(int N
) const
529 if( wxApp::s_macDefaultEncodingIsPC
)
531 return wxMacMakePCStringFromMac( m_stringArray
[N
] ) ;
534 return m_stringArray
[N
] ;
537 void wxListBox::DoInsertItems(const wxArrayString
& items
, int pos
)
539 wxCHECK_RET( pos
>= 0 && pos
<= m_noItems
,
540 wxT("invalid index in wxListBox::InsertItems") );
542 int nItems
= items
.GetCount();
544 for ( int i
= 0 ; i
< nItems
; i
++ )
546 m_stringArray
.Insert( items
[i
] , pos
+ i
) ;
547 m_dataArray
.Insert( NULL
, pos
+ i
) ;
548 MacInsert( pos
+ i
, items
[i
] ) ;
554 void wxListBox::SetString(int N
, const wxString
& s
)
557 if( wxApp::s_macDefaultEncodingIsPC
)
559 str
= wxMacMakeMacStringFromPC( s
) ;
563 m_stringArray
[N
] = str
;
567 wxSize
wxListBox::DoGetBestSize() const
569 return wxSize(100, 100);
572 int wxListBox::GetCount() const
577 void wxListBox::SetupColours()
579 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
580 SetForegroundColour(GetParent()->GetForegroundColour());
583 void wxListBox::Refresh(bool eraseBack
, const wxRect
*rect
)
586 WindowRef rootwindow
= GetMacRootWindow() ;
587 wxWindow
* wxrootwindow
= wxFindWinFromMacWindow( rootwindow
) ;
588 wxMacDrawingHelper
focus( wxrootwindow
);
590 UMADrawControl(m_macControl
);
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 ( LGetSelect( true , &cell
, m_macList
) )
668 LSetSelect( false , cell
, m_macList
) ;
672 LSetSelect( select
, cell
, m_macList
) ;
673 LAutoScroll( m_macList
) ;
677 bool wxListBox::MacIsSelected( int n
) const
679 Cell cell
= { 0 , 0 } ;
681 return LGetSelect( false , &cell
, m_macList
) ;
684 void wxListBox::MacDestroy()
686 // DisposeExtLDEFInfo( m_macList ) ;
689 int wxListBox::MacGetSelection() const
691 Cell cell
= { 0 , 0 } ;
692 if ( LGetSelect( true , &cell
, m_macList
) )
698 int wxListBox::MacGetSelections( wxArrayInt
& aSelections
) const
704 Cell cell
= { 0 , 0 } ;
707 while ( LGetSelect( true , &cell
, m_macList
) )
709 aSelections
.Add( cell
.v
) ;
716 void wxListBox::MacSet( int n
, const char * text
)
718 // our implementation does not store anything in the list
719 // so we just have to redraw
720 Cell cell
= { 0 , 0 } ;
722 // LSetCell(text, strlen(text), cell, m_macList);
726 void wxListBox::MacScrollTo( int n
)
728 // TODO implement scrolling
731 void wxListBox::OnSize( const wxSizeEvent
&event
)
736 GetListCellSize(m_macList
, &pt
);
738 pt
= (**m_macList
).cellSize
;
740 pt
.h
= m_width
- 15 ;
741 LCellSize( pt
, m_macList
) ;
744 void wxListBox::MacHandleControlClick( ControlHandle control
, SInt16 controlpart
)
746 Boolean wasDoubleClick
= false ;
749 ::GetControlData( m_macControl
, kControlNoPart
, kControlListBoxDoubleClickTag
, sizeof( wasDoubleClick
) , (char*) &wasDoubleClick
, &result
) ;
750 if ( !wasDoubleClick
)
760 void wxListBox::MacSetRedraw( bool doDraw
)
762 LSetDrawingMode( doDraw
, m_macList
) ;
766 void wxListBox::MacDoClick()
768 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_SELECTED
, m_windowId
);
769 event
.SetEventObject( this );
771 wxArrayInt aSelections
;
772 int n
, count
= GetSelections(aSelections
);
776 if ( HasClientObjectData() )
777 event
.SetClientObject( GetClientObject(n
) );
778 else if ( HasClientUntypedData() )
779 event
.SetClientData( GetClientData(n
) );
780 event
.SetString( GetString(n
) );
787 event
.m_commandInt
= n
;
789 GetEventHandler()->ProcessEvent(event
);
792 void wxListBox::MacDoDoubleClick()
794 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
, m_windowId
);
795 event
.SetEventObject( this );
796 GetEventHandler()->ProcessEvent(event
) ;