]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/listbox.mm
Added wxMGL switch
[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
DE
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>
981af57c 26
812edc25
DE
27IMPLEMENT_DYNAMIC_CLASS(wxListBox, wxControl)
28BEGIN_EVENT_TABLE(wxListBox, wxListBoxBase)
29END_EVENT_TABLE()
bcaadf7e 30WX_IMPLEMENT_COCOA_OWNER(wxListBox,NSTableView,NSControl,NSView)
812edc25
DE
31
32bool 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
bcaadf7e
DE
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)]);
981af57c 55 [m_cocoaNSView release];
4a3dcf1d 56 [GetNSTableView() setHeaderView: nil];
981af57c 57
bcaadf7e
DE
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];
4a3dcf1d 65 [tableColumn release];
bcaadf7e 66
4a3dcf1d 67 [GetNSTableView() sizeToFit];
bcaadf7e 68 // Finish
812edc25
DE
69 if(m_parent)
70 m_parent->CocoaAddChild(this);
4a3dcf1d
DE
71 // NSTableView does WEIRD things with sizes. Wrapping it in an
72 // NSScrollView seems to be the only reasonable solution.
73 CocoaCreateNSScrollView();
bcaadf7e
DE
74 SetInitialFrameRect(pos,size);
75
812edc25
DE
76 return true;
77}
78
79wxListBox::~wxListBox()
80{
bcaadf7e
DE
81 [GetNSTableView() setDataSource: nil];
82 [m_cocoaDataSource release];
83 [m_cocoaItems release];
911e17c6 84 DisassociateNSTableView(GetNSTableView());
bcaadf7e
DE
85}
86
87int wxListBox::CocoaDataSource_numberOfRows()
88{
89 return [m_cocoaItems count];
90}
91
92struct objc_object* wxListBox::CocoaDataSource_objectForTableColumn(
93 WX_NSTableColumn tableColumn, int rowIndex)
94{
95 return [m_cocoaItems objectAtIndex:rowIndex];
812edc25
DE
96}
97
98// pure virtuals from wxListBoxBase
99bool wxListBox::IsSelected(int n) const
100{
bcaadf7e 101 return [GetNSTableView() isRowSelected: n];
812edc25
DE
102}
103
104void wxListBox::SetSelection(int n, bool select)
105{
bcaadf7e
DE
106 if(select)
107 [GetNSTableView() selectRow: n byExtendingSelection:NO];
108 else
109 [GetNSTableView() deselectRow: n];
812edc25
DE
110}
111
112int wxListBox::GetSelections(wxArrayInt& aSelections) const
113{
bcaadf7e
DE
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];
812edc25
DE
121}
122
123void wxListBox::DoInsertItems(const wxArrayString& items, int pos)
124{
bcaadf7e
DE
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];
812edc25
DE
132}
133
134void wxListBox::DoSetItems(const wxArrayString& items, void **clientData)
135{
bcaadf7e
DE
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];
812edc25
DE
146}
147
148void wxListBox::DoSetFirstItem(int n)
149{
150}
151
152
153// pure virtuals from wxItemContainer
154 // deleting items
155void wxListBox::Clear()
156{
157}
158
159void wxListBox::Delete(int n)
160{
161}
162
163 // accessing strings
164int wxListBox::GetCount() const
165{
166 return 0;
167}
168
169wxString wxListBox::GetString(int n) const
170{
171 return wxEmptyString;
172}
173
174void wxListBox::SetString(int n, const wxString& s)
175{
176}
177
178int wxListBox::FindString(const wxString& s) const
179{
180 return 0;
181}
182
183 // selection
184void wxListBox::Select(int n)
185{
186}
187
188int wxListBox::GetSelection() const
189{
190 return 0;
191}
192
193int wxListBox::DoAppend(const wxString& item)
194{
195 return 0;
196}
197
198void wxListBox::DoSetItemClientData(int n, void* clientData)
199{
200}
201
202void* wxListBox::DoGetItemClientData(int n) const
203{
204 return NULL;
205}
206
207void wxListBox::DoSetItemClientObject(int n, wxClientData* clientData)
208{
209}
210
211wxClientData* wxListBox::DoGetItemClientObject(int n) const
212{
213 return NULL;
214}
215