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