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
* /*peer*/, const Rect
* bounds
, CFStringRef crf
)
84 OptionBits attributes
= 0;
85 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
86 if ( UMAGetSystemVersion() >= 0x1040 )
88 attributes
= kHISearchFieldAttributesSearchIcon
;
91 HIRect hibounds
= { { bounds
->left
, bounds
->top
}, { bounds
->right
-bounds
->left
, bounds
->bottom
-bounds
->top
} };
92 verify_noerr( HISearchFieldCreate(
99 HIViewSetVisible (m_controlRef
, true);
102 // search field options
103 void wxMacSearchFieldControl::ShowSearchButton( bool show
)
105 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
106 if ( UMAGetSystemVersion() >= 0x1040 )
109 OptionBits clear
= 0;
112 set
|= kHISearchFieldAttributesSearchIcon
;
116 clear
|= kHISearchFieldAttributesSearchIcon
;
118 HISearchFieldChangeAttributes( m_controlRef
, set
, clear
);
123 bool wxMacSearchFieldControl::IsSearchButtonVisible() const
125 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
126 OptionBits attributes
= 0;
127 verify_noerr( HISearchFieldGetAttributes( m_controlRef
, &attributes
) );
128 return ( attributes
& kHISearchFieldAttributesSearchIcon
) != 0;
134 void wxMacSearchFieldControl::ShowCancelButton( bool show
)
137 OptionBits clear
= 0;
140 set
|= kHISearchFieldAttributesCancel
;
144 clear
|= kHISearchFieldAttributesCancel
;
146 HISearchFieldChangeAttributes( m_controlRef
, set
, clear
);
149 bool wxMacSearchFieldControl::IsCancelButtonVisible() const
151 OptionBits attributes
= 0;
152 verify_noerr( HISearchFieldGetAttributes( m_controlRef
, &attributes
) );
153 return ( attributes
& kHISearchFieldAttributesCancel
) != 0;
156 void wxMacSearchFieldControl::SetSearchMenu( wxMenu
* menu
)
161 verify_noerr( HISearchFieldSetSearchMenu( m_controlRef
, MAC_WXHMENU(m_menu
->GetHMenu()) ) );
165 verify_noerr( HISearchFieldSetSearchMenu( m_controlRef
, 0 ) );
169 wxMenu
* wxMacSearchFieldControl::GetSearchMenu() const
175 void wxMacSearchFieldControl::SetDescriptiveText(const wxString
& text
)
177 verify_noerr( HISearchFieldSetDescriptiveText(
179 wxMacCFStringHolder( text
, wxFont::GetDefaultEncoding() )));
182 wxString
wxMacSearchFieldControl::GetDescriptiveText() const
185 verify_noerr( HISearchFieldCopyDescriptiveText( m_controlRef
, &cfStr
));
188 return wxMacCFStringHolder(cfStr
).AsString();
192 return wxEmptyString
;
198 // ============================================================================
200 // ============================================================================
202 static pascal OSStatus
wxMacSearchControlEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
204 OSStatus result
= eventNotHandledErr
;
206 wxMacCarbonEvent
cEvent( event
) ;
208 ControlRef controlRef
;
209 wxSearchCtrl
* thisWindow
= (wxSearchCtrl
*) data
;
210 cEvent
.GetParameter( kEventParamDirectObject
, &controlRef
) ;
212 switch( GetEventKind( event
) )
214 case kEventSearchFieldCancelClicked
:
215 thisWindow
->MacSearchFieldCancelHit( handler
, event
) ;
217 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
218 case kEventSearchFieldSearchClicked
:
219 thisWindow
->MacSearchFieldSearchHit( handler
, event
) ;
227 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacSearchControlEventHandler
)
230 // ----------------------------------------------------------------------------
231 // wxSearchCtrl creation
232 // ----------------------------------------------------------------------------
237 wxSearchCtrl::wxSearchCtrl()
242 wxSearchCtrl::wxSearchCtrl(wxWindow
*parent
, wxWindowID id
,
243 const wxString
& value
,
247 const wxValidator
& validator
,
248 const wxString
& name
)
252 Create(parent
, id
, value
, pos
, size
, style
, validator
, name
);
255 void wxSearchCtrl::Init()
260 bool wxSearchCtrl::Create(wxWindow
*parent
, wxWindowID id
,
261 const wxString
& value
,
265 const wxValidator
& validator
,
266 const wxString
& name
)
268 if ( !wxTextCtrl::Create(parent
, id
, wxEmptyString
, pos
, size
, wxBORDER_NONE
| style
, validator
, name
) )
273 EventHandlerRef searchEventHandler
;
274 InstallControlEventHandler( m_peer
->GetControlRef(), GetwxMacSearchControlEventHandlerUPP(),
275 GetEventTypeCount(eventList
), eventList
, this,
276 (EventHandlerRef
*)&searchEventHandler
);
281 wxSearchCtrl::~wxSearchCtrl()
286 wxSize
wxSearchCtrl::DoGetBestSize() const
288 wxSize size
= wxWindow::DoGetBestSize();
289 // it seems to return a default width of about 16, which is way too small here.
290 if (size
.GetWidth() < 100)
296 void wxSearchCtrl::SetFocus()
298 // NB: We have to implement SetFocus a little differently because kControlFocusNextPart
299 // leads to setting the focus on the search icon rather than the text area.
300 // We get around this by explicitly telling the control to set focus to the
302 if ( !AcceptsFocus() )
305 wxWindow
* former
= FindFocus() ;
306 if ( former
== this )
309 // as we cannot rely on the control features to find out whether we are in full keyboard mode,
310 // we can only leave in case of an error
311 OSStatus err
= m_peer
->SetFocus( kControlEditTextPart
) ;
312 if ( err
== errCouldntSetFocus
)
315 SetUserFocusWindow( (WindowRef
)MacGetTopLevelWindowRef() );
318 // search control specific interfaces
319 // wxSearchCtrl owns menu after this call
320 void wxSearchCtrl::SetMenu( wxMenu
* menu
)
322 if ( menu
== m_menu
)
330 m_menu
->SetInvokingWindow( 0 );
338 m_menu
->SetInvokingWindow( this );
341 GetPeer()->SetSearchMenu( m_menu
);
344 wxMenu
* wxSearchCtrl::GetMenu()
349 void wxSearchCtrl::ShowSearchButton( bool show
)
351 if ( IsSearchButtonVisible() == show
)
356 GetPeer()->ShowSearchButton( show
);
359 bool wxSearchCtrl::IsSearchButtonVisible() const
361 return GetPeer()->IsSearchButtonVisible();
365 void wxSearchCtrl::ShowCancelButton( bool show
)
367 if ( IsCancelButtonVisible() == show
)
372 GetPeer()->ShowCancelButton( show
);
375 bool wxSearchCtrl::IsCancelButtonVisible() const
377 return GetPeer()->IsCancelButtonVisible();
380 void wxSearchCtrl::SetDescriptiveText(const wxString
& text
)
382 GetPeer()->SetDescriptiveText(text
);
385 wxString
wxSearchCtrl::GetDescriptiveText() const
387 return GetPeer()->GetDescriptiveText();
390 wxInt32
wxSearchCtrl::MacSearchFieldSearchHit(WXEVENTHANDLERREF
WXUNUSED(handler
) , WXEVENTREF
WXUNUSED(event
) )
392 wxCommandEvent
event(wxEVT_COMMAND_SEARCHCTRL_SEARCH_BTN
, m_windowId
);
393 event
.SetEventObject(this);
394 ProcessCommand(event
);
395 return eventNotHandledErr
;
398 wxInt32
wxSearchCtrl::MacSearchFieldCancelHit(WXEVENTHANDLERREF
WXUNUSED(handler
) , WXEVENTREF
WXUNUSED(event
) )
400 wxCommandEvent
event(wxEVT_COMMAND_SEARCHCTRL_CANCEL_BTN
, m_windowId
);
401 event
.SetEventObject(this);
402 ProcessCommand(event
);
403 return eventNotHandledErr
;
407 void wxSearchCtrl::CreatePeer(
410 const wxSize
& size
, long style
)
413 #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
414 if ( UMAGetSystemVersion() >= 0x1030 )
416 m_peer
= new wxMacSearchFieldControl( this , str
, pos
, size
, style
);
422 wxTextCtrl::CreatePeer( str
, pos
, size
, style
);
426 #endif // wxUSE_NATIVE_SEARCH_CONTROL
428 #endif // wxUSE_SEARCHCTRL