1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/carbon/srchctrl.cpp
3 // Purpose: implements mac carbon wxSearchCtrl
4 // Author: Vince Harron
7 // Copyright: Vince Harron
8 // License: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
20 #include "wx/srchctrl.h"
26 #if wxUSE_NATIVE_SEARCH_CONTROL
28 #include "wx/mac/uma.h"
29 #include "wx/mac/carbon/private/mactext.h"
31 BEGIN_EVENT_TABLE(wxSearchCtrl
, wxSearchCtrlBase
)
34 IMPLEMENT_DYNAMIC_CLASS(wxSearchCtrl
, wxSearchCtrlBase
)
36 // ============================================================================
37 // wxMacSearchFieldControl
38 // ============================================================================
40 #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
43 static const EventTypeSpec eventList
[] =
45 { kEventClassSearchField
, kEventSearchFieldCancelClicked
} ,
46 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
47 { kEventClassSearchField
, kEventSearchFieldSearchClicked
} ,
51 class wxMacSearchFieldControl
: public wxMacUnicodeTextControl
54 wxMacSearchFieldControl( wxTextCtrl
*wxPeer
,
57 const wxSize
& size
, long style
) : wxMacUnicodeTextControl( wxPeer
)
59 Create( wxPeer
, str
, pos
, size
, style
);
62 // search field options
63 virtual void ShowSearchButton( bool show
);
64 virtual bool IsSearchButtonVisible() const;
66 virtual void ShowCancelButton( bool show
);
67 virtual bool IsCancelButtonVisible() const;
69 virtual void SetSearchMenu( wxMenu
* menu
);
70 virtual wxMenu
* GetSearchMenu() const;
72 virtual void SetDescriptiveText(const wxString
& text
);
73 virtual wxString
GetDescriptiveText() const;
76 virtual void CreateControl( wxTextCtrl
* peer
, const Rect
* bounds
, CFStringRef crf
);
82 void wxMacSearchFieldControl::CreateControl(wxTextCtrl
* WXUNUSED(peer
),
84 CFStringRef
WXUNUSED(crf
))
86 OptionBits attributes
= 0;
87 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
88 if ( UMAGetSystemVersion() >= 0x1040 )
90 attributes
= kHISearchFieldAttributesSearchIcon
;
93 HIRect hibounds
= { { bounds
->left
, bounds
->top
}, { bounds
->right
-bounds
->left
, bounds
->bottom
-bounds
->top
} };
94 verify_noerr( HISearchFieldCreate(
101 HIViewSetVisible (m_controlRef
, true);
104 // search field options
105 void wxMacSearchFieldControl::ShowSearchButton( bool show
)
107 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
108 if ( UMAGetSystemVersion() >= 0x1040 )
111 OptionBits clear
= 0;
114 set
|= kHISearchFieldAttributesSearchIcon
;
118 clear
|= kHISearchFieldAttributesSearchIcon
;
120 HISearchFieldChangeAttributes( m_controlRef
, set
, clear
);
125 bool wxMacSearchFieldControl::IsSearchButtonVisible() const
127 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
128 OptionBits attributes
= 0;
129 verify_noerr( HISearchFieldGetAttributes( m_controlRef
, &attributes
) );
130 return ( attributes
& kHISearchFieldAttributesSearchIcon
) != 0;
136 void wxMacSearchFieldControl::ShowCancelButton( bool show
)
139 OptionBits clear
= 0;
142 set
|= kHISearchFieldAttributesCancel
;
146 clear
|= kHISearchFieldAttributesCancel
;
148 HISearchFieldChangeAttributes( m_controlRef
, set
, clear
);
151 bool wxMacSearchFieldControl::IsCancelButtonVisible() const
153 OptionBits attributes
= 0;
154 verify_noerr( HISearchFieldGetAttributes( m_controlRef
, &attributes
) );
155 return ( attributes
& kHISearchFieldAttributesCancel
) != 0;
158 void wxMacSearchFieldControl::SetSearchMenu( wxMenu
* menu
)
163 verify_noerr( HISearchFieldSetSearchMenu( m_controlRef
, MAC_WXHMENU(m_menu
->GetHMenu()) ) );
167 verify_noerr( HISearchFieldSetSearchMenu( m_controlRef
, 0 ) );
171 wxMenu
* wxMacSearchFieldControl::GetSearchMenu() const
177 void wxMacSearchFieldControl::SetDescriptiveText(const wxString
& text
)
179 verify_noerr( HISearchFieldSetDescriptiveText(
181 wxMacCFStringHolder( text
, wxFont::GetDefaultEncoding() )));
184 wxString
wxMacSearchFieldControl::GetDescriptiveText() const
187 verify_noerr( HISearchFieldCopyDescriptiveText( m_controlRef
, &cfStr
));
190 return wxMacCFStringHolder(cfStr
).AsString();
194 return wxEmptyString
;
200 // ============================================================================
202 // ============================================================================
204 static pascal OSStatus
wxMacSearchControlEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
206 OSStatus result
= eventNotHandledErr
;
208 wxMacCarbonEvent
cEvent( event
) ;
210 ControlRef controlRef
;
211 wxSearchCtrl
* thisWindow
= (wxSearchCtrl
*) data
;
212 cEvent
.GetParameter( kEventParamDirectObject
, &controlRef
) ;
214 switch( GetEventKind( event
) )
216 case kEventSearchFieldCancelClicked
:
217 thisWindow
->MacSearchFieldCancelHit( handler
, event
) ;
219 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
220 case kEventSearchFieldSearchClicked
:
221 thisWindow
->MacSearchFieldSearchHit( handler
, event
) ;
229 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacSearchControlEventHandler
)
232 // ----------------------------------------------------------------------------
233 // wxSearchCtrl creation
234 // ----------------------------------------------------------------------------
239 wxSearchCtrl::wxSearchCtrl()
244 wxSearchCtrl::wxSearchCtrl(wxWindow
*parent
, wxWindowID id
,
245 const wxString
& value
,
249 const wxValidator
& validator
,
250 const wxString
& name
)
254 Create(parent
, id
, value
, pos
, size
, style
, validator
, name
);
257 void wxSearchCtrl::Init()
262 bool wxSearchCtrl::Create(wxWindow
*parent
, wxWindowID id
,
263 const wxString
& value
,
267 const wxValidator
& validator
,
268 const wxString
& name
)
270 if ( !wxTextCtrl::Create(parent
, id
, wxEmptyString
, pos
, size
, wxBORDER_NONE
| style
, validator
, name
) )
275 EventHandlerRef searchEventHandler
;
276 InstallControlEventHandler( m_peer
->GetControlRef(), GetwxMacSearchControlEventHandlerUPP(),
277 GetEventTypeCount(eventList
), eventList
, this,
278 (EventHandlerRef
*)&searchEventHandler
);
285 wxSearchCtrl::~wxSearchCtrl()
290 wxSize
wxSearchCtrl::DoGetBestSize() const
292 wxSize size
= wxWindow::DoGetBestSize();
293 // it seems to return a default width of about 16, which is way too small here.
294 if (size
.GetWidth() < 100)
300 void wxSearchCtrl::SetFocus()
302 // NB: We have to implement SetFocus a little differently because kControlFocusNextPart
303 // leads to setting the focus on the search icon rather than the text area.
304 // We get around this by explicitly telling the control to set focus to the
306 if ( !AcceptsFocus() )
309 wxWindow
* former
= FindFocus() ;
310 if ( former
== this )
313 // as we cannot rely on the control features to find out whether we are in full keyboard mode,
314 // we can only leave in case of an error
315 OSStatus err
= m_peer
->SetFocus( kControlEditTextPart
) ;
316 if ( err
== errCouldntSetFocus
)
319 SetUserFocusWindow( (WindowRef
)MacGetTopLevelWindowRef() );
322 // search control specific interfaces
323 // wxSearchCtrl owns menu after this call
324 void wxSearchCtrl::SetMenu( wxMenu
* menu
)
326 if ( menu
== m_menu
)
334 m_menu
->SetInvokingWindow( 0 );
342 m_menu
->SetInvokingWindow( this );
345 GetPeer()->SetSearchMenu( m_menu
);
348 wxMenu
* wxSearchCtrl::GetMenu()
353 void wxSearchCtrl::ShowSearchButton( bool show
)
355 if ( IsSearchButtonVisible() == show
)
360 GetPeer()->ShowSearchButton( show
);
363 bool wxSearchCtrl::IsSearchButtonVisible() const
365 return GetPeer()->IsSearchButtonVisible();
369 void wxSearchCtrl::ShowCancelButton( bool show
)
371 if ( IsCancelButtonVisible() == show
)
376 GetPeer()->ShowCancelButton( show
);
379 bool wxSearchCtrl::IsCancelButtonVisible() const
381 return GetPeer()->IsCancelButtonVisible();
384 void wxSearchCtrl::SetDescriptiveText(const wxString
& text
)
386 GetPeer()->SetDescriptiveText(text
);
389 wxString
wxSearchCtrl::GetDescriptiveText() const
391 return GetPeer()->GetDescriptiveText();
394 wxInt32
wxSearchCtrl::MacSearchFieldSearchHit(WXEVENTHANDLERREF
WXUNUSED(handler
) , WXEVENTREF
WXUNUSED(event
) )
396 wxCommandEvent
event(wxEVT_COMMAND_SEARCHCTRL_SEARCH_BTN
, m_windowId
);
397 event
.SetEventObject(this);
398 ProcessCommand(event
);
399 return eventNotHandledErr
;
402 wxInt32
wxSearchCtrl::MacSearchFieldCancelHit(WXEVENTHANDLERREF
WXUNUSED(handler
) , WXEVENTREF
WXUNUSED(event
) )
404 wxCommandEvent
event(wxEVT_COMMAND_SEARCHCTRL_CANCEL_BTN
, m_windowId
);
405 event
.SetEventObject(this);
406 ProcessCommand(event
);
407 return eventNotHandledErr
;
411 void wxSearchCtrl::CreatePeer(
414 const wxSize
& size
, long style
)
417 #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
418 if ( UMAGetSystemVersion() >= 0x1030 )
420 m_peer
= new wxMacSearchFieldControl( this , str
, pos
, size
, style
);
426 wxTextCtrl::CreatePeer( str
, pos
, size
, style
);
430 #endif // wxUSE_NATIVE_SEARCH_CONTROL
432 #endif // wxUSE_SEARCHCTRL