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