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