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