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 CreateControl( wxTextCtrl
* peer
, const Rect
* bounds
, CFStringRef crf
);
78 void wxMacSearchFieldControl::CreateControl( wxTextCtrl
* /*peer*/, const Rect
* bounds
, CFStringRef crf
)
80 OptionBits attributes
= 0;
81 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
82 if ( UMAGetSystemVersion() >= 0x1040 )
84 attributes
= kHISearchFieldAttributesSearchIcon
;
87 HIRect hibounds
= { { bounds
->left
, bounds
->top
}, { bounds
->right
-bounds
->left
, bounds
->bottom
-bounds
->top
} };
88 verify_noerr( HISearchFieldCreate(
95 HIViewSetVisible (m_controlRef
, true);
98 // search field options
99 void wxMacSearchFieldControl::ShowSearchButton( bool show
)
101 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
102 if ( UMAGetSystemVersion() >= 0x1040 )
105 OptionBits clear
= 0;
108 set
|= kHISearchFieldAttributesSearchIcon
;
112 clear
|= kHISearchFieldAttributesSearchIcon
;
114 HISearchFieldChangeAttributes( m_controlRef
, set
, clear
);
119 bool wxMacSearchFieldControl::IsSearchButtonVisible() const
121 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
122 OptionBits attributes
= 0;
123 verify_noerr( HISearchFieldGetAttributes( m_controlRef
, &attributes
) );
124 return ( attributes
& kHISearchFieldAttributesSearchIcon
) != 0;
130 void wxMacSearchFieldControl::ShowCancelButton( bool show
)
133 OptionBits clear
= 0;
136 set
|= kHISearchFieldAttributesCancel
;
140 clear
|= kHISearchFieldAttributesCancel
;
142 HISearchFieldChangeAttributes( m_controlRef
, set
, clear
);
145 bool wxMacSearchFieldControl::IsCancelButtonVisible() const
147 OptionBits attributes
= 0;
148 verify_noerr( HISearchFieldGetAttributes( m_controlRef
, &attributes
) );
149 return ( attributes
& kHISearchFieldAttributesCancel
) != 0;
152 void wxMacSearchFieldControl::SetSearchMenu( wxMenu
* menu
)
157 verify_noerr( HISearchFieldSetSearchMenu( m_controlRef
, MAC_WXHMENU(m_menu
->GetHMenu()) ) );
161 verify_noerr( HISearchFieldSetSearchMenu( m_controlRef
, 0 ) );
165 wxMenu
* wxMacSearchFieldControl::GetSearchMenu() const
172 // ============================================================================
174 // ============================================================================
176 static pascal OSStatus
wxMacSearchControlEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
178 OSStatus result
= eventNotHandledErr
;
180 wxMacCarbonEvent
cEvent( event
) ;
182 ControlRef controlRef
;
183 wxSearchCtrl
* thisWindow
= (wxSearchCtrl
*) data
;
184 cEvent
.GetParameter( kEventParamDirectObject
, &controlRef
) ;
186 switch( GetEventKind( event
) )
188 case kEventSearchFieldCancelClicked
:
189 thisWindow
->MacSearchFieldCancelHit( handler
, event
) ;
191 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
192 case kEventSearchFieldSearchClicked
:
193 thisWindow
->MacSearchFieldSearchHit( handler
, event
) ;
201 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacSearchControlEventHandler
)
204 // ----------------------------------------------------------------------------
205 // wxSearchCtrl creation
206 // ----------------------------------------------------------------------------
211 wxSearchCtrl::wxSearchCtrl()
216 wxSearchCtrl::wxSearchCtrl(wxWindow
*parent
, wxWindowID id
,
217 const wxString
& value
,
221 const wxValidator
& validator
,
222 const wxString
& name
)
226 Create(parent
, id
, value
, pos
, size
, style
, validator
, name
);
229 void wxSearchCtrl::Init()
234 bool wxSearchCtrl::Create(wxWindow
*parent
, wxWindowID id
,
235 const wxString
& value
,
239 const wxValidator
& validator
,
240 const wxString
& name
)
242 if ( !wxTextCtrl::Create(parent
, id
, wxEmptyString
, pos
, size
, wxBORDER_NONE
| style
, validator
, name
) )
247 EventHandlerRef searchEventHandler
;
248 InstallControlEventHandler( m_peer
->GetControlRef(), GetwxMacSearchControlEventHandlerUPP(),
249 GetEventTypeCount(eventList
), eventList
, this,
250 (EventHandlerRef
*)&searchEventHandler
);
255 wxSearchCtrl::~wxSearchCtrl()
260 wxSize
wxSearchCtrl::DoGetBestSize() const
262 wxSize size
= wxWindow::DoGetBestSize();
263 // it seems to return a default width of about 16, which is way too small here.
264 if (size
.GetWidth() < 100)
270 void wxSearchCtrl::SetFocus()
272 // NB: We have to implement SetFocus a little differently because kControlFocusNextPart
273 // leads to setting the focus on the search icon rather than the text area.
274 // We get around this by explicitly telling the control to set focus to the
276 if ( !AcceptsFocus() )
279 wxWindow
* former
= FindFocus() ;
280 if ( former
== this )
283 // as we cannot rely on the control features to find out whether we are in full keyboard mode,
284 // we can only leave in case of an error
285 OSStatus err
= m_peer
->SetFocus( kControlEditTextPart
) ;
286 if ( err
== errCouldntSetFocus
)
289 SetUserFocusWindow( (WindowRef
)MacGetTopLevelWindowRef() );
292 // search control specific interfaces
293 // wxSearchCtrl owns menu after this call
294 void wxSearchCtrl::SetMenu( wxMenu
* menu
)
296 if ( menu
== m_menu
)
304 m_menu
->SetInvokingWindow( 0 );
312 m_menu
->SetInvokingWindow( this );
315 GetPeer()->SetSearchMenu( m_menu
);
318 wxMenu
* wxSearchCtrl::GetMenu()
323 void wxSearchCtrl::ShowSearchButton( bool show
)
325 if ( IsSearchButtonVisible() == show
)
330 GetPeer()->ShowSearchButton( show
);
333 bool wxSearchCtrl::IsSearchButtonVisible() const
335 return GetPeer()->IsSearchButtonVisible();
339 void wxSearchCtrl::ShowCancelButton( bool show
)
341 if ( IsCancelButtonVisible() == show
)
346 GetPeer()->ShowCancelButton( show
);
349 bool wxSearchCtrl::IsCancelButtonVisible() const
351 return GetPeer()->IsCancelButtonVisible();
354 wxInt32
wxSearchCtrl::MacSearchFieldSearchHit(WXEVENTHANDLERREF
WXUNUSED(handler
) , WXEVENTREF
WXUNUSED(event
) )
356 wxCommandEvent
event(wxEVT_COMMAND_SEARCHCTRL_SEARCH_BTN
, m_windowId
);
357 event
.SetEventObject(this);
358 ProcessCommand(event
);
359 return eventNotHandledErr
;
362 wxInt32
wxSearchCtrl::MacSearchFieldCancelHit(WXEVENTHANDLERREF
WXUNUSED(handler
) , WXEVENTREF
WXUNUSED(event
) )
364 wxCommandEvent
event(wxEVT_COMMAND_SEARCHCTRL_CANCEL_BTN
, m_windowId
);
365 event
.SetEventObject(this);
366 ProcessCommand(event
);
367 return eventNotHandledErr
;
371 void wxSearchCtrl::CreatePeer(
374 const wxSize
& size
, long style
)
377 #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
378 if ( UMAGetSystemVersion() >= 0x1030 )
380 m_peer
= new wxMacSearchFieldControl( this , str
, pos
, size
, style
);
386 wxTextCtrl::CreatePeer( str
, pos
, size
, style
);
390 #endif // wxUSE_NATIVE_SEARCH_CONTROL
392 #endif // wxUSE_SEARCHCTRL