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