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::FreeData()
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
];
268 m_aItems
[uiCount
] = NULL
;
274 #endif // wxUSE_OWNER_DRAWN
275 if ( HasClientObjectData() )
277 for ( size_t n
= 0; n
< (size_t)m_noItems
; n
++ )
279 delete GetClientObject(n
);
284 void wxListBox::DoSetSize(int x
, int y
,
285 int width
, int height
,
288 wxControl::DoSetSize( x
, y
, width
, height
, sizeFlags
) ;
291 GetControlBounds( (ControlHandle
) m_macControl
, &bounds
) ;
292 ControlRef control
= GetListVerticalScrollBar( (ListHandle
)m_macList
) ;
296 GetControlBounds( control
, &scrollbounds
) ;
297 if( scrollbounds
.right
!= bounds
.right
+ 1 )
299 UMAMoveControl( control
, bounds
.right
- (scrollbounds
.right
- scrollbounds
.left
) + 1 ,
305 void wxListBox::DoSetFirstItem(int N
)
310 void wxListBox::Delete(int N
)
312 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
313 wxT("invalid index in wxListBox::Delete") );
315 #if wxUSE_OWNER_DRAWN
317 m_aItems
.RemoveAt(N
);
318 #else // !wxUSE_OWNER_DRAWN
319 if ( HasClientObjectData() )
321 delete GetClientObject(N
);
323 #endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
324 m_stringArray
.RemoveAt( N
) ;
325 m_dataArray
.RemoveAt( N
) ;
331 int wxListBox::DoAppend(const wxString
& item
)
333 int index
= m_noItems
;
334 if( wxApp::s_macDefaultEncodingIsPC
)
336 m_stringArray
.Add( wxMacMakeMacStringFromPC( item
) ) ;
337 m_dataArray
.Add( NULL
);
340 m_stringArray
.Add( item
) ;
341 m_dataArray
.Add( NULL
);
344 DoSetItemClientData( index
, NULL
) ;
350 void wxListBox::DoSetItems(const wxArrayString
& choices
, void** clientData
)
352 MacSetRedraw( false ) ;
354 int n
= choices
.GetCount();
356 for( int i
= 0 ; i
< n
; ++i
)
360 #if wxUSE_OWNER_DRAWN
361 wxASSERT_MSG(clientData
[i
] == NULL
,
362 wxT("Can't use client data with owner-drawn listboxes"));
363 #else // !wxUSE_OWNER_DRAWN
364 Append( choices
[i
] , clientData
[i
] ) ;
368 Append( choices
[i
] ) ;
371 #if wxUSE_OWNER_DRAWN
372 if ( m_windowStyle
& wxLB_OWNERDRAW
) {
373 // first delete old items
374 size_t ui
= m_aItems
.Count();
375 while ( ui
-- != 0 ) {
381 // then create new ones
382 for ( ui
= 0; ui
< (size_t)m_noItems
; ui
++ ) {
383 wxOwnerDrawn
*pNewItem
= CreateItem(ui
);
384 pNewItem
->SetName(choices
[ui
]);
385 m_aItems
.Add(pNewItem
);
388 #endif // wxUSE_OWNER_DRAWN
389 MacSetRedraw( true ) ;
392 bool wxListBox::HasMultipleSelection() const
394 return (m_windowStyle
& wxLB_MULTIPLE
) || (m_windowStyle
& wxLB_EXTENDED
);
397 int wxListBox::FindString(const wxString
& st
) const
400 if( wxApp::s_macDefaultEncodingIsPC
)
402 s
= wxMacMakeMacStringFromPC( st
) ;
407 if ( s
.Right(1) == "*" )
409 wxString search
= s
.Left( s
.Length() - 1 ) ;
410 int len
= search
.Length() ;
414 c2pstrcpy( (StringPtr
) s2
, search
.c_str() ) ;
416 strcpy( (char *) s2
, search
.c_str() ) ;
417 c2pstr( (char *) s2
) ;
420 for ( int i
= 0 ; i
< m_noItems
; ++ i
)
423 c2pstrcpy( (StringPtr
) s1
, m_stringArray
[i
].Left( len
).c_str() ) ;
425 strcpy( (char *) s1
, m_stringArray
[i
].Left( len
).c_str() ) ;
426 c2pstr( (char *) s1
) ;
428 if ( EqualString( s1
, s2
, false , false ) )
431 if ( s
.Left(1) == "*" && s
.Length() > 1 )
435 for ( int i
= 0 ; i
< m_noItems
; ++i
)
437 if ( GetString(i
).Lower().Matches(s
) )
448 c2pstrcpy( (StringPtr
) s2
, s
.c_str() ) ;
450 strcpy( (char *) s2
, s
.c_str() ) ;
451 c2pstr( (char *) s2
) ;
454 for ( int i
= 0 ; i
< m_noItems
; ++ i
)
457 c2pstrcpy( (StringPtr
) s1
, m_stringArray
[i
].c_str() ) ;
459 strcpy( (char *) s1
, m_stringArray
[i
].c_str() ) ;
460 c2pstr( (char *) s1
) ;
462 if ( EqualString( s1
, s2
, false , false ) )
469 void wxListBox::Clear()
473 m_stringArray
.Empty() ;
474 m_dataArray
.Empty() ;
478 void wxListBox::SetSelection(int N
, bool select
)
480 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
481 "invalid index in wxListBox::SetSelection" );
482 MacSetSelection( N
, select
) ;
483 GetSelections( m_selectionPreImage
) ;
486 bool wxListBox::IsSelected(int N
) const
488 wxCHECK_MSG( N
>= 0 && N
< m_noItems
, FALSE
,
489 "invalid index in wxListBox::Selected" );
491 return MacIsSelected( N
) ;
494 void *wxListBox::DoGetItemClientData(int N
) const
496 wxCHECK_MSG( N
>= 0 && N
< m_noItems
, NULL
,
497 wxT("invalid index in wxListBox::GetClientData"));
499 return (void *)m_dataArray
[N
];
502 wxClientData
*wxListBox::DoGetItemClientObject(int N
) const
504 return (wxClientData
*) DoGetItemClientData( N
) ;
507 void wxListBox::DoSetItemClientData(int N
, void *Client_data
)
509 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
510 "invalid index in wxListBox::SetClientData" );
512 #if wxUSE_OWNER_DRAWN
513 if ( m_windowStyle
& wxLB_OWNERDRAW
)
515 // client data must be pointer to wxOwnerDrawn, otherwise we would crash
516 // in OnMeasure/OnDraw.
517 wxFAIL_MSG(wxT("Can't use client data with owner-drawn listboxes"));
519 #endif // wxUSE_OWNER_DRAWN
520 wxASSERT_MSG( m_dataArray
.GetCount() >= N
, "invalid client_data array" ) ;
522 if ( m_dataArray
.GetCount() > N
)
524 m_dataArray
[N
] = (char*) Client_data
;
528 m_dataArray
.Add( (char*) Client_data
) ;
532 void wxListBox::DoSetItemClientObject(int n
, wxClientData
* clientData
)
534 DoSetItemClientData(n
, clientData
);
537 // Return number of selections and an array of selected integers
538 int wxListBox::GetSelections(wxArrayInt
& aSelections
) const
540 return MacGetSelections( aSelections
) ;
543 // Get single selection, for single choice list items
544 int wxListBox::GetSelection() const
546 return MacGetSelection() ;
549 // Find string for position
550 wxString
wxListBox::GetString(int N
) const
552 if( wxApp::s_macDefaultEncodingIsPC
)
554 return wxMacMakePCStringFromMac( m_stringArray
[N
] ) ;
557 return m_stringArray
[N
] ;
560 void wxListBox::DoInsertItems(const wxArrayString
& items
, int pos
)
562 wxCHECK_RET( pos
>= 0 && pos
<= m_noItems
,
563 wxT("invalid index in wxListBox::InsertItems") );
565 int nItems
= items
.GetCount();
567 for ( int i
= 0 ; i
< nItems
; i
++ )
569 m_stringArray
.Insert( items
[i
] , pos
+ i
) ;
570 m_dataArray
.Insert( NULL
, pos
+ i
) ;
571 MacInsert( pos
+ i
, items
[i
] ) ;
577 void wxListBox::SetString(int N
, const wxString
& s
)
580 if( wxApp::s_macDefaultEncodingIsPC
)
582 str
= wxMacMakeMacStringFromPC( s
) ;
586 m_stringArray
[N
] = str
;
590 wxSize
wxListBox::DoGetBestSize() const
592 return wxSize(100, 100);
595 int wxListBox::GetCount() const
600 void wxListBox::SetupColours()
602 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
603 SetForegroundColour(GetParent()->GetForegroundColour());
606 void wxListBox::Refresh(bool eraseBack
, const wxRect
*rect
)
608 wxControl::Refresh( eraseBack
, rect
) ;
609 // MacRedrawControl() ;
612 #if wxUSE_OWNER_DRAWN
614 class wxListBoxItem
: public wxOwnerDrawn
617 wxListBoxItem(const wxString
& str
= "");
620 wxListBoxItem::wxListBoxItem(const wxString
& str
) : wxOwnerDrawn(str
, FALSE
)
622 // no bitmaps/checkmarks
626 wxOwnerDrawn
*wxListBox::CreateItem(size_t n
)
628 return new wxListBoxItem();
631 #endif //USE_OWNER_DRAWN
633 // ============================================================================
634 // list box control implementation
635 // ============================================================================
637 void MacDrawStringCell(Rect
*cellRect
, Cell lCell
, ListHandle theList
, long refCon
)
640 // typecast our refCon
641 list
= (wxListBox
*)refCon
;
643 MoveTo(cellRect
->left
+ 4 , cellRect
->top
+ 10 );
644 const wxString text
= list
->m_stringArray
[lCell
.v
] ;
645 ::TextFont( kFontIDMonaco
) ;
648 DrawText(text
, 0 , text
.Length());
652 void wxListBox::MacDelete( int N
)
654 LDelRow( 1 , N
, (ListHandle
)m_macList
) ;
658 void wxListBox::MacInsert( int n
, const char * text
)
660 Cell cell
= { 0 , 0 } ;
662 LAddRow( 1 , cell
.v
, (ListHandle
)m_macList
) ;
663 // LSetCell(text, strlen(text), cell, m_macList);
667 void wxListBox::MacAppend( const char * text
)
669 Cell cell
= { 0 , 0 } ;
670 cell
.v
= (**(ListHandle
)m_macList
).dataBounds
.bottom
;
671 LAddRow( 1 , cell
.v
, (ListHandle
)m_macList
) ;
672 // LSetCell(text, strlen(text), cell, m_macList);
676 void wxListBox::MacClear()
678 LDelRow( (**(ListHandle
)m_macList
).dataBounds
.bottom
, 0 ,(ListHandle
) m_macList
) ;
682 void wxListBox::MacSetSelection( int n
, bool select
)
684 Cell cell
= { 0 , 0 } ;
685 if ( ! (m_windowStyle
& wxLB_MULTIPLE
) )
687 if ( LGetSelect( true , &cell
, (ListHandle
)m_macList
) )
689 LSetSelect( false , cell
, (ListHandle
)m_macList
) ;
694 LSetSelect( select
, cell
, (ListHandle
)m_macList
) ;
695 LAutoScroll( (ListHandle
)m_macList
) ;
699 bool wxListBox::MacIsSelected( int n
) const
701 Cell cell
= { 0 , 0 } ;
703 return LGetSelect( false , &cell
, (ListHandle
)m_macList
) ;
706 void wxListBox::MacDestroy()
708 // DisposeExtLDEFInfo( m_macList ) ;
711 int wxListBox::MacGetSelection() const
713 Cell cell
= { 0 , 0 } ;
714 if ( LGetSelect( true , &cell
, (ListHandle
)m_macList
) )
720 int wxListBox::MacGetSelections( wxArrayInt
& aSelections
) const
726 Cell cell
= { 0 , 0 } ;
729 while ( LGetSelect( true , &cell
,(ListHandle
) m_macList
) )
731 aSelections
.Add( cell
.v
) ;
738 void wxListBox::MacSet( int n
, const char * text
)
740 // our implementation does not store anything in the list
741 // so we just have to redraw
742 Cell cell
= { 0 , 0 } ;
744 // LSetCell(text, strlen(text), cell, m_macList);
748 void wxListBox::MacScrollTo( int n
)
750 // TODO implement scrolling
753 void wxListBox::OnSize( const wxSizeEvent
&event
)
758 GetListCellSize((ListHandle
)m_macList
, &pt
);
760 pt
= (**(ListHandle
)m_macList
).cellSize
;
762 pt
.h
= m_width
- 15 ;
763 LCellSize( pt
, (ListHandle
)m_macList
) ;
766 void wxListBox::MacHandleControlClick( WXWidget control
, wxInt16 controlpart
)
768 Boolean wasDoubleClick
= false ;
771 ::GetControlData( (ControlHandle
) m_macControl
, kControlNoPart
, kControlListBoxDoubleClickTag
, sizeof( wasDoubleClick
) , (char*) &wasDoubleClick
, &result
) ;
772 if ( !wasDoubleClick
)
782 void wxListBox::MacSetRedraw( bool doDraw
)
784 LSetDrawingMode( doDraw
, (ListHandle
)m_macList
) ;
788 void wxListBox::MacDoClick()
790 wxArrayInt aSelections
;
791 int n
, count
= GetSelections(aSelections
);
793 if ( count
== m_selectionPreImage
.GetCount() )
795 bool hasChanged
= false ;
796 for ( int i
= 0 ; i
< count
; ++i
)
798 if ( aSelections
[i
] != m_selectionPreImage
[i
] )
810 m_selectionPreImage
= aSelections
;
812 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_SELECTED
, m_windowId
);
813 event
.SetEventObject( this );
818 if ( HasClientObjectData() )
819 event
.SetClientObject( GetClientObject(n
) );
820 else if ( HasClientUntypedData() )
821 event
.SetClientData( GetClientData(n
) );
822 event
.SetString( GetString(n
) );
829 event
.m_commandInt
= n
;
831 GetEventHandler()->ProcessEvent(event
);
834 void wxListBox::MacDoDoubleClick()
836 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
, m_windowId
);
837 event
.SetEventObject( this );
838 GetEventHandler()->ProcessEvent(event
) ;
841 static long sLastTypeIn
= 0 ;
843 void wxListBox::OnChar(wxKeyEvent
& event
)
845 EventRecord
*ev
= (EventRecord
*) (wxTheApp
->MacGetCurrentEvent() ) ;
848 keychar
= short(ev
->message
& charCodeMask
);
849 keycode
= short(ev
->message
& keyCodeMask
) >> 8 ;
850 if ( event
.KeyCode() == WXK_SPACE
)
852 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_SELECTED
, m_windowId
);
853 event
.SetEventObject( this );
855 wxArrayInt aSelections
;
856 int n
, count
= GetSelections(aSelections
);
860 if ( HasClientObjectData() )
861 event
.SetClientObject( GetClientObject(n
) );
862 else if ( HasClientUntypedData() )
863 event
.SetClientData( GetClientData(n
) );
864 event
.SetString( GetString(n
) );
871 event
.m_commandInt
= n
;
873 GetEventHandler()->ProcessEvent(event
);
875 else if ( event
.KeyCode() == WXK_DOWN
|| event
.KeyCode() == WXK_UP
)
879 // ::HandleControlKey( (ControlHandle) m_macControl , keycode , keychar , ev->modifiers ) ;
883 if ( ev
->when
> m_lastTypeIn
+ 60 )
887 m_lastTypeIn
= ev
->when
;
888 m_typeIn
+= (char) event
.KeyCode() ;
889 int line
= FindString("*"+m_typeIn
+"*") ;