]>
Commit | Line | Data |
---|---|---|
2ac013b1 | 1 | ///////////////////////////////////////////////////////////////////////////// |
e9576ca5 SC |
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 | ///////////////////////////////////////////////////////////////////////////// | |
e9576ca5 SC |
11 | #ifdef __GNUG__ |
12 | #pragma implementation "choice.h" | |
13 | #endif | |
d8c736e5 | 14 | #include "wx/defs.h" |
e9576ca5 | 15 | #include "wx/choice.h" |
03e11df5 | 16 | #include "wx/menu.h" |
519cb848 | 17 | #include "wx/mac/uma.h" |
2f1ae414 | 18 | #if !USE_SHARED_LIBRARY |
e9576ca5 | 19 | IMPLEMENT_DYNAMIC_CLASS(wxChoice, wxControl) |
2f1ae414 | 20 | #endif |
892e461e | 21 | extern MenuHandle NewUniqueMenu() ; |
5b781a67 SC |
22 | wxChoice::~wxChoice() |
23 | { | |
24 | // DeleteMenu( m_macPopUpMenuId ) ; | |
e7b596fb | 25 | // DisposeMenu( m_macPopUpMenuHandle ) ; |
5b781a67 | 26 | } |
e9576ca5 SC |
27 | bool wxChoice::Create(wxWindow *parent, wxWindowID id, |
28 | const wxPoint& pos, | |
29 | const wxSize& size, | |
03e11df5 GD |
30 | int n, const wxString choices[], |
31 | long style, | |
e9576ca5 SC |
32 | const wxValidator& validator, |
33 | const wxString& name) | |
34 | { | |
519cb848 SC |
35 | Rect bounds ; |
36 | Str255 title ; | |
37 | ||
38 | MacPreControlCreate( parent , id , "" , pos , size ,style, validator , name , &bounds , title ) ; | |
76a5e5d2 | 39 | m_macControl = ::NewControl( MAC_WXHWND(parent->MacGetRootWindow()) , &bounds , title , false , 0 , -12345 , 0 , |
8208e181 | 40 | kControlPopupButtonProc + kControlPopupFixedWidthVariant , (long) this ) ; |
519cb848 | 41 | |
892e461e | 42 | m_macPopUpMenuHandle = NewUniqueMenu() ; |
76a5e5d2 | 43 | SetControlData( (ControlHandle) m_macControl , kControlNoPart , kControlPopupButtonMenuHandleTag , sizeof( MenuHandle ) , (char*) &m_macPopUpMenuHandle) ; |
467e3168 SC |
44 | SetControl32BitMinimum( (ControlHandle) m_macControl , 0 ) ; |
45 | SetControl32BitMaximum( (ControlHandle) m_macControl , 0) ; | |
2f1ae414 | 46 | if ( n > 0 ) |
467e3168 | 47 | SetControl32BitValue( (ControlHandle) m_macControl , 1 ) ; |
519cb848 | 48 | MacPostControlCreate() ; |
b668a735 SC |
49 | for ( int i = 0; i < n; i++ ) |
50 | { | |
51 | Append(choices[i]); | |
52 | } | |
519cb848 | 53 | return TRUE; |
e9576ca5 | 54 | } |
b668a735 SC |
55 | // ---------------------------------------------------------------------------- |
56 | // adding/deleting items to/from the list | |
57 | // ---------------------------------------------------------------------------- | |
2597135a | 58 | int wxChoice::DoAppend(const wxString& item) |
e9576ca5 | 59 | { |
2e97b58c | 60 | UMAAppendMenuItem(MAC_WXHMENU( m_macPopUpMenuHandle ) , item); |
2f1ae414 | 61 | m_strings.Add( item ) ; |
b668a735 SC |
62 | m_datas.Add( NULL ) ; |
63 | int index = m_strings.GetCount() - 1 ; | |
64 | DoSetItemClientData( index , NULL ) ; | |
467e3168 | 65 | SetControl32BitMaximum( (ControlHandle) m_macControl , GetCount()) ; |
b668a735 | 66 | return index ; |
2597135a | 67 | } |
b668a735 | 68 | void wxChoice::Delete(int n) |
2597135a | 69 | { |
b668a735 | 70 | wxCHECK_RET( n < GetCount(), wxT("invalid item index in wxChoice::Delete") ); |
b668a735 | 71 | if ( HasClientObjectData() ) |
2597135a | 72 | { |
b668a735 | 73 | delete GetClientObject(n); |
2597135a | 74 | } |
76a5e5d2 | 75 | ::DeleteMenuItem( MAC_WXHMENU(m_macPopUpMenuHandle) , n + 1) ; |
2f1ae414 | 76 | m_strings.Remove( n ) ; |
3ef585df | 77 | m_datas.RemoveAt( n ) ; |
467e3168 | 78 | SetControl32BitMaximum( (ControlHandle) m_macControl , GetCount()) ; |
e9576ca5 | 79 | } |
e9576ca5 SC |
80 | void wxChoice::Clear() |
81 | { | |
4b651a46 | 82 | FreeData(); |
b668a735 | 83 | for ( int i = 0 ; i < GetCount() ; i++ ) |
519cb848 | 84 | { |
76a5e5d2 | 85 | ::DeleteMenuItem( MAC_WXHMENU(m_macPopUpMenuHandle) , 1 ) ; |
4b651a46 | 86 | } |
b668a735 SC |
87 | m_strings.Empty() ; |
88 | m_datas.Empty() ; | |
467e3168 | 89 | SetControl32BitMaximum( (ControlHandle) m_macControl , 0 ) ; |
e9576ca5 | 90 | } |
4b651a46 | 91 | void wxChoice::FreeData() |
b668a735 SC |
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 | } | |
b668a735 SC |
102 | // ---------------------------------------------------------------------------- |
103 | // selection | |
104 | // ---------------------------------------------------------------------------- | |
e9576ca5 SC |
105 | int wxChoice::GetSelection() const |
106 | { | |
467e3168 | 107 | return GetControl32BitValue( (ControlHandle) m_macControl ) -1 ; |
e9576ca5 | 108 | } |
b668a735 | 109 | void wxChoice::SetSelection(int n) |
519cb848 | 110 | { |
467e3168 | 111 | SetControl32BitValue( (ControlHandle) m_macControl , n + 1 ) ; |
519cb848 | 112 | } |
b668a735 SC |
113 | // ---------------------------------------------------------------------------- |
114 | // string list functions | |
115 | // ---------------------------------------------------------------------------- | |
b668a735 | 116 | int wxChoice::GetCount() const |
e9576ca5 | 117 | { |
b668a735 | 118 | return m_strings.GetCount() ; |
e9576ca5 | 119 | } |
e9576ca5 SC |
120 | int wxChoice::FindString(const wxString& s) const |
121 | { | |
b668a735 | 122 | for( int i = 0 ; i < GetCount() ; i++ ) |
519cb848 | 123 | { |
b668a735 | 124 | if ( GetString( i ).IsSameAs(s, FALSE) ) |
519cb848 SC |
125 | return i ; |
126 | } | |
b668a735 SC |
127 | return wxNOT_FOUND ; |
128 | } | |
b668a735 SC |
129 | void wxChoice::SetString(int n, const wxString& s) |
130 | { | |
4b651a46 | 131 | wxFAIL_MSG(wxT("wxChoice::SetString() not yet implemented")); |
b668a735 SC |
132 | #if 0 // should do this, but no Insert() so far |
133 | Delete(n); | |
134 | Insert(n + 1, s); | |
135 | #endif | |
e9576ca5 SC |
136 | } |
137 | ||
138 | wxString wxChoice::GetString(int n) const | |
139 | { | |
2f1ae414 | 140 | return m_strings[n] ; |
e9576ca5 | 141 | } |
b668a735 SC |
142 | // ---------------------------------------------------------------------------- |
143 | // client data | |
144 | // ---------------------------------------------------------------------------- | |
b668a735 SC |
145 | void wxChoice::DoSetItemClientData( int n, void* clientData ) |
146 | { | |
d81cc883 | 147 | wxCHECK_RET( n >= 0 && (size_t)n < m_datas.GetCount(), |
b668a735 | 148 | "invalid index in wxChoice::SetClientData" ); |
b668a735 | 149 | |
d81cc883 | 150 | m_datas[n] = (char*) clientData ; |
b668a735 | 151 | } |
d81cc883 | 152 | void *wxChoice::DoGetItemClientData(int n) const |
b668a735 | 153 | { |
d81cc883 | 154 | wxCHECK_MSG( n >= 0 && (size_t)n < m_datas.GetCount(), NULL, |
b668a735 | 155 | "invalid index in wxChoice::GetClientData" ); |
d81cc883 | 156 | return (void *)m_datas[n]; |
b668a735 | 157 | } |
b668a735 SC |
158 | void wxChoice::DoSetItemClientObject( int n, wxClientData* clientData ) |
159 | { | |
160 | DoSetItemClientData(n, clientData); | |
161 | } | |
b668a735 | 162 | wxClientData* wxChoice::DoGetItemClientObject( int n ) const |
e9576ca5 | 163 | { |
b668a735 | 164 | return (wxClientData *)DoGetItemClientData(n); |
e9576ca5 | 165 | } |
76a5e5d2 | 166 | void wxChoice::MacHandleControlClick( WXWidget control , wxInt16 controlpart ) |
b668a735 SC |
167 | { |
168 | wxCommandEvent event(wxEVT_COMMAND_CHOICE_SELECTED, m_windowId ); | |
f2d990ad SC |
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); | |
f2d990ad SC |
176 | if ( HasClientObjectData() ) |
177 | event.SetClientObject( GetClientObject(n) ); | |
178 | else if ( HasClientUntypedData() ) | |
179 | event.SetClientData( GetClientData(n) ); | |
f2d990ad SC |
180 | ProcessCommand(event); |
181 | } | |
9453cf2b | 182 | } |
f2d990ad | 183 | wxSize wxChoice::DoGetBestSize() const |
b668a735 | 184 | { |
f2d990ad SC |
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 ; | |
f2d990ad | 196 | wxFontRefData * font = (wxFontRefData*) m_font.GetRefData() ; |
f2d990ad SC |
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 | } | |
f2d990ad SC |
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 | } | |
f2d990ad SC |
215 | // Add room for the popup arrow |
216 | lbWidth += 2 * lbHeight ; | |
f2d990ad SC |
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); | |
2ac013b1 | 224 | } |