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::SetSearchButtonVisible( 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::GetSearchButtonVisible() const
123 OptionBits attributes
= 0;
124 verify_noerr( HISearchFieldGetAttributes( m_controlRef
, &attributes
) );
125 return ( attributes
& kHISearchFieldAttributesSearchIcon
) != 0;
128 void wxMacSearchFieldControl::SetCancelButtonVisible( bool show
)
131 OptionBits clear
= 0;
134 set
|= kHISearchFieldAttributesCancel
;
138 clear
|= kHISearchFieldAttributesCancel
;
140 HISearchFieldChangeAttributes( m_controlRef
, set
, clear
);
143 bool wxMacSearchFieldControl::GetCancelButtonVisible() 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 wxTextCtrl
* textCtrl
= wxDynamicCast( thisWindow
, wxTextCtrl
) ;
183 cEvent
.GetParameter( kEventParamDirectObject
, &controlRef
) ;
185 switch( GetEventKind( event
) )
187 case kEventSearchFieldCancelClicked
:
188 thisWindow
->MacSearchFieldCancelHit( handler
, event
) ;
190 case kEventSearchFieldSearchClicked
:
191 thisWindow
->MacSearchFieldSearchHit( handler
, event
) ;
198 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacSearchControlEventHandler
)
201 // ----------------------------------------------------------------------------
202 // wxSearchCtrl creation
203 // ----------------------------------------------------------------------------
208 wxSearchCtrl::wxSearchCtrl()
213 wxSearchCtrl::wxSearchCtrl(wxWindow
*parent
, wxWindowID id
,
214 const wxString
& value
,
218 const wxValidator
& validator
,
219 const wxString
& name
)
223 Create(parent
, id
, value
, pos
, size
, style
, validator
, name
);
226 void wxSearchCtrl::Init()
231 bool wxSearchCtrl::Create(wxWindow
*parent
, wxWindowID id
,
232 const wxString
& value
,
236 const wxValidator
& validator
,
237 const wxString
& name
)
239 if ( !wxTextCtrl::Create(parent
, id
, wxEmptyString
, pos
, size
, wxBORDER_NONE
| style
, validator
, name
) )
244 EventHandlerRef searchEventHandler
;
245 InstallControlEventHandler( m_peer
->GetControlRef(), GetwxMacSearchControlEventHandlerUPP(),
246 GetEventTypeCount(eventList
), eventList
, this,
247 (EventHandlerRef
*)&searchEventHandler
);
252 wxSearchCtrl::~wxSearchCtrl()
257 wxSize
wxSearchCtrl::DoGetBestSize() const
259 wxSize size
= wxWindow::DoGetBestSize();
260 // it seems to return a default width of about 16, which is way too small here.
261 if (size
.GetWidth() < 100)
267 void wxSearchCtrl::SetFocus()
269 // NB: We have to implement SetFocus a little differently because kControlFocusNextPart
270 // leads to setting the focus on the search icon rather than the text area.
271 // We get around this by explicitly telling the control to set focus to the
273 if ( !AcceptsFocus() )
276 wxWindow
* former
= FindFocus() ;
277 if ( former
== this )
280 // as we cannot rely on the control features to find out whether we are in full keyboard mode,
281 // we can only leave in case of an error
282 OSStatus err
= m_peer
->SetFocus( kControlEditTextPart
) ;
283 if ( err
== errCouldntSetFocus
)
286 SetUserFocusWindow( (WindowRef
)MacGetTopLevelWindowRef() );
289 // search control specific interfaces
290 // wxSearchCtrl owns menu after this call
291 void wxSearchCtrl::SetMenu( wxMenu
* menu
)
293 if ( menu
== m_menu
)
301 m_menu
->SetInvokingWindow( 0 );
309 m_menu
->SetInvokingWindow( this );
312 GetPeer()->SetSearchMenu( m_menu
);
315 wxMenu
* wxSearchCtrl::GetMenu()
320 void wxSearchCtrl::SetSearchButtonVisible( bool show
)
322 if ( GetSearchButtonVisible() == show
)
327 GetPeer()->SetSearchButtonVisible( show
);
330 bool wxSearchCtrl::GetSearchButtonVisible() const
332 return GetPeer()->GetSearchButtonVisible();
336 void wxSearchCtrl::SetCancelButtonVisible( bool show
)
338 if ( GetCancelButtonVisible() == show
)
343 GetPeer()->SetCancelButtonVisible( show
);
346 bool wxSearchCtrl::GetCancelButtonVisible() const
348 return GetPeer()->GetCancelButtonVisible();
351 wxInt32
wxSearchCtrl::MacSearchFieldSearchHit(WXEVENTHANDLERREF
WXUNUSED(handler
) , WXEVENTREF
WXUNUSED(event
) )
353 wxCommandEvent
event(wxEVT_COMMAND_SEARCHCTRL_SEARCH
, m_windowId
);
354 event
.SetEventObject(this);
355 ProcessCommand(event
);
356 return eventNotHandledErr
;
359 wxInt32
wxSearchCtrl::MacSearchFieldCancelHit(WXEVENTHANDLERREF
WXUNUSED(handler
) , WXEVENTREF
WXUNUSED(event
) )
361 wxCommandEvent
event(wxEVT_COMMAND_SEARCHCTRL_CANCEL
, m_windowId
);
362 event
.SetEventObject(this);
363 ProcessCommand(event
);
364 return eventNotHandledErr
;
368 void wxSearchCtrl::CreatePeer(
371 const wxSize
& size
, long style
)
374 #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
375 if ( UMAGetSystemVersion() >= 0x1030 )
377 m_peer
= new wxMacSearchFieldControl( this , str
, pos
, size
, style
);
383 wxTextCtrl::CreatePeer( str
, pos
, size
, style
);
387 #endif // wxUSE_NATIVE_SEARCH_CONTROL
389 #endif // wxUSE_SEARCHCTRL