]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/listbox.mm
do take the toolbar into account for Windows CE, otherwise the menus overlap with...
[wxWidgets.git] / src / cocoa / listbox.mm
CommitLineData
812edc25
DE
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
449c5673
DE
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
812edc25 18
bcaadf7e 19#include "wx/cocoa/string.h"
bed6fe0c 20#include "wx/cocoa/autorelease.h"
bcaadf7e
DE
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>
981af57c 27
812edc25
DE
28IMPLEMENT_DYNAMIC_CLASS(wxListBox, wxControl)
29BEGIN_EVENT_TABLE(wxListBox, wxListBoxBase)
30END_EVENT_TABLE()
bcaadf7e 31WX_IMPLEMENT_COCOA_OWNER(wxListBox,NSTableView,NSControl,NSView)
812edc25 32
584ad2a3
MB
33bool 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
812edc25
DE
47bool 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{
bed6fe0c 55 wxAutoNSAutoreleasePool pool;
812edc25
DE
56 if(!CreateControl(parent,winid,pos,size,style,validator,name))
57 return false;
58
bcaadf7e
DE
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_clientData.Clear();
67 // Initialize n elements to NULL
68 m_clientData.SetCount(n,NULL);
69
70 SetNSTableView([[NSTableView alloc] initWithFrame: MakeDefaultNSRect(size)]);
981af57c 71 [m_cocoaNSView release];
4a3dcf1d 72 [GetNSTableView() setHeaderView: nil];
981af57c 73
bcaadf7e
DE
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];
4a3dcf1d 81 [tableColumn release];
bcaadf7e 82
4a3dcf1d 83 [GetNSTableView() sizeToFit];
bcaadf7e 84 // Finish
812edc25
DE
85 if(m_parent)
86 m_parent->CocoaAddChild(this);
4a3dcf1d
DE
87 // NSTableView does WEIRD things with sizes. Wrapping it in an
88 // NSScrollView seems to be the only reasonable solution.
89 CocoaCreateNSScrollView();
bcaadf7e
DE
90 SetInitialFrameRect(pos,size);
91
812edc25
DE
92 return true;
93}
94
95wxListBox::~wxListBox()
96{
bcaadf7e
DE
97 [GetNSTableView() setDataSource: nil];
98 [m_cocoaDataSource release];
99 [m_cocoaItems release];
911e17c6 100 DisassociateNSTableView(GetNSTableView());
bcaadf7e
DE
101}
102
103int wxListBox::CocoaDataSource_numberOfRows()
104{
105 return [m_cocoaItems count];
106}
107
108struct objc_object* wxListBox::CocoaDataSource_objectForTableColumn(
109 WX_NSTableColumn tableColumn, int rowIndex)
110{
111 return [m_cocoaItems objectAtIndex:rowIndex];
812edc25
DE
112}
113
114// pure virtuals from wxListBoxBase
115bool wxListBox::IsSelected(int n) const
116{
bcaadf7e 117 return [GetNSTableView() isRowSelected: n];
812edc25
DE
118}
119
120void wxListBox::SetSelection(int n, bool select)
121{
bcaadf7e
DE
122 if(select)
123 [GetNSTableView() selectRow: n byExtendingSelection:NO];
124 else
125 [GetNSTableView() deselectRow: n];
812edc25
DE
126}
127
128int wxListBox::GetSelections(wxArrayInt& aSelections) const
129{
bcaadf7e
DE
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];
812edc25
DE
137}
138
139void wxListBox::DoInsertItems(const wxArrayString& items, int pos)
140{
bcaadf7e
DE
141 for(int i=int(items.GetCount())-1; i >= 0; i--)
142 {
143 [m_cocoaItems insertObject: wxNSStringWithWxString(items[i])
144 atIndex: pos];
145 m_clientData.Insert(NULL,pos);
146 }
147 [GetNSTableView() reloadData];
812edc25
DE
148}
149
150void wxListBox::DoSetItems(const wxArrayString& items, void **clientData)
151{
bcaadf7e
DE
152 // Remove everything
153 [m_cocoaItems removeAllObjects];
154 m_clientData.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_clientData.Add(clientData[i]);
160 }
161 [GetNSTableView() reloadData];
812edc25
DE
162}
163
164void wxListBox::DoSetFirstItem(int n)
165{
166}
167
168
169// pure virtuals from wxItemContainer
170 // deleting items
171void wxListBox::Clear()
172{
173}
174
175void wxListBox::Delete(int n)
176{
177}
178
179 // accessing strings
180int wxListBox::GetCount() const
181{
182 return 0;
183}
184
185wxString wxListBox::GetString(int n) const
186{
187 return wxEmptyString;
188}
189
190void wxListBox::SetString(int n, const wxString& s)
191{
192}
193
194int wxListBox::FindString(const wxString& s) const
195{
196 return 0;
197}
198
199 // selection
200void wxListBox::Select(int n)
201{
202}
203
204int wxListBox::GetSelection() const
205{
206 return 0;
207}
208
209int wxListBox::DoAppend(const wxString& item)
210{
211 return 0;
212}
213
214void wxListBox::DoSetItemClientData(int n, void* clientData)
215{
216}
217
218void* wxListBox::DoGetItemClientData(int n) const
219{
220 return NULL;
221}
222
223void wxListBox::DoSetItemClientObject(int n, wxClientData* clientData)
224{
225}
226
227wxClientData* wxListBox::DoGetItemClientObject(int n) const
228{
229 return NULL;
230}
231