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 static const EventTypeSpec eventList
[] =
42 { kEventClassSearchField
, kEventSearchFieldCancelClicked
} ,
43 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
44 { kEventClassSearchField
, kEventSearchFieldSearchClicked
} ,
48 class wxMacSearchFieldControl
: public wxMacUnicodeTextControl
51 wxMacSearchFieldControl( wxTextCtrl
*wxPeer
,
54 const wxSize
& size
, long style
) : wxMacUnicodeTextControl( wxPeer
)
56 Create( wxPeer
, str
, pos
, size
, style
);
59 // search field options
60 virtual void ShowSearchButton( bool show
);
61 virtual bool IsSearchButtonVisible() const;
63 virtual void ShowCancelButton( bool show
);
64 virtual bool IsCancelButtonVisible() const;
66 virtual void SetSearchMenu( wxMenu
* menu
);
67 virtual wxMenu
* GetSearchMenu() const;
69 virtual void SetDescriptiveText(const wxString
& text
);
70 virtual wxString
GetDescriptiveText() const;
73 virtual void CreateControl( wxTextCtrl
* peer
, const Rect
* bounds
, CFStringRef crf
);
79 void wxMacSearchFieldControl::CreateControl(wxTextCtrl
* WXUNUSED(peer
),
81 CFStringRef
WXUNUSED(crf
))
83 OptionBits attributes
= 0;
84 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
85 if ( UMAGetSystemVersion() >= 0x1040 )
87 attributes
= kHISearchFieldAttributesSearchIcon
;
90 HIRect hibounds
= { { bounds
->left
, bounds
->top
}, { bounds
->right
-bounds
->left
, bounds
->bottom
-bounds
->top
} };
91 verify_noerr( HISearchFieldCreate(
98 HIViewSetVisible (m_controlRef
, true);
101 // search field options
102 void wxMacSearchFieldControl::ShowSearchButton( bool show
)
104 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
105 if ( UMAGetSystemVersion() >= 0x1040 )
108 OptionBits clear
= 0;
111 set
|= kHISearchFieldAttributesSearchIcon
;
115 clear
|= kHISearchFieldAttributesSearchIcon
;
117 HISearchFieldChangeAttributes( m_controlRef
, set
, clear
);
122 bool wxMacSearchFieldControl::IsSearchButtonVisible() const
124 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
125 OptionBits attributes
= 0;
126 verify_noerr( HISearchFieldGetAttributes( m_controlRef
, &attributes
) );
127 return ( attributes
& kHISearchFieldAttributesSearchIcon
) != 0;
133 void wxMacSearchFieldControl::ShowCancelButton( bool show
)
136 OptionBits clear
= 0;
139 set
|= kHISearchFieldAttributesCancel
;
143 clear
|= kHISearchFieldAttributesCancel
;
145 HISearchFieldChangeAttributes( m_controlRef
, set
, clear
);
148 bool wxMacSearchFieldControl::IsCancelButtonVisible() const
150 OptionBits attributes
= 0;
151 verify_noerr( HISearchFieldGetAttributes( m_controlRef
, &attributes
) );
152 return ( attributes
& kHISearchFieldAttributesCancel
) != 0;
155 void wxMacSearchFieldControl::SetSearchMenu( wxMenu
* menu
)
160 verify_noerr( HISearchFieldSetSearchMenu( m_controlRef
, MAC_WXHMENU(m_menu
->GetHMenu()) ) );
164 verify_noerr( HISearchFieldSetSearchMenu( m_controlRef
, 0 ) );
168 wxMenu
* wxMacSearchFieldControl::GetSearchMenu() const
174 void wxMacSearchFieldControl::SetDescriptiveText(const wxString
& text
)
176 verify_noerr( HISearchFieldSetDescriptiveText(
178 wxMacCFStringHolder( text
, wxFont::GetDefaultEncoding() )));
181 wxString
wxMacSearchFieldControl::GetDescriptiveText() const
184 verify_noerr( HISearchFieldCopyDescriptiveText( m_controlRef
, &cfStr
));
187 return wxMacCFStringHolder(cfStr
).AsString();
191 return wxEmptyString
;
195 // ============================================================================
197 // ============================================================================
199 static pascal OSStatus
wxMacSearchControlEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
201 OSStatus result
= eventNotHandledErr
;
203 wxMacCarbonEvent
cEvent( event
) ;
205 ControlRef controlRef
;
206 wxSearchCtrl
* thisWindow
= (wxSearchCtrl
*) data
;
207 cEvent
.GetParameter( kEventParamDirectObject
, &controlRef
) ;
209 switch( GetEventKind( event
) )
211 case kEventSearchFieldCancelClicked
:
212 thisWindow
->MacSearchFieldCancelHit( handler
, event
) ;
214 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
215 case kEventSearchFieldSearchClicked
:
216 thisWindow
->MacSearchFieldSearchHit( handler
, event
) ;
224 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacSearchControlEventHandler
)
227 // ----------------------------------------------------------------------------
228 // wxSearchCtrl creation
229 // ----------------------------------------------------------------------------
234 wxSearchCtrl::wxSearchCtrl()
239 wxSearchCtrl::wxSearchCtrl(wxWindow
*parent
, wxWindowID id
,
240 const wxString
& value
,
244 const wxValidator
& validator
,
245 const wxString
& name
)
249 Create(parent
, id
, value
, pos
, size
, style
, validator
, name
);
252 void wxSearchCtrl::Init()
257 bool wxSearchCtrl::Create(wxWindow
*parent
, wxWindowID id
,
258 const wxString
& value
,
262 const wxValidator
& validator
,
263 const wxString
& name
)
265 if ( !wxTextCtrl::Create(parent
, id
, wxEmptyString
, pos
, size
, wxBORDER_NONE
| style
, validator
, name
) )
270 EventHandlerRef searchEventHandler
;
271 InstallControlEventHandler( m_peer
->GetControlRef(), GetwxMacSearchControlEventHandlerUPP(),
272 GetEventTypeCount(eventList
), eventList
, this,
273 (EventHandlerRef
*)&searchEventHandler
);
280 wxSearchCtrl::~wxSearchCtrl()
285 wxSize
wxSearchCtrl::DoGetBestSize() const
287 wxSize size
= wxWindow::DoGetBestSize();
288 // it seems to return a default width of about 16, which is way too small here.
289 if (size
.GetWidth() < 100)
295 void wxSearchCtrl::SetFocus()
297 // NB: We have to implement SetFocus a little differently because kControlFocusNextPart
298 // leads to setting the focus on the search icon rather than the text area.
299 // We get around this by explicitly telling the control to set focus to the
301 if ( !AcceptsFocus() )
304 wxWindow
* former
= FindFocus() ;
305 if ( former
== this )
308 // as we cannot rely on the control features to find out whether we are in full keyboard mode,
309 // we can only leave in case of an error
310 OSStatus err
= m_peer
->SetFocus( kControlEditTextPart
) ;
311 if ( err
== errCouldntSetFocus
)
314 SetUserFocusWindow( (WindowRef
)MacGetTopLevelWindowRef() );
317 // search control specific interfaces
318 // wxSearchCtrl owns menu after this call
319 void wxSearchCtrl::SetMenu( wxMenu
* menu
)
321 if ( menu
== m_menu
)
329 m_menu
->SetInvokingWindow( 0 );
337 m_menu
->SetInvokingWindow( this );
340 GetPeer()->SetSearchMenu( m_menu
);
343 wxMenu
* wxSearchCtrl::GetMenu()
348 void wxSearchCtrl::ShowSearchButton( bool show
)
350 if ( IsSearchButtonVisible() == show
)
355 GetPeer()->ShowSearchButton( show
);
358 bool wxSearchCtrl::IsSearchButtonVisible() const
360 return GetPeer()->IsSearchButtonVisible();
364 void wxSearchCtrl::ShowCancelButton( bool show
)
366 if ( IsCancelButtonVisible() == show
)
371 GetPeer()->ShowCancelButton( show
);
374 bool wxSearchCtrl::IsCancelButtonVisible() const
376 return GetPeer()->IsCancelButtonVisible();
379 void wxSearchCtrl::SetDescriptiveText(const wxString
& text
)
381 GetPeer()->SetDescriptiveText(text
);
384 wxString
wxSearchCtrl::GetDescriptiveText() const
386 return GetPeer()->GetDescriptiveText();
389 wxInt32
wxSearchCtrl::MacSearchFieldSearchHit(WXEVENTHANDLERREF
WXUNUSED(handler
) , WXEVENTREF
WXUNUSED(event
) )
391 wxCommandEvent
event(wxEVT_COMMAND_SEARCHCTRL_SEARCH_BTN
, m_windowId
);
392 event
.SetEventObject(this);
393 ProcessCommand(event
);
394 return eventNotHandledErr
;
397 wxInt32
wxSearchCtrl::MacSearchFieldCancelHit(WXEVENTHANDLERREF
WXUNUSED(handler
) , WXEVENTREF
WXUNUSED(event
) )
399 wxCommandEvent
event(wxEVT_COMMAND_SEARCHCTRL_CANCEL_BTN
, m_windowId
);
400 event
.SetEventObject(this);
401 ProcessCommand(event
);
402 return eventNotHandledErr
;
406 void wxSearchCtrl::CreatePeer(
409 const wxSize
& size
, long style
)
412 if ( UMAGetSystemVersion() >= 0x1030 )
414 m_peer
= new wxMacSearchFieldControl( this , str
, pos
, size
, style
);
419 wxTextCtrl::CreatePeer( str
, pos
, size
, style
);
423 #endif // wxUSE_NATIVE_SEARCH_CONTROL
425 #endif // wxUSE_SEARCHCTRL