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