]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/listbox.mm
new layout was messing with separator positions
[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
065e208e 9// Licence: wxWidgets licence
812edc25
DE
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{
50d2af52
RN
55/*
56wxLB_SINGLE
57Single-selection list.
58
59wxLB_MULTIPLE
60Multiple-selection list: the user can toggle multiple items on and off.
61
62wxLB_EXTENDED
63Extended-selection list: the user can select multiple items using the SHIFT key and the mouse or special key combinations.
64
65wxLB_HSCROLL
66Create horizontal scrollbar if contents are too wide (Windows only).
67
68wxLB_ALWAYS_SB
69Always show a vertical scrollbar.
70
71wxLB_NEEDED_SB
72Only create a vertical scrollbar if needed.
73
74wxLB_SORT
75The listbox contents are sorted in alphabetical order.
76*/
bed6fe0c 77 wxAutoNSAutoreleasePool pool;
812edc25
DE
78 if(!CreateControl(parent,winid,pos,size,style,validator,name))
79 return false;
80
bcaadf7e
DE
81 // Provide the data
82 m_cocoaItems = [[NSMutableArray arrayWithCapacity:n] retain];
83 for(int i=0; i < n; i++)
84 {
85 [m_cocoaItems addObject: wxNSStringWithWxString(choices[i])];
86 }
87 // Remove everything
c0ccf6a9 88 m_itemClientData.Clear();
bcaadf7e 89 // Initialize n elements to NULL
c0ccf6a9 90 m_itemClientData.SetCount(n,NULL);
bcaadf7e
DE
91
92 SetNSTableView([[NSTableView alloc] initWithFrame: MakeDefaultNSRect(size)]);
981af57c 93 [m_cocoaNSView release];
4a3dcf1d 94 [GetNSTableView() setHeaderView: nil];
981af57c 95
bcaadf7e
DE
96 // Set up the data source
97 m_cocoaDataSource = [[wxCocoaNSTableDataSource alloc] init];
98 [GetNSTableView() setDataSource:m_cocoaDataSource];
99
100 // Add the single column
101 NSTableColumn *tableColumn = [[NSTableColumn alloc] initWithIdentifier:nil];
102 [GetNSTableView() addTableColumn: tableColumn];
4a3dcf1d 103 [tableColumn release];
bcaadf7e 104
4a3dcf1d 105 [GetNSTableView() sizeToFit];
bcaadf7e 106 // Finish
812edc25
DE
107 if(m_parent)
108 m_parent->CocoaAddChild(this);
4a3dcf1d
DE
109 // NSTableView does WEIRD things with sizes. Wrapping it in an
110 // NSScrollView seems to be the only reasonable solution.
111 CocoaCreateNSScrollView();
bcaadf7e 112 SetInitialFrameRect(pos,size);
50d2af52
RN
113
114 // Set up extended/multiple selection flags
115 if ((style & wxLB_EXTENDED) || (style & wxLB_MULTIPLE))
116 //diff is that mult requires shift down for multi selection
117 [GetNSTableView() setAllowsMultipleSelection:true];
118
119 [GetNSTableView() setAllowsColumnSelection:false];
bcaadf7e 120
812edc25
DE
121 return true;
122}
123
124wxListBox::~wxListBox()
125{
bcaadf7e
DE
126 [GetNSTableView() setDataSource: nil];
127 [m_cocoaDataSource release];
128 [m_cocoaItems release];
911e17c6 129 DisassociateNSTableView(GetNSTableView());
bcaadf7e
DE
130}
131
132int wxListBox::CocoaDataSource_numberOfRows()
133{
134 return [m_cocoaItems count];
135}
136
137struct objc_object* wxListBox::CocoaDataSource_objectForTableColumn(
138 WX_NSTableColumn tableColumn, int rowIndex)
139{
140 return [m_cocoaItems objectAtIndex:rowIndex];
812edc25
DE
141}
142
143// pure virtuals from wxListBoxBase
144bool wxListBox::IsSelected(int n) const
145{
bcaadf7e 146 return [GetNSTableView() isRowSelected: n];
812edc25
DE
147}
148
c6179a84 149void wxListBox::DoSetSelection(int n, bool select)
812edc25 150{
bcaadf7e
DE
151 if(select)
152 [GetNSTableView() selectRow: n byExtendingSelection:NO];
153 else
154 [GetNSTableView() deselectRow: n];
812edc25
DE
155}
156
157int wxListBox::GetSelections(wxArrayInt& aSelections) const
158{
bcaadf7e
DE
159 aSelections.Clear();
160 NSEnumerator *enumerator = [GetNSTableView() selectedRowEnumerator];
161 while(NSNumber *num = [enumerator nextObject])
162 {
163 aSelections.Add([num intValue]);
164 }
165 return [GetNSTableView() numberOfSelectedRows];
812edc25
DE
166}
167
168void wxListBox::DoInsertItems(const wxArrayString& items, int pos)
169{
50d2af52
RN
170 wxAutoNSAutoreleasePool pool;
171
bcaadf7e
DE
172 for(int i=int(items.GetCount())-1; i >= 0; i--)
173 {
174 [m_cocoaItems insertObject: wxNSStringWithWxString(items[i])
175 atIndex: pos];
c0ccf6a9 176 m_itemClientData.Insert(NULL,pos);
bcaadf7e
DE
177 }
178 [GetNSTableView() reloadData];
812edc25
DE
179}
180
181void wxListBox::DoSetItems(const wxArrayString& items, void **clientData)
182{
50d2af52
RN
183 wxAutoNSAutoreleasePool pool;
184
bcaadf7e
DE
185 // Remove everything
186 [m_cocoaItems removeAllObjects];
c0ccf6a9 187 m_itemClientData.Clear();
bcaadf7e
DE
188 // Provide the data
189 for(size_t i=0; i < items.GetCount(); i++)
190 {
191 [m_cocoaItems addObject: wxNSStringWithWxString(items[i])];
c0ccf6a9 192 m_itemClientData.Add(clientData[i]);
bcaadf7e
DE
193 }
194 [GetNSTableView() reloadData];
812edc25
DE
195}
196
197void wxListBox::DoSetFirstItem(int n)
198{
50d2af52
RN
199 [m_cocoaItems exchangeObjectAtIndex:0 withObjectAtIndex:n];
200 void* pOld = m_itemClientData[n];
201 m_itemClientData[n] = m_itemClientData[0];
202 m_itemClientData[0] = pOld;
203 [GetNSTableView() reloadData];
812edc25
DE
204}
205
206
207// pure virtuals from wxItemContainer
208 // deleting items
209void wxListBox::Clear()
210{
50d2af52
RN
211 [m_cocoaItems removeAllObjects];
212 m_itemClientData.Clear();
213 [GetNSTableView() reloadData];
812edc25
DE
214}
215
216void wxListBox::Delete(int n)
217{
50d2af52
RN
218 [m_cocoaItems removeObjectAtIndex:n];
219 m_itemClientData.RemoveAt(n);
220 [GetNSTableView() reloadData];
812edc25
DE
221}
222
223 // accessing strings
224int wxListBox::GetCount() const
225{
50d2af52 226 return [m_cocoaItems count];
812edc25
DE
227}
228
229wxString wxListBox::GetString(int n) const
230{
50d2af52 231 return wxStringWithNSString([m_cocoaItems objectAtIndex:n]);
812edc25
DE
232}
233
234void wxListBox::SetString(int n, const wxString& s)
235{
50d2af52
RN
236 wxAutoNSAutoreleasePool pool;
237 [m_cocoaItems removeObjectAtIndex:n];
238 [m_cocoaItems insertObject: wxNSStringWithWxString(s) atIndex: n];
239 [GetNSTableView() reloadData];
812edc25
DE
240}
241
242int wxListBox::FindString(const wxString& s) const
243{
50d2af52
RN
244 wxAutoNSAutoreleasePool pool;
245 return [m_cocoaItems indexOfObject:wxNSStringWithWxString(s)];
812edc25
DE
246}
247
248 // selection
812edc25
DE
249int wxListBox::GetSelection() const
250{
50d2af52 251 return [GetNSTableView() selectedRow];
812edc25
DE
252}
253
254int wxListBox::DoAppend(const wxString& item)
255{
50d2af52
RN
256 wxAutoNSAutoreleasePool pool;
257 [m_cocoaItems addObject:wxNSStringWithWxString(item)];
258 [GetNSTableView() reloadData];
259 m_itemClientData.Add(NULL);
260 return [m_cocoaItems count];
812edc25
DE
261}
262
263void wxListBox::DoSetItemClientData(int n, void* clientData)
264{
50d2af52 265 m_itemClientData[n] = clientData;
812edc25
DE
266}
267
268void* wxListBox::DoGetItemClientData(int n) const
269{
50d2af52 270 return m_itemClientData[n];
812edc25
DE
271}
272
273void wxListBox::DoSetItemClientObject(int n, wxClientData* clientData)
274{
50d2af52 275 m_itemClientData[n] = (void*) clientData;
812edc25
DE
276}
277
278wxClientData* wxListBox::DoGetItemClientObject(int n) const
279{
50d2af52 280 return (wxClientData*) m_itemClientData[n];
812edc25
DE
281}
282