]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/choice.cpp
routing all apple events to the document manager instance if present, fully supportin...
[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 Str255 label;
61 wxMenuItem::MacBuildMenuString( label , NULL , NULL , item ,false);
62 MacAppendMenu(MAC_WXHMENU( m_macPopUpMenuHandle ) , "\pA");
63 SetMenuItemText(MAC_WXHMENU( m_macPopUpMenuHandle ) ,
64 (SInt16) ::CountMenuItems(MAC_WXHMENU( m_macPopUpMenuHandle ) ), label);
65 // was AppendMenu( MAC_WXHMENU( m_macPopUpMenuHandle ) , label ) ;
66 m_strings.Add( item ) ;
67 m_datas.Add( NULL ) ;
68 int index = m_strings.GetCount() - 1 ;
69 DoSetItemClientData( index , NULL ) ;
70 SetControl32BitMaximum( (ControlHandle) m_macControl , GetCount()) ;
71 return index ;
72 }
73 void wxChoice::Delete(int n)
74 {
75 wxCHECK_RET( n < GetCount(), wxT("invalid item index in wxChoice::Delete") );
76 if ( HasClientObjectData() )
77 {
78 delete GetClientObject(n);
79 }
80 ::DeleteMenuItem( MAC_WXHMENU(m_macPopUpMenuHandle) , n + 1) ;
81 m_strings.Remove( n ) ;
82 m_datas.RemoveAt( n ) ;
83 SetControl32BitMaximum( (ControlHandle) m_macControl , GetCount()) ;
84 }
85 void wxChoice::Clear()
86 {
87 FreeData();
88 for ( int i = 0 ; i < GetCount() ; i++ )
89 {
90 ::DeleteMenuItem( MAC_WXHMENU(m_macPopUpMenuHandle) , 1 ) ;
91 }
92 m_strings.Empty() ;
93 m_datas.Empty() ;
94 SetControl32BitMaximum( (ControlHandle) m_macControl , 0 ) ;
95 }
96 void wxChoice::FreeData()
97 {
98 if ( HasClientObjectData() )
99 {
100 size_t count = GetCount();
101 for ( size_t n = 0; n < count; n++ )
102 {
103 delete GetClientObject(n);
104 }
105 }
106 }
107 // ----------------------------------------------------------------------------
108 // selection
109 // ----------------------------------------------------------------------------
110 int wxChoice::GetSelection() const
111 {
112 return GetControl32BitValue( (ControlHandle) m_macControl ) -1 ;
113 }
114 void wxChoice::SetSelection(int n)
115 {
116 SetControl32BitValue( (ControlHandle) m_macControl , n + 1 ) ;
117 }
118 // ----------------------------------------------------------------------------
119 // string list functions
120 // ----------------------------------------------------------------------------
121 int wxChoice::GetCount() const
122 {
123 return m_strings.GetCount() ;
124 }
125 int wxChoice::FindString(const wxString& s) const
126 {
127 for( int i = 0 ; i < GetCount() ; i++ )
128 {
129 if ( GetString( i ).IsSameAs(s, FALSE) )
130 return i ;
131 }
132 return wxNOT_FOUND ;
133 }
134 void wxChoice::SetString(int n, const wxString& s)
135 {
136 wxFAIL_MSG(wxT("wxChoice::SetString() not yet implemented"));
137 #if 0 // should do this, but no Insert() so far
138 Delete(n);
139 Insert(n + 1, s);
140 #endif
141 }
142
143 wxString wxChoice::GetString(int n) const
144 {
145 return m_strings[n] ;
146 }
147 // ----------------------------------------------------------------------------
148 // client data
149 // ----------------------------------------------------------------------------
150 void wxChoice::DoSetItemClientData( int n, void* clientData )
151 {
152 wxCHECK_RET( n >= 0 && (size_t)n < m_datas.GetCount(),
153 "invalid index in wxChoice::SetClientData" );
154
155 m_datas[n] = (char*) clientData ;
156 }
157 void *wxChoice::DoGetItemClientData(int n) const
158 {
159 wxCHECK_MSG( n >= 0 && (size_t)n < m_datas.GetCount(), NULL,
160 "invalid index in wxChoice::GetClientData" );
161 return (void *)m_datas[n];
162 }
163 void wxChoice::DoSetItemClientObject( int n, wxClientData* clientData )
164 {
165 DoSetItemClientData(n, clientData);
166 }
167 wxClientData* wxChoice::DoGetItemClientObject( int n ) const
168 {
169 return (wxClientData *)DoGetItemClientData(n);
170 }
171 void wxChoice::MacHandleControlClick( WXWidget control , wxInt16 controlpart )
172 {
173 wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, m_windowId );
174 int n = GetSelection();
175 // actually n should be made sure by the os to be a valid selection, but ...
176 if ( n > -1 )
177 {
178 event.SetInt( n );
179 event.SetString(GetStringSelection());
180 event.SetEventObject(this);
181 if ( HasClientObjectData() )
182 event.SetClientObject( GetClientObject(n) );
183 else if ( HasClientUntypedData() )
184 event.SetClientData( GetClientData(n) );
185 ProcessCommand(event);
186 }
187 }
188 wxSize wxChoice::DoGetBestSize() const
189 {
190 int lbWidth = 100; // some defaults
191 int lbHeight = 20;
192 int wLine;
193 #if TARGET_CARBON
194 long metric ;
195 GetThemeMetric(kThemeMetricPopupButtonHeight , &metric );
196 lbHeight = metric ;
197 #endif
198 {
199 wxMacPortStateHelper st( UMAGetWindowPort( (WindowRef) MacGetRootWindow() ) ) ;
200 Rect drawRect ;
201 wxFontRefData * font = (wxFontRefData*) m_font.GetRefData() ;
202 if ( font )
203 {
204 ::TextFont( font->m_macFontNum ) ;
205 ::TextSize( short(font->m_macFontSize) ) ;
206 ::TextFace( font->m_macFontStyle ) ;
207 }
208 else
209 {
210 ::TextFont( kFontIDMonaco ) ;
211 ::TextSize( 9 );
212 ::TextFace( 0 ) ;
213 }
214 // Find the widest line
215 for(int i = 0; i < GetCount(); i++) {
216 wxString str(GetString(i));
217 wLine = ::TextWidth( str.c_str() , 0 , str.Length() ) ;
218 lbWidth = wxMax(lbWidth, wLine);
219 }
220 // Add room for the popup arrow
221 lbWidth += 2 * lbHeight ;
222 // And just a bit more
223 int cy = 12 ;
224 int cx = ::TextWidth( "X" , 0 , 1 ) ;
225 lbWidth += cx ;
226
227 }
228 return wxSize(lbWidth, lbHeight);
229 }