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