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