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