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
)
30 EVT_CHAR( wxListBox::OnChar
)
34 #include "wx/mac/uma.h"
36 #if PRAGMA_STRUCT_ALIGN
37 #pragma options align=mac68k
38 #elif PRAGMA_STRUCT_PACKPUSH
40 #elif PRAGMA_STRUCT_PACK
45 unsigned short instruction
;
47 } ldefRec
, *ldefPtr
, **ldefHandle
;
49 #if PRAGMA_STRUCT_ALIGN
50 #pragma options align=reset
51 #elif PRAGMA_STRUCT_PACKPUSH
53 #elif PRAGMA_STRUCT_PACK
59 static pascal void wxMacListDefinition( short message
, Boolean isSelected
, Rect
*drawRect
,
60 Cell cell
, short dataOffset
, short dataLength
,
61 ListHandle listHandle
) ;
64 static pascal void wxMacListDefinition( short message
, Boolean isSelected
, Rect
*drawRect
,
65 Cell cell
, short dataOffset
, short dataLength
,
66 ListHandle listHandle
)
70 RgnHandle savedClipRegion
;
74 SetPort((**listHandle
).port
);
75 grafPtr
= (**listHandle
).port
;
76 // typecast our refCon
77 list
= (wxListBox
*) GetControlReference( (ControlHandle
) GetListRefCon(listHandle
) );
79 // Calculate the cell rect.
90 const wxString text
= list
->m_stringArray
[cell
.v
] ;
92 // Save the current clip region, and set the clip region to the area we are about
95 savedClipRegion
= NewRgn();
96 GetClip( savedClipRegion
);
99 EraseRect( drawRect
);
101 MoveTo(drawRect
->left
+ 4 , drawRect
->top
+ 10 );
102 ::TextFont( kFontIDMonaco
) ;
106 DrawText(text
, 0 , text
.Length());
107 // If the cell is hilited, do the hilite now. Paint the cell contents with the
108 // appropriate QuickDraw transform mode.
111 savedPenMode
= GetPortPenMode( (CGrafPtr
) grafPtr
);
112 SetPortPenMode( (CGrafPtr
)grafPtr
, hilitetransfermode
);
113 PaintRect( drawRect
);
114 SetPortPenMode( (CGrafPtr
)grafPtr
, savedPenMode
);
117 // Restore the saved clip region.
119 SetClip( savedClipRegion
);
120 DisposeRgn( savedClipRegion
);
125 // Hilite or unhilite the cell. Paint the cell contents with the
126 // appropriate QuickDraw transform mode.
129 savedPenMode
= GetPortPenMode( (CGrafPtr
)grafPtr
);
130 SetPortPenMode( (CGrafPtr
)grafPtr
, hilitetransfermode
);
131 PaintRect( drawRect
);
132 SetPortPenMode( (CGrafPtr
)grafPtr
, savedPenMode
);
140 extern "C" void MacDrawStringCell(Rect
*cellRect
, Cell lCell
, ListHandle theList
, long refCon
) ;
141 const short kwxMacListWithVerticalScrollbar
= 128 ;
142 const short kwxMacListItemHeight
= 14 ;
144 // ============================================================================
145 // list box control implementation
146 // ============================================================================
149 wxListBox::wxListBox()
156 static ListDefUPP macListDefUPP
= NULL
;
158 bool wxListBox::Create(wxWindow
*parent
, wxWindowID id
,
161 int n
, const wxString choices
[],
163 const wxValidator
& validator
,
164 const wxString
& name
)
166 m_noItems
= 0 ; // this will be increased by our append command
172 MacPreControlCreate( parent
, id
, "" , pos
, size
,style
, validator
, name
, &bounds
, title
) ;
175 listDef
.defType
= kListDefUserProcType
;
176 if ( macListDefUPP
== NULL
)
178 macListDefUPP
= NewListDefUPP( wxMacListDefinition
);
180 listDef
.u
.userProc
= macListDefUPP
;
186 CreateListBoxControl( MAC_WXHWND(parent
->MacGetRootWindow()), &bounds
, false, 0, 1, false, true,
187 kwxMacListItemHeight
, kwxMacListItemHeight
, false, &listDef
, (ControlRef
*)&m_macControl
);
189 GetControlData( (ControlHandle
) m_macControl
, kControlNoPart
, kControlListBoxListHandleTag
,
190 sizeof(ListHandle
), (Ptr
) &m_macList
, &asize
);
192 SetControlReference( (ControlHandle
) m_macControl
, (long) this);
193 SetControlVisibility( (ControlHandle
) m_macControl
, false, false);
198 wxStAppResource resload
;
199 m_macControl
= ::NewControl( MAC_WXHWND(parent
->MacGetRootWindow()) , &bounds
, title
, false ,
200 kwxMacListWithVerticalScrollbar
, 0 , 0,
201 kControlListBoxProc
, (long) this ) ;
202 ::GetControlData( (ControlHandle
) m_macControl
, kControlNoPart
, kControlListBoxListHandleTag
,
203 sizeof( ListHandle
) , (char*) &m_macList
, &result
) ;
205 HLock( (Handle
) m_macList
) ;
207 ldef
= (ldefHandle
) NewHandle( sizeof(ldefRec
) ) ;
208 if ( (**(ListHandle
)m_macList
).listDefProc
!= NULL
)
210 (**ldef
).instruction
= 0x4EF9; /* JMP instruction */
211 (**ldef
).function
= (void(*)()) listDef
.u
.userProc
;
212 (**(ListHandle
)m_macList
).listDefProc
= (Handle
) ldef
;
215 Point pt
= (**(ListHandle
)m_macList
).cellSize
;
216 pt
.v
= kwxMacListItemHeight
;
217 LCellSize( pt
, (ListHandle
)m_macList
) ;
218 LAddColumn( 1 , 0 , (ListHandle
)m_macList
) ;
220 OptionBits options
= 0;
221 if ( style
& wxLB_MULTIPLE
)
223 options
+= lNoExtend
;
225 else if ( style
& wxLB_EXTENDED
)
227 options
+= lExtendDrag
;
233 SetListSelectionFlags((ListHandle
)m_macList
, options
);
235 MacPostControlCreate() ;
237 for ( int i
= 0 ; i
< n
; i
++ )
239 Append( choices
[i
] ) ;
242 LSetDrawingMode( true , (ListHandle
)m_macList
) ;
247 wxListBox::~wxListBox()
253 DisposeHandle( (**(ListHandle
)m_macList
).listDefProc
) ;
254 (**(ListHandle
)m_macList
).listDefProc
= NULL
;
260 void wxListBox::Free()
262 #if wxUSE_OWNER_DRAWN
263 if ( m_windowStyle
& wxLB_OWNERDRAW
)
265 size_t uiCount
= m_aItems
.Count();
266 while ( uiCount
-- != 0 ) {
267 delete m_aItems
[uiCount
];
273 #endif // wxUSE_OWNER_DRAWN
274 if ( HasClientObjectData() )
276 for ( size_t n
= 0; n
< (size_t)m_noItems
; n
++ )
278 delete GetClientObject(n
);
283 void wxListBox::DoSetSize(int x
, int y
,
284 int width
, int height
,
287 wxControl::DoSetSize( x
, y
, width
, height
, sizeFlags
) ;
290 GetControlBounds( (ControlHandle
) m_macControl
, &bounds
) ;
291 ControlRef control
= GetListVerticalScrollBar( (ListHandle
)m_macList
) ;
295 GetControlBounds( control
, &scrollbounds
) ;
296 if( scrollbounds
.right
!= bounds
.right
+ 1 )
298 UMAMoveControl( control
, bounds
.right
- (scrollbounds
.right
- scrollbounds
.left
) + 1 ,
304 void wxListBox::DoSetFirstItem(int N
)
309 void wxListBox::Delete(int N
)
311 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
312 wxT("invalid index in wxListBox::Delete") );
314 #if wxUSE_OWNER_DRAWN
316 m_aItems
.RemoveAt(N
);
317 #else // !wxUSE_OWNER_DRAWN
318 if ( HasClientObjectData() )
320 delete GetClientObject(N
);
322 #endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
323 m_stringArray
.RemoveAt( N
) ;
324 m_dataArray
.RemoveAt( N
) ;
330 int wxListBox::DoAppend(const wxString
& item
)
332 int index
= m_noItems
;
333 if( wxApp::s_macDefaultEncodingIsPC
)
335 m_stringArray
.Add( wxMacMakeMacStringFromPC( item
) ) ;
336 m_dataArray
.Add( NULL
);
339 m_stringArray
.Add( item
) ;
340 m_dataArray
.Add( NULL
);
343 DoSetItemClientData( index
, NULL
) ;
349 void wxListBox::DoSetItems(const wxArrayString
& choices
, void** clientData
)
351 MacSetRedraw( false ) ;
353 int n
= choices
.GetCount();
355 for( int i
= 0 ; i
< n
; ++i
)
359 #if wxUSE_OWNER_DRAWN
360 wxASSERT_MSG(clientData
[i
] == NULL
,
361 wxT("Can't use client data with owner-drawn listboxes"));
362 #else // !wxUSE_OWNER_DRAWN
363 Append( choices
[i
] , clientData
[i
] ) ;
367 Append( choices
[i
] ) ;
370 #if wxUSE_OWNER_DRAWN
371 if ( m_windowStyle
& wxLB_OWNERDRAW
) {
372 // first delete old items
373 size_t ui
= m_aItems
.Count();
374 while ( ui
-- != 0 ) {
379 // then create new ones
380 for ( ui
= 0; ui
< (size_t)m_noItems
; ui
++ ) {
381 wxOwnerDrawn
*pNewItem
= CreateItem(ui
);
382 pNewItem
->SetName(choices
[ui
]);
383 m_aItems
.Add(pNewItem
);
386 #endif // wxUSE_OWNER_DRAWN
387 MacSetRedraw( true ) ;
390 bool wxListBox::HasMultipleSelection() const
392 return (m_windowStyle
& wxLB_MULTIPLE
) || (m_windowStyle
& wxLB_EXTENDED
);
395 int wxListBox::FindString(const wxString
& st
) const
398 if( wxApp::s_macDefaultEncodingIsPC
)
400 s
= wxMacMakeMacStringFromPC( st
) ;
405 if ( s
.Right(1) == "*" )
407 wxString search
= s
.Left( s
.Length() - 1 ) ;
408 int len
= search
.Length() ;
412 c2pstrcpy( (StringPtr
) s2
, search
.c_str() ) ;
414 strcpy( (char *) s2
, search
.c_str() ) ;
415 c2pstr( (char *) s2
) ;
418 for ( int i
= 0 ; i
< m_noItems
; ++ i
)
421 c2pstrcpy( (StringPtr
) s1
, m_stringArray
[i
].Left( len
).c_str() ) ;
423 strcpy( (char *) s1
, m_stringArray
[i
].Left( len
).c_str() ) ;
424 c2pstr( (char *) s1
) ;
426 if ( EqualString( s1
, s2
, false , false ) )
429 if ( s
.Left(1) == "*" && s
.Length() > 1 )
433 for ( int i
= 0 ; i
< m_noItems
; ++i
)
435 if ( GetString(i
).Lower().Matches(s
) )
446 c2pstrcpy( (StringPtr
) s2
, s
.c_str() ) ;
448 strcpy( (char *) s2
, s
.c_str() ) ;
449 c2pstr( (char *) s2
) ;
452 for ( int i
= 0 ; i
< m_noItems
; ++ i
)
455 c2pstrcpy( (StringPtr
) s1
, m_stringArray
[i
].c_str() ) ;
457 strcpy( (char *) s1
, m_stringArray
[i
].c_str() ) ;
458 c2pstr( (char *) s1
) ;
460 if ( EqualString( s1
, s2
, false , false ) )
467 void wxListBox::Clear()
471 m_stringArray
.Empty() ;
472 m_dataArray
.Empty() ;
476 void wxListBox::SetSelection(int N
, bool select
)
478 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
479 "invalid index in wxListBox::SetSelection" );
480 MacSetSelection( N
, select
) ;
481 GetSelections( m_selectionPreImage
) ;
484 bool wxListBox::IsSelected(int N
) const
486 wxCHECK_MSG( N
>= 0 && N
< m_noItems
, FALSE
,
487 "invalid index in wxListBox::Selected" );
489 return MacIsSelected( N
) ;
492 void *wxListBox::DoGetItemClientData(int N
) const
494 wxCHECK_MSG( N
>= 0 && N
< m_noItems
, NULL
,
495 wxT("invalid index in wxListBox::GetClientData"));
497 return (void *)m_dataArray
[N
];
500 wxClientData
*wxListBox::DoGetItemClientObject(int N
) const
502 return (wxClientData
*) DoGetItemClientData( N
) ;
505 void wxListBox::DoSetItemClientData(int N
, void *Client_data
)
507 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
508 "invalid index in wxListBox::SetClientData" );
510 #if wxUSE_OWNER_DRAWN
511 if ( m_windowStyle
& wxLB_OWNERDRAW
)
513 // client data must be pointer to wxOwnerDrawn, otherwise we would crash
514 // in OnMeasure/OnDraw.
515 wxFAIL_MSG(wxT("Can't use client data with owner-drawn listboxes"));
517 #endif // wxUSE_OWNER_DRAWN
518 wxASSERT_MSG( m_dataArray
.GetCount() >= N
, "invalid client_data array" ) ;
520 if ( m_dataArray
.GetCount() > N
)
522 m_dataArray
[N
] = (char*) Client_data
;
526 m_dataArray
.Add( (char*) Client_data
) ;
530 void wxListBox::DoSetItemClientObject(int n
, wxClientData
* clientData
)
532 DoSetItemClientData(n
, clientData
);
535 // Return number of selections and an array of selected integers
536 int wxListBox::GetSelections(wxArrayInt
& aSelections
) const
538 return MacGetSelections( aSelections
) ;
541 // Get single selection, for single choice list items
542 int wxListBox::GetSelection() const
544 return MacGetSelection() ;
547 // Find string for position
548 wxString
wxListBox::GetString(int N
) const
550 if( wxApp::s_macDefaultEncodingIsPC
)
552 return wxMacMakePCStringFromMac( m_stringArray
[N
] ) ;
555 return m_stringArray
[N
] ;
558 void wxListBox::DoInsertItems(const wxArrayString
& items
, int pos
)
560 wxCHECK_RET( pos
>= 0 && pos
<= m_noItems
,
561 wxT("invalid index in wxListBox::InsertItems") );
563 int nItems
= items
.GetCount();
565 for ( int i
= 0 ; i
< nItems
; i
++ )
567 m_stringArray
.Insert( items
[i
] , pos
+ i
) ;
568 m_dataArray
.Insert( NULL
, pos
+ i
) ;
569 MacInsert( pos
+ i
, items
[i
] ) ;
575 void wxListBox::SetString(int N
, const wxString
& s
)
578 if( wxApp::s_macDefaultEncodingIsPC
)
580 str
= wxMacMakeMacStringFromPC( s
) ;
584 m_stringArray
[N
] = str
;
588 wxSize
wxListBox::DoGetBestSize() const
590 return wxSize(100, 100);
593 int wxListBox::GetCount() const
598 void wxListBox::SetupColours()
600 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
601 SetForegroundColour(GetParent()->GetForegroundColour());
604 void wxListBox::Refresh(bool eraseBack
, const wxRect
*rect
)
606 wxControl::Refresh( eraseBack
, rect
) ;
607 // MacRedrawControl() ;
610 #if wxUSE_OWNER_DRAWN
612 class wxListBoxItem
: public wxOwnerDrawn
615 wxListBoxItem(const wxString
& str
= "");
618 wxListBoxItem::wxListBoxItem(const wxString
& str
) : wxOwnerDrawn(str
, FALSE
)
620 // no bitmaps/checkmarks
624 wxOwnerDrawn
*wxListBox::CreateItem(size_t n
)
626 return new wxListBoxItem();
629 #endif //USE_OWNER_DRAWN
631 // ============================================================================
632 // list box control implementation
633 // ============================================================================
635 void MacDrawStringCell(Rect
*cellRect
, Cell lCell
, ListHandle theList
, long refCon
)
638 // typecast our refCon
639 list
= (wxListBox
*)refCon
;
641 MoveTo(cellRect
->left
+ 4 , cellRect
->top
+ 10 );
642 const wxString text
= list
->m_stringArray
[lCell
.v
] ;
643 ::TextFont( kFontIDMonaco
) ;
646 DrawText(text
, 0 , text
.Length());
650 void wxListBox::MacDelete( int N
)
652 LDelRow( 1 , N
, (ListHandle
)m_macList
) ;
656 void wxListBox::MacInsert( int n
, const char * text
)
658 Cell cell
= { 0 , 0 } ;
660 LAddRow( 1 , cell
.v
, (ListHandle
)m_macList
) ;
661 // LSetCell(text, strlen(text), cell, m_macList);
665 void wxListBox::MacAppend( const char * text
)
667 Cell cell
= { 0 , 0 } ;
668 cell
.v
= (**(ListHandle
)m_macList
).dataBounds
.bottom
;
669 LAddRow( 1 , cell
.v
, (ListHandle
)m_macList
) ;
670 // LSetCell(text, strlen(text), cell, m_macList);
674 void wxListBox::MacClear()
676 LDelRow( (**(ListHandle
)m_macList
).dataBounds
.bottom
, 0 ,(ListHandle
) m_macList
) ;
680 void wxListBox::MacSetSelection( int n
, bool select
)
682 Cell cell
= { 0 , 0 } ;
683 if ( ! (m_windowStyle
& wxLB_MULTIPLE
) )
685 if ( LGetSelect( true , &cell
, (ListHandle
)m_macList
) )
687 LSetSelect( false , cell
, (ListHandle
)m_macList
) ;
692 LSetSelect( select
, cell
, (ListHandle
)m_macList
) ;
693 LAutoScroll( (ListHandle
)m_macList
) ;
697 bool wxListBox::MacIsSelected( int n
) const
699 Cell cell
= { 0 , 0 } ;
701 return LGetSelect( false , &cell
, (ListHandle
)m_macList
) ;
704 void wxListBox::MacDestroy()
706 // DisposeExtLDEFInfo( m_macList ) ;
709 int wxListBox::MacGetSelection() const
711 Cell cell
= { 0 , 0 } ;
712 if ( LGetSelect( true , &cell
, (ListHandle
)m_macList
) )
718 int wxListBox::MacGetSelections( wxArrayInt
& aSelections
) const
724 Cell cell
= { 0 , 0 } ;
727 while ( LGetSelect( true , &cell
,(ListHandle
) m_macList
) )
729 aSelections
.Add( cell
.v
) ;
736 void wxListBox::MacSet( int n
, const char * text
)
738 // our implementation does not store anything in the list
739 // so we just have to redraw
740 Cell cell
= { 0 , 0 } ;
742 // LSetCell(text, strlen(text), cell, m_macList);
746 void wxListBox::MacScrollTo( int n
)
748 // TODO implement scrolling
751 void wxListBox::OnSize( const wxSizeEvent
&event
)
756 GetListCellSize((ListHandle
)m_macList
, &pt
);
758 pt
= (**(ListHandle
)m_macList
).cellSize
;
760 pt
.h
= m_width
- 15 ;
761 LCellSize( pt
, (ListHandle
)m_macList
) ;
764 void wxListBox::MacHandleControlClick( WXWidget control
, wxInt16 controlpart
)
766 Boolean wasDoubleClick
= false ;
769 ::GetControlData( (ControlHandle
) m_macControl
, kControlNoPart
, kControlListBoxDoubleClickTag
, sizeof( wasDoubleClick
) , (char*) &wasDoubleClick
, &result
) ;
770 if ( !wasDoubleClick
)
780 void wxListBox::MacSetRedraw( bool doDraw
)
782 LSetDrawingMode( doDraw
, (ListHandle
)m_macList
) ;
786 void wxListBox::MacDoClick()
788 wxArrayInt aSelections
;
789 int n
, count
= GetSelections(aSelections
);
791 if ( count
== m_selectionPreImage
.GetCount() )
793 bool hasChanged
= false ;
794 for ( int i
= 0 ; i
< count
; ++i
)
796 if ( aSelections
[i
] != m_selectionPreImage
[i
] )
808 m_selectionPreImage
= aSelections
;
810 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_SELECTED
, m_windowId
);
811 event
.SetEventObject( this );
816 if ( HasClientObjectData() )
817 event
.SetClientObject( GetClientObject(n
) );
818 else if ( HasClientUntypedData() )
819 event
.SetClientData( GetClientData(n
) );
820 event
.SetString( GetString(n
) );
827 event
.m_commandInt
= n
;
829 GetEventHandler()->ProcessEvent(event
);
832 void wxListBox::MacDoDoubleClick()
834 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
, m_windowId
);
835 event
.SetEventObject( this );
836 GetEventHandler()->ProcessEvent(event
) ;
839 static long sLastTypeIn
= 0 ;
841 void wxListBox::OnChar(wxKeyEvent
& event
)
843 EventRecord
*ev
= (EventRecord
*) (wxTheApp
->MacGetCurrentEvent() ) ;
846 keychar
= short(ev
->message
& charCodeMask
);
847 keycode
= short(ev
->message
& keyCodeMask
) >> 8 ;
848 if ( event
.KeyCode() == WXK_SPACE
)
850 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_SELECTED
, m_windowId
);
851 event
.SetEventObject( this );
853 wxArrayInt aSelections
;
854 int n
, count
= GetSelections(aSelections
);
858 if ( HasClientObjectData() )
859 event
.SetClientObject( GetClientObject(n
) );
860 else if ( HasClientUntypedData() )
861 event
.SetClientData( GetClientData(n
) );
862 event
.SetString( GetString(n
) );
869 event
.m_commandInt
= n
;
871 GetEventHandler()->ProcessEvent(event
);
873 else if ( event
.KeyCode() == WXK_DOWN
|| event
.KeyCode() == WXK_UP
)
875 ::HandleControlKey( (ControlHandle
) m_macControl
, keycode
, keychar
, ev
->modifiers
) ;
879 if ( ev
->when
> m_lastTypeIn
+ 60 )
883 m_lastTypeIn
= ev
->when
;
884 m_typeIn
+= (char) event
.KeyCode() ;
885 int line
= FindString("*"+m_typeIn
+"*") ;