1 /////////////////////////////////////////////////////////////////////////////
2 // Name: cocoa/listbox.mm
4 // Author: David Elliott
8 // Copyright: (c) 2003 David Elliott
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #include "wx/listbox.h"
16 #include "wx/cocoa/string.h"
17 #include "wx/cocoa/NSTableDataSource.h"
19 #import <Foundation/NSArray.h>
20 #import <Foundation/NSEnumerator.h>
21 #import <AppKit/NSTableView.h>
22 #import <AppKit/NSTableColumn.h>
24 IMPLEMENT_DYNAMIC_CLASS(wxListBox, wxControl)
25 BEGIN_EVENT_TABLE(wxListBox, wxListBoxBase)
27 WX_IMPLEMENT_COCOA_OWNER(wxListBox,NSTableView,NSControl,NSView)
29 bool wxListBox::Create(wxWindow *parent, wxWindowID winid,
32 int n, const wxString choices[],
34 const wxValidator& validator,
37 if(!CreateControl(parent,winid,pos,size,style,validator,name))
41 m_cocoaItems = [[NSMutableArray arrayWithCapacity:n] retain];
42 for(int i=0; i < n; i++)
44 [m_cocoaItems addObject: wxNSStringWithWxString(choices[i])];
48 // Initialize n elements to NULL
49 m_clientData.SetCount(n,NULL);
51 SetNSTableView([[NSTableView alloc] initWithFrame: MakeDefaultNSRect(size)]);
52 [m_cocoaNSView release];
53 [GetNSTableView() setHeaderView: nil];
55 // Set up the data source
56 m_cocoaDataSource = [[wxCocoaNSTableDataSource alloc] init];
57 [GetNSTableView() setDataSource:m_cocoaDataSource];
59 // Add the single column
60 NSTableColumn *tableColumn = [[NSTableColumn alloc] initWithIdentifier:nil];
61 [GetNSTableView() addTableColumn: tableColumn];
62 [tableColumn release];
64 [GetNSTableView() sizeToFit];
67 m_parent->CocoaAddChild(this);
68 // NSTableView does WEIRD things with sizes. Wrapping it in an
69 // NSScrollView seems to be the only reasonable solution.
70 CocoaCreateNSScrollView();
71 SetInitialFrameRect(pos,size);
76 wxListBox::~wxListBox()
78 [GetNSTableView() setDataSource: nil];
79 [m_cocoaDataSource release];
80 [m_cocoaItems release];
81 DisassociateNSTableView(m_cocoaNSView);
84 int wxListBox::CocoaDataSource_numberOfRows()
86 return [m_cocoaItems count];
89 struct objc_object* wxListBox::CocoaDataSource_objectForTableColumn(
90 WX_NSTableColumn tableColumn, int rowIndex)
92 return [m_cocoaItems objectAtIndex:rowIndex];
95 // pure virtuals from wxListBoxBase
96 bool wxListBox::IsSelected(int n) const
98 return [GetNSTableView() isRowSelected: n];
101 void wxListBox::SetSelection(int n, bool select)
104 [GetNSTableView() selectRow: n byExtendingSelection:NO];
106 [GetNSTableView() deselectRow: n];
109 int wxListBox::GetSelections(wxArrayInt& aSelections) const
112 NSEnumerator *enumerator = [GetNSTableView() selectedRowEnumerator];
113 while(NSNumber *num = [enumerator nextObject])
115 aSelections.Add([num intValue]);
117 return [GetNSTableView() numberOfSelectedRows];
120 void wxListBox::DoInsertItems(const wxArrayString& items, int pos)
122 for(int i=int(items.GetCount())-1; i >= 0; i--)
124 [m_cocoaItems insertObject: wxNSStringWithWxString(items[i])
126 m_clientData.Insert(NULL,pos);
128 [GetNSTableView() reloadData];
131 void wxListBox::DoSetItems(const wxArrayString& items, void **clientData)
134 [m_cocoaItems removeAllObjects];
135 m_clientData.Clear();
137 for(size_t i=0; i < items.GetCount(); i++)
139 [m_cocoaItems addObject: wxNSStringWithWxString(items[i])];
140 m_clientData.Add(clientData[i]);
142 [GetNSTableView() reloadData];
145 void wxListBox::DoSetFirstItem(int n)
150 // pure virtuals from wxItemContainer
152 void wxListBox::Clear()
156 void wxListBox::Delete(int n)
161 int wxListBox::GetCount() const
166 wxString wxListBox::GetString(int n) const
168 return wxEmptyString;
171 void wxListBox::SetString(int n, const wxString& s)
175 int wxListBox::FindString(const wxString& s) const
181 void wxListBox::Select(int n)
185 int wxListBox::GetSelection() const
190 int wxListBox::DoAppend(const wxString& item)
195 void wxListBox::DoSetItemClientData(int n, void* clientData)
199 void* wxListBox::DoGetItemClientData(int n) const
204 void wxListBox::DoSetItemClientObject(int n, wxClientData* clientData)
208 wxClientData* wxListBox::DoGetItemClientObject(int n) const