]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/srchctrl.cpp
Remove AdjustedBestSize property since the function it calls is deprecated.
[wxWidgets.git] / src / mac / carbon / srchctrl.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/carbon/srchctrl.cpp
3 // Purpose: implements mac carbon wxSearchCtrl
4 // Author: Vince Harron
5 // Created: 2006-02-19
6 // RCS-ID: $Id$
7 // Copyright: Vince Harron
8 // License: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
10
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
13
14 #ifdef __BORLANDC__
15 #pragma hdrstop
16 #endif
17
18 #if wxUSE_SEARCHCTRL
19
20 #include "wx/srchctrl.h"
21
22 #ifndef WX_PRECOMP
23 #include "wx/menu.h"
24 #endif //WX_PRECOMP
25
26 #if wxUSE_NATIVE_SEARCH_CONTROL
27
28 #include "wx/mac/uma.h"
29 #include "wx/mac/carbon/private/mactext.h"
30
31 BEGIN_EVENT_TABLE(wxSearchCtrl, wxSearchCtrlBase)
32 END_EVENT_TABLE()
33
34 IMPLEMENT_DYNAMIC_CLASS(wxSearchCtrl, wxSearchCtrlBase)
35
36 // ============================================================================
37 // wxMacSearchFieldControl
38 // ============================================================================
39
40 #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
41
42
43 static const EventTypeSpec eventList[] =
44 {
45 { kEventClassSearchField, kEventSearchFieldCancelClicked } ,
46 { kEventClassSearchField, kEventSearchFieldSearchClicked } ,
47 };
48
49 class wxMacSearchFieldControl : public wxMacUnicodeTextControl
50 {
51 public :
52 wxMacSearchFieldControl( wxTextCtrl *wxPeer,
53 const wxString& str,
54 const wxPoint& pos,
55 const wxSize& size, long style ) : wxMacUnicodeTextControl( wxPeer )
56 {
57 Create( wxPeer, str, pos, size, style );
58 }
59
60 // search field options
61 virtual void ShowSearchButton( bool show );
62 virtual bool IsSearchButtonVisible() const;
63
64 virtual void ShowCancelButton( bool show );
65 virtual bool IsCancelButtonVisible() const;
66
67 virtual void SetSearchMenu( wxMenu* menu );
68 virtual wxMenu* GetSearchMenu() const;
69 protected :
70 virtual void CreateControl( wxTextCtrl* peer, const Rect* bounds, CFStringRef crf );
71
72 private:
73 wxMenu* m_menu;
74 } ;
75
76 void wxMacSearchFieldControl::CreateControl( wxTextCtrl* /*peer*/, const Rect* bounds, CFStringRef crf )
77 {
78 OptionBits attributes = 0;
79 if ( UMAGetSystemVersion() >= 0x1040 )
80 {
81 attributes = kHISearchFieldAttributesSearchIcon;
82 }
83 HIRect hibounds = { { bounds->left, bounds->top }, { bounds->right-bounds->left, bounds->bottom-bounds->top } };
84 verify_noerr( HISearchFieldCreate(
85 &hibounds,
86 attributes,
87 0, // MenuRef
88 CFSTR("Search"),
89 &m_controlRef
90 ) );
91 HIViewSetVisible (m_controlRef, true);
92 }
93
94 // search field options
95 void wxMacSearchFieldControl::ShowSearchButton( bool show )
96 {
97 if ( UMAGetSystemVersion() >= 0x1040 )
98 {
99 OptionBits set = 0;
100 OptionBits clear = 0;
101 if ( show )
102 {
103 set |= kHISearchFieldAttributesSearchIcon;
104 }
105 else
106 {
107 clear |= kHISearchFieldAttributesSearchIcon;
108 }
109 HISearchFieldChangeAttributes( m_controlRef, set, clear );
110 }
111 }
112
113 bool wxMacSearchFieldControl::IsSearchButtonVisible() const
114 {
115 OptionBits attributes = 0;
116 verify_noerr( HISearchFieldGetAttributes( m_controlRef, &attributes ) );
117 return ( attributes & kHISearchFieldAttributesSearchIcon ) != 0;
118 }
119
120 void wxMacSearchFieldControl::ShowCancelButton( bool show )
121 {
122 OptionBits set = 0;
123 OptionBits clear = 0;
124 if ( show )
125 {
126 set |= kHISearchFieldAttributesCancel;
127 }
128 else
129 {
130 clear |= kHISearchFieldAttributesCancel;
131 }
132 HISearchFieldChangeAttributes( m_controlRef, set, clear );
133 }
134
135 bool wxMacSearchFieldControl::IsCancelButtonVisible() const
136 {
137 OptionBits attributes = 0;
138 verify_noerr( HISearchFieldGetAttributes( m_controlRef, &attributes ) );
139 return ( attributes & kHISearchFieldAttributesCancel ) != 0;
140 }
141
142 void wxMacSearchFieldControl::SetSearchMenu( wxMenu* menu )
143 {
144 m_menu = menu;
145 if ( m_menu )
146 {
147 verify_noerr( HISearchFieldSetSearchMenu( m_controlRef, MAC_WXHMENU(m_menu->GetHMenu()) ) );
148 }
149 else
150 {
151 verify_noerr( HISearchFieldSetSearchMenu( m_controlRef, 0 ) );
152 }
153 }
154
155 wxMenu* wxMacSearchFieldControl::GetSearchMenu() const
156 {
157 return m_menu;
158 }
159
160 #endif
161
162 // ============================================================================
163 // implementation
164 // ============================================================================
165
166 static pascal OSStatus wxMacSearchControlEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
167 {
168 OSStatus result = eventNotHandledErr ;
169
170 wxMacCarbonEvent cEvent( event ) ;
171
172 ControlRef controlRef ;
173 wxSearchCtrl* thisWindow = (wxSearchCtrl*) data ;
174 cEvent.GetParameter( kEventParamDirectObject , &controlRef ) ;
175
176 switch( GetEventKind( event ) )
177 {
178 case kEventSearchFieldCancelClicked :
179 thisWindow->MacSearchFieldCancelHit( handler , event ) ;
180 break ;
181 case kEventSearchFieldSearchClicked :
182 thisWindow->MacSearchFieldSearchHit( handler , event ) ;
183 break ;
184 }
185
186 return result ;
187 }
188
189 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacSearchControlEventHandler )
190
191
192 // ----------------------------------------------------------------------------
193 // wxSearchCtrl creation
194 // ----------------------------------------------------------------------------
195
196 // creation
197 // --------
198
199 wxSearchCtrl::wxSearchCtrl()
200 {
201 Init();
202 }
203
204 wxSearchCtrl::wxSearchCtrl(wxWindow *parent, wxWindowID id,
205 const wxString& value,
206 const wxPoint& pos,
207 const wxSize& size,
208 long style,
209 const wxValidator& validator,
210 const wxString& name)
211 {
212 Init();
213
214 Create(parent, id, value, pos, size, style, validator, name);
215 }
216
217 void wxSearchCtrl::Init()
218 {
219 m_menu = 0;
220 }
221
222 bool wxSearchCtrl::Create(wxWindow *parent, wxWindowID id,
223 const wxString& value,
224 const wxPoint& pos,
225 const wxSize& size,
226 long style,
227 const wxValidator& validator,
228 const wxString& name)
229 {
230 if ( !wxTextCtrl::Create(parent, id, wxEmptyString, pos, size, wxBORDER_NONE | style, validator, name) )
231 {
232 return false;
233 }
234
235 EventHandlerRef searchEventHandler;
236 InstallControlEventHandler( m_peer->GetControlRef(), GetwxMacSearchControlEventHandlerUPP(),
237 GetEventTypeCount(eventList), eventList, this,
238 (EventHandlerRef *)&searchEventHandler);
239
240 return true;
241 }
242
243 wxSearchCtrl::~wxSearchCtrl()
244 {
245 delete m_menu;
246 }
247
248 wxSize wxSearchCtrl::DoGetBestSize() const
249 {
250 wxSize size = wxWindow::DoGetBestSize();
251 // it seems to return a default width of about 16, which is way too small here.
252 if (size.GetWidth() < 100)
253 size.SetWidth(100);
254
255 return size;
256 }
257
258 void wxSearchCtrl::SetFocus()
259 {
260 // NB: We have to implement SetFocus a little differently because kControlFocusNextPart
261 // leads to setting the focus on the search icon rather than the text area.
262 // We get around this by explicitly telling the control to set focus to the
263 // text area.
264 if ( !AcceptsFocus() )
265 return ;
266
267 wxWindow* former = FindFocus() ;
268 if ( former == this )
269 return ;
270
271 // as we cannot rely on the control features to find out whether we are in full keyboard mode,
272 // we can only leave in case of an error
273 OSStatus err = m_peer->SetFocus( kControlEditTextPart ) ;
274 if ( err == errCouldntSetFocus )
275 return ;
276
277 SetUserFocusWindow( (WindowRef)MacGetTopLevelWindowRef() );
278 }
279
280 // search control specific interfaces
281 // wxSearchCtrl owns menu after this call
282 void wxSearchCtrl::SetMenu( wxMenu* menu )
283 {
284 if ( menu == m_menu )
285 {
286 // no change
287 return;
288 }
289
290 if ( m_menu )
291 {
292 m_menu->SetInvokingWindow( 0 );
293 }
294
295 delete m_menu;
296 m_menu = menu;
297
298 if ( m_menu )
299 {
300 m_menu->SetInvokingWindow( this );
301 }
302
303 GetPeer()->SetSearchMenu( m_menu );
304 }
305
306 wxMenu* wxSearchCtrl::GetMenu()
307 {
308 return m_menu;
309 }
310
311 void wxSearchCtrl::ShowSearchButton( bool show )
312 {
313 if ( IsSearchButtonVisible() == show )
314 {
315 // no change
316 return;
317 }
318 GetPeer()->ShowSearchButton( show );
319 }
320
321 bool wxSearchCtrl::IsSearchButtonVisible() const
322 {
323 return GetPeer()->IsSearchButtonVisible();
324 }
325
326
327 void wxSearchCtrl::ShowCancelButton( bool show )
328 {
329 if ( IsCancelButtonVisible() == show )
330 {
331 // no change
332 return;
333 }
334 GetPeer()->ShowCancelButton( show );
335 }
336
337 bool wxSearchCtrl::IsCancelButtonVisible() const
338 {
339 return GetPeer()->IsCancelButtonVisible();
340 }
341
342 wxInt32 wxSearchCtrl::MacSearchFieldSearchHit(WXEVENTHANDLERREF WXUNUSED(handler) , WXEVENTREF WXUNUSED(event) )
343 {
344 wxCommandEvent event(wxEVT_COMMAND_SEARCHCTRL_SEARCH, m_windowId );
345 event.SetEventObject(this);
346 ProcessCommand(event);
347 return eventNotHandledErr ;
348 }
349
350 wxInt32 wxSearchCtrl::MacSearchFieldCancelHit(WXEVENTHANDLERREF WXUNUSED(handler) , WXEVENTREF WXUNUSED(event) )
351 {
352 wxCommandEvent event(wxEVT_COMMAND_SEARCHCTRL_CANCEL, m_windowId );
353 event.SetEventObject(this);
354 ProcessCommand(event);
355 return eventNotHandledErr ;
356 }
357
358
359 void wxSearchCtrl::CreatePeer(
360 const wxString& str,
361 const wxPoint& pos,
362 const wxSize& size, long style )
363 {
364 #ifdef __WXMAC_OSX__
365 #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
366 if ( UMAGetSystemVersion() >= 0x1030 )
367 {
368 m_peer = new wxMacSearchFieldControl( this , str , pos , size , style );
369 }
370 #endif
371 #endif
372 if ( !m_peer )
373 {
374 wxTextCtrl::CreatePeer( str, pos, size, style );
375 }
376 }
377
378 #endif // wxUSE_NATIVE_SEARCH_CONTROL
379
380 #endif // wxUSE_SEARCHCTRL