1 ///////////////////////////////////////////////////////////////////////////////
8 // Copyright: (c) AUTHOR
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "listbox.h"
16 #include "wx/listbox.h"
17 #include "wx/settings.h"
18 #include "wx/dynarray.h"
24 #if !USE_SHARED_LIBRARY
25 IMPLEMENT_DYNAMIC_CLASS(wxListBox
, wxControl
)
27 BEGIN_EVENT_TABLE(wxListBox
, wxControl
)
28 EVT_SIZE( wxListBox::OnSize
)
32 #include <wx/mac/uma.h>
34 extern "C" void MacDrawStringCell(Rect
*cellRect
, Cell lCell
, ListHandle theList
, long refCon
) ;
35 const short kwxMacListWithVerticalScrollbar
= 128 ;
37 // ============================================================================
38 // list box control implementation
39 // ============================================================================
42 wxListBox::wxListBox()
48 bool wxListBox::Create(wxWindow
*parent
, wxWindowID id
,
51 int n
, const wxString choices
[],
53 const wxValidator
& validator
,
56 m_noItems
= 0 ; // this will be increased by our append command
61 m_macHorizontalBorder
= 5 ; // additional pixels around the real control
62 m_macVerticalBorder
= 5 ;
64 MacPreControlCreate( parent
, id
, "" , pos
, size
,style
, validator
, name
, &bounds
, title
) ;
66 m_macControl
= UMANewControl( parent
->GetMacRootWindow() , &bounds
, title
, true , kwxMacListWithVerticalScrollbar
, 0 , 0,
67 kControlListBoxProc
, (long) this ) ;
70 UMAGetControlData( m_macControl
, kControlNoPart
, kControlListBoxListHandleTag
, sizeof( ListHandle
) , (char*) &m_macList
, &result
) ;
72 NewExtLDEFInfo( m_macList
, MacDrawStringCell
, (long) this ) ;
73 (**m_macList
).selFlags
= lOnlyOne
;
74 if ( style
& wxLB_MULTIPLE
)
76 (**m_macList
).selFlags
+= lNoExtend
;
78 else if ( style
& wxLB_EXTENDED
)
80 (**m_macList
).selFlags
+= lExtendDrag
;
82 Point pt
= (**m_macList
).cellSize
;
84 LCellSize( pt
, m_macList
) ;
86 LAddColumn( 1 , 0 , m_macList
) ;
88 MacPostControlCreate() ;
90 ControlFontStyleRec controlstyle
;
91 controlstyle
.flags
= kControlUseFontMask
+ kControlUseSizeMask
;
92 //controlstyle.font = kControlFontSmallSystemFont ;
93 controlstyle
.font
= kFontIDMonaco
;
94 controlstyle
.size
= 9 ;
95 ::UMASetControlFontStyle( m_macControl
, &controlstyle
) ;
97 for ( int i
= 0 ; i
< n
; i
++ )
99 Append( choices
[i
] ) ;
102 LSetDrawingMode( true , m_macList
) ;
107 wxListBox::~wxListBox()
109 // DisposeExtLDEFInfo( m_macList ) ;
112 void wxListBox::SetFirstItem(int N
)
117 void wxListBox::SetFirstItem(const wxString
& s
)
119 MacScrollTo( FindString( s
) ) ;
122 void wxListBox::Delete(int N
)
124 m_dataArray
.Remove( N
) ;
125 m_stringArray
.Remove( N
) ;
131 void wxListBox::Append(const wxString
& item
)
133 Append( item
, NULL
) ;
136 void wxListBox::Append(const wxString
& item
, char *Client_data
)
138 if( wxApp::s_macDefaultEncodingIsPC
)
140 m_stringArray
.Add( wxMacMakeMacStringFromPC( item
) ) ;
143 m_stringArray
.Add( item
) ;
144 m_dataArray
.Add( Client_data
) ;
150 void wxListBox::Set(int n
, const wxString
*choices
, char** clientData
)
153 for( int i
= 0 ; i
< n
; ++i
)
156 Append( choices
[i
] , clientData
[0] ) ;
158 Append( choices
[i
] ) ;
162 int wxListBox::FindString(const wxString
& st
) const
165 if( wxApp::s_macDefaultEncodingIsPC
)
167 s
= wxMacMakeMacStringFromPC( st
) ;
172 if ( s
.Right(1) == "*" )
174 wxString search
= s
.Left( s
.Length() - 1 ) ;
175 int len
= search
.Length() ;
176 for ( int i
= 0 ; i
< m_noItems
; ++ i
)
178 if ( equalstring( m_stringArray
[i
].Left( len
) , search
, false , false ) )
184 for ( int i
= 0 ; i
< m_noItems
; ++ i
)
186 if ( equalstring( m_stringArray
[i
] , s
, false , false ) )
193 void wxListBox::Clear()
196 m_stringArray
.Empty() ;
197 m_dataArray
.Empty() ;
201 void wxListBox::SetSelection(int N
, bool select
)
203 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
204 "invalid index in wxListBox::SetSelection" );
205 MacSetSelection( N
, select
) ;
208 bool wxListBox::Selected(int N
) const
210 wxCHECK_MSG( N
>= 0 && N
< m_noItems
, FALSE
,
211 "invalid index in wxListBox::Selected" );
213 return MacIsSelected( N
) ;
216 void wxListBox::Deselect(int N
)
218 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
219 "invalid index in wxListBox::Deselect" );
221 SetSelection( N
, false ) ;
224 char *wxListBox::GetClientData(int N
) const
226 wxCHECK_MSG( N
>= 0 && N
< m_noItems
, NULL
,
227 "invalid index in wxListBox::GetClientData" );
229 return m_dataArray
[N
];
232 void wxListBox::SetClientData(int N
, char *Client_data
)
234 wxCHECK_RET( N
>= 0 && N
< m_noItems
,
235 "invalid index in wxListBox::SetClientData" );
237 m_dataArray
[N
] = Client_data
;
240 // Return number of selections and an array of selected integers
241 int wxListBox::GetSelections(wxArrayInt
& aSelections
) const
243 return MacGetSelections( aSelections
) ;
246 if ((m_windowStyle & wxLB_MULTIMacE) || (m_windowStyle & wxLB_EXTENDED))
249 for ( int n = 0; n < no_sel; n++ )
254 else // single-selection listbox
263 // Get single selection, for single choice list items
264 int wxListBox::GetSelection() const
266 return MacGetSelection() ;
269 // Find string for position
270 wxString
wxListBox::GetString(int N
) const
272 if( wxApp::s_macDefaultEncodingIsPC
)
274 return wxMacMakePCStringFromMac( m_stringArray
[N
] ) ;
277 return m_stringArray
[N
] ;
280 void wxListBox::InsertItems(int nItems
, const wxString items
[], int pos
)
282 for ( int i
= 0 ; i
< nItems
; i
++ )
284 m_stringArray
.Insert( items
[i
] , pos
+ i
) ;
285 m_dataArray
.Insert( NULL
, pos
+ i
) ;
286 MacInsert( pos
+ i
, items
[i
] ) ;
292 void wxListBox::SetString(int N
, const wxString
& s
)
294 m_stringArray
[N
] = s
;
298 int wxListBox::Number () const
303 // For single selection items only
304 wxString
wxListBox::GetStringSelection () const
306 int sel
= GetSelection ();
309 return GetString (sel
);
315 bool wxListBox::SetStringSelection (const wxString
& s
, bool flag
)
317 int sel
= FindString (s
);
320 SetSelection (sel
, flag
);
327 void wxListBox::Command (wxCommandEvent
& event
)
329 if (event
.m_extraLong
)
330 SetSelection (event
.m_commandInt
);
333 Deselect (event
.m_commandInt
);
336 ProcessCommand (event
);
339 // ============================================================================
340 // list box control implementation
341 // ============================================================================
343 void MacDrawStringCell(Rect
*cellRect
, Cell lCell
, ListHandle theList
, long refCon
)
346 // typecast our refCon
347 list
= (wxListBox
*)refCon
;
349 MoveTo(cellRect
->left
+ 4 , cellRect
->top
+ 10 );
350 const wxString text
= list
->m_stringArray
[lCell
.v
] ;
351 ::TextFont( kFontIDMonaco
) ;
354 DrawText(text
, 0 , text
.Length());
358 void wxListBox::MacDelete( int N
)
362 Cell cell
= { 0 , 0 } ;
363 UMAGetControlData( m_macControl
, kControlNoPart
, kControlListBoxListHandleTag
, sizeof( ListHandle
) , (char*) &list
, &result
) ;
364 LDelRow( 1 , N
, list
) ;
367 void wxListBox::MacInsert( int n
, const char * text
)
374 LAddRow( 1 , cell
.v
, m_macList
) ;
377 void wxListBox::MacAppend( const char * text
)
379 Cell cell
= { 0 , 0 } ;
380 cell
.v
= (**m_macList
).dataBounds
.bottom
;
381 LAddRow( 1 , cell
.v
, m_macList
) ;
384 void wxListBox::MacClear()
386 LDelRow( (**m_macList
).dataBounds
.bottom
, 0 , m_macList
) ;
389 void wxListBox::MacSetSelection( int n
, bool select
)
391 Cell cell
= { 0 , 0 } ;
392 if ( LGetSelect( TRUE
, &cell
, m_macList
) )
394 LSetSelect( false , cell
, m_macList
) ;
398 LSetSelect( select
, cell
, m_macList
) ;
399 LAutoScroll( m_macList
) ;
402 bool wxListBox::MacIsSelected( int n
) const
404 Cell cell
= { 0 , 0 } ;
406 return LGetSelect( false , &cell
, m_macList
) ;
409 void wxListBox::MacDestroy()
411 // DisposeExtLDEFInfo( m_macList ) ;
414 int wxListBox::MacGetSelection() const
416 Cell cell
= { 0 , 0 } ;
417 if ( LGetSelect( true , &cell
, m_macList
) )
423 int wxListBox::MacGetSelections( wxArrayInt
& aSelections
) const
429 Cell cell
= { 0 , 0 } ;
432 while ( LGetSelect( true , &cell
, m_macList
) )
434 aSelections
.Add( cell
.v
) ;
441 void wxListBox::MacSet( int n
, const char * text
)
443 // our implementation does not store anything in the list
444 // so we just have to redraw
445 Cell cell
= { 0 , 0 } ;
447 LDraw( cell
, m_macList
) ;
450 void wxListBox::MacScrollTo( int n
)
452 // TODO implement scrolling
455 void wxListBox::OnSize( const wxSizeEvent
&event
)
457 Point pt
= (**m_macList
).cellSize
;
458 pt
.h
= m_width
- 15 /* scrollbar */ - m_macHorizontalBorder
* 2 ;
459 LCellSize( pt
, m_macList
) ;
462 void wxListBox::MacHandleControlClick( ControlHandle control
, SInt16 controlpart
)
464 Boolean wasDoubleClick
= false ;
467 UMAGetControlData( m_macControl
, kControlNoPart
, kControlListBoxDoubleClickTag
, sizeof( wasDoubleClick
) , (char*) &wasDoubleClick
, &result
) ;
468 if ( !wasDoubleClick
)
478 void wxListBox::MacSetRedraw( bool doDraw
)
480 LSetDrawingMode( doDraw
, m_macList
) ;
484 void wxListBox::MacDoClick()
486 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_SELECTED
, m_windowId
);
487 wxArrayInt aSelections
;
488 int count
= GetSelections(aSelections
);
491 event
.m_commandInt
= aSelections
[0] ;
492 event
.m_clientData
= GetClientData(event
.m_commandInt
);
493 wxString
str(GetString(event
.m_commandInt
));
495 event
.m_commandString
= copystring((char *)(const char *)str
);
501 event.m_commandInt = -1 ;
502 event.m_commandString = copystring("") ;
506 event
.SetEventObject( this );
507 ProcessCommand(event
);
508 if (event
.m_commandString
)
509 delete[] event
.m_commandString
;
512 void wxListBox::MacDoDoubleClick()
514 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
, m_windowId
);
515 event
.SetEventObject( this );
516 GetEventHandler()->ProcessEvent(event
) ;