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/NSTableDataSource.h"
22 #import <Foundation/NSArray.h>
23 #import <Foundation/NSEnumerator.h>
24 #import <AppKit/NSTableView.h>
25 #import <AppKit/NSTableColumn.h>
27 IMPLEMENT_DYNAMIC_CLASS(wxListBox, wxControl)
28 BEGIN_EVENT_TABLE(wxListBox, wxListBoxBase)
30 WX_IMPLEMENT_COCOA_OWNER(wxListBox,NSTableView,NSControl,NSView)
32 bool wxListBox::Create(wxWindow *parent, wxWindowID winid,
35 int n, const wxString choices[],
37 const wxValidator& validator,
40 if(!CreateControl(parent,winid,pos,size,style,validator,name))
44 m_cocoaItems = [[NSMutableArray arrayWithCapacity:n] retain];
45 for(int i=0; i < n; i++)
47 [m_cocoaItems addObject: wxNSStringWithWxString(choices[i])];
51 // Initialize n elements to NULL
52 m_clientData.SetCount(n,NULL);
54 SetNSTableView([[NSTableView alloc] initWithFrame: MakeDefaultNSRect(size)]);
55 [m_cocoaNSView release];
56 [GetNSTableView() setHeaderView: nil];
58 // Set up the data source
59 m_cocoaDataSource = [[wxCocoaNSTableDataSource alloc] init];
60 [GetNSTableView() setDataSource:m_cocoaDataSource];
62 // Add the single column
63 NSTableColumn *tableColumn = [[NSTableColumn alloc] initWithIdentifier:nil];
64 [GetNSTableView() addTableColumn: tableColumn];
65 [tableColumn release];
67 [GetNSTableView() sizeToFit];
70 m_parent->CocoaAddChild(this);
71 // NSTableView does WEIRD things with sizes. Wrapping it in an
72 // NSScrollView seems to be the only reasonable solution.
73 CocoaCreateNSScrollView();
74 SetInitialFrameRect(pos,size);
79 wxListBox::~wxListBox()
81 [GetNSTableView() setDataSource: nil];
82 [m_cocoaDataSource release];
83 [m_cocoaItems release];
84 DisassociateNSTableView(GetNSTableView());
87 int wxListBox::CocoaDataSource_numberOfRows()
89 return [m_cocoaItems count];
92 struct objc_object* wxListBox::CocoaDataSource_objectForTableColumn(
93 WX_NSTableColumn tableColumn, int rowIndex)
95 return [m_cocoaItems objectAtIndex:rowIndex];
98 // pure virtuals from wxListBoxBase
99 bool wxListBox::IsSelected(int n) const
101 return [GetNSTableView() isRowSelected: n];
104 void wxListBox::SetSelection(int n, bool select)
107 [GetNSTableView() selectRow: n byExtendingSelection:NO];
109 [GetNSTableView() deselectRow: n];
112 int wxListBox::GetSelections(wxArrayInt& aSelections) const
115 NSEnumerator *enumerator = [GetNSTableView() selectedRowEnumerator];
116 while(NSNumber *num = [enumerator nextObject])
118 aSelections.Add([num intValue]);
120 return [GetNSTableView() numberOfSelectedRows];
123 void wxListBox::DoInsertItems(const wxArrayString& items, int pos)
125 for(int i=int(items.GetCount())-1; i >= 0; i--)
127 [m_cocoaItems insertObject: wxNSStringWithWxString(items[i])
129 m_clientData.Insert(NULL,pos);
131 [GetNSTableView() reloadData];
134 void wxListBox::DoSetItems(const wxArrayString& items, void **clientData)
137 [m_cocoaItems removeAllObjects];
138 m_clientData.Clear();
140 for(size_t i=0; i < items.GetCount(); i++)
142 [m_cocoaItems addObject: wxNSStringWithWxString(items[i])];
143 m_clientData.Add(clientData[i]);
145 [GetNSTableView() reloadData];
148 void wxListBox::DoSetFirstItem(int n)
153 // pure virtuals from wxItemContainer
155 void wxListBox::Clear()
159 void wxListBox::Delete(int n)
164 int wxListBox::GetCount() const
169 wxString wxListBox::GetString(int n) const
171 return wxEmptyString;
174 void wxListBox::SetString(int n, const wxString& s)
178 int wxListBox::FindString(const wxString& s) const
184 void wxListBox::Select(int n)
188 int wxListBox::GetSelection() const
193 int wxListBox::DoAppend(const wxString& item)
198 void wxListBox::DoSetItemClientData(int n, void* clientData)
202 void* wxListBox::DoGetItemClientData(int n) const
207 void wxListBox::DoSetItemClientObject(int n, wxClientData* clientData)
211 wxClientData* wxListBox::DoGetItemClientObject(int n) const