]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/mac/choice.cpp
bug fixes
[wxWidgets.git] / src / mac / choice.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: choice.cpp
3// Purpose: wxChoice
4// Author: AUTHOR
5// Modified by:
6// Created: ??/??/98
7// RCS-ID: $Id$
8// Copyright: (c) AUTHOR
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11#ifdef __GNUG__
12#pragma implementation "choice.h"
13#endif
14#include "wx/defs.h"
15#include "wx/choice.h"
16#include "wx/menu.h"
17#include "wx/mac/uma.h"
18#if !USE_SHARED_LIBRARY
19IMPLEMENT_DYNAMIC_CLASS(wxChoice, wxControl)
20#endif
21extern MenuHandle NewUniqueMenu() ;
22wxChoice::~wxChoice()
23{
24 if ( HasClientObjectData() )
25 {
26 size_t i, max = GetCount();
27
28 for ( i = 0; i < max; ++i )
29 delete GetClientObject(i);
30 }
31
32 // DeleteMenu( m_macPopUpMenuId ) ;
33 // DisposeMenu( m_macPopUpMenuHandle ) ;
34}
35bool wxChoice::Create(wxWindow *parent, wxWindowID id,
36 const wxPoint& pos,
37 const wxSize& size,
38 int n, const wxString choices[],
39 long style,
40 const wxValidator& validator,
41 const wxString& name)
42{
43 Rect bounds ;
44 Str255 title ;
45
46 MacPreControlCreate( parent , id , "" , pos , size ,style, validator , name , &bounds , title ) ;
47 m_macControl = ::NewControl( MAC_WXHWND(parent->MacGetRootWindow()) , &bounds , title , false , 0 , -12345 , 0 ,
48 kControlPopupButtonProc + kControlPopupFixedWidthVariant , (long) this ) ;
49
50 m_macPopUpMenuHandle = NewUniqueMenu() ;
51 SetControlData( (ControlHandle) m_macControl , kControlNoPart , kControlPopupButtonMenuHandleTag , sizeof( MenuHandle ) , (char*) &m_macPopUpMenuHandle) ;
52 SetControl32BitMinimum( (ControlHandle) m_macControl , 0 ) ;
53 SetControl32BitMaximum( (ControlHandle) m_macControl , 0) ;
54 if ( n > 0 )
55 SetControl32BitValue( (ControlHandle) m_macControl , 1 ) ;
56 MacPostControlCreate() ;
57 for ( int i = 0; i < n; i++ )
58 {
59 Append(choices[i]);
60 }
61 return TRUE;
62}
63// ----------------------------------------------------------------------------
64// adding/deleting items to/from the list
65// ----------------------------------------------------------------------------
66int wxChoice::DoAppend(const wxString& item)
67{
68 UMAAppendMenuItem(MAC_WXHMENU( m_macPopUpMenuHandle ) , item);
69 m_strings.Add( item ) ;
70 m_datas.Add( NULL ) ;
71 int index = m_strings.GetCount() - 1 ;
72 DoSetItemClientData( index , NULL ) ;
73 SetControl32BitMaximum( (ControlHandle) m_macControl , GetCount()) ;
74 return index ;
75}
76void wxChoice::Delete(int n)
77{
78 wxCHECK_RET( n < GetCount(), wxT("invalid item index in wxChoice::Delete") );
79 if ( HasClientObjectData() )
80 {
81 delete GetClientObject(n);
82 }
83 ::DeleteMenuItem( MAC_WXHMENU(m_macPopUpMenuHandle) , n + 1) ;
84 m_strings.Remove( n ) ;
85 m_datas.RemoveAt( n ) ;
86 SetControl32BitMaximum( (ControlHandle) m_macControl , GetCount()) ;
87}
88void wxChoice::Clear()
89{
90 FreeData();
91 for ( int i = 0 ; i < GetCount() ; i++ )
92 {
93 ::DeleteMenuItem( MAC_WXHMENU(m_macPopUpMenuHandle) , 1 ) ;
94 }
95 m_strings.Empty() ;
96 m_datas.Empty() ;
97 SetControl32BitMaximum( (ControlHandle) m_macControl , 0 ) ;
98}
99void wxChoice::FreeData()
100{
101 if ( HasClientObjectData() )
102 {
103 size_t count = GetCount();
104 for ( size_t n = 0; n < count; n++ )
105 {
106 delete GetClientObject(n);
107 }
108 }
109}
110// ----------------------------------------------------------------------------
111// selection
112// ----------------------------------------------------------------------------
113int wxChoice::GetSelection() const
114{
115 return GetControl32BitValue( (ControlHandle) m_macControl ) -1 ;
116}
117void wxChoice::SetSelection(int n)
118{
119 SetControl32BitValue( (ControlHandle) m_macControl , n + 1 ) ;
120}
121// ----------------------------------------------------------------------------
122// string list functions
123// ----------------------------------------------------------------------------
124int wxChoice::GetCount() const
125{
126 return m_strings.GetCount() ;
127}
128int wxChoice::FindString(const wxString& s) const
129{
130 for( int i = 0 ; i < GetCount() ; i++ )
131 {
132 if ( GetString( i ).IsSameAs(s, FALSE) )
133 return i ;
134 }
135 return wxNOT_FOUND ;
136}
137void wxChoice::SetString(int n, const wxString& s)
138{
139 wxFAIL_MSG(wxT("wxChoice::SetString() not yet implemented"));
140#if 0 // should do this, but no Insert() so far
141 Delete(n);
142 Insert(n + 1, s);
143#endif
144}
145
146wxString wxChoice::GetString(int n) const
147{
148 return m_strings[n] ;
149}
150// ----------------------------------------------------------------------------
151// client data
152// ----------------------------------------------------------------------------
153void wxChoice::DoSetItemClientData( int n, void* clientData )
154{
155 wxCHECK_RET( n >= 0 && (size_t)n < m_datas.GetCount(),
156 "invalid index in wxChoice::SetClientData" );
157
158 m_datas[n] = (char*) clientData ;
159}
160void *wxChoice::DoGetItemClientData(int n) const
161{
162 wxCHECK_MSG( n >= 0 && (size_t)n < m_datas.GetCount(), NULL,
163 "invalid index in wxChoice::GetClientData" );
164 return (void *)m_datas[n];
165}
166void wxChoice::DoSetItemClientObject( int n, wxClientData* clientData )
167{
168 DoSetItemClientData(n, clientData);
169}
170wxClientData* wxChoice::DoGetItemClientObject( int n ) const
171{
172 return (wxClientData *)DoGetItemClientData(n);
173}
174void wxChoice::MacHandleControlClick( WXWidget control , wxInt16 controlpart )
175{
176 wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, m_windowId );
177 int n = GetSelection();
178 // actually n should be made sure by the os to be a valid selection, but ...
179 if ( n > -1 )
180 {
181 event.SetInt( n );
182 event.SetString(GetStringSelection());
183 event.SetEventObject(this);
184 if ( HasClientObjectData() )
185 event.SetClientObject( GetClientObject(n) );
186 else if ( HasClientUntypedData() )
187 event.SetClientData( GetClientData(n) );
188 ProcessCommand(event);
189 }
190}
191wxSize wxChoice::DoGetBestSize() const
192{
193 int lbWidth = 100; // some defaults
194 int lbHeight = 20;
195 int wLine;
196#if TARGET_CARBON
197 long metric ;
198 GetThemeMetric(kThemeMetricPopupButtonHeight , &metric );
199 lbHeight = metric ;
200#endif
201 {
202 wxMacPortStateHelper st( UMAGetWindowPort( (WindowRef) MacGetRootWindow() ) ) ;
203 Rect drawRect ;
204 wxFontRefData * font = (wxFontRefData*) m_font.GetRefData() ;
205 if ( font )
206 {
207 ::TextFont( font->m_macFontNum ) ;
208 ::TextSize( short(font->m_macFontSize) ) ;
209 ::TextFace( font->m_macFontStyle ) ;
210 }
211 else
212 {
213 ::TextFont( kFontIDMonaco ) ;
214 ::TextSize( 9 );
215 ::TextFace( 0 ) ;
216 }
217 // Find the widest line
218 for(int i = 0; i < GetCount(); i++) {
219 wxString str(GetString(i));
220 wLine = ::TextWidth( str.c_str() , 0 , str.Length() ) ;
221 lbWidth = wxMax(lbWidth, wLine);
222 }
223 // Add room for the popup arrow
224 lbWidth += 2 * lbHeight ;
225 // And just a bit more
226 int cy = 12 ;
227 int cx = ::TextWidth( "X" , 0 , 1 ) ;
228 lbWidth += cx ;
229
230 }
231 return wxSize(lbWidth, lbHeight);
232}