]> git.saurik.com Git - wxWidgets.git/blob - src/cocoa/listbox.mm
todos, cleanups, detabified
[wxWidgets.git] / src / cocoa / listbox.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: cocoa/listbox.mm
3 // Purpose: wxListBox
4 // Author: David Elliott
5 // Modified by:
6 // Created: 2003/03/18
7 // RCS-ID: $Id:
8 // Copyright: (c) 2003 David Elliott
9 // Licence: wxWidgets licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13 #ifndef WX_PRECOMP
14 #include "wx/log.h"
15 #include "wx/app.h"
16 #include "wx/listbox.h"
17 #endif //WX_PRECOMP
18
19 #include "wx/cocoa/string.h"
20 #include "wx/cocoa/autorelease.h"
21 #include "wx/cocoa/NSTableDataSource.h"
22
23 #import <Foundation/NSArray.h>
24 #import <Foundation/NSEnumerator.h>
25 #import <AppKit/NSTableView.h>
26 #import <AppKit/NSTableColumn.h>
27
28 IMPLEMENT_DYNAMIC_CLASS(wxListBox, wxControl)
29 BEGIN_EVENT_TABLE(wxListBox, wxListBoxBase)
30 END_EVENT_TABLE()
31 WX_IMPLEMENT_COCOA_OWNER(wxListBox,NSTableView,NSControl,NSView)
32
33 bool wxListBox::Create(wxWindow *parent, wxWindowID winid,
34 const wxPoint& pos,
35 const wxSize& size,
36 const wxArrayString& choices,
37 long style,
38 const wxValidator& validator,
39 const wxString& name)
40 {
41 wxCArrayString chs(choices);
42
43 return Create(parent, winid, pos, size, chs.GetCount(), chs.GetStrings(),
44 style, validator, name);
45 }
46
47 bool wxListBox::Create(wxWindow *parent, wxWindowID winid,
48 const wxPoint& pos,
49 const wxSize& size,
50 int n, const wxString choices[],
51 long style,
52 const wxValidator& validator,
53 const wxString& name)
54 {
55 wxAutoNSAutoreleasePool pool;
56 if(!CreateControl(parent,winid,pos,size,style,validator,name))
57 return false;
58
59 // Provide the data
60 m_cocoaItems = [[NSMutableArray arrayWithCapacity:n] retain];
61 for(int i=0; i < n; i++)
62 {
63 [m_cocoaItems addObject: wxNSStringWithWxString(choices[i])];
64 }
65 // Remove everything
66 m_itemClientData.Clear();
67 // Initialize n elements to NULL
68 m_itemClientData.SetCount(n,NULL);
69
70 SetNSTableView([[NSTableView alloc] initWithFrame: MakeDefaultNSRect(size)]);
71 [m_cocoaNSView release];
72 [GetNSTableView() setHeaderView: nil];
73
74 // Set up the data source
75 m_cocoaDataSource = [[wxCocoaNSTableDataSource alloc] init];
76 [GetNSTableView() setDataSource:m_cocoaDataSource];
77
78 // Add the single column
79 NSTableColumn *tableColumn = [[NSTableColumn alloc] initWithIdentifier:nil];
80 [GetNSTableView() addTableColumn: tableColumn];
81 [tableColumn release];
82
83 [GetNSTableView() sizeToFit];
84 // Finish
85 if(m_parent)
86 m_parent->CocoaAddChild(this);
87 // NSTableView does WEIRD things with sizes. Wrapping it in an
88 // NSScrollView seems to be the only reasonable solution.
89 CocoaCreateNSScrollView();
90 SetInitialFrameRect(pos,size);
91
92 return true;
93 }
94
95 wxListBox::~wxListBox()
96 {
97 [GetNSTableView() setDataSource: nil];
98 [m_cocoaDataSource release];
99 [m_cocoaItems release];
100 DisassociateNSTableView(GetNSTableView());
101 }
102
103 int wxListBox::CocoaDataSource_numberOfRows()
104 {
105 return [m_cocoaItems count];
106 }
107
108 struct objc_object* wxListBox::CocoaDataSource_objectForTableColumn(
109 WX_NSTableColumn tableColumn, int rowIndex)
110 {
111 return [m_cocoaItems objectAtIndex:rowIndex];
112 }
113
114 // pure virtuals from wxListBoxBase
115 bool wxListBox::IsSelected(int n) const
116 {
117 return [GetNSTableView() isRowSelected: n];
118 }
119
120 void wxListBox::SetSelection(int n, bool select)
121 {
122 if(select)
123 [GetNSTableView() selectRow: n byExtendingSelection:NO];
124 else
125 [GetNSTableView() deselectRow: n];
126 }
127
128 int wxListBox::GetSelections(wxArrayInt& aSelections) const
129 {
130 aSelections.Clear();
131 NSEnumerator *enumerator = [GetNSTableView() selectedRowEnumerator];
132 while(NSNumber *num = [enumerator nextObject])
133 {
134 aSelections.Add([num intValue]);
135 }
136 return [GetNSTableView() numberOfSelectedRows];
137 }
138
139 void wxListBox::DoInsertItems(const wxArrayString& items, int pos)
140 {
141 for(int i=int(items.GetCount())-1; i >= 0; i--)
142 {
143 [m_cocoaItems insertObject: wxNSStringWithWxString(items[i])
144 atIndex: pos];
145 m_itemClientData.Insert(NULL,pos);
146 }
147 [GetNSTableView() reloadData];
148 }
149
150 void wxListBox::DoSetItems(const wxArrayString& items, void **clientData)
151 {
152 // Remove everything
153 [m_cocoaItems removeAllObjects];
154 m_itemClientData.Clear();
155 // Provide the data
156 for(size_t i=0; i < items.GetCount(); i++)
157 {
158 [m_cocoaItems addObject: wxNSStringWithWxString(items[i])];
159 m_itemClientData.Add(clientData[i]);
160 }
161 [GetNSTableView() reloadData];
162 }
163
164 void wxListBox::DoSetFirstItem(int n)
165 {
166 }
167
168
169 // pure virtuals from wxItemContainer
170 // deleting items
171 void wxListBox::Clear()
172 {
173 }
174
175 void wxListBox::Delete(int n)
176 {
177 }
178
179 // accessing strings
180 int wxListBox::GetCount() const
181 {
182 return 0;
183 }
184
185 wxString wxListBox::GetString(int n) const
186 {
187 return wxEmptyString;
188 }
189
190 void wxListBox::SetString(int n, const wxString& s)
191 {
192 }
193
194 int wxListBox::FindString(const wxString& s) const
195 {
196 return 0;
197 }
198
199 // selection
200 void wxListBox::Select(int n)
201 {
202 }
203
204 int wxListBox::GetSelection() const
205 {
206 return 0;
207 }
208
209 int wxListBox::DoAppend(const wxString& item)
210 {
211 return 0;
212 }
213
214 void wxListBox::DoSetItemClientData(int n, void* clientData)
215 {
216 }
217
218 void* wxListBox::DoGetItemClientData(int n) const
219 {
220 return NULL;
221 }
222
223 void wxListBox::DoSetItemClientObject(int n, wxClientData* clientData)
224 {
225 }
226
227 wxClientData* wxListBox::DoGetItemClientObject(int n) const
228 {
229 return NULL;
230 }
231