1 /////////////////////////////////////////////////////////////////////////////
2 // Name: cocoa/listbox.mm
4 // Author: David Elliott
8 // Copyright: (c) 2003 David Elliott
9 // Licence: wxWidgets licence
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 const wxArrayString& choices,
38 const wxValidator& validator,
41 wxCArrayString chs(choices);
43 return Create(parent, winid, pos, size, chs.GetCount(), chs.GetStrings(),
44 style, validator, name);
47 bool wxListBox::Create(wxWindow *parent, wxWindowID winid,
50 int n, const wxString choices[],
52 const wxValidator& validator,
55 wxAutoNSAutoreleasePool pool;
56 if(!CreateControl(parent,winid,pos,size,style,validator,name))
60 m_cocoaItems = [[NSMutableArray arrayWithCapacity:n] retain];
61 for(int i=0; i < n; i++)
63 [m_cocoaItems addObject: wxNSStringWithWxString(choices[i])];
66 m_itemClientData.Clear();
67 // Initialize n elements to NULL
68 m_itemClientData.SetCount(n,NULL);
70 SetNSTableView([[NSTableView alloc] initWithFrame: MakeDefaultNSRect(size)]);
71 [m_cocoaNSView release];
72 [GetNSTableView() setHeaderView: nil];
74 // Set up the data source
75 m_cocoaDataSource = [[wxCocoaNSTableDataSource alloc] init];
76 [GetNSTableView() setDataSource:m_cocoaDataSource];
78 // Add the single column
79 NSTableColumn *tableColumn = [[NSTableColumn alloc] initWithIdentifier:nil];
80 [GetNSTableView() addTableColumn: tableColumn];
81 [tableColumn release];
83 [GetNSTableView() sizeToFit];
86 m_parent->CocoaAddChild(this);
87 // NSTableView does WEIRD things with sizes. Wrapping it in an
88 // NSScrollView seems to be the only reasonable solution.
89 CocoaCreateNSScrollView();
90 SetInitialFrameRect(pos,size);
95 wxListBox::~wxListBox()
97 [GetNSTableView() setDataSource: nil];
98 [m_cocoaDataSource release];
99 [m_cocoaItems release];
100 DisassociateNSTableView(GetNSTableView());
103 int wxListBox::CocoaDataSource_numberOfRows()
105 return [m_cocoaItems count];
108 struct objc_object* wxListBox::CocoaDataSource_objectForTableColumn(
109 WX_NSTableColumn tableColumn, int rowIndex)
111 return [m_cocoaItems objectAtIndex:rowIndex];
114 // pure virtuals from wxListBoxBase
115 bool wxListBox::IsSelected(int n) const
117 return [GetNSTableView() isRowSelected: n];
120 void wxListBox::SetSelection(int n, bool select)
123 [GetNSTableView() selectRow: n byExtendingSelection:NO];
125 [GetNSTableView() deselectRow: n];
128 int wxListBox::GetSelections(wxArrayInt& aSelections) const
131 NSEnumerator *enumerator = [GetNSTableView() selectedRowEnumerator];
132 while(NSNumber *num = [enumerator nextObject])
134 aSelections.Add([num intValue]);
136 return [GetNSTableView() numberOfSelectedRows];
139 void wxListBox::DoInsertItems(const wxArrayString& items, int pos)
141 for(int i=int(items.GetCount())-1; i >= 0; i--)
143 [m_cocoaItems insertObject: wxNSStringWithWxString(items[i])
145 m_itemClientData.Insert(NULL,pos);
147 [GetNSTableView() reloadData];
150 void wxListBox::DoSetItems(const wxArrayString& items, void **clientData)
153 [m_cocoaItems removeAllObjects];
154 m_itemClientData.Clear();
156 for(size_t i=0; i < items.GetCount(); i++)
158 [m_cocoaItems addObject: wxNSStringWithWxString(items[i])];
159 m_itemClientData.Add(clientData[i]);
161 [GetNSTableView() reloadData];
164 void wxListBox::DoSetFirstItem(int n)
169 // pure virtuals from wxItemContainer
171 void wxListBox::Clear()
175 void wxListBox::Delete(int n)
180 int wxListBox::GetCount() const
185 wxString wxListBox::GetString(int n) const
187 return wxEmptyString;
190 void wxListBox::SetString(int n, const wxString& s)
194 int wxListBox::FindString(const wxString& s) const
200 void wxListBox::Select(int n)
204 int wxListBox::GetSelection() const
209 int wxListBox::DoAppend(const wxString& item)
214 void wxListBox::DoSetItemClientData(int n, void* clientData)
218 void* wxListBox::DoGetItemClientData(int n) const
223 void wxListBox::DoSetItemClientObject(int n, wxClientData* clientData)
227 wxClientData* wxListBox::DoGetItemClientObject(int n) const