]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/listbox.mm
many compilation fixes for WinCE using VC8 (it now build, although still doesn't...
[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
a236aa20 172int wxListBox::DoInsertItems(const wxArrayStringsAdapter & items, unsigned int pos, void **clientData, wxClientDataType type)
812edc25 173{
50d2af52
RN
174 wxAutoNSAutoreleasePool pool;
175
a236aa20
VZ
176 const unsigned int numItems = items.GetCount();
177 for ( unsigned int i = 0; i < numItems; ++i, ++pos )
bcaadf7e
DE
178 {
179 [m_cocoaItems insertObject: wxNSStringWithWxString(items[i])
180 atIndex: pos];
a236aa20
VZ
181 m_itemClientData.Insert(NULL, pos);
182 AssignNewItemClientData(pos, clientData, i, type);
bcaadf7e 183 }
11e62fe6 184
bcaadf7e 185 [GetNSTableView() reloadData];
a236aa20 186 return pos - 1;
812edc25
DE
187}
188
189void wxListBox::DoSetFirstItem(int n)
190{
50d2af52
RN
191 [m_cocoaItems exchangeObjectAtIndex:0 withObjectAtIndex:n];
192 void* pOld = m_itemClientData[n];
193 m_itemClientData[n] = m_itemClientData[0];
194 m_itemClientData[0] = pOld;
195 [GetNSTableView() reloadData];
812edc25
DE
196}
197
198
199// pure virtuals from wxItemContainer
200 // deleting items
a236aa20 201void wxListBox::DoClear()
812edc25 202{
50d2af52
RN
203 [m_cocoaItems removeAllObjects];
204 m_itemClientData.Clear();
205 [GetNSTableView() reloadData];
812edc25
DE
206}
207
a236aa20 208void wxListBox::DoDeleteOneItem(unsigned int n)
812edc25 209{
50d2af52
RN
210 [m_cocoaItems removeObjectAtIndex:n];
211 m_itemClientData.RemoveAt(n);
11e62fe6 212 [GetNSTableView() reloadData];
812edc25
DE
213}
214
215 // accessing strings
aa61d352 216unsigned int wxListBox::GetCount() const
812edc25 217{
aa61d352 218 return (unsigned int)[m_cocoaItems count];
812edc25
DE
219}
220
aa61d352 221wxString wxListBox::GetString(unsigned int n) const
812edc25 222{
50d2af52 223 return wxStringWithNSString([m_cocoaItems objectAtIndex:n]);
812edc25
DE
224}
225
aa61d352 226void wxListBox::SetString(unsigned int n, const wxString& s)
812edc25 227{
50d2af52
RN
228 wxAutoNSAutoreleasePool pool;
229 [m_cocoaItems removeObjectAtIndex:n];
230 [m_cocoaItems insertObject: wxNSStringWithWxString(s) atIndex: n];
11e62fe6 231 [GetNSTableView() reloadData];
812edc25
DE
232}
233
11e62fe6 234int wxListBox::FindString(const wxString& s, bool bCase) const
812edc25 235{
11e62fe6 236 // FIXME: use wxItemContainerImmutable::FindString for bCase parameter
50d2af52
RN
237 wxAutoNSAutoreleasePool pool;
238 return [m_cocoaItems indexOfObject:wxNSStringWithWxString(s)];
812edc25
DE
239}
240
241 // selection
812edc25
DE
242int wxListBox::GetSelection() const
243{
50d2af52 244 return [GetNSTableView() selectedRow];
812edc25
DE
245}
246
aa61d352 247void wxListBox::DoSetItemClientData(unsigned int n, void* clientData)
812edc25 248{
50d2af52 249 m_itemClientData[n] = clientData;
812edc25
DE
250}
251
aa61d352 252void* wxListBox::DoGetItemClientData(unsigned int n) const
812edc25 253{
50d2af52 254 return m_itemClientData[n];
812edc25
DE
255}
256
a236aa20 257#endif // wxUSE_LISTBOX