]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/listbox.mm
Set default attributes after a SetSelection
[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>
981af57c 34
b1294ada 35IMPLEMENT_DYNAMIC_CLASS(wxListBox, wxControlWithItems)
812edc25
DE
36BEGIN_EVENT_TABLE(wxListBox, wxListBoxBase)
37END_EVENT_TABLE()
bcaadf7e 38WX_IMPLEMENT_COCOA_OWNER(wxListBox,NSTableView,NSControl,NSView)
812edc25 39
584ad2a3
MB
40bool wxListBox::Create(wxWindow *parent, wxWindowID winid,
41 const wxPoint& pos,
42 const wxSize& size,
43 const wxArrayString& choices,
44 long style,
45 const wxValidator& validator,
46 const wxString& name)
47{
48 wxCArrayString chs(choices);
49
50 return Create(parent, winid, pos, size, chs.GetCount(), chs.GetStrings(),
51 style, validator, name);
52}
53
812edc25
DE
54bool wxListBox::Create(wxWindow *parent, wxWindowID winid,
55 const wxPoint& pos,
56 const wxSize& size,
57 int n, const wxString choices[],
58 long style,
59 const wxValidator& validator,
60 const wxString& name)
61{
50d2af52 62/*
11e62fe6
WS
63wxLB_SINGLE
64Single-selection list.
50d2af52 65
11e62fe6
WS
66wxLB_MULTIPLE
67Multiple-selection list: the user can toggle multiple items on and off.
50d2af52 68
11e62fe6
WS
69wxLB_EXTENDED
70Extended-selection list: the user can select multiple items using the SHIFT key and the mouse or special key combinations.
50d2af52 71
11e62fe6
WS
72wxLB_HSCROLL
73Create horizontal scrollbar if contents are too wide (Windows only).
50d2af52 74
11e62fe6
WS
75wxLB_ALWAYS_SB
76Always show a vertical scrollbar.
50d2af52 77
11e62fe6
WS
78wxLB_NEEDED_SB
79Only create a vertical scrollbar if needed.
50d2af52 80
11e62fe6
WS
81wxLB_SORT
82The listbox contents are sorted in alphabetical order.
50d2af52 83*/
bed6fe0c 84 wxAutoNSAutoreleasePool pool;
812edc25
DE
85 if(!CreateControl(parent,winid,pos,size,style,validator,name))
86 return false;
87
bcaadf7e 88 // Provide the data
1a393573 89 m_cocoaItems = wxGCSafeRetain([NSMutableArray arrayWithCapacity:n]);
bcaadf7e
DE
90 for(int i=0; i < n; i++)
91 {
92 [m_cocoaItems addObject: wxNSStringWithWxString(choices[i])];
93 }
94 // Remove everything
c0ccf6a9 95 m_itemClientData.Clear();
bcaadf7e 96 // Initialize n elements to NULL
c0ccf6a9 97 m_itemClientData.SetCount(n,NULL);
bcaadf7e
DE
98
99 SetNSTableView([[NSTableView alloc] initWithFrame: MakeDefaultNSRect(size)]);
981af57c 100 [m_cocoaNSView release];
4a3dcf1d 101 [GetNSTableView() setHeaderView: nil];
981af57c 102
bcaadf7e 103 // Set up the data source
e7e1ad7d 104 m_cocoaDataSource = [[WX_GET_OBJC_CLASS(wxCocoaNSTableDataSource) alloc] init];
bcaadf7e
DE
105 [GetNSTableView() setDataSource:m_cocoaDataSource];
106
107 // Add the single column
108 NSTableColumn *tableColumn = [[NSTableColumn alloc] initWithIdentifier:nil];
109 [GetNSTableView() addTableColumn: tableColumn];
4a3dcf1d 110 [tableColumn release];
bcaadf7e 111
4a3dcf1d 112 [GetNSTableView() sizeToFit];
bcaadf7e 113 // Finish
812edc25
DE
114 if(m_parent)
115 m_parent->CocoaAddChild(this);
4a3dcf1d
DE
116 // NSTableView does WEIRD things with sizes. Wrapping it in an
117 // NSScrollView seems to be the only reasonable solution.
118 CocoaCreateNSScrollView();
bcaadf7e 119 SetInitialFrameRect(pos,size);
11e62fe6 120
97795793 121 [m_wxCocoaScrollView->GetNSScrollView() setHasVerticalScroller:YES];
fd3fe3ec
DE
122 // Pre-10.3: Always show vertical scroller, never show horizontal scroller
123 // Post-10.3: Show scrollers dynamically (turn them both on, set auto-hide)
124 if([m_wxCocoaScrollView->GetNSScrollView() respondsToSelector:@selector(setAutohidesScrollers:)])
125 {
126 [m_wxCocoaScrollView->GetNSScrollView() setHasHorizontalScroller:YES];
127 [m_wxCocoaScrollView->GetNSScrollView() setAutohidesScrollers:YES];
128 }
97795793 129
50d2af52 130 // Set up extended/multiple selection flags
11e62fe6 131 if ((style & wxLB_EXTENDED) || (style & wxLB_MULTIPLE))
50d2af52
RN
132 //diff is that mult requires shift down for multi selection
133 [GetNSTableView() setAllowsMultipleSelection:true];
134
135 [GetNSTableView() setAllowsColumnSelection:false];
bcaadf7e 136
812edc25
DE
137 return true;
138}
139
140wxListBox::~wxListBox()
141{
bcaadf7e
DE
142 [GetNSTableView() setDataSource: nil];
143 [m_cocoaDataSource release];
1a393573
DE
144 wxGCSafeRelease(m_cocoaItems);
145 m_cocoaItems = nil;
911e17c6 146 DisassociateNSTableView(GetNSTableView());
bcaadf7e
DE
147}
148
149int wxListBox::CocoaDataSource_numberOfRows()
150{
151 return [m_cocoaItems count];
152}
153
154struct objc_object* wxListBox::CocoaDataSource_objectForTableColumn(
155 WX_NSTableColumn tableColumn, int rowIndex)
156{
157 return [m_cocoaItems objectAtIndex:rowIndex];
812edc25
DE
158}
159
160// pure virtuals from wxListBoxBase
161bool wxListBox::IsSelected(int n) const
162{
bcaadf7e 163 return [GetNSTableView() isRowSelected: n];
812edc25
DE
164}
165
c6179a84 166void wxListBox::DoSetSelection(int n, bool select)
812edc25 167{
bcaadf7e
DE
168 if(select)
169 [GetNSTableView() selectRow: n byExtendingSelection:NO];
170 else
171 [GetNSTableView() deselectRow: n];
812edc25
DE
172}
173
174int wxListBox::GetSelections(wxArrayInt& aSelections) const
175{
bcaadf7e
DE
176 aSelections.Clear();
177 NSEnumerator *enumerator = [GetNSTableView() selectedRowEnumerator];
178 while(NSNumber *num = [enumerator nextObject])
179 {
180 aSelections.Add([num intValue]);
181 }
182 return [GetNSTableView() numberOfSelectedRows];
812edc25
DE
183}
184
a236aa20 185int wxListBox::DoInsertItems(const wxArrayStringsAdapter & items, unsigned int pos, void **clientData, wxClientDataType type)
812edc25 186{
50d2af52
RN
187 wxAutoNSAutoreleasePool pool;
188
a236aa20
VZ
189 const unsigned int numItems = items.GetCount();
190 for ( unsigned int i = 0; i < numItems; ++i, ++pos )
bcaadf7e
DE
191 {
192 [m_cocoaItems insertObject: wxNSStringWithWxString(items[i])
193 atIndex: pos];
a236aa20
VZ
194 m_itemClientData.Insert(NULL, pos);
195 AssignNewItemClientData(pos, clientData, i, type);
bcaadf7e 196 }
11e62fe6 197
bcaadf7e 198 [GetNSTableView() reloadData];
a236aa20 199 return pos - 1;
812edc25
DE
200}
201
202void wxListBox::DoSetFirstItem(int n)
203{
50d2af52
RN
204 [m_cocoaItems exchangeObjectAtIndex:0 withObjectAtIndex:n];
205 void* pOld = m_itemClientData[n];
206 m_itemClientData[n] = m_itemClientData[0];
207 m_itemClientData[0] = pOld;
208 [GetNSTableView() reloadData];
812edc25
DE
209}
210
211
212// pure virtuals from wxItemContainer
213 // deleting items
a236aa20 214void wxListBox::DoClear()
812edc25 215{
50d2af52
RN
216 [m_cocoaItems removeAllObjects];
217 m_itemClientData.Clear();
218 [GetNSTableView() reloadData];
812edc25
DE
219}
220
a236aa20 221void wxListBox::DoDeleteOneItem(unsigned int n)
812edc25 222{
50d2af52
RN
223 [m_cocoaItems removeObjectAtIndex:n];
224 m_itemClientData.RemoveAt(n);
11e62fe6 225 [GetNSTableView() reloadData];
812edc25
DE
226}
227
228 // accessing strings
aa61d352 229unsigned int wxListBox::GetCount() const
812edc25 230{
aa61d352 231 return (unsigned int)[m_cocoaItems count];
812edc25
DE
232}
233
aa61d352 234wxString wxListBox::GetString(unsigned int n) const
812edc25 235{
50d2af52 236 return wxStringWithNSString([m_cocoaItems objectAtIndex:n]);
812edc25
DE
237}
238
aa61d352 239void wxListBox::SetString(unsigned int n, const wxString& s)
812edc25 240{
50d2af52
RN
241 wxAutoNSAutoreleasePool pool;
242 [m_cocoaItems removeObjectAtIndex:n];
243 [m_cocoaItems insertObject: wxNSStringWithWxString(s) atIndex: n];
11e62fe6 244 [GetNSTableView() reloadData];
812edc25
DE
245}
246
11e62fe6 247int wxListBox::FindString(const wxString& s, bool bCase) const
812edc25 248{
11e62fe6 249 // FIXME: use wxItemContainerImmutable::FindString for bCase parameter
50d2af52
RN
250 wxAutoNSAutoreleasePool pool;
251 return [m_cocoaItems indexOfObject:wxNSStringWithWxString(s)];
812edc25
DE
252}
253
254 // selection
812edc25
DE
255int wxListBox::GetSelection() const
256{
50d2af52 257 return [GetNSTableView() selectedRow];
812edc25
DE
258}
259
aa61d352 260void wxListBox::DoSetItemClientData(unsigned int n, void* clientData)
812edc25 261{
50d2af52 262 m_itemClientData[n] = clientData;
812edc25
DE
263}
264
aa61d352 265void* wxListBox::DoGetItemClientData(unsigned int n) const
812edc25 266{
50d2af52 267 return m_itemClientData[n];
812edc25
DE
268}
269
a236aa20 270#endif // wxUSE_LISTBOX