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