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
)
33 #include "wx/mac/uma.h"
37 unsigned short instruction
;
39 } ldefRec
, *ldefPtr
, **ldefHandle
;
43 static pascal void wxMacListDefinition( short message
, Boolean isSelected
, Rect
*drawRect
,
44 Cell cell
, short dataOffset
, short dataLength
,
45 ListHandle listHandle
) ;
48 static pascal void wxMacListDefinition( short message
, Boolean isSelected
, Rect
*drawRect
,
49 Cell cell
, short dataOffset
, short dataLength
,
50 ListHandle listHandle
)
55 RgnHandle savedClipRegion
;
59 SetPort((**listHandle
).port
);
60 grafPtr
= (**listHandle
).port
;
61 // typecast our refCon
62 list
= (wxListBox
*) GetControlReference( (ControlHandle
) GetListRefCon(listHandle
) );
64 // Calculate the cell rect.
75 const wxString text
= list
->m_stringArray
[cell
.v
] ;
77 // Save the current clip region, and set the clip region to the area we are about
80 savedClipRegion
= NewRgn();
81 GetClip( savedClipRegion
);
84 EraseRect( drawRect
);
86 MoveTo(drawRect
->left
+ 4 , drawRect
->top
+ 10 );
87 ::TextFont( kFontIDMonaco
) ;
91 DrawText(text
, 0 , text
.Length());
92 // If the cell is hilited, do the hilite now. Paint the cell contents with the
93 // appropriate QuickDraw transform mode.
96 savedPenMode
= GetPortPenMode( grafPtr
);
97 SetPortPenMode( grafPtr
, hilitetransfermode
);
98 PaintRect( drawRect
);
99 SetPortPenMode( grafPtr
, savedPenMode
);
102 // Restore the saved clip region.
104 SetClip( savedClipRegion
);
105 DisposeRgn( savedClipRegion
);
110 // Hilite or unhilite the cell. Paint the cell contents with the
111 // appropriate QuickDraw transform mode.
114 savedPenMode
= GetPortPenMode( grafPtr
);
115 SetPortPenMode( grafPtr
, hilitetransfermode
);
116 PaintRect( drawRect
);
117 SetPortPenMode( grafPtr
, savedPenMode
);
125 extern "C" void MacDrawStringCell(Rect
*cellRect
, Cell lCell
, ListHandle theList
, long refCon
) ;
126 const short kwxMacListWithVerticalScrollbar
= 128 ;
127 const short kwxMacListItemHeight
= 14 ;
129 // ============================================================================
130 // list box control implementation
131 // ============================================================================
134 wxListBox::wxListBox()
141 static ListDefUPP macListDefUPP
= NULL
;
143 bool wxListBox::Create(wxWindow
*parent
, wxWindowID id
,
146 int n
, const wxString choices
[],
148 const wxValidator
& validator
,
149 const wxString
& name
)
151 m_noItems
= 0 ; // this will be increased by our append command
157 MacPreControlCreate( parent
, id
, "" , pos
, size
,style
, validator
, name
, &bounds
, title
) ;
160 listDef
.defType
= kListDefUserProcType
;
161 if ( macListDefUPP
== NULL
)
163 macListDefUPP
= NewListDefUPP( wxMacListDefinition
);
165 listDef
.u
.userProc
= macListDefUPP
;
171 CreateListBoxControl( parent
->MacGetRootWindow(), &bounds
, false, 0, 1, false, true,
172 kwxMacListItemHeight
, kwxMacListItemHeight
, 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);
184 m_macControl
= ::NewControl( parent
->MacGetRootWindow() , &bounds
, title
, false ,
185 kwxMacListWithVerticalScrollbar
, 0 , 0,
186 kControlListBoxProc
, (long) this ) ;
187 ::GetControlData( m_macControl
, kControlNoPart
, kControlListBoxListHandleTag
,
188 sizeof( ListHandle
) , (char*) &m_macList
, &result
) ;
190 HLock( (Handle
) m_macList
) ;
192 ldef
= (ldefHandle
) NewHandle( sizeof(ldefRec
) ) ;
193 if ( (**m_macList
).listDefProc
!= NULL
)
195 (**ldef
).instruction
= 0x4EF9; /* JMP instruction */
196 (**ldef
).function
= (void(*)()) listDef
.u
.userProc
;
197 (**m_macList
).listDefProc
= (Handle
) ldef
;
200 Point pt
= (**m_macList
).cellSize
;
201 pt
.v
= kwxMacListItemHeight
;
202 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
)
590 wxControl::Refresh( eraseBack
, rect
) ;
591 // MacRedrawControl() ;
594 #if wxUSE_OWNER_DRAWN
596 class wxListBoxItem
: public wxOwnerDrawn
599 wxListBoxItem(const wxString
& str
= "");
602 wxListBoxItem::wxListBoxItem(const wxString
& str
) : wxOwnerDrawn(str
, FALSE
)
604 // no bitmaps/checkmarks
608 wxOwnerDrawn
*wxListBox::CreateItem(size_t n
)
610 return new wxListBoxItem();
613 #endif //USE_OWNER_DRAWN
615 // ============================================================================
616 // list box control implementation
617 // ============================================================================
619 void MacDrawStringCell(Rect
*cellRect
, Cell lCell
, ListHandle theList
, long refCon
)
622 // typecast our refCon
623 list
= (wxListBox
*)refCon
;
625 MoveTo(cellRect
->left
+ 4 , cellRect
->top
+ 10 );
626 const wxString text
= list
->m_stringArray
[lCell
.v
] ;
627 ::TextFont( kFontIDMonaco
) ;
630 DrawText(text
, 0 , text
.Length());
634 void wxListBox::MacDelete( int N
)
636 LDelRow( 1 , N
, m_macList
) ;
640 void wxListBox::MacInsert( int n
, const char * text
)
642 Cell cell
= { 0 , 0 } ;
644 LAddRow( 1 , cell
.v
, m_macList
) ;
645 // LSetCell(text, strlen(text), cell, m_macList);
649 void wxListBox::MacAppend( const char * text
)
651 Cell cell
= { 0 , 0 } ;
652 cell
.v
= (**m_macList
).dataBounds
.bottom
;
653 LAddRow( 1 , cell
.v
, m_macList
) ;
654 // LSetCell(text, strlen(text), cell, m_macList);
658 void wxListBox::MacClear()
660 LDelRow( (**m_macList
).dataBounds
.bottom
, 0 , m_macList
) ;
664 void wxListBox::MacSetSelection( int n
, bool select
)
666 Cell cell
= { 0 , 0 } ;
667 if ( ! (m_windowStyle
& wxLB_MULTIPLE
) )
669 if ( LGetSelect( true , &cell
, m_macList
) )
671 LSetSelect( false , cell
, m_macList
) ;
676 LSetSelect( select
, cell
, m_macList
) ;
677 LAutoScroll( m_macList
) ;
681 bool wxListBox::MacIsSelected( int n
) const
683 Cell cell
= { 0 , 0 } ;
685 return LGetSelect( false , &cell
, m_macList
) ;
688 void wxListBox::MacDestroy()
690 // DisposeExtLDEFInfo( m_macList ) ;
693 int wxListBox::MacGetSelection() const
695 Cell cell
= { 0 , 0 } ;
696 if ( LGetSelect( true , &cell
, m_macList
) )
702 int wxListBox::MacGetSelections( wxArrayInt
& aSelections
) const
708 Cell cell
= { 0 , 0 } ;
711 while ( LGetSelect( true , &cell
, m_macList
) )
713 aSelections
.Add( cell
.v
) ;
720 void wxListBox::MacSet( int n
, const char * text
)
722 // our implementation does not store anything in the list
723 // so we just have to redraw
724 Cell cell
= { 0 , 0 } ;
726 // LSetCell(text, strlen(text), cell, m_macList);
730 void wxListBox::MacScrollTo( int n
)
732 // TODO implement scrolling
735 void wxListBox::OnSize( const wxSizeEvent
&event
)
740 GetListCellSize(m_macList
, &pt
);
742 pt
= (**m_macList
).cellSize
;
744 pt
.h
= m_width
- 15 ;
745 LCellSize( pt
, m_macList
) ;
748 void wxListBox::MacHandleControlClick( ControlHandle control
, SInt16 controlpart
)
750 Boolean wasDoubleClick
= false ;
753 ::GetControlData( m_macControl
, kControlNoPart
, kControlListBoxDoubleClickTag
, sizeof( wasDoubleClick
) , (char*) &wasDoubleClick
, &result
) ;
754 if ( !wasDoubleClick
)
764 void wxListBox::MacSetRedraw( bool doDraw
)
766 LSetDrawingMode( doDraw
, m_macList
) ;
770 void wxListBox::MacDoClick()
772 wxArrayInt aSelections
;
773 int n
, count
= GetSelections(aSelections
);
775 if ( count
== m_selectionPreImage
.GetCount() )
777 bool hasChanged
= false ;
778 for ( int i
= 0 ; i
< count
; ++i
)
780 if ( aSelections
[i
] != m_selectionPreImage
[i
] )
792 m_selectionPreImage
= aSelections
;
794 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_SELECTED
, m_windowId
);
795 event
.SetEventObject( this );
800 if ( HasClientObjectData() )
801 event
.SetClientObject( GetClientObject(n
) );
802 else if ( HasClientUntypedData() )
803 event
.SetClientData( GetClientData(n
) );
804 event
.SetString( GetString(n
) );
811 event
.m_commandInt
= n
;
813 GetEventHandler()->ProcessEvent(event
);
816 void wxListBox::MacDoDoubleClick()
818 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
, m_windowId
);
819 event
.SetEventObject( this );
820 GetEventHandler()->ProcessEvent(event
) ;