1 /////////////////////////////////////////////////////////////////////////////
2 // Name: cocoa/listbox.mm
4 // Author: David Elliott
8 // Copyright: (c) 2003 David Elliott
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
16 #include "wx/listbox.h"
19 #include "wx/cocoa/string.h"
20 #include "wx/cocoa/autorelease.h"
21 #include "wx/cocoa/NSTableDataSource.h"
23 #import <Foundation/NSArray.h>
24 #import <Foundation/NSEnumerator.h>
25 #import <AppKit/NSTableView.h>
26 #import <AppKit/NSTableColumn.h>
28 IMPLEMENT_DYNAMIC_CLASS(wxListBox, wxControl)
29 BEGIN_EVENT_TABLE(wxListBox, wxListBoxBase)
31 WX_IMPLEMENT_COCOA_OWNER(wxListBox,NSTableView,NSControl,NSView)
33 bool wxListBox::Create(wxWindow *parent, wxWindowID winid,
36 int n, const wxString choices[],
38 const wxValidator& validator,
41 wxAutoNSAutoreleasePool pool;
42 if(!CreateControl(parent,winid,pos,size,style,validator,name))
46 m_cocoaItems = [[NSMutableArray arrayWithCapacity:n] retain];
47 for(int i=0; i < n; i++)
49 [m_cocoaItems addObject: wxNSStringWithWxString(choices[i])];
53 // Initialize n elements to NULL
54 m_clientData.SetCount(n,NULL);
56 SetNSTableView([[NSTableView alloc] initWithFrame: MakeDefaultNSRect(size)]);
57 [m_cocoaNSView release];
58 [GetNSTableView() setHeaderView: nil];
60 // Set up the data source
61 m_cocoaDataSource = [[wxCocoaNSTableDataSource alloc] init];
62 [GetNSTableView() setDataSource:m_cocoaDataSource];
64 // Add the single column
65 NSTableColumn *tableColumn = [[NSTableColumn alloc] initWithIdentifier:nil];
66 [GetNSTableView() addTableColumn: tableColumn];
67 [tableColumn release];
69 [GetNSTableView() sizeToFit];
72 m_parent->CocoaAddChild(this);
73 // NSTableView does WEIRD things with sizes. Wrapping it in an
74 // NSScrollView seems to be the only reasonable solution.
75 CocoaCreateNSScrollView();
76 SetInitialFrameRect(pos,size);
81 wxListBox::~wxListBox()
83 [GetNSTableView() setDataSource: nil];
84 [m_cocoaDataSource release];
85 [m_cocoaItems release];
86 DisassociateNSTableView(GetNSTableView());
89 int wxListBox::CocoaDataSource_numberOfRows()
91 return [m_cocoaItems count];
94 struct objc_object* wxListBox::CocoaDataSource_objectForTableColumn(
95 WX_NSTableColumn tableColumn, int rowIndex)
97 return [m_cocoaItems objectAtIndex:rowIndex];
100 // pure virtuals from wxListBoxBase
101 bool wxListBox::IsSelected(int n) const
103 return [GetNSTableView() isRowSelected: n];
106 void wxListBox::SetSelection(int n, bool select)
109 [GetNSTableView() selectRow: n byExtendingSelection:NO];
111 [GetNSTableView() deselectRow: n];
114 int wxListBox::GetSelections(wxArrayInt& aSelections) const
117 NSEnumerator *enumerator = [GetNSTableView() selectedRowEnumerator];
118 while(NSNumber *num = [enumerator nextObject])
120 aSelections.Add([num intValue]);
122 return [GetNSTableView() numberOfSelectedRows];
125 void wxListBox::DoInsertItems(const wxArrayString& items, int pos)
127 for(int i=int(items.GetCount())-1; i >= 0; i--)
129 [m_cocoaItems insertObject: wxNSStringWithWxString(items[i])
131 m_clientData.Insert(NULL,pos);
133 [GetNSTableView() reloadData];
136 void wxListBox::DoSetItems(const wxArrayString& items, void **clientData)
139 [m_cocoaItems removeAllObjects];
140 m_clientData.Clear();
142 for(size_t i=0; i < items.GetCount(); i++)
144 [m_cocoaItems addObject: wxNSStringWithWxString(items[i])];
145 m_clientData.Add(clientData[i]);
147 [GetNSTableView() reloadData];
150 void wxListBox::DoSetFirstItem(int n)
155 // pure virtuals from wxItemContainer
157 void wxListBox::Clear()
161 void wxListBox::Delete(int n)
166 int wxListBox::GetCount() const
171 wxString wxListBox::GetString(int n) const
173 return wxEmptyString;
176 void wxListBox::SetString(int n, const wxString& s)
180 int wxListBox::FindString(const wxString& s) const
186 void wxListBox::Select(int n)
190 int wxListBox::GetSelection() const
195 int wxListBox::DoAppend(const wxString& item)
200 void wxListBox::DoSetItemClientData(int n, void* clientData)
204 void* wxListBox::DoGetItemClientData(int n) const
209 void wxListBox::DoSetItemClientObject(int n, wxClientData* clientData)
213 wxClientData* wxListBox::DoGetItemClientObject(int n) const