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 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
32 #include "wx/srchctrl.h"
34 #if wxUSE_NATIVE_SEARCH_CONTROL
36 #include "wx/mac/uma.h"
37 #include "wx/mac/carbon/private/mactext.h"
39 BEGIN_EVENT_TABLE(wxSearchCtrl
, wxSearchCtrlBase
)
42 IMPLEMENT_DYNAMIC_CLASS(wxSearchCtrl
, wxSearchCtrlBase
)
44 // ============================================================================
45 // wxMacSearchFieldControl
46 // ============================================================================
48 #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
51 static const EventTypeSpec eventList
[] =
53 { kEventClassSearchField
, kEventSearchFieldCancelClicked
} ,
54 { kEventClassSearchField
, kEventSearchFieldSearchClicked
} ,
57 class wxMacSearchFieldControl
: public wxMacUnicodeTextControl
60 wxMacSearchFieldControl( wxTextCtrl
*wxPeer
,
63 const wxSize
& size
, long style
) : wxMacUnicodeTextControl( wxPeer
)
65 Create( wxPeer
, str
, pos
, size
, style
);
68 // search field options
69 virtual void ShowSearchButton( bool show
);
70 virtual bool IsSearchButtonVisible() const;
72 virtual void ShowCancelButton( bool show
);
73 virtual bool IsCancelButtonVisible() const;
75 virtual void SetSearchMenu( wxMenu
* menu
);
76 virtual wxMenu
* GetSearchMenu() const;
78 virtual void CreateControl( wxTextCtrl
* peer
, const Rect
* bounds
, CFStringRef crf
);
84 void wxMacSearchFieldControl::CreateControl( wxTextCtrl
* /*peer*/, const Rect
* bounds
, CFStringRef crf
)
86 OptionBits attributes
= 0;
87 if ( UMAGetSystemVersion() >= 0x1040 )
89 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 ( UMAGetSystemVersion() >= 0x1040 )
108 OptionBits clear
= 0;
111 set
|= kHISearchFieldAttributesSearchIcon
;
115 clear
|= kHISearchFieldAttributesSearchIcon
;
117 HISearchFieldChangeAttributes( m_controlRef
, set
, clear
);
121 bool wxMacSearchFieldControl::IsSearchButtonVisible() const
123 OptionBits attributes
= 0;
124 verify_noerr( HISearchFieldGetAttributes( m_controlRef
, &attributes
) );
125 return ( attributes
& kHISearchFieldAttributesSearchIcon
) != 0;
128 void wxMacSearchFieldControl::ShowCancelButton( bool show
)
131 OptionBits clear
= 0;
134 set
|= kHISearchFieldAttributesCancel
;
138 clear
|= kHISearchFieldAttributesCancel
;
140 HISearchFieldChangeAttributes( m_controlRef
, set
, clear
);
143 bool wxMacSearchFieldControl::IsCancelButtonVisible() const
145 OptionBits attributes
= 0;
146 verify_noerr( HISearchFieldGetAttributes( m_controlRef
, &attributes
) );
147 return ( attributes
& kHISearchFieldAttributesCancel
) != 0;
150 void wxMacSearchFieldControl::SetSearchMenu( wxMenu
* menu
)
155 verify_noerr( HISearchFieldSetSearchMenu( m_controlRef
, MAC_WXHMENU(m_menu
->GetHMenu()) ) );
159 verify_noerr( HISearchFieldSetSearchMenu( m_controlRef
, 0 ) );
163 wxMenu
* wxMacSearchFieldControl::GetSearchMenu() const
170 // ============================================================================
172 // ============================================================================
174 static pascal OSStatus
wxMacSearchControlEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
176 OSStatus result
= eventNotHandledErr
;
178 wxMacCarbonEvent
cEvent( event
) ;
180 ControlRef controlRef
;
181 wxSearchCtrl
* thisWindow
= (wxSearchCtrl
*) data
;
182 cEvent
.GetParameter( kEventParamDirectObject
, &controlRef
) ;
184 switch( GetEventKind( event
) )
186 case kEventSearchFieldCancelClicked
:
187 thisWindow
->MacSearchFieldCancelHit( handler
, event
) ;
189 case kEventSearchFieldSearchClicked
:
190 thisWindow
->MacSearchFieldSearchHit( handler
, event
) ;
197 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacSearchControlEventHandler
)
200 // ----------------------------------------------------------------------------
201 // wxSearchCtrl creation
202 // ----------------------------------------------------------------------------
207 wxSearchCtrl::wxSearchCtrl()
212 wxSearchCtrl::wxSearchCtrl(wxWindow
*parent
, wxWindowID id
,
213 const wxString
& value
,
217 const wxValidator
& validator
,
218 const wxString
& name
)
222 Create(parent
, id
, value
, pos
, size
, style
, validator
, name
);
225 void wxSearchCtrl::Init()
230 bool wxSearchCtrl::Create(wxWindow
*parent
, wxWindowID id
,
231 const wxString
& value
,
235 const wxValidator
& validator
,
236 const wxString
& name
)
238 if ( !wxTextCtrl::Create(parent
, id
, wxEmptyString
, pos
, size
, wxBORDER_NONE
| style
, validator
, name
) )
243 EventHandlerRef searchEventHandler
;
244 InstallControlEventHandler( m_peer
->GetControlRef(), GetwxMacSearchControlEventHandlerUPP(),
245 GetEventTypeCount(eventList
), eventList
, this,
246 (EventHandlerRef
*)&searchEventHandler
);
251 wxSearchCtrl::~wxSearchCtrl()
256 wxSize
wxSearchCtrl::DoGetBestSize() const
258 wxSize size
= wxWindow::DoGetBestSize();
259 // it seems to return a default width of about 16, which is way too small here.
260 if (size
.GetWidth() < 100)
266 void wxSearchCtrl::SetFocus()
268 // NB: We have to implement SetFocus a little differently because kControlFocusNextPart
269 // leads to setting the focus on the search icon rather than the text area.
270 // We get around this by explicitly telling the control to set focus to the
272 if ( !AcceptsFocus() )
275 wxWindow
* former
= FindFocus() ;
276 if ( former
== this )
279 // as we cannot rely on the control features to find out whether we are in full keyboard mode,
280 // we can only leave in case of an error
281 OSStatus err
= m_peer
->SetFocus( kControlEditTextPart
) ;
282 if ( err
== errCouldntSetFocus
)
285 SetUserFocusWindow( (WindowRef
)MacGetTopLevelWindowRef() );
288 // search control specific interfaces
289 // wxSearchCtrl owns menu after this call
290 void wxSearchCtrl::SetMenu( wxMenu
* menu
)
292 if ( menu
== m_menu
)
300 m_menu
->SetInvokingWindow( 0 );
308 m_menu
->SetInvokingWindow( this );
311 GetPeer()->SetSearchMenu( m_menu
);
314 wxMenu
* wxSearchCtrl::GetMenu()
319 void wxSearchCtrl::ShowSearchButton( bool show
)
321 if ( IsSearchButtonVisible() == show
)
326 GetPeer()->ShowSearchButton( show
);
329 bool wxSearchCtrl::IsSearchButtonVisible() const
331 return GetPeer()->IsSearchButtonVisible();
335 void wxSearchCtrl::ShowCancelButton( bool show
)
337 if ( IsCancelButtonVisible() == show
)
342 GetPeer()->ShowCancelButton( show
);
345 bool wxSearchCtrl::IsCancelButtonVisible() const
347 return GetPeer()->IsCancelButtonVisible();
350 wxInt32
wxSearchCtrl::MacSearchFieldSearchHit(WXEVENTHANDLERREF
WXUNUSED(handler
) , WXEVENTREF
WXUNUSED(event
) )
352 wxCommandEvent
event(wxEVT_COMMAND_SEARCHCTRL_SEARCH
, m_windowId
);
353 event
.SetEventObject(this);
354 ProcessCommand(event
);
355 return eventNotHandledErr
;
358 wxInt32
wxSearchCtrl::MacSearchFieldCancelHit(WXEVENTHANDLERREF
WXUNUSED(handler
) , WXEVENTREF
WXUNUSED(event
) )
360 wxCommandEvent
event(wxEVT_COMMAND_SEARCHCTRL_CANCEL
, m_windowId
);
361 event
.SetEventObject(this);
362 ProcessCommand(event
);
363 return eventNotHandledErr
;
367 void wxSearchCtrl::CreatePeer(
370 const wxSize
& size
, long style
)
373 #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
374 if ( UMAGetSystemVersion() >= 0x1030 )
376 m_peer
= new wxMacSearchFieldControl( this , str
, pos
, size
, style
);
382 wxTextCtrl::CreatePeer( str
, pos
, size
, style
);
386 #endif // wxUSE_NATIVE_SEARCH_CONTROL
388 #endif // wxUSE_SEARCHCTRL