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
);
86 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
->MacGetRootWindow(), &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
->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
) ;
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::DoSetSize(int x
, int y
,
269 int width
, int height
,
272 wxControl::DoSetSize( x
, y
, width
, height
, sizeFlags
) ;
275 GetControlBounds( m_macControl
, &bounds
) ;
276 ControlRef control
= GetListVerticalScrollBar( m_macList
) ;
280 GetControlBounds( control
, &scrollbounds
) ;
281 if( scrollbounds
.right
!= bounds
.right
+ 1 )
283 UMAMoveControl( control
, bounds
.right
- (scrollbounds
.right
- scrollbounds
.left
) + 1 ,
289 void wxListBox::DoSetFirstItem(int N
)
294 void wxListBox::Delete(int N
)
296 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
297 wxT("invalid index in wxListBox::Delete") );
299 #if wxUSE_OWNER_DRAWN
301 m_aItems
.RemoveAt(N
);
302 #else // !wxUSE_OWNER_DRAWN
303 if ( HasClientObjectData() )
305 delete GetClientObject(N
);
307 #endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
308 m_stringArray
.Remove( N
) ;
309 m_dataArray
.RemoveAt( N
) ;
315 int wxListBox::DoAppend(const wxString
& item
)
317 int index
= m_noItems
;
318 if( wxApp::s_macDefaultEncodingIsPC
)
320 m_stringArray
.Add( wxMacMakeMacStringFromPC( item
) ) ;
321 m_dataArray
.Add( NULL
);
324 m_stringArray
.Add( item
) ;
325 m_dataArray
.Add( NULL
);
328 DoSetItemClientData( index
, NULL
) ;
334 void wxListBox::DoSetItems(const wxArrayString
& choices
, void** clientData
)
336 MacSetRedraw( false ) ;
338 int n
= choices
.GetCount();
340 for( int i
= 0 ; i
< n
; ++i
)
344 #if wxUSE_OWNER_DRAWN
345 wxASSERT_MSG(clientData
[i
] == NULL
,
346 wxT("Can't use client data with owner-drawn listboxes"));
347 #else // !wxUSE_OWNER_DRAWN
348 Append( choices
[i
] , clientData
[i
] ) ;
352 Append( choices
[i
] ) ;
355 #if wxUSE_OWNER_DRAWN
356 if ( m_windowStyle
& wxLB_OWNERDRAW
) {
357 // first delete old items
358 size_t ui
= m_aItems
.Count();
359 while ( ui
-- != 0 ) {
364 // then create new ones
365 for ( ui
= 0; ui
< (size_t)m_noItems
; ui
++ ) {
366 wxOwnerDrawn
*pNewItem
= CreateItem(ui
);
367 pNewItem
->SetName(choices
[ui
]);
368 m_aItems
.Add(pNewItem
);
371 #endif // wxUSE_OWNER_DRAWN
372 MacSetRedraw( true ) ;
375 bool wxListBox::HasMultipleSelection() const
377 return (m_windowStyle
& wxLB_MULTIPLE
) || (m_windowStyle
& wxLB_EXTENDED
);
380 int wxListBox::FindString(const wxString
& st
) const
383 if( wxApp::s_macDefaultEncodingIsPC
)
385 s
= wxMacMakeMacStringFromPC( st
) ;
390 if ( s
.Right(1) == "*" )
392 wxString search
= s
.Left( s
.Length() - 1 ) ;
393 int len
= search
.Length() ;
397 c2pstrcpy( (StringPtr
) s2
, search
.c_str() ) ;
399 strcpy( (char *) s2
, search
.c_str() ) ;
400 c2pstr( (char *) s2
) ;
403 for ( int i
= 0 ; i
< m_noItems
; ++ i
)
406 c2pstrcpy( (StringPtr
) s1
, m_stringArray
[i
].Left( len
).c_str() ) ;
408 strcpy( (char *) s1
, m_stringArray
[i
].Left( len
).c_str() ) ;
409 c2pstr( (char *) s1
) ;
411 if ( EqualString( s1
, s2
, false , false ) )
414 if ( s
.Left(1) == "*" && s
.Length() > 1 )
417 for ( int i
= 0 ; i
< m_noItems
; ++i
)
419 if ( GetString(i
).Lower().Matches(s
) )
430 c2pstrcpy( (StringPtr
) s2
, s
.c_str() ) ;
432 strcpy( (char *) s2
, s
.c_str() ) ;
433 c2pstr( (char *) s2
) ;
436 for ( int i
= 0 ; i
< m_noItems
; ++ i
)
439 c2pstrcpy( (StringPtr
) s1
, m_stringArray
[i
].c_str() ) ;
441 strcpy( (char *) s1
, m_stringArray
[i
].c_str() ) ;
442 c2pstr( (char *) s1
) ;
444 if ( EqualString( s1
, s2
, false , false ) )
451 void wxListBox::Clear()
455 m_stringArray
.Empty() ;
456 m_dataArray
.Empty() ;
460 void wxListBox::SetSelection(int N
, bool select
)
462 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
463 "invalid index in wxListBox::SetSelection" );
464 MacSetSelection( N
, select
) ;
465 GetSelections( m_selectionPreImage
) ;
468 bool wxListBox::IsSelected(int N
) const
470 wxCHECK_MSG( N
>= 0 && N
< m_noItems
, FALSE
,
471 "invalid index in wxListBox::Selected" );
473 return MacIsSelected( N
) ;
476 void *wxListBox::DoGetItemClientData(int N
) const
478 wxCHECK_MSG( N
>= 0 && N
< m_noItems
, NULL
,
479 wxT("invalid index in wxListBox::GetClientData"));
481 return (void *)m_dataArray
[N
];
484 wxClientData
*wxListBox::DoGetItemClientObject(int N
) const
486 return (wxClientData
*) DoGetItemClientData( N
) ;
489 void wxListBox::DoSetItemClientData(int N
, void *Client_data
)
491 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
492 "invalid index in wxListBox::SetClientData" );
494 #if wxUSE_OWNER_DRAWN
495 if ( m_windowStyle
& wxLB_OWNERDRAW
)
497 // client data must be pointer to wxOwnerDrawn, otherwise we would crash
498 // in OnMeasure/OnDraw.
499 wxFAIL_MSG(wxT("Can't use client data with owner-drawn listboxes"));
501 #endif // wxUSE_OWNER_DRAWN
502 wxASSERT_MSG( m_dataArray
.GetCount() >= N
, "invalid client_data array" ) ;
504 if ( m_dataArray
.GetCount() > N
)
506 m_dataArray
[N
] = (char*) Client_data
;
510 m_dataArray
.Add( (char*) Client_data
) ;
514 void wxListBox::DoSetItemClientObject(int n
, wxClientData
* clientData
)
516 DoSetItemClientData(n
, clientData
);
519 // Return number of selections and an array of selected integers
520 int wxListBox::GetSelections(wxArrayInt
& aSelections
) const
522 return MacGetSelections( aSelections
) ;
525 // Get single selection, for single choice list items
526 int wxListBox::GetSelection() const
528 return MacGetSelection() ;
531 // Find string for position
532 wxString
wxListBox::GetString(int N
) const
534 if( wxApp::s_macDefaultEncodingIsPC
)
536 return wxMacMakePCStringFromMac( m_stringArray
[N
] ) ;
539 return m_stringArray
[N
] ;
542 void wxListBox::DoInsertItems(const wxArrayString
& items
, int pos
)
544 wxCHECK_RET( pos
>= 0 && pos
<= m_noItems
,
545 wxT("invalid index in wxListBox::InsertItems") );
547 int nItems
= items
.GetCount();
549 for ( int i
= 0 ; i
< nItems
; i
++ )
551 m_stringArray
.Insert( items
[i
] , pos
+ i
) ;
552 m_dataArray
.Insert( NULL
, pos
+ i
) ;
553 MacInsert( pos
+ i
, items
[i
] ) ;
559 void wxListBox::SetString(int N
, const wxString
& s
)
562 if( wxApp::s_macDefaultEncodingIsPC
)
564 str
= wxMacMakeMacStringFromPC( s
) ;
568 m_stringArray
[N
] = str
;
572 wxSize
wxListBox::DoGetBestSize() const
574 return wxSize(100, 100);
577 int wxListBox::GetCount() const
582 void wxListBox::SetupColours()
584 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
585 SetForegroundColour(GetParent()->GetForegroundColour());
588 void wxListBox::Refresh(bool eraseBack
, const wxRect
*rect
)
591 WindowRef rootwindow
= MacGetRootWindow() ;
592 wxWindow
* wxrootwindow
= wxFindWinFromMacWindow( rootwindow
) ;
593 wxMacDrawingHelper
focus( wxrootwindow
);
595 UMADrawControl(m_macControl
);
598 #if wxUSE_OWNER_DRAWN
600 class wxListBoxItem
: public wxOwnerDrawn
603 wxListBoxItem(const wxString
& str
= "");
606 wxListBoxItem::wxListBoxItem(const wxString
& str
) : wxOwnerDrawn(str
, FALSE
)
608 // no bitmaps/checkmarks
612 wxOwnerDrawn
*wxListBox::CreateItem(size_t n
)
614 return new wxListBoxItem();
617 #endif //USE_OWNER_DRAWN
619 // ============================================================================
620 // list box control implementation
621 // ============================================================================
623 void MacDrawStringCell(Rect
*cellRect
, Cell lCell
, ListHandle theList
, long refCon
)
626 // typecast our refCon
627 list
= (wxListBox
*)refCon
;
629 MoveTo(cellRect
->left
+ 4 , cellRect
->top
+ 10 );
630 const wxString text
= list
->m_stringArray
[lCell
.v
] ;
631 ::TextFont( kFontIDMonaco
) ;
634 DrawText(text
, 0 , text
.Length());
638 void wxListBox::MacDelete( int N
)
640 LDelRow( 1 , N
, m_macList
) ;
644 void wxListBox::MacInsert( int n
, const char * text
)
646 Cell cell
= { 0 , 0 } ;
648 LAddRow( 1 , cell
.v
, m_macList
) ;
649 // LSetCell(text, strlen(text), cell, m_macList);
653 void wxListBox::MacAppend( const char * text
)
655 Cell cell
= { 0 , 0 } ;
656 cell
.v
= (**m_macList
).dataBounds
.bottom
;
657 LAddRow( 1 , cell
.v
, m_macList
) ;
658 // LSetCell(text, strlen(text), cell, m_macList);
662 void wxListBox::MacClear()
664 LDelRow( (**m_macList
).dataBounds
.bottom
, 0 , m_macList
) ;
668 void wxListBox::MacSetSelection( int n
, bool select
)
670 Cell cell
= { 0 , 0 } ;
671 if ( ! (m_windowStyle
& wxLB_MULTIPLE
) )
673 if ( LGetSelect( true , &cell
, m_macList
) )
675 LSetSelect( false , cell
, m_macList
) ;
680 LSetSelect( select
, cell
, m_macList
) ;
681 LAutoScroll( m_macList
) ;
685 bool wxListBox::MacIsSelected( int n
) const
687 Cell cell
= { 0 , 0 } ;
689 return LGetSelect( false , &cell
, m_macList
) ;
692 void wxListBox::MacDestroy()
694 // DisposeExtLDEFInfo( m_macList ) ;
697 int wxListBox::MacGetSelection() const
699 Cell cell
= { 0 , 0 } ;
700 if ( LGetSelect( true , &cell
, m_macList
) )
706 int wxListBox::MacGetSelections( wxArrayInt
& aSelections
) const
712 Cell cell
= { 0 , 0 } ;
715 while ( LGetSelect( true , &cell
, m_macList
) )
717 aSelections
.Add( cell
.v
) ;
724 void wxListBox::MacSet( int n
, const char * text
)
726 // our implementation does not store anything in the list
727 // so we just have to redraw
728 Cell cell
= { 0 , 0 } ;
730 // LSetCell(text, strlen(text), cell, m_macList);
734 void wxListBox::MacScrollTo( int n
)
736 // TODO implement scrolling
739 void wxListBox::OnSize( const wxSizeEvent
&event
)
744 GetListCellSize(m_macList
, &pt
);
746 pt
= (**m_macList
).cellSize
;
748 pt
.h
= m_width
- 15 ;
749 LCellSize( pt
, m_macList
) ;
752 void wxListBox::MacHandleControlClick( ControlHandle control
, SInt16 controlpart
)
754 Boolean wasDoubleClick
= false ;
757 ::GetControlData( m_macControl
, kControlNoPart
, kControlListBoxDoubleClickTag
, sizeof( wasDoubleClick
) , (char*) &wasDoubleClick
, &result
) ;
758 if ( !wasDoubleClick
)
768 void wxListBox::MacSetRedraw( bool doDraw
)
770 LSetDrawingMode( doDraw
, m_macList
) ;
774 void wxListBox::MacDoClick()
776 wxArrayInt aSelections
;
777 int n
, count
= GetSelections(aSelections
);
779 if ( count
== m_selectionPreImage
.GetCount() )
781 bool hasChanged
= false ;
782 for ( int i
= 0 ; i
< count
; ++i
)
784 if ( aSelections
[i
] != m_selectionPreImage
[i
] )
796 m_selectionPreImage
= aSelections
;
798 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_SELECTED
, m_windowId
);
799 event
.SetEventObject( this );
804 if ( HasClientObjectData() )
805 event
.SetClientObject( GetClientObject(n
) );
806 else if ( HasClientUntypedData() )
807 event
.SetClientData( GetClientData(n
) );
808 event
.SetString( GetString(n
) );
815 event
.m_commandInt
= n
;
817 GetEventHandler()->ProcessEvent(event
);
820 void wxListBox::MacDoDoubleClick()
822 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
, m_windowId
);
823 event
.SetEventObject( this );
824 GetEventHandler()->ProcessEvent(event
) ;