Better listbox scrollbar behavior.
[wxWidgets.git] / src / cocoa / listbox.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        src/cocoa/listbox.mm
3 // Purpose:     wxListBox
4 // Author:      David Elliott
5 // Modified by:
6 // Created:     2003/03/18
7 // Id:          $Id$
8 // Copyright:   (c) 2003 David Elliott
9 // Licence:     wxWidgets licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #if wxUSE_LISTBOX
15
16 #include "wx/listbox.h"
17
18 #ifndef WX_PRECOMP
19     #include "wx/log.h"
20     #include "wx/app.h"
21 #endif //WX_PRECOMP
22
23 #include "wx/cocoa/string.h"
24 #include "wx/cocoa/autorelease.h"
25 #include "wx/cocoa/ObjcRef.h"
26 #include "wx/cocoa/private/scrollview.h"
27 #include "wx/cocoa/NSTableDataSource.h"
28
29 #import <Foundation/NSArray.h>
30 #import <Foundation/NSEnumerator.h>
31 #import <AppKit/NSTableView.h>
32 #import <AppKit/NSTableColumn.h>
33 #import <AppKit/NSScrollView.h>
34
35 IMPLEMENT_DYNAMIC_CLASS(wxListBox, wxControlWithItems)
36 BEGIN_EVENT_TABLE(wxListBox, wxListBoxBase)
37 END_EVENT_TABLE()
38 WX_IMPLEMENT_COCOA_OWNER(wxListBox,NSTableView,NSControl,NSView)
39
40 bool wxListBox::Create(wxWindow *parent, wxWindowID winid,
41             const wxPoint& pos,
42             const wxSize& size,
43             const wxArrayString& choices,
44             long style,
45             const wxValidator& validator,
46             const wxString& name)
47 {
48     wxCArrayString chs(choices);
49
50     return Create(parent, winid, pos, size, chs.GetCount(), chs.GetStrings(),
51                   style, validator, name);
52 }
53
54 bool wxListBox::Create(wxWindow *parent, wxWindowID winid,
55             const wxPoint& pos,
56             const wxSize& size,
57             int n, const wxString choices[],
58             long style,
59             const wxValidator& validator,
60             const wxString& name)
61 {
62 /*
63 wxLB_SINGLE
64 Single-selection list.
65
66 wxLB_MULTIPLE
67 Multiple-selection list: the user can toggle multiple items on and off.
68
69 wxLB_EXTENDED
70 Extended-selection list: the user can select multiple items using the SHIFT key and the mouse or special key combinations.
71
72 wxLB_HSCROLL
73 Create horizontal scrollbar if contents are too wide (Windows only).
74
75 wxLB_ALWAYS_SB
76 Always show a vertical scrollbar.
77
78 wxLB_NEEDED_SB
79 Only create a vertical scrollbar if needed.
80
81 wxLB_SORT
82 The listbox contents are sorted in alphabetical order.
83 */
84     wxAutoNSAutoreleasePool pool;
85     if(!CreateControl(parent,winid,pos,size,style,validator,name))
86         return false;
87
88     // Provide the data
89     m_cocoaItems = wxGCSafeRetain([NSMutableArray arrayWithCapacity:n]);
90     for(int i=0; i < n; i++)
91     {
92         [m_cocoaItems addObject: wxNSStringWithWxString(choices[i])];
93     }
94     // Remove everything
95     m_itemClientData.Clear();
96     // Initialize n elements to NULL
97     m_itemClientData.SetCount(n,NULL);
98
99     SetNSTableView([[NSTableView alloc] initWithFrame: MakeDefaultNSRect(size)]);
100     [m_cocoaNSView release];
101     [GetNSTableView() setHeaderView: nil];
102
103     // Set up the data source
104     m_cocoaDataSource = [[WX_GET_OBJC_CLASS(wxCocoaNSTableDataSource) alloc] init];
105     [GetNSTableView() setDataSource:m_cocoaDataSource];
106
107     // Add the single column
108     NSTableColumn *tableColumn = [[NSTableColumn alloc] initWithIdentifier:nil];
109     [GetNSTableView() addTableColumn: tableColumn];
110     [tableColumn release];
111
112     [GetNSTableView() sizeToFit];
113     // Finish
114     if(m_parent)
115         m_parent->CocoaAddChild(this);
116     // NSTableView does WEIRD things with sizes.  Wrapping it in an
117     // NSScrollView seems to be the only reasonable solution.
118     CocoaCreateNSScrollView();
119     SetInitialFrameRect(pos,size);
120
121     [m_wxCocoaScrollView->GetNSScrollView() setHasVerticalScroller:YES];
122     // Pre-10.3: Always show vertical scroller, never show horizontal scroller
123     // Post-10.3: Show scrollers dynamically (turn them both on, set auto-hide)
124     if([m_wxCocoaScrollView->GetNSScrollView() respondsToSelector:@selector(setAutohidesScrollers:)])
125     {
126         [m_wxCocoaScrollView->GetNSScrollView() setHasHorizontalScroller:YES];
127         [m_wxCocoaScrollView->GetNSScrollView() setAutohidesScrollers:YES];
128     }
129
130     // Set up extended/multiple selection flags
131     if ((style & wxLB_EXTENDED) || (style & wxLB_MULTIPLE))
132         //diff is that mult requires shift down for multi selection
133         [GetNSTableView() setAllowsMultipleSelection:true];
134
135     [GetNSTableView() setAllowsColumnSelection:false];
136
137     return true;
138 }
139
140 wxListBox::~wxListBox()
141 {
142     [GetNSTableView() setDataSource: nil];
143     [m_cocoaDataSource release];
144     wxGCSafeRelease(m_cocoaItems);
145     m_cocoaItems = nil;
146     DisassociateNSTableView(GetNSTableView());
147 }
148
149 int wxListBox::CocoaDataSource_numberOfRows()
150 {
151     return [m_cocoaItems count];
152 }
153
154 struct objc_object* wxListBox::CocoaDataSource_objectForTableColumn(
155         WX_NSTableColumn tableColumn, int rowIndex)
156 {
157     return [m_cocoaItems objectAtIndex:rowIndex];
158 }
159
160 // pure virtuals from wxListBoxBase
161 bool wxListBox::IsSelected(int n) const
162 {
163     return [GetNSTableView() isRowSelected: n];
164 }
165
166 void wxListBox::DoSetSelection(int n, bool select)
167 {
168     if(select)
169         [GetNSTableView() selectRow: n byExtendingSelection:NO];
170     else
171         [GetNSTableView() deselectRow: n];
172 }
173
174 int wxListBox::GetSelections(wxArrayInt& aSelections) const
175 {
176     aSelections.Clear();
177     NSEnumerator *enumerator = [GetNSTableView() selectedRowEnumerator];
178     while(NSNumber *num = [enumerator nextObject])
179     {
180         aSelections.Add([num intValue]);
181     }
182     return [GetNSTableView() numberOfSelectedRows];
183 }
184
185 int wxListBox::DoInsertItems(const wxArrayStringsAdapter & items, unsigned int pos, void **clientData, wxClientDataType type)
186 {
187     wxAutoNSAutoreleasePool pool;
188
189     const unsigned int numItems = items.GetCount();
190     for ( unsigned int i = 0; i < numItems; ++i, ++pos )
191     {
192         [m_cocoaItems insertObject: wxNSStringWithWxString(items[i])
193             atIndex: pos];
194         m_itemClientData.Insert(NULL, pos);
195         AssignNewItemClientData(pos, clientData, i, type);
196     }
197
198     [GetNSTableView() reloadData];
199     return pos - 1;
200 }
201
202 void wxListBox::DoSetFirstItem(int n)
203 {
204     [m_cocoaItems exchangeObjectAtIndex:0 withObjectAtIndex:n];
205     void* pOld = m_itemClientData[n];
206     m_itemClientData[n] = m_itemClientData[0];
207     m_itemClientData[0] = pOld;
208     [GetNSTableView() reloadData];
209 }
210
211
212 // pure virtuals from wxItemContainer
213     // deleting items
214 void wxListBox::DoClear()
215 {
216     [m_cocoaItems removeAllObjects];
217     m_itemClientData.Clear();
218     [GetNSTableView() reloadData];
219 }
220
221 void wxListBox::DoDeleteOneItem(unsigned int n)
222 {
223     [m_cocoaItems removeObjectAtIndex:n];
224     m_itemClientData.RemoveAt(n);
225     [GetNSTableView() reloadData];
226 }
227
228     // accessing strings
229 unsigned int wxListBox::GetCount() const
230 {
231     return (unsigned int)[m_cocoaItems count];
232 }
233
234 wxString wxListBox::GetString(unsigned int n) const
235 {
236     return wxStringWithNSString([m_cocoaItems objectAtIndex:n]);
237 }
238
239 void wxListBox::SetString(unsigned int n, const wxString& s)
240 {
241     wxAutoNSAutoreleasePool pool;
242     [m_cocoaItems removeObjectAtIndex:n];
243     [m_cocoaItems insertObject: wxNSStringWithWxString(s) atIndex: n];
244     [GetNSTableView() reloadData];
245 }
246
247 int wxListBox::FindString(const wxString& s, bool bCase) const
248 {
249     // FIXME: use wxItemContainerImmutable::FindString for bCase parameter
250     wxAutoNSAutoreleasePool pool;
251     return [m_cocoaItems indexOfObject:wxNSStringWithWxString(s)];
252 }
253
254     // selection
255 int wxListBox::GetSelection() const
256 {
257     return [GetNSTableView() selectedRow];
258 }
259
260 void wxListBox::DoSetItemClientData(unsigned int n, void* clientData)
261 {
262     m_itemClientData[n] = clientData;
263 }
264
265 void* wxListBox::DoGetItemClientData(unsigned int n) const
266 {
267     return m_itemClientData[n];
268 }
269
270 #endif // wxUSE_LISTBOX