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