]> git.saurik.com Git - wxWidgets.git/blob - src/cocoa/listbox.mm
e6d13e33ebe8cbfae9ac8aaae39761135468b299
[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 // Force showing of a vertical scrollbar
122 [m_wxCocoaScrollView->GetNSScrollView() setHasVerticalScroller:YES];
123
124 // Set up extended/multiple selection flags
125 if ((style & wxLB_EXTENDED) || (style & wxLB_MULTIPLE))
126 //diff is that mult requires shift down for multi selection
127 [GetNSTableView() setAllowsMultipleSelection:true];
128
129 [GetNSTableView() setAllowsColumnSelection:false];
130
131 return true;
132 }
133
134 wxListBox::~wxListBox()
135 {
136 [GetNSTableView() setDataSource: nil];
137 [m_cocoaDataSource release];
138 wxGCSafeRelease(m_cocoaItems);
139 m_cocoaItems = nil;
140 DisassociateNSTableView(GetNSTableView());
141 }
142
143 int wxListBox::CocoaDataSource_numberOfRows()
144 {
145 return [m_cocoaItems count];
146 }
147
148 struct objc_object* wxListBox::CocoaDataSource_objectForTableColumn(
149 WX_NSTableColumn tableColumn, int rowIndex)
150 {
151 return [m_cocoaItems objectAtIndex:rowIndex];
152 }
153
154 // pure virtuals from wxListBoxBase
155 bool wxListBox::IsSelected(int n) const
156 {
157 return [GetNSTableView() isRowSelected: n];
158 }
159
160 void wxListBox::DoSetSelection(int n, bool select)
161 {
162 if(select)
163 [GetNSTableView() selectRow: n byExtendingSelection:NO];
164 else
165 [GetNSTableView() deselectRow: n];
166 }
167
168 int wxListBox::GetSelections(wxArrayInt& aSelections) const
169 {
170 aSelections.Clear();
171 NSEnumerator *enumerator = [GetNSTableView() selectedRowEnumerator];
172 while(NSNumber *num = [enumerator nextObject])
173 {
174 aSelections.Add([num intValue]);
175 }
176 return [GetNSTableView() numberOfSelectedRows];
177 }
178
179 int wxListBox::DoInsertItems(const wxArrayStringsAdapter & items, unsigned int pos, void **clientData, wxClientDataType type)
180 {
181 wxAutoNSAutoreleasePool pool;
182
183 const unsigned int numItems = items.GetCount();
184 for ( unsigned int i = 0; i < numItems; ++i, ++pos )
185 {
186 [m_cocoaItems insertObject: wxNSStringWithWxString(items[i])
187 atIndex: pos];
188 m_itemClientData.Insert(NULL, pos);
189 AssignNewItemClientData(pos, clientData, i, type);
190 }
191
192 [GetNSTableView() reloadData];
193 return pos - 1;
194 }
195
196 void wxListBox::DoSetFirstItem(int n)
197 {
198 [m_cocoaItems exchangeObjectAtIndex:0 withObjectAtIndex:n];
199 void* pOld = m_itemClientData[n];
200 m_itemClientData[n] = m_itemClientData[0];
201 m_itemClientData[0] = pOld;
202 [GetNSTableView() reloadData];
203 }
204
205
206 // pure virtuals from wxItemContainer
207 // deleting items
208 void wxListBox::DoClear()
209 {
210 [m_cocoaItems removeAllObjects];
211 m_itemClientData.Clear();
212 [GetNSTableView() reloadData];
213 }
214
215 void wxListBox::DoDeleteOneItem(unsigned int n)
216 {
217 [m_cocoaItems removeObjectAtIndex:n];
218 m_itemClientData.RemoveAt(n);
219 [GetNSTableView() reloadData];
220 }
221
222 // accessing strings
223 unsigned int wxListBox::GetCount() const
224 {
225 return (unsigned int)[m_cocoaItems count];
226 }
227
228 wxString wxListBox::GetString(unsigned int n) const
229 {
230 return wxStringWithNSString([m_cocoaItems objectAtIndex:n]);
231 }
232
233 void wxListBox::SetString(unsigned int n, const wxString& s)
234 {
235 wxAutoNSAutoreleasePool pool;
236 [m_cocoaItems removeObjectAtIndex:n];
237 [m_cocoaItems insertObject: wxNSStringWithWxString(s) atIndex: n];
238 [GetNSTableView() reloadData];
239 }
240
241 int wxListBox::FindString(const wxString& s, bool bCase) const
242 {
243 // FIXME: use wxItemContainerImmutable::FindString for bCase parameter
244 wxAutoNSAutoreleasePool pool;
245 return [m_cocoaItems indexOfObject:wxNSStringWithWxString(s)];
246 }
247
248 // selection
249 int wxListBox::GetSelection() const
250 {
251 return [GetNSTableView() selectedRow];
252 }
253
254 void wxListBox::DoSetItemClientData(unsigned int n, void* clientData)
255 {
256 m_itemClientData[n] = clientData;
257 }
258
259 void* wxListBox::DoGetItemClientData(unsigned int n) const
260 {
261 return m_itemClientData[n];
262 }
263
264 #endif // wxUSE_LISTBOX