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