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 { kEventClassSearchField
, kEventSearchFieldSearchClicked
} ,
49 class wxMacSearchFieldControl
: public wxMacUnicodeTextControl
52 wxMacSearchFieldControl( wxTextCtrl
*wxPeer
,
55 const wxSize
& size
, long style
) : wxMacUnicodeTextControl( wxPeer
)
57 Create( wxPeer
, str
, pos
, size
, style
);
60 // search field options
61 virtual void ShowSearchButton( bool show
);
62 virtual bool IsSearchButtonVisible() const;
64 virtual void ShowCancelButton( bool show
);
65 virtual bool IsCancelButtonVisible() const;
67 virtual void SetSearchMenu( wxMenu
* menu
);
68 virtual wxMenu
* GetSearchMenu() const;
70 virtual void CreateControl( wxTextCtrl
* peer
, const Rect
* bounds
, CFStringRef crf
);
76 void wxMacSearchFieldControl::CreateControl( wxTextCtrl
* /*peer*/, const Rect
* bounds
, CFStringRef crf
)
78 OptionBits attributes
= 0;
79 if ( UMAGetSystemVersion() >= 0x1040 )
81 attributes
= kHISearchFieldAttributesSearchIcon
;
83 HIRect hibounds
= { { bounds
->left
, bounds
->top
}, { bounds
->right
-bounds
->left
, bounds
->bottom
-bounds
->top
} };
84 verify_noerr( HISearchFieldCreate(
91 HIViewSetVisible (m_controlRef
, true);
94 // search field options
95 void wxMacSearchFieldControl::ShowSearchButton( bool show
)
97 if ( UMAGetSystemVersion() >= 0x1040 )
100 OptionBits clear
= 0;
103 set
|= kHISearchFieldAttributesSearchIcon
;
107 clear
|= kHISearchFieldAttributesSearchIcon
;
109 HISearchFieldChangeAttributes( m_controlRef
, set
, clear
);
113 bool wxMacSearchFieldControl::IsSearchButtonVisible() const
115 OptionBits attributes
= 0;
116 verify_noerr( HISearchFieldGetAttributes( m_controlRef
, &attributes
) );
117 return ( attributes
& kHISearchFieldAttributesSearchIcon
) != 0;
120 void wxMacSearchFieldControl::ShowCancelButton( bool show
)
123 OptionBits clear
= 0;
126 set
|= kHISearchFieldAttributesCancel
;
130 clear
|= kHISearchFieldAttributesCancel
;
132 HISearchFieldChangeAttributes( m_controlRef
, set
, clear
);
135 bool wxMacSearchFieldControl::IsCancelButtonVisible() const
137 OptionBits attributes
= 0;
138 verify_noerr( HISearchFieldGetAttributes( m_controlRef
, &attributes
) );
139 return ( attributes
& kHISearchFieldAttributesCancel
) != 0;
142 void wxMacSearchFieldControl::SetSearchMenu( wxMenu
* menu
)
147 verify_noerr( HISearchFieldSetSearchMenu( m_controlRef
, MAC_WXHMENU(m_menu
->GetHMenu()) ) );
151 verify_noerr( HISearchFieldSetSearchMenu( m_controlRef
, 0 ) );
155 wxMenu
* wxMacSearchFieldControl::GetSearchMenu() const
162 // ============================================================================
164 // ============================================================================
166 static pascal OSStatus
wxMacSearchControlEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
168 OSStatus result
= eventNotHandledErr
;
170 wxMacCarbonEvent
cEvent( event
) ;
172 ControlRef controlRef
;
173 wxSearchCtrl
* thisWindow
= (wxSearchCtrl
*) data
;
174 cEvent
.GetParameter( kEventParamDirectObject
, &controlRef
) ;
176 switch( GetEventKind( event
) )
178 case kEventSearchFieldCancelClicked
:
179 thisWindow
->MacSearchFieldCancelHit( handler
, event
) ;
181 case kEventSearchFieldSearchClicked
:
182 thisWindow
->MacSearchFieldSearchHit( handler
, event
) ;
189 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacSearchControlEventHandler
)
192 // ----------------------------------------------------------------------------
193 // wxSearchCtrl creation
194 // ----------------------------------------------------------------------------
199 wxSearchCtrl::wxSearchCtrl()
204 wxSearchCtrl::wxSearchCtrl(wxWindow
*parent
, wxWindowID id
,
205 const wxString
& value
,
209 const wxValidator
& validator
,
210 const wxString
& name
)
214 Create(parent
, id
, value
, pos
, size
, style
, validator
, name
);
217 void wxSearchCtrl::Init()
222 bool wxSearchCtrl::Create(wxWindow
*parent
, wxWindowID id
,
223 const wxString
& value
,
227 const wxValidator
& validator
,
228 const wxString
& name
)
230 if ( !wxTextCtrl::Create(parent
, id
, wxEmptyString
, pos
, size
, wxBORDER_NONE
| style
, validator
, name
) )
235 EventHandlerRef searchEventHandler
;
236 InstallControlEventHandler( m_peer
->GetControlRef(), GetwxMacSearchControlEventHandlerUPP(),
237 GetEventTypeCount(eventList
), eventList
, this,
238 (EventHandlerRef
*)&searchEventHandler
);
243 wxSearchCtrl::~wxSearchCtrl()
248 wxSize
wxSearchCtrl::DoGetBestSize() const
250 wxSize size
= wxWindow::DoGetBestSize();
251 // it seems to return a default width of about 16, which is way too small here.
252 if (size
.GetWidth() < 100)
258 void wxSearchCtrl::SetFocus()
260 // NB: We have to implement SetFocus a little differently because kControlFocusNextPart
261 // leads to setting the focus on the search icon rather than the text area.
262 // We get around this by explicitly telling the control to set focus to the
264 if ( !AcceptsFocus() )
267 wxWindow
* former
= FindFocus() ;
268 if ( former
== this )
271 // as we cannot rely on the control features to find out whether we are in full keyboard mode,
272 // we can only leave in case of an error
273 OSStatus err
= m_peer
->SetFocus( kControlEditTextPart
) ;
274 if ( err
== errCouldntSetFocus
)
277 SetUserFocusWindow( (WindowRef
)MacGetTopLevelWindowRef() );
280 // search control specific interfaces
281 // wxSearchCtrl owns menu after this call
282 void wxSearchCtrl::SetMenu( wxMenu
* menu
)
284 if ( menu
== m_menu
)
292 m_menu
->SetInvokingWindow( 0 );
300 m_menu
->SetInvokingWindow( this );
303 GetPeer()->SetSearchMenu( m_menu
);
306 wxMenu
* wxSearchCtrl::GetMenu()
311 void wxSearchCtrl::ShowSearchButton( bool show
)
313 if ( IsSearchButtonVisible() == show
)
318 GetPeer()->ShowSearchButton( show
);
321 bool wxSearchCtrl::IsSearchButtonVisible() const
323 return GetPeer()->IsSearchButtonVisible();
327 void wxSearchCtrl::ShowCancelButton( bool show
)
329 if ( IsCancelButtonVisible() == show
)
334 GetPeer()->ShowCancelButton( show
);
337 bool wxSearchCtrl::IsCancelButtonVisible() const
339 return GetPeer()->IsCancelButtonVisible();
342 wxInt32
wxSearchCtrl::MacSearchFieldSearchHit(WXEVENTHANDLERREF
WXUNUSED(handler
) , WXEVENTREF
WXUNUSED(event
) )
344 wxCommandEvent
event(wxEVT_COMMAND_SEARCHCTRL_SEARCH_BTN
, m_windowId
);
345 event
.SetEventObject(this);
346 ProcessCommand(event
);
347 return eventNotHandledErr
;
350 wxInt32
wxSearchCtrl::MacSearchFieldCancelHit(WXEVENTHANDLERREF
WXUNUSED(handler
) , WXEVENTREF
WXUNUSED(event
) )
352 wxCommandEvent
event(wxEVT_COMMAND_SEARCHCTRL_CANCEL_BTN
, m_windowId
);
353 event
.SetEventObject(this);
354 ProcessCommand(event
);
355 return eventNotHandledErr
;
359 void wxSearchCtrl::CreatePeer(
362 const wxSize
& size
, long style
)
365 #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
366 if ( UMAGetSystemVersion() >= 0x1030 )
368 m_peer
= new wxMacSearchFieldControl( this , str
, pos
, size
, style
);
374 wxTextCtrl::CreatePeer( str
, pos
, size
, style
);
378 #endif // wxUSE_NATIVE_SEARCH_CONTROL
380 #endif // wxUSE_SEARCHCTRL