]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/listbox.mm
Add wxControlWithItems::SendSelectionChangedEvent() helper.
[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
526954c5 9// Licence: wxWindows 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"
1a393573 25#include "wx/cocoa/ObjcRef.h"
97795793 26#include "wx/cocoa/private/scrollview.h"
bcaadf7e
DE
27#include "wx/cocoa/NSTableDataSource.h"
28
29#import <Foundation/NSArray.h>
30#import <Foundation/NSEnumerator.h>
31#import <AppKit/NSTableView.h>
32#import <AppKit/NSTableColumn.h>
97795793 33#import <AppKit/NSScrollView.h>
580cc1eb
DE
34#import <AppKit/NSCell.h>
35
580cc1eb
DE
36
37// ============================================================================
38// helper functions
39// ============================================================================
40
41static CGFloat _TableColumnMaxWidthForItems(NSTableColumn *tableColumn, NSArray *items)
42{
43 wxAutoNSAutoreleasePool pool;
44
45 NSCell *dataCell = [[[tableColumn dataCell] copy] autorelease];
46 CGFloat width = 0.0f;
47 NSEnumerator *itemEnum = [items objectEnumerator];
48 NSString *item;
49 while( (item = [itemEnum nextObject]) != nil )
50 {
51 [dataCell setStringValue: item];
52 NSSize itemSize = [dataCell cellSize];
53 CGFloat itemWidth = itemSize.width;
54 if(itemWidth > width)
55 width = itemWidth;
56 }
57 return width;
58}
59
60static void _SetWidthOfTableColumnToFitItems(NSTableColumn *tableColumn, NSArray *items)
61{
62 CGFloat width = _TableColumnMaxWidthForItems(tableColumn, items);
63 [tableColumn setWidth:width];
64 [tableColumn setMinWidth:width];
65}
66
67// ============================================================================
68// class wxListBox
69// ============================================================================
981af57c 70
812edc25
DE
71BEGIN_EVENT_TABLE(wxListBox, wxListBoxBase)
72END_EVENT_TABLE()
bcaadf7e 73WX_IMPLEMENT_COCOA_OWNER(wxListBox,NSTableView,NSControl,NSView)
812edc25 74
584ad2a3
MB
75bool wxListBox::Create(wxWindow *parent, wxWindowID winid,
76 const wxPoint& pos,
77 const wxSize& size,
78 const wxArrayString& choices,
79 long style,
80 const wxValidator& validator,
81 const wxString& name)
82{
83 wxCArrayString chs(choices);
84
85 return Create(parent, winid, pos, size, chs.GetCount(), chs.GetStrings(),
86 style, validator, name);
87}
88
812edc25
DE
89bool wxListBox::Create(wxWindow *parent, wxWindowID winid,
90 const wxPoint& pos,
91 const wxSize& size,
92 int n, const wxString choices[],
93 long style,
94 const wxValidator& validator,
95 const wxString& name)
96{
50d2af52 97/*
11e62fe6
WS
98wxLB_SINGLE
99Single-selection list.
50d2af52 100
11e62fe6
WS
101wxLB_MULTIPLE
102Multiple-selection list: the user can toggle multiple items on and off.
50d2af52 103
11e62fe6
WS
104wxLB_EXTENDED
105Extended-selection list: the user can select multiple items using the SHIFT key and the mouse or special key combinations.
50d2af52 106
11e62fe6
WS
107wxLB_HSCROLL
108Create horizontal scrollbar if contents are too wide (Windows only).
50d2af52 109
11e62fe6
WS
110wxLB_ALWAYS_SB
111Always show a vertical scrollbar.
50d2af52 112
11e62fe6
WS
113wxLB_NEEDED_SB
114Only create a vertical scrollbar if needed.
50d2af52 115
11e62fe6
WS
116wxLB_SORT
117The listbox contents are sorted in alphabetical order.
50d2af52 118*/
bed6fe0c 119 wxAutoNSAutoreleasePool pool;
812edc25
DE
120 if(!CreateControl(parent,winid,pos,size,style,validator,name))
121 return false;
122
bcaadf7e 123 // Provide the data
1a393573 124 m_cocoaItems = wxGCSafeRetain([NSMutableArray arrayWithCapacity:n]);
bcaadf7e
DE
125 for(int i=0; i < n; i++)
126 {
127 [m_cocoaItems addObject: wxNSStringWithWxString(choices[i])];
128 }
129 // Remove everything
c0ccf6a9 130 m_itemClientData.Clear();
bcaadf7e 131 // Initialize n elements to NULL
c0ccf6a9 132 m_itemClientData.SetCount(n,NULL);
bcaadf7e
DE
133
134 SetNSTableView([[NSTableView alloc] initWithFrame: MakeDefaultNSRect(size)]);
981af57c 135 [m_cocoaNSView release];
4a3dcf1d 136 [GetNSTableView() setHeaderView: nil];
981af57c 137
bcaadf7e 138 // Set up the data source
861b3043 139 m_cocoaDataSource = [[WX_GET_OBJC_CLASS(wxCocoaNSTableDataSource) alloc] init];
bcaadf7e
DE
140 [GetNSTableView() setDataSource:m_cocoaDataSource];
141
142 // Add the single column
143 NSTableColumn *tableColumn = [[NSTableColumn alloc] initWithIdentifier:nil];
144 [GetNSTableView() addTableColumn: tableColumn];
0f5a779f
VZ
145 // By default, entries should not be editable
146 [tableColumn setEditable:NO];
4a3dcf1d 147 [tableColumn release];
bcaadf7e 148
4a3dcf1d 149 [GetNSTableView() sizeToFit];
bcaadf7e 150 // Finish
812edc25
DE
151 if(m_parent)
152 m_parent->CocoaAddChild(this);
4a3dcf1d
DE
153 // NSTableView does WEIRD things with sizes. Wrapping it in an
154 // NSScrollView seems to be the only reasonable solution.
155 CocoaCreateNSScrollView();
bcaadf7e 156 SetInitialFrameRect(pos,size);
11e62fe6 157
0f5a779f 158 if ((style & wxLB_NEEDED_SB) || (style & wxLB_ALWAYS_SB))
fd3fe3ec 159 {
0f5a779f
VZ
160 [m_wxCocoaScrollView->GetNSScrollView() setHasVerticalScroller: YES];
161 }
162
163 if (style & wxLB_HSCROLL)
164 {
165 [m_wxCocoaScrollView->GetNSScrollView() setHasHorizontalScroller: YES];
166 }
167
168 // We can't set auto-hiding individually for horizontal/vertical scrollers,
169 // so we have settled on always allowing hiding for both unless the vertical
170 // setting is "always show".
171 if (((style & wxLB_NEEDED_SB) || (style & wxLB_HSCROLL)) && !(style & wxLB_ALWAYS_SB))
172 {
173 if ([m_wxCocoaScrollView->GetNSScrollView() respondsToSelector:@selector(setAutohidesScrollers:)])
174 {
175 [m_wxCocoaScrollView->GetNSScrollView() setAutohidesScrollers: YES];
176 }
fd3fe3ec 177 }
97795793 178
50d2af52 179 // Set up extended/multiple selection flags
11e62fe6 180 if ((style & wxLB_EXTENDED) || (style & wxLB_MULTIPLE))
50d2af52
RN
181 //diff is that mult requires shift down for multi selection
182 [GetNSTableView() setAllowsMultipleSelection:true];
183
184 [GetNSTableView() setAllowsColumnSelection:false];
580cc1eb 185 _SetWidthOfTableColumnToFitItems(tableColumn, m_cocoaItems);
812edc25
DE
186 return true;
187}
188
c86ec17b
DE
189wxSize wxListBox::DoGetBestSize() const
190{
191 wxSize size = wxControlWithItems::DoGetBestSize();
192 // Limit best size to 100x100. It can be smaller if none of the items are very
193 // wide or if there aren't many items, but anything bigger than 100x100 ought
194 // to be asked for by the programmer. The 100x100 size is based on being barely
195 // enough for a scroller to be usable.
196 if(size.GetWidth() > 100)
197 size.SetWidth(100);
198 if(size.GetHeight() > 100)
199 size.SetHeight(100);
200 return size;
201}
202
812edc25
DE
203wxListBox::~wxListBox()
204{
bcaadf7e
DE
205 [GetNSTableView() setDataSource: nil];
206 [m_cocoaDataSource release];
1a393573
DE
207 wxGCSafeRelease(m_cocoaItems);
208 m_cocoaItems = nil;
911e17c6 209 DisassociateNSTableView(GetNSTableView());
bcaadf7e
DE
210}
211
580cc1eb
DE
212bool wxListBox::_WxCocoa_GetNeedsUpdate()
213{
861b3043 214 return m_needsUpdate;
580cc1eb
DE
215}
216
217void wxListBox::_WxCocoa_SetNeedsUpdate(bool needsUpdate)
218{
861b3043 219 m_needsUpdate = needsUpdate;
580cc1eb
DE
220}
221
861b3043 222void wxListBox::OnInternalIdle()
580cc1eb 223{
861b3043 224 wxControlWithItems::OnInternalIdle();
580cc1eb
DE
225 if(_WxCocoa_GetNeedsUpdate())
226 {
227 _SetWidthOfTableColumnToFitItems([[GetNSTableView() tableColumns] objectAtIndex:0], m_cocoaItems);
228 [GetNSTableView() tile];
229 [GetNSTableView() reloadData];
230 _WxCocoa_SetNeedsUpdate(false);
231 }
232}
233
bcaadf7e
DE
234int wxListBox::CocoaDataSource_numberOfRows()
235{
236 return [m_cocoaItems count];
237}
238
239struct objc_object* wxListBox::CocoaDataSource_objectForTableColumn(
240 WX_NSTableColumn tableColumn, int rowIndex)
241{
242 return [m_cocoaItems objectAtIndex:rowIndex];
812edc25
DE
243}
244
245// pure virtuals from wxListBoxBase
246bool wxListBox::IsSelected(int n) const
247{
bcaadf7e 248 return [GetNSTableView() isRowSelected: n];
812edc25
DE
249}
250
c6179a84 251void wxListBox::DoSetSelection(int n, bool select)
812edc25 252{
bcaadf7e
DE
253 if(select)
254 [GetNSTableView() selectRow: n byExtendingSelection:NO];
255 else
256 [GetNSTableView() deselectRow: n];
812edc25
DE
257}
258
259int wxListBox::GetSelections(wxArrayInt& aSelections) const
260{
bcaadf7e
DE
261 aSelections.Clear();
262 NSEnumerator *enumerator = [GetNSTableView() selectedRowEnumerator];
263 while(NSNumber *num = [enumerator nextObject])
264 {
265 aSelections.Add([num intValue]);
266 }
267 return [GetNSTableView() numberOfSelectedRows];
812edc25
DE
268}
269
a236aa20 270int wxListBox::DoInsertItems(const wxArrayStringsAdapter & items, unsigned int pos, void **clientData, wxClientDataType type)
812edc25 271{
50d2af52
RN
272 wxAutoNSAutoreleasePool pool;
273
a236aa20
VZ
274 const unsigned int numItems = items.GetCount();
275 for ( unsigned int i = 0; i < numItems; ++i, ++pos )
bcaadf7e
DE
276 {
277 [m_cocoaItems insertObject: wxNSStringWithWxString(items[i])
278 atIndex: pos];
a236aa20
VZ
279 m_itemClientData.Insert(NULL, pos);
280 AssignNewItemClientData(pos, clientData, i, type);
bcaadf7e 281 }
11e62fe6 282
580cc1eb 283 _WxCocoa_SetNeedsUpdate(true);
a236aa20 284 return pos - 1;
812edc25
DE
285}
286
287void wxListBox::DoSetFirstItem(int n)
288{
50d2af52
RN
289 [m_cocoaItems exchangeObjectAtIndex:0 withObjectAtIndex:n];
290 void* pOld = m_itemClientData[n];
291 m_itemClientData[n] = m_itemClientData[0];
292 m_itemClientData[0] = pOld;
580cc1eb 293 _WxCocoa_SetNeedsUpdate(true);
812edc25
DE
294}
295
296
297// pure virtuals from wxItemContainer
298 // deleting items
a236aa20 299void wxListBox::DoClear()
812edc25 300{
50d2af52
RN
301 [m_cocoaItems removeAllObjects];
302 m_itemClientData.Clear();
580cc1eb 303 _WxCocoa_SetNeedsUpdate(true);
812edc25
DE
304}
305
a236aa20 306void wxListBox::DoDeleteOneItem(unsigned int n)
812edc25 307{
50d2af52
RN
308 [m_cocoaItems removeObjectAtIndex:n];
309 m_itemClientData.RemoveAt(n);
580cc1eb 310 _WxCocoa_SetNeedsUpdate(true);
812edc25
DE
311}
312
313 // accessing strings
aa61d352 314unsigned int wxListBox::GetCount() const
812edc25 315{
aa61d352 316 return (unsigned int)[m_cocoaItems count];
812edc25
DE
317}
318
aa61d352 319wxString wxListBox::GetString(unsigned int n) const
812edc25 320{
50d2af52 321 return wxStringWithNSString([m_cocoaItems objectAtIndex:n]);
812edc25
DE
322}
323
aa61d352 324void wxListBox::SetString(unsigned int n, const wxString& s)
812edc25 325{
50d2af52
RN
326 wxAutoNSAutoreleasePool pool;
327 [m_cocoaItems removeObjectAtIndex:n];
328 [m_cocoaItems insertObject: wxNSStringWithWxString(s) atIndex: n];
580cc1eb 329 _WxCocoa_SetNeedsUpdate(true);
812edc25
DE
330}
331
11e62fe6 332int wxListBox::FindString(const wxString& s, bool bCase) const
812edc25 333{
11e62fe6 334 // FIXME: use wxItemContainerImmutable::FindString for bCase parameter
50d2af52
RN
335 wxAutoNSAutoreleasePool pool;
336 return [m_cocoaItems indexOfObject:wxNSStringWithWxString(s)];
812edc25
DE
337}
338
339 // selection
812edc25
DE
340int wxListBox::GetSelection() const
341{
50d2af52 342 return [GetNSTableView() selectedRow];
812edc25
DE
343}
344
aa61d352 345void wxListBox::DoSetItemClientData(unsigned int n, void* clientData)
812edc25 346{
50d2af52 347 m_itemClientData[n] = clientData;
812edc25
DE
348}
349
aa61d352 350void* wxListBox::DoGetItemClientData(unsigned int n) const
812edc25 351{
50d2af52 352 return m_itemClientData[n];
812edc25
DE
353}
354
a236aa20 355#endif // wxUSE_LISTBOX