]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/mac/listbox.cpp
Tries to fix wxListBox focus problems.
[wxWidgets.git] / src / mac / 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/app.h"
17#include "wx/listbox.h"
18#include "wx/settings.h"
19#include "wx/dynarray.h"
20#include "wx/log.h"
21
22#include "wx/utils.h"
23#ifndef __DARWIN__
24// #include "extldef.h"
25#endif
26
27#if !USE_SHARED_LIBRARY
28 IMPLEMENT_DYNAMIC_CLASS(wxListBox, wxControl)
29
30BEGIN_EVENT_TABLE(wxListBox, wxControl)
31 EVT_SIZE( wxListBox::OnSize )
32END_EVENT_TABLE()
33#endif
34
35#include "wx/mac/uma.h"
36
37
38typedef struct {
39 unsigned short instruction;
40 void (*function)();
41} ldefRec, *ldefPtr, **ldefHandle;
42
43extern "C"
44{
45static pascal void wxMacListDefinition( short message, Boolean isSelected, Rect *drawRect,
46 Cell cell, short dataOffset, short dataLength,
47 ListHandle listHandle ) ;
48}
49
50static pascal void wxMacListDefinition( short message, Boolean isSelected, Rect *drawRect,
51 Cell cell, short dataOffset, short dataLength,
52 ListHandle listHandle )
53{
54 FontInfo fontInfo;
55 GrafPtr savePort;
56 GrafPtr grafPtr;
57 RgnHandle savedClipRegion;
58 SInt32 savedPenMode;
59 wxListBox* list;
60 GetPort(&savePort);
61 SetPort((**listHandle).port);
62 grafPtr = (**listHandle).port ;
63 // typecast our refCon
64 list = (wxListBox*) GetControlReference( (ControlHandle) GetListRefCon(listHandle) );
65
66 // Calculate the cell rect.
67
68 switch( message ) {
69 case lInitMsg:
70 break;
71
72 case lCloseMsg:
73 break;
74
75 case lDrawMsg:
76 {
77 const wxString text = list->m_stringArray[cell.v] ;
78
79 // Save the current clip region, and set the clip region to the area we are about
80 // to draw.
81
82 savedClipRegion = NewRgn();
83 GetClip( savedClipRegion );
84 ClipRect( drawRect );
85 EraseRect( drawRect );
86
87
88 MoveTo(drawRect->left + 4 , drawRect->top + 10 );
89 ::TextFont( kFontIDMonaco ) ;
90 ::TextSize( 9 );
91 ::TextFace( 0 ) ;
92
93 DrawText(text, 0 , text.Length());
94 // If the cell is hilited, do the hilite now. Paint the cell contents with the
95 // appropriate QuickDraw transform mode.
96
97 if( isSelected ) {
98 savedPenMode = GetPortPenMode( grafPtr );
99 SetPortPenMode( grafPtr, hilitetransfermode );
100 PaintRect( drawRect );
101 SetPortPenMode( grafPtr, savedPenMode );
102 }
103
104 // Restore the saved clip region.
105
106 SetClip( savedClipRegion );
107 DisposeRgn( savedClipRegion );
108 }
109 break;
110 case lHiliteMsg:
111
112 // Hilite or unhilite the cell. Paint the cell contents with the
113 // appropriate QuickDraw transform mode.
114
115 GetPort( &grafPtr );
116 savedPenMode = GetPortPenMode( grafPtr );
117 SetPortPenMode( grafPtr, hilitetransfermode );
118 PaintRect( drawRect );
119 SetPortPenMode( grafPtr, savedPenMode );
120 break;
121 default :
122 break ;
123 }
124 SetPort(savePort);
125}
126
127extern "C" void MacDrawStringCell(Rect *cellRect, Cell lCell, ListHandle theList, long refCon) ;
128const short kwxMacListWithVerticalScrollbar = 128 ;
129
130// ============================================================================
131// list box control implementation
132// ============================================================================
133
134// Listbox item
135wxListBox::wxListBox()
136{
137 m_noItems = 0;
138 m_selected = 0;
139 m_macList = NULL ;
140}
141
142static ListDefUPP macListDefUPP = NULL ;
143
144bool wxListBox::Create(wxWindow *parent, wxWindowID id,
145 const wxPoint& pos,
146 const wxSize& size,
147 int n, const wxString choices[],
148 long style,
149 const wxValidator& validator,
150 const wxString& name)
151{
152 m_noItems = 0 ; // this will be increased by our append command
153 m_selected = 0;
154
155 Rect bounds ;
156 Str255 title ;
157
158 MacPreControlCreate( parent , id , "" , pos , size ,style, validator , name , &bounds , title ) ;
159
160 ListDefSpec listDef;
161 listDef.defType = kListDefUserProcType;
162 if ( macListDefUPP == NULL )
163 {
164 macListDefUPP = NewListDefUPP( wxMacListDefinition );
165 }
166 listDef.u.userProc = macListDefUPP ;
167#if TARGET_CARBON
168 Size asize;
169
170
171 CreateListBoxControl( parent->GetMacRootWindow(), &bounds, false, 0, 1, false, true,
172 14, 14, false, &listDef, &m_macControl );
173
174 GetControlData(m_macControl, kControlNoPart, kControlListBoxListHandleTag,
175 sizeof(ListHandle), (Ptr) &m_macList, &asize);
176
177 SetControlReference(m_macControl, (long) this);
178 SetControlVisibility(m_macControl, false, false);
179
180#else
181 long result ;
182
183 m_macControl = ::NewControl( parent->GetMacRootWindow() , &bounds , title , false ,
184 kwxMacListWithVerticalScrollbar , 0 , 0,
185 kControlListBoxProc , (long) this ) ;
186 ::GetControlData( m_macControl , kControlNoPart , kControlListBoxListHandleTag ,
187 sizeof( ListHandle ) , (char*) &m_macList , &result ) ;
188
189 HLock( (Handle) m_macList ) ;
190 ldefHandle ldef ;
191 ldef = (ldefHandle) NewHandle( sizeof(ldefRec) ) ;
192 if ( (**m_macList).listDefProc != NULL )
193 {
194 (**ldef).instruction = 0x4EF9; /* JMP instruction */
195 (**ldef).function = (void(*)()) listDef.u.userProc;
196 (**m_macList).listDefProc = (Handle) ldef ;
197 }
198
199 Point pt = (**m_macList).cellSize ;
200 pt.v = 14 ;
201 LCellSize( pt , m_macList ) ;
202
203 LAddColumn( 1 , 0 , m_macList ) ;
204#endif
205 OptionBits options = 0;
206 if ( style & wxLB_MULTIPLE )
207 {
208 options += lNoExtend ;
209 }
210 else if ( style & wxLB_EXTENDED )
211 {
212 options += lExtendDrag ;
213 }
214 else
215 {
216 options = lOnlyOne ;
217 }
218 SetListSelectionFlags(m_macList, options);
219
220 MacPostControlCreate() ;
221
222 for ( int i = 0 ; i < n ; i++ )
223 {
224 Append( choices[i] ) ;
225 }
226
227 LSetDrawingMode( true , m_macList ) ;
228
229 return TRUE;
230}
231
232wxListBox::~wxListBox()
233{
234 Free() ;
235 if ( m_macList )
236 {
237#if !TARGET_CARBON
238 DisposeHandle( (**m_macList).listDefProc ) ;
239 (**m_macList).listDefProc = NULL ;
240#endif
241 m_macList = NULL ;
242 }
243}
244
245void wxListBox::Free()
246{
247#if wxUSE_OWNER_DRAWN
248 if ( m_windowStyle & wxLB_OWNERDRAW )
249 {
250 size_t uiCount = m_aItems.Count();
251 while ( uiCount-- != 0 ) {
252 delete m_aItems[uiCount];
253 }
254
255 m_aItems.Clear();
256 }
257 else
258#endif // wxUSE_OWNER_DRAWN
259 if ( HasClientObjectData() )
260 {
261 for ( size_t n = 0; n < (size_t)m_noItems; n++ )
262 {
263 delete GetClientObject(n);
264 }
265 }
266}
267
268void wxListBox::DoSetFirstItem(int N)
269{
270 MacScrollTo( N ) ;
271}
272
273void wxListBox::Delete(int N)
274{
275 wxCHECK_RET( N >= 0 && N < m_noItems,
276 wxT("invalid index in wxListBox::Delete") );
277
278#if wxUSE_OWNER_DRAWN
279 delete m_aItems[N];
280 m_aItems.RemoveAt(N);
281#else // !wxUSE_OWNER_DRAWN
282 if ( HasClientObjectData() )
283 {
284 delete GetClientObject(N);
285 }
286#endif // wxUSE_OWNER_DRAWN/!wxUSE_OWNER_DRAWN
287 m_stringArray.Remove( N ) ;
288 m_dataArray.RemoveAt( N ) ;
289 m_noItems --;
290
291 MacDelete( N ) ;
292}
293
294int wxListBox::DoAppend(const wxString& item)
295{
296 int index = m_noItems ;
297 if( wxApp::s_macDefaultEncodingIsPC )
298 {
299 m_stringArray.Add( wxMacMakeMacStringFromPC( item ) ) ;
300 m_dataArray.Add( NULL );
301 }
302 else {
303 m_stringArray.Add( item ) ;
304 m_dataArray.Add( NULL );
305 }
306 m_noItems ++;
307 DoSetItemClientData( index , NULL ) ;
308 MacAppend( item ) ;
309
310 return index ;
311}
312
313void wxListBox::DoSetItems(const wxArrayString& choices, void** clientData)
314{
315 MacSetRedraw( false ) ;
316 Clear() ;
317 int n = choices.GetCount();
318
319 for( int i = 0 ; i < n ; ++i )
320 {
321 if ( clientData )
322 {
323#if wxUSE_OWNER_DRAWN
324 wxASSERT_MSG(clientData[i] == NULL,
325 wxT("Can't use client data with owner-drawn listboxes"));
326#else // !wxUSE_OWNER_DRAWN
327 Append( choices[i] , clientData[i] ) ;
328 #endif
329 }
330 else
331 Append( choices[i] ) ;
332 }
333
334#if wxUSE_OWNER_DRAWN
335 if ( m_windowStyle & wxLB_OWNERDRAW ) {
336 // first delete old items
337 size_t ui = m_aItems.Count();
338 while ( ui-- != 0 ) {
339 delete m_aItems[ui];
340 }
341 m_aItems.Empty();
342
343 // then create new ones
344 for ( ui = 0; ui < (size_t)m_noItems; ui++ ) {
345 wxOwnerDrawn *pNewItem = CreateItem(ui);
346 pNewItem->SetName(choices[ui]);
347 m_aItems.Add(pNewItem);
348 }
349 }
350#endif // wxUSE_OWNER_DRAWN
351 MacSetRedraw( true ) ;
352}
353
354bool wxListBox::HasMultipleSelection() const
355{
356 return (m_windowStyle & wxLB_MULTIPLE) || (m_windowStyle & wxLB_EXTENDED);
357}
358
359int wxListBox::FindString(const wxString& st) const
360{
361 wxString s ;
362 if( wxApp::s_macDefaultEncodingIsPC )
363 {
364 s = wxMacMakeMacStringFromPC( st ) ;
365 }
366 else
367 s = st ;
368
369 if ( s.Right(1) == "*" )
370 {
371 wxString search = s.Left( s.Length() - 1 ) ;
372 int len = search.Length() ;
373 Str255 s1 , s2 ;
374
375#if TARGET_CARBON
376 c2pstrcpy( (StringPtr) s2 , search.c_str() ) ;
377#else
378 strcpy( (char *) s2 , search.c_str() ) ;
379 c2pstr( (char *) s2 ) ;
380#endif
381
382 for ( int i = 0 ; i < m_noItems ; ++ i )
383 {
384#if TARGET_CARBON
385 c2pstrcpy( (StringPtr) s1 , m_stringArray[i].Left( len ).c_str() ) ;
386#else
387 strcpy( (char *) s1 , m_stringArray[i].Left( len ).c_str() ) ;
388 c2pstr( (char *) s1 ) ;
389#endif
390 if ( EqualString( s1 , s2 , false , false ) )
391 return i ;
392 }
393 if ( s.Left(1) == "*" && s.Length() > 1 )
394 {
395 s.MakeLower() ;
396 for ( int i = 0 ; i < m_noItems ; ++i )
397 {
398 if ( GetString(i).Lower().Matches(s) )
399 return i ;
400 }
401 }
402
403 }
404 else
405 {
406 Str255 s1 , s2 ;
407
408#if TARGET_CARBON
409 c2pstrcpy( (StringPtr) s2 , s.c_str() ) ;
410#else
411 strcpy( (char *) s2 , s.c_str() ) ;
412 c2pstr( (char *) s2 ) ;
413#endif
414
415 for ( int i = 0 ; i < m_noItems ; ++ i )
416 {
417#if TARGET_CARBON
418 c2pstrcpy( (StringPtr) s1 , m_stringArray[i].c_str() ) ;
419#else
420 strcpy( (char *) s1 , m_stringArray[i].c_str() ) ;
421 c2pstr( (char *) s1 ) ;
422#endif
423 if ( EqualString( s1 , s2 , false , false ) )
424 return i ;
425 }
426 }
427 return -1;
428}
429
430void wxListBox::Clear()
431{
432 Free();
433 m_noItems = 0;
434 m_stringArray.Empty() ;
435 m_dataArray.Empty() ;
436 MacClear() ;
437}
438
439void wxListBox::SetSelection(int N, bool select)
440{
441 wxCHECK_RET( N >= 0 && N < m_noItems,
442 "invalid index in wxListBox::SetSelection" );
443 MacSetSelection( N , select ) ;
444 GetSelections( m_selectionPreImage ) ;
445}
446
447bool wxListBox::IsSelected(int N) const
448{
449 wxCHECK_MSG( N >= 0 && N < m_noItems, FALSE,
450 "invalid index in wxListBox::Selected" );
451
452 return MacIsSelected( N ) ;
453}
454
455void *wxListBox::DoGetItemClientData(int N) const
456{
457 wxCHECK_MSG( N >= 0 && N < m_noItems, NULL,
458 wxT("invalid index in wxListBox::GetClientData"));
459
460 return (void *)m_dataArray[N];
461}
462
463wxClientData *wxListBox::DoGetItemClientObject(int N) const
464{
465 return (wxClientData *) DoGetItemClientData( N ) ;
466}
467
468void wxListBox::DoSetItemClientData(int N, void *Client_data)
469{
470 wxCHECK_RET( N >= 0 && N < m_noItems,
471 "invalid index in wxListBox::SetClientData" );
472
473#if wxUSE_OWNER_DRAWN
474 if ( m_windowStyle & wxLB_OWNERDRAW )
475 {
476 // client data must be pointer to wxOwnerDrawn, otherwise we would crash
477 // in OnMeasure/OnDraw.
478 wxFAIL_MSG(wxT("Can't use client data with owner-drawn listboxes"));
479 }
480#endif // wxUSE_OWNER_DRAWN
481 wxASSERT_MSG( m_dataArray.GetCount() >= N , "invalid client_data array" ) ;
482
483 if ( m_dataArray.GetCount() > N )
484 {
485 m_dataArray[N] = (char*) Client_data ;
486 }
487 else
488 {
489 m_dataArray.Add( (char*) Client_data ) ;
490 }
491}
492
493void wxListBox::DoSetItemClientObject(int n, wxClientData* clientData)
494{
495 DoSetItemClientData(n, clientData);
496}
497
498// Return number of selections and an array of selected integers
499int wxListBox::GetSelections(wxArrayInt& aSelections) const
500{
501 return MacGetSelections( aSelections ) ;
502}
503
504// Get single selection, for single choice list items
505int wxListBox::GetSelection() const
506{
507 return MacGetSelection() ;
508}
509
510// Find string for position
511wxString wxListBox::GetString(int N) const
512{
513 if( wxApp::s_macDefaultEncodingIsPC )
514 {
515 return wxMacMakePCStringFromMac( m_stringArray[N] ) ;
516 }
517 else
518 return m_stringArray[N] ;
519}
520
521void wxListBox::DoInsertItems(const wxArrayString& items, int pos)
522{
523 wxCHECK_RET( pos >= 0 && pos <= m_noItems,
524 wxT("invalid index in wxListBox::InsertItems") );
525
526 int nItems = items.GetCount();
527
528 for ( int i = 0 ; i < nItems ; i++ )
529 {
530 m_stringArray.Insert( items[i] , pos + i ) ;
531 m_dataArray.Insert( NULL , pos + i ) ;
532 MacInsert( pos + i , items[i] ) ;
533 }
534
535 m_noItems += nItems;
536}
537
538void wxListBox::SetString(int N, const wxString& s)
539{
540 wxString str ;
541 if( wxApp::s_macDefaultEncodingIsPC )
542 {
543 str = wxMacMakeMacStringFromPC( s ) ;
544 }
545 else
546 str = s ;
547 m_stringArray[N] = str ;
548 MacSet( N , s ) ;
549}
550
551wxSize wxListBox::DoGetBestSize() const
552{
553 return wxSize(100, 100);
554}
555
556int wxListBox::GetCount() const
557{
558 return m_noItems;
559}
560
561void wxListBox::SetupColours()
562{
563 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW));
564 SetForegroundColour(GetParent()->GetForegroundColour());
565}
566
567void wxListBox::Refresh(bool eraseBack, const wxRect *rect)
568{
569 // Set up port
570 WindowRef rootwindow = GetMacRootWindow() ;
571 wxWindow* wxrootwindow = wxFindWinFromMacWindow( rootwindow ) ;
572 wxMacDrawingHelper focus( wxrootwindow );
573
574 UMADrawControl(m_macControl);
575}
576
577#if wxUSE_OWNER_DRAWN
578
579class wxListBoxItem : public wxOwnerDrawn
580{
581public:
582 wxListBoxItem(const wxString& str = "");
583};
584
585wxListBoxItem::wxListBoxItem(const wxString& str) : wxOwnerDrawn(str, FALSE)
586{
587 // no bitmaps/checkmarks
588 SetMarginWidth(0);
589}
590
591wxOwnerDrawn *wxListBox::CreateItem(size_t n)
592{
593 return new wxListBoxItem();
594}
595
596#endif //USE_OWNER_DRAWN
597
598// ============================================================================
599// list box control implementation
600// ============================================================================
601
602void MacDrawStringCell(Rect *cellRect, Cell lCell, ListHandle theList, long refCon)
603{
604 wxListBox* list;
605 // typecast our refCon
606 list = (wxListBox*)refCon;
607
608 MoveTo(cellRect->left + 4 , cellRect->top + 10 );
609 const wxString text = list->m_stringArray[lCell.v] ;
610 ::TextFont( kFontIDMonaco ) ;
611 ::TextSize( 9 );
612 ::TextFace( 0 ) ;
613 DrawText(text, 0 , text.Length());
614
615}
616
617void wxListBox::MacDelete( int N )
618{
619 LDelRow( 1 , N , m_macList) ;
620 Refresh();
621}
622
623void wxListBox::MacInsert( int n , const char * text)
624{
625 Cell cell = { 0 , 0 } ;
626 cell.v = n ;
627 LAddRow( 1 , cell.v , m_macList ) ;
628// LSetCell(text, strlen(text), cell, m_macList);
629 Refresh();
630}
631
632void wxListBox::MacAppend( const char * text)
633{
634 Cell cell = { 0 , 0 } ;
635 cell.v = (**m_macList).dataBounds.bottom ;
636 LAddRow( 1 , cell.v , m_macList ) ;
637 // LSetCell(text, strlen(text), cell, m_macList);
638 Refresh();
639}
640
641void wxListBox::MacClear()
642{
643 LDelRow( (**m_macList).dataBounds.bottom , 0 , m_macList ) ;
644 Refresh();
645}
646
647void wxListBox::MacSetSelection( int n , bool select )
648{
649 Cell cell = { 0 , 0 } ;
650 if ( ! (m_windowStyle & wxLB_MULTIPLE) )
651 {
652 if ( LGetSelect( true , &cell , m_macList ) )
653 {
654 LSetSelect( false , cell , m_macList ) ;
655 }
656 }
657
658 cell.v = n ;
659 LSetSelect( select , cell , m_macList ) ;
660 LAutoScroll( m_macList ) ;
661 Refresh();
662}
663
664bool wxListBox::MacIsSelected( int n ) const
665{
666 Cell cell = { 0 , 0 } ;
667 cell.v = n ;
668 return LGetSelect( false , &cell , m_macList ) ;
669}
670
671void wxListBox::MacDestroy()
672{
673// DisposeExtLDEFInfo( m_macList ) ;
674}
675
676int wxListBox::MacGetSelection() const
677{
678 Cell cell = { 0 , 0 } ;
679 if ( LGetSelect( true , &cell , m_macList ) )
680 return cell.v ;
681 else
682 return -1 ;
683}
684
685int wxListBox::MacGetSelections( wxArrayInt& aSelections ) const
686{
687 int no_sel = 0 ;
688
689 aSelections.Empty();
690
691 Cell cell = { 0 , 0 } ;
692 cell.v = 0 ;
693
694 while ( LGetSelect( true , &cell , m_macList ) )
695 {
696 aSelections.Add( cell.v ) ;
697 no_sel++ ;
698 cell.v++ ;
699 }
700 return no_sel ;
701}
702
703void wxListBox::MacSet( int n , const char * text )
704{
705 // our implementation does not store anything in the list
706 // so we just have to redraw
707 Cell cell = { 0 , 0 } ;
708 cell.v = n ;
709// LSetCell(text, strlen(text), cell, m_macList);
710 Refresh();
711}
712
713void wxListBox::MacScrollTo( int n )
714{
715 // TODO implement scrolling
716}
717
718void wxListBox::OnSize( const wxSizeEvent &event)
719{
720 Point pt;
721
722#if TARGET_CARBON
723 GetListCellSize(m_macList, &pt);
724#else
725 pt = (**m_macList).cellSize ;
726#endif
727 pt.h = m_width - 15 ;
728 LCellSize( pt , m_macList ) ;
729}
730
731void wxListBox::MacHandleControlClick( ControlHandle control , SInt16 controlpart )
732{
733 Boolean wasDoubleClick = false ;
734 long result ;
735
736 ::GetControlData( m_macControl , kControlNoPart , kControlListBoxDoubleClickTag , sizeof( wasDoubleClick ) , (char*) &wasDoubleClick , &result ) ;
737 if ( !wasDoubleClick )
738 {
739 MacDoClick() ;
740 }
741 else
742 {
743 MacDoDoubleClick() ;
744 }
745}
746
747void wxListBox::MacSetRedraw( bool doDraw )
748{
749 LSetDrawingMode( doDraw , m_macList ) ;
750
751}
752
753void wxListBox::MacDoClick()
754{
755 wxArrayInt aSelections;
756 int n, count = GetSelections(aSelections);
757
758 if ( count == m_selectionPreImage.GetCount() )
759 {
760 bool hasChanged = false ;
761 for ( int i = 0 ; i < count ; ++i )
762 {
763 if ( aSelections[i] != m_selectionPreImage[i] )
764 {
765 hasChanged = true ;
766 break ;
767 }
768 }
769 if ( !hasChanged )
770 return ;
771 }
772
773 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, m_windowId);
774 event.SetEventObject( this );
775
776 if ( count > 0 )
777 {
778 n = aSelections[0];
779 if ( HasClientObjectData() )
780 event.SetClientObject( GetClientObject(n) );
781 else if ( HasClientUntypedData() )
782 event.SetClientData( GetClientData(n) );
783 event.SetString( GetString(n) );
784 }
785 else
786 {
787 n = -1;
788 }
789
790 event.m_commandInt = n;
791
792 GetEventHandler()->ProcessEvent(event);
793}
794
795void wxListBox::MacDoDoubleClick()
796{
797 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED, m_windowId);
798 event.SetEventObject( this );
799 GetEventHandler()->ProcessEvent(event) ;
800}