]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/mac/carbon/listbox.cpp
OGL: misc.cpp clashes with misc.c frim iODBC, therefore
[wxWidgets.git] / src / mac / carbon / listbox.cpp
... / ...
CommitLineData
1///////////////////////////////////////////////////////////////////////////////
2// Name: listbox.cpp
3// Purpose: wxListBox
4// Author: AUTHOR
5// Modified by:
6// Created: ??/??/98
7// RCS-ID: $Id$
8// Copyright: (c) AUTHOR
9// Licence: wxWindows licence
10///////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
13#pragma implementation "listbox.h"
14#endif
15
16#include "wx/listbox.h"
17#include "wx/settings.h"
18#include "wx/dynarray.h"
19#include "wx/log.h"
20
21#include "wx/utils.h"
22#include "extldef.h"
23
24 IMPLEMENT_DYNAMIC_CLASS(wxListBox, wxControl)
25
26BEGIN_EVENT_TABLE(wxListBox, wxControl)
27 EVT_SIZE( wxListBox::OnSize )
28END_EVENT_TABLE()
29
30#include <wx/mac/uma.h>
31
32extern "C" void MacDrawStringCell(Rect *cellRect, Cell lCell, ListHandle theList, long refCon) ;
33const short kwxMacListWithVerticalScrollbar = 128 ;
34
35// ============================================================================
36// list box control implementation
37// ============================================================================
38
39// Listbox item
40wxListBox::wxListBox()
41{
42 m_noItems = 0;
43 m_selected = 0;
44}
45
46bool wxListBox::Create(wxWindow *parent, wxWindowID id,
47 const wxPoint& pos,
48 const wxSize& size,
49 int n, const wxString choices[],
50 long style,
51 const wxValidator& validator,
52 const wxString& name)
53{
54 m_noItems = 0 ; // this will be increased by our append command
55 m_selected = 0;
56
57 Rect bounds ;
58 Str255 title ;
59 m_macHorizontalBorder = 5 ; // additional pixels around the real control
60 m_macVerticalBorder = 5 ;
61
62 MacPreControlCreate( parent , id , "" , pos , size ,style, validator , name , &bounds , title ) ;
63
64 m_macControl = UMANewControl( parent->GetMacRootWindow() , &bounds , title , true , kwxMacListWithVerticalScrollbar , 0 , 0,
65 kControlListBoxProc , (long) this ) ;
66
67 long result ;
68 UMAGetControlData( m_macControl , kControlNoPart , kControlListBoxListHandleTag , sizeof( ListHandle ) , (char*) &m_macList , &result ) ;
69
70 NewExtLDEFInfo( m_macList , MacDrawStringCell , (long) this ) ;
71 (**m_macList).selFlags = 0 ;
72 if ( style & wxLB_MULTIPLE )
73 {
74 (**m_macList).selFlags += lNoExtend ;
75 }
76 else if ( style & wxLB_EXTENDED )
77 {
78 (**m_macList).selFlags += lExtendDrag ;
79 }
80 else
81 {
82 (**m_macList).selFlags = lOnlyOne ;
83 }
84 Point pt = (**m_macList).cellSize ;
85 pt.v = 14 ;
86 LCellSize( pt , m_macList ) ;
87
88 LAddColumn( 1 , 0 , m_macList ) ;
89
90 MacPostControlCreate() ;
91
92 ControlFontStyleRec controlstyle ;
93 controlstyle.flags = kControlUseFontMask + kControlUseSizeMask ;
94 //controlstyle.font = kControlFontSmallSystemFont ;
95 controlstyle.font = kFontIDMonaco ;
96 controlstyle.size = 9 ;
97 ::UMASetControlFontStyle( m_macControl , &controlstyle ) ;
98
99 for ( int i = 0 ; i < n ; i++ )
100 {
101 Append( choices[i] ) ;
102 }
103
104 LSetDrawingMode( true , m_macList ) ;
105
106 return TRUE;
107}
108
109wxListBox::~wxListBox()
110{
111 Free() ;
112 DisposeExtLDEFInfo( m_macList ) ;
113}
114
115void wxListBox::Free()
116{
117#if wxUSE_OWNER_DRAWN
118 if ( m_windowStyle & wxLB_OWNERDRAW )
119 {
120 size_t uiCount = m_aItems.Count();
121 while ( uiCount-- != 0 ) {
122 delete m_aItems[uiCount];
123 }
124
125 m_aItems.Clear();
126 }
127 else
128#endif // wxUSE_OWNER_DRAWN
129 if ( HasClientObjectData() )
130 {
131 for ( size_t n = 0; n < (size_t)m_noItems; n++ )
132 {
133 delete GetClientObject(n);
134 }
135 }
136}
137
138void wxListBox::DoSetFirstItem(int N)
139{
140 MacScrollTo( N ) ;
141}
142
143void wxListBox::Delete(int N)
144{
145 wxCHECK_RET( N >= 0 && N < m_noItems,
146 wxT("invalid index in wxListBox::Delete") );
147
148#if wxUSE_OWNER_DRAWN
149 delete m_aItems[N];
150 m_aItems.Remove(N);
151#else // !wxUSE_OWNER_DRAWN
152 if ( HasClientObjectData() )
153 {
154 delete GetClientObject(N);
155 }
156#endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
157 m_stringArray.Remove( N ) ;
158 m_dataArray.Remove( N ) ;
159 m_noItems --;
160
161 MacDelete( N ) ;
162}
163
164int wxListBox::DoAppend(const wxString& item)
165{
166 int index = m_noItems ;
167 if( wxApp::s_macDefaultEncodingIsPC )
168 {
169 m_stringArray.Add( wxMacMakeMacStringFromPC( item ) ) ;
170 }
171 else
172 m_stringArray.Add( item ) ;
173 m_noItems ++;
174 MacAppend( item ) ;
175
176 return index ;
177}
178
179void wxListBox::DoSetItems(const wxArrayString& choices, void** clientData)
180{
181 MacSetRedraw( false ) ;
182 Clear() ;
183 int n = choices.GetCount();
184
185 for( int i = 0 ; i < n ; ++i )
186 {
187 if ( clientData )
188 {
189#if wxUSE_OWNER_DRAWN
190 wxASSERT_MSG(clientData[i] == NULL,
191 wxT("Can't use client data with owner-drawn listboxes"));
192#else // !wxUSE_OWNER_DRAWN
193 Append( choices[i] , clientData[i] ) ;
194 #endif
195 }
196 else
197 Append( choices[i] ) ;
198 }
199
200#if wxUSE_OWNER_DRAWN
201 if ( m_windowStyle & wxLB_OWNERDRAW ) {
202 // first delete old items
203 size_t ui = m_aItems.Count();
204 while ( ui-- != 0 ) {
205 delete m_aItems[ui];
206 }
207 m_aItems.Empty();
208
209 // then create new ones
210 for ( ui = 0; ui < (size_t)m_noItems; ui++ ) {
211 wxOwnerDrawn *pNewItem = CreateItem(ui);
212 pNewItem->SetName(choices[ui]);
213 m_aItems.Add(pNewItem);
214 }
215 }
216#endif // wxUSE_OWNER_DRAWN
217 MacSetRedraw( true ) ;
218}
219
220bool wxListBox::HasMultipleSelection() const
221{
222 return (m_windowStyle & wxLB_MULTIPLE) || (m_windowStyle & wxLB_EXTENDED);
223}
224
225int wxListBox::FindString(const wxString& st) const
226{
227 wxString s ;
228 if( wxApp::s_macDefaultEncodingIsPC )
229 {
230 s = wxMacMakeMacStringFromPC( st ) ;
231 }
232 else
233 s = st ;
234
235 if ( s.Right(1) == "*" )
236 {
237 wxString search = s.Left( s.Length() - 1 ) ;
238 int len = search.Length() ;
239 for ( int i = 0 ; i < m_noItems ; ++ i )
240 {
241 if ( equalstring( m_stringArray[i].Left( len ) , search , false , false ) )
242 return i ;
243 }
244 }
245 else
246 {
247 for ( int i = 0 ; i < m_noItems ; ++ i )
248 {
249 if ( equalstring( m_stringArray[i] , s , false , false ) )
250 return i ;
251 }
252 }
253 return -1;
254}
255
256void wxListBox::Clear()
257{
258 Free();
259 m_noItems = 0;
260 m_stringArray.Empty() ;
261 m_dataArray.Empty() ;
262 MacClear() ;
263}
264
265void wxListBox::SetSelection(int N, bool select)
266{
267 wxCHECK_RET( N >= 0 && N < m_noItems,
268 "invalid index in wxListBox::SetSelection" );
269 MacSetSelection( N , select ) ;
270}
271
272bool wxListBox::IsSelected(int N) const
273{
274 wxCHECK_MSG( N >= 0 && N < m_noItems, FALSE,
275 "invalid index in wxListBox::Selected" );
276
277 return MacIsSelected( N ) ;
278}
279
280void *wxListBox::DoGetItemClientData(int N) const
281{
282 wxCHECK_MSG( N >= 0 && N < m_noItems, NULL,
283 "invalid index in wxListBox::GetClientData" );
284
285 return (void *)m_dataArray[N];
286}
287
288wxClientData *wxListBox::DoGetItemClientObject(int N) const
289{
290 return (wxClientData *) DoGetItemClientData( N ) ;
291}
292
293void wxListBox::DoSetItemClientData(int N, void *Client_data)
294{
295 wxCHECK_RET( N >= 0 && N < m_noItems,
296 "invalid index in wxListBox::SetClientData" );
297
298#if wxUSE_OWNER_DRAWN
299 if ( m_windowStyle & wxLB_OWNERDRAW )
300 {
301 // client data must be pointer to wxOwnerDrawn, otherwise we would crash
302 // in OnMeasure/OnDraw.
303 wxFAIL_MSG(wxT("Can't use client data with owner-drawn listboxes"));
304 }
305#endif // wxUSE_OWNER_DRAWN
306 wxASSERT_MSG( m_dataArray.GetCount() >= N , "invalid client_data array" ) ;
307
308 if ( m_dataArray.GetCount() > N )
309 {
310 m_dataArray[N] = (char*) Client_data ;
311 }
312 else
313 {
314 m_dataArray.Add( (char*) Client_data ) ;
315 }
316}
317
318void wxListBox::DoSetItemClientObject(int n, wxClientData* clientData)
319{
320 DoSetItemClientData(n, clientData);
321}
322
323// Return number of selections and an array of selected integers
324int wxListBox::GetSelections(wxArrayInt& aSelections) const
325{
326 return MacGetSelections( aSelections ) ;
327
328/* TODO
329 if ((m_windowStyle & wxLB_MULTIMacE) || (m_windowStyle & wxLB_EXTENDED))
330 {
331 int no_sel = ??
332 for ( int n = 0; n < no_sel; n++ )
333 aSelections.Add(??);
334
335 return no_sel;
336 }
337 else // single-selection listbox
338 {
339 aSelections.Add(??);
340
341 return 1;
342 }
343*/
344}
345
346// Get single selection, for single choice list items
347int wxListBox::GetSelection() const
348{
349 return MacGetSelection() ;
350}
351
352// Find string for position
353wxString wxListBox::GetString(int N) const
354{
355 if( wxApp::s_macDefaultEncodingIsPC )
356 {
357 return wxMacMakePCStringFromMac( m_stringArray[N] ) ;
358 }
359 else
360 return m_stringArray[N] ;
361}
362
363void wxListBox::DoInsertItems(const wxArrayString& items, int pos)
364{
365 wxCHECK_RET( pos >= 0 && pos <= m_noItems,
366 wxT("invalid index in wxListBox::InsertItems") );
367
368 int nItems = items.GetCount();
369
370 for ( int i = 0 ; i < nItems ; i++ )
371 {
372 m_stringArray.Insert( items[i] , pos + i ) ;
373 m_dataArray.Insert( NULL , pos + i ) ;
374 MacInsert( pos + i , items[i] ) ;
375 }
376
377 m_noItems += nItems;
378}
379
380void wxListBox::SetString(int N, const wxString& s)
381{
382 m_stringArray[N] = s ;
383 MacSet( N , s ) ;
384}
385
386wxSize wxListBox::DoGetBestSize() const
387{
388 return wxSize(100, 100);
389}
390
391int wxListBox::GetCount() const
392{
393 return m_noItems;
394}
395
396void wxListBox::SetupColours()
397{
398 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW));
399 SetForegroundColour(GetParent()->GetForegroundColour());
400}
401
402#if wxUSE_OWNER_DRAWN
403
404class wxListBoxItem : public wxOwnerDrawn
405{
406public:
407 wxListBoxItem(const wxString& str = "");
408};
409
410wxListBoxItem::wxListBoxItem(const wxString& str) : wxOwnerDrawn(str, FALSE)
411{
412 // no bitmaps/checkmarks
413 SetMarginWidth(0);
414}
415
416wxOwnerDrawn *wxListBox::CreateItem(size_t n)
417{
418 return new wxListBoxItem();
419}
420
421#endif //USE_OWNER_DRAWN
422
423// ============================================================================
424// list box control implementation
425// ============================================================================
426
427void MacDrawStringCell(Rect *cellRect, Cell lCell, ListHandle theList, long refCon)
428{
429 wxListBox* list;
430 // typecast our refCon
431 list = (wxListBox*)refCon;
432
433 MoveTo(cellRect->left + 4 , cellRect->top + 10 );
434 const wxString text = list->m_stringArray[lCell.v] ;
435 ::TextFont( kFontIDMonaco ) ;
436 ::TextSize( 9 );
437 ::TextFace( 0 ) ;
438 DrawText(text, 0 , text.Length());
439
440}
441
442void wxListBox::MacDelete( int N )
443{
444 ListHandle list ;
445 long result ;
446 Cell cell = { 0 , 0 } ;
447 UMAGetControlData( m_macControl , kControlNoPart , kControlListBoxListHandleTag , sizeof( ListHandle ) , (char*) &list , &result ) ;
448 LDelRow( 1 , N , list ) ;
449}
450
451void wxListBox::MacInsert( int n , const char * text)
452{
453 Cell cell ;
454
455 cell.h = 0 ;
456 cell.v = n ;
457
458 LAddRow( 1 , cell.v , m_macList ) ;
459}
460
461void wxListBox::MacAppend( const char * text)
462{
463 Cell cell = { 0 , 0 } ;
464 cell.v = (**m_macList).dataBounds.bottom ;
465 LAddRow( 1 , cell.v , m_macList ) ;
466}
467
468void wxListBox::MacClear()
469{
470 LDelRow( (**m_macList).dataBounds.bottom , 0 , m_macList ) ;
471}
472
473void wxListBox::MacSetSelection( int n , bool select )
474{
475 Cell cell = { 0 , 0 } ;
476 if ( LGetSelect( TRUE , &cell , m_macList ) )
477 {
478 LSetSelect( false , cell , m_macList ) ;
479 }
480
481 cell.v = n ;
482 LSetSelect( select , cell , m_macList ) ;
483 LAutoScroll( m_macList ) ;
484}
485
486bool wxListBox::MacIsSelected( int n ) const
487{
488 Cell cell = { 0 , 0 } ;
489 cell.v = n ;
490 return LGetSelect( false , &cell , m_macList ) ;
491}
492
493void wxListBox::MacDestroy()
494{
495// DisposeExtLDEFInfo( m_macList ) ;
496}
497
498int wxListBox::MacGetSelection() const
499{
500 Cell cell = { 0 , 0 } ;
501 if ( LGetSelect( true , &cell , m_macList ) )
502 return cell.v ;
503 else
504 return -1 ;
505}
506
507int wxListBox::MacGetSelections( wxArrayInt& aSelections ) const
508{
509 int no_sel = 0 ;
510
511 aSelections.Empty();
512
513 Cell cell = { 0 , 0 } ;
514 cell.v = 0 ;
515
516 while ( LGetSelect( true , &cell , m_macList ) )
517 {
518 aSelections.Add( cell.v ) ;
519 no_sel++ ;
520 cell.v++ ;
521 }
522 return no_sel ;
523}
524
525void wxListBox::MacSet( int n , const char * text )
526{
527 // our implementation does not store anything in the list
528 // so we just have to redraw
529 Cell cell = { 0 , 0 } ;
530 cell.v = n ;
531 LDraw( cell , m_macList ) ;
532}
533
534void wxListBox::MacScrollTo( int n )
535{
536 // TODO implement scrolling
537}
538
539void wxListBox::OnSize( const wxSizeEvent &event)
540{
541 Point pt = (**m_macList).cellSize ;
542 pt.h = m_width - 15 /* scrollbar */ - m_macHorizontalBorder * 2 ;
543 LCellSize( pt , m_macList ) ;
544}
545
546void wxListBox::MacHandleControlClick( ControlHandle control , SInt16 controlpart )
547{
548 Boolean wasDoubleClick = false ;
549 long result ;
550
551 UMAGetControlData( m_macControl , kControlNoPart , kControlListBoxDoubleClickTag , sizeof( wasDoubleClick ) , (char*) &wasDoubleClick , &result ) ;
552 if ( !wasDoubleClick )
553 {
554 MacDoClick() ;
555 }
556 else
557 {
558 MacDoDoubleClick() ;
559 }
560}
561
562void wxListBox::MacSetRedraw( bool doDraw )
563{
564 LSetDrawingMode( doDraw , m_macList ) ;
565
566}
567
568void wxListBox::MacDoClick()
569{
570 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, m_windowId);
571 event.SetEventObject( this );
572
573 wxArrayInt aSelections;
574 int n, count = GetSelections(aSelections);
575 if ( count > 0 )
576 {
577 n = aSelections[0];
578 if ( HasClientObjectData() )
579 event.SetClientObject( GetClientObject(n) );
580 else if ( HasClientUntypedData() )
581 event.SetClientData( GetClientData(n) );
582 event.SetString( GetString(n) );
583 }
584 else
585 {
586 n = -1;
587 }
588
589 event.m_commandInt = n;
590
591 GetEventHandler()->ProcessEvent(event);
592}
593
594void wxListBox::MacDoDoubleClick()
595{
596 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED, m_windowId);
597 event.SetEventObject( this );
598 GetEventHandler()->ProcessEvent(event) ;
599}