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