1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/carbon/srchctrl.cpp
3 // Purpose: implements mac carbon wxSearchCtrl
4 // Author: Vince Harron
8 // Copyright: Vince Harron
9 // License: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
33 #include "wx/srchctrl.h"
35 #if USE_NATIVE_SEARCH_CONTROL
37 #include "wx/mac/uma.h"
38 #include "wx/mac/carbon/private/mactext.h"
40 BEGIN_EVENT_TABLE(wxSearchCtrl
, wxSearchCtrlBase
)
43 IMPLEMENT_DYNAMIC_CLASS(wxSearchCtrl
, wxSearchCtrlBase
)
45 // ============================================================================
46 // wxMacSearchFieldControl
47 // ============================================================================
49 #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
52 static const EventTypeSpec eventList
[] =
54 { kEventClassSearchField
, kEventSearchFieldCancelClicked
} ,
55 { kEventClassSearchField
, kEventSearchFieldSearchClicked
} ,
58 class wxMacSearchFieldControl
: public wxMacUnicodeTextControl
61 wxMacSearchFieldControl( wxTextCtrl
*wxPeer
,
64 const wxSize
& size
, long style
) : wxMacUnicodeTextControl( wxPeer
)
66 Create( wxPeer
, str
, pos
, size
, style
);
69 // search field options
70 virtual void ShowSearchButton( bool show
);
71 virtual bool IsSearchButtonVisible() const;
73 virtual void ShowCancelButton( bool show
);
74 virtual bool IsCancelButtonVisible() const;
76 virtual void SetSearchMenu( wxMenu
* menu
);
77 virtual wxMenu
* GetSearchMenu() const;
79 virtual void CreateControl( wxTextCtrl
* peer
, const Rect
* bounds
, CFStringRef crf
);
85 void wxMacSearchFieldControl::CreateControl( wxTextCtrl
* /*peer*/, const Rect
* bounds
, CFStringRef crf
)
87 OptionBits attributes
= 0;
88 if ( UMAGetSystemVersion() >= 0x1040 )
90 attributes
= kHISearchFieldAttributesSearchIcon
;
92 HIRect hibounds
= { { bounds
->left
, bounds
->top
}, { bounds
->right
-bounds
->left
, bounds
->bottom
-bounds
->top
} };
93 verify_noerr( HISearchFieldCreate(
100 HIViewSetVisible (m_controlRef
, true);
103 // search field options
104 void wxMacSearchFieldControl::SetSearchButtonVisible( bool show
)
106 if ( UMAGetSystemVersion() >= 0x1040 )
109 OptionBits clear
= 0;
112 set
|= kHISearchFieldAttributesSearchIcon
;
116 clear
|= kHISearchFieldAttributesSearchIcon
;
118 HISearchFieldChangeAttributes( m_controlRef
, set
, clear
);
122 bool wxMacSearchFieldControl::GetSearchButtonVisible() const
124 OptionBits attributes
= 0;
125 verify_noerr( HISearchFieldGetAttributes( m_controlRef
, &attributes
) );
126 return ( attributes
& kHISearchFieldAttributesSearchIcon
) != 0;
129 void wxMacSearchFieldControl::SetCancelButtonVisible( bool show
)
132 OptionBits clear
= 0;
135 set
|= kHISearchFieldAttributesCancel
;
139 clear
|= kHISearchFieldAttributesCancel
;
141 HISearchFieldChangeAttributes( m_controlRef
, set
, clear
);
144 bool wxMacSearchFieldControl::GetCancelButtonVisible() const
146 OptionBits attributes
= 0;
147 verify_noerr( HISearchFieldGetAttributes( m_controlRef
, &attributes
) );
148 return ( attributes
& kHISearchFieldAttributesCancel
) != 0;
151 void wxMacSearchFieldControl::SetSearchMenu( wxMenu
* menu
)
156 verify_noerr( HISearchFieldSetSearchMenu( m_controlRef
, MAC_WXHMENU(m_menu
->GetHMenu()) ) );
160 verify_noerr( HISearchFieldSetSearchMenu( m_controlRef
, 0 ) );
164 wxMenu
* wxMacSearchFieldControl::GetSearchMenu() const
171 // ============================================================================
173 // ============================================================================
175 static pascal OSStatus
wxMacSearchControlEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
177 OSStatus result
= eventNotHandledErr
;
179 wxMacCarbonEvent
cEvent( event
) ;
181 ControlRef controlRef
;
182 wxSearchCtrl
* thisWindow
= (wxSearchCtrl
*) data
;
183 wxTextCtrl
* textCtrl
= wxDynamicCast( thisWindow
, wxTextCtrl
) ;
184 cEvent
.GetParameter( kEventParamDirectObject
, &controlRef
) ;
186 switch( GetEventKind( event
) )
188 case kEventSearchFieldCancelClicked
:
189 thisWindow
->MacSearchFieldCancelHit( handler
, event
) ;
191 case kEventSearchFieldSearchClicked
:
192 thisWindow
->MacSearchFieldSearchHit( handler
, event
) ;
199 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacSearchControlEventHandler
)
202 // ----------------------------------------------------------------------------
203 // wxSearchCtrl creation
204 // ----------------------------------------------------------------------------
209 wxSearchCtrl::wxSearchCtrl()
214 wxSearchCtrl::wxSearchCtrl(wxWindow
*parent
, wxWindowID id
,
215 const wxString
& value
,
219 const wxValidator
& validator
,
220 const wxString
& name
)
224 Create(parent
, id
, value
, pos
, size
, style
, validator
, name
);
227 void wxSearchCtrl::Init()
232 bool wxSearchCtrl::Create(wxWindow
*parent
, wxWindowID id
,
233 const wxString
& value
,
237 const wxValidator
& validator
,
238 const wxString
& name
)
240 if ( !wxTextCtrl::Create(parent
, id
, wxEmptyString
, pos
, size
, wxBORDER_NONE
| style
, validator
, name
) )
245 EventHandlerRef searchEventHandler
;
246 InstallControlEventHandler( m_peer
->GetControlRef(), GetwxMacSearchControlEventHandlerUPP(),
247 GetEventTypeCount(eventList
), eventList
, this,
248 (EventHandlerRef
*)&searchEventHandler
);
253 wxSearchCtrl::~wxSearchCtrl()
258 wxSize
wxSearchCtrl::DoGetBestSize() const
260 wxSize size
= wxWindow::DoGetBestSize();
261 // it seems to return a default width of about 16, which is way too small here.
262 if (size
.GetWidth() < 100)
268 void wxSearchCtrl::SetFocus()
270 // NB: We have to implement SetFocus a little differently because kControlFocusNextPart
271 // leads to setting the focus on the search icon rather than the text area.
272 // We get around this by explicitly telling the control to set focus to the
274 if ( !AcceptsFocus() )
277 wxWindow
* former
= FindFocus() ;
278 if ( former
== this )
281 // as we cannot rely on the control features to find out whether we are in full keyboard mode,
282 // we can only leave in case of an error
283 OSStatus err
= m_peer
->SetFocus( kControlEditTextPart
) ;
284 if ( err
== errCouldntSetFocus
)
287 SetUserFocusWindow( (WindowRef
)MacGetTopLevelWindowRef() );
290 // search control specific interfaces
291 // wxSearchCtrl owns menu after this call
292 void wxSearchCtrl::SetMenu( wxMenu
* menu
)
294 if ( menu
== m_menu
)
302 m_menu
->SetInvokingWindow( 0 );
310 m_menu
->SetInvokingWindow( this );
313 GetPeer()->SetSearchMenu( m_menu
);
316 wxMenu
* wxSearchCtrl::GetMenu()
321 void wxSearchCtrl::SetSearchButtonVisible( bool show
)
323 if ( GetSearchButtonVisible() == show
)
328 GetPeer()->SetSearchButtonVisible( show
);
331 bool wxSearchCtrl::GetSearchButtonVisible() const
333 return GetPeer()->GetSearchButtonVisible();
337 void wxSearchCtrl::SetCancelButtonVisible( bool show
)
339 if ( GetCancelButtonVisible() == show
)
344 GetPeer()->SetCancelButtonVisible( show
);
347 bool wxSearchCtrl::GetCancelButtonVisible() const
349 return GetPeer()->GetCancelButtonVisible();
352 wxInt32
wxSearchCtrl::MacSearchFieldSearchHit(WXEVENTHANDLERREF
WXUNUSED(handler
) , WXEVENTREF
WXUNUSED(event
) )
354 wxCommandEvent
event(wxEVT_COMMAND_SEARCHCTRL_SEARCH
, m_windowId
);
355 event
.SetEventObject(this);
356 ProcessCommand(event
);
357 return eventNotHandledErr
;
360 wxInt32
wxSearchCtrl::MacSearchFieldCancelHit(WXEVENTHANDLERREF
WXUNUSED(handler
) , WXEVENTREF
WXUNUSED(event
) )
362 wxCommandEvent
event(wxEVT_COMMAND_SEARCHCTRL_CANCEL
, m_windowId
);
363 event
.SetEventObject(this);
364 ProcessCommand(event
);
365 return eventNotHandledErr
;
369 void wxSearchCtrl::CreatePeer(
372 const wxSize
& size
, long style
)
375 #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
376 if ( UMAGetSystemVersion() >= 0x1030 )
378 m_peer
= new wxMacSearchFieldControl( this , str
, pos
, size
, style
);
384 wxTextCtrl::CreatePeer( str
, pos
, size
, style
);
388 #endif // USE_NATIVE_SEARCH_CONTROL
390 #endif // wxUSE_SEARCHCTRL