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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "srchctrl.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
37 #include "wx/srchctrl.h"
39 #if USE_NATIVE_SEARCH_CONTROL
41 #include "wx/mac/uma.h"
42 #include "wx/mac/carbon/private/mactext.h"
44 BEGIN_EVENT_TABLE(wxSearchCtrl
, wxSearchCtrlBase
)
47 IMPLEMENT_DYNAMIC_CLASS(wxSearchCtrl
, wxSearchCtrlBase
)
49 // ============================================================================
50 // wxMacSearchFieldControl
51 // ============================================================================
53 #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
56 static const EventTypeSpec eventList
[] =
58 { kEventClassSearchField
, kEventSearchFieldCancelClicked
} ,
59 { kEventClassSearchField
, kEventSearchFieldSearchClicked
} ,
62 class wxMacSearchFieldControl
: public wxMacUnicodeTextControl
65 wxMacSearchFieldControl( wxTextCtrl
*wxPeer
,
68 const wxSize
& size
, long style
) : wxMacUnicodeTextControl( wxPeer
)
70 Create( wxPeer
, str
, pos
, size
, style
);
73 // search field options
74 virtual void SetSearchButtonVisible( bool show
);
75 virtual bool GetSearchButtonVisible() const;
77 virtual void SetCancelButtonVisible( bool show
);
78 virtual bool GetCancelButtonVisible() const;
80 virtual void SetSearchMenu( wxMenu
* menu
);
81 virtual wxMenu
* GetSearchMenu() const;
83 virtual void CreateControl( wxTextCtrl
* peer
, const Rect
* bounds
, CFStringRef crf
);
89 void wxMacSearchFieldControl::CreateControl( wxTextCtrl
* /*peer*/, const Rect
* bounds
, CFStringRef crf
)
91 OptionBits attributes
= 0;
92 if ( UMAGetSystemVersion() >= 0x1040 )
94 attributes
= kHISearchFieldAttributesSearchIcon
;
96 HIRect hibounds
= { { bounds
->left
, bounds
->top
}, { bounds
->right
-bounds
->left
, bounds
->bottom
-bounds
->top
} };
97 verify_noerr( HISearchFieldCreate(
104 HIViewSetVisible (m_controlRef
, true);
107 // search field options
108 void wxMacSearchFieldControl::SetSearchButtonVisible( bool show
)
110 if ( UMAGetSystemVersion() >= 0x1040 )
113 OptionBits clear
= 0;
116 set
|= kHISearchFieldAttributesSearchIcon
;
120 clear
|= kHISearchFieldAttributesSearchIcon
;
122 HISearchFieldChangeAttributes( m_controlRef
, set
, clear
);
126 bool wxMacSearchFieldControl::GetSearchButtonVisible() const
128 OptionBits attributes
= 0;
129 verify_noerr( HISearchFieldGetAttributes( m_controlRef
, &attributes
) );
130 return ( attributes
& kHISearchFieldAttributesSearchIcon
) != 0;
133 void wxMacSearchFieldControl::SetCancelButtonVisible( bool show
)
136 OptionBits clear
= 0;
139 set
|= kHISearchFieldAttributesCancel
;
143 clear
|= kHISearchFieldAttributesCancel
;
145 HISearchFieldChangeAttributes( m_controlRef
, set
, clear
);
148 bool wxMacSearchFieldControl::GetCancelButtonVisible() 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
175 // ============================================================================
177 // ============================================================================
179 static pascal OSStatus
wxMacSearchControlEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
181 OSStatus result
= eventNotHandledErr
;
183 wxMacCarbonEvent
cEvent( event
) ;
185 ControlRef controlRef
;
186 wxSearchCtrl
* thisWindow
= (wxSearchCtrl
*) data
;
187 wxTextCtrl
* textCtrl
= wxDynamicCast( thisWindow
, wxTextCtrl
) ;
188 cEvent
.GetParameter( kEventParamDirectObject
, &controlRef
) ;
190 switch( GetEventKind( event
) )
192 case kEventSearchFieldCancelClicked
:
193 thisWindow
->MacSearchFieldCancelHit( handler
, event
) ;
195 case kEventSearchFieldSearchClicked
:
196 thisWindow
->MacSearchFieldSearchHit( handler
, event
) ;
203 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacSearchControlEventHandler
)
206 // ----------------------------------------------------------------------------
207 // wxSearchCtrl creation
208 // ----------------------------------------------------------------------------
213 wxSearchCtrl::wxSearchCtrl()
218 wxSearchCtrl::wxSearchCtrl(wxWindow
*parent
, wxWindowID id
,
219 const wxString
& value
,
223 const wxValidator
& validator
,
224 const wxString
& name
)
228 Create(parent
, id
, value
, pos
, size
, style
, validator
, name
);
231 void wxSearchCtrl::Init()
236 bool wxSearchCtrl::Create(wxWindow
*parent
, wxWindowID id
,
237 const wxString
& value
,
241 const wxValidator
& validator
,
242 const wxString
& name
)
244 if ( !wxTextCtrl::Create(parent
, id
, wxEmptyString
, pos
, size
, wxBORDER_NONE
| style
, validator
, name
) )
249 EventHandlerRef searchEventHandler
;
250 InstallControlEventHandler( m_peer
->GetControlRef(), GetwxMacSearchControlEventHandlerUPP(),
251 GetEventTypeCount(eventList
), eventList
, this,
252 (EventHandlerRef
*)&searchEventHandler
);
257 wxSearchCtrl::~wxSearchCtrl()
262 wxSize
wxSearchCtrl::DoGetBestSize() const
264 wxSize size
= wxWindow::DoGetBestSize();
265 // it seems to return a default width of about 16, which is way too small here.
266 if (size
.GetWidth() < 100)
272 void wxSearchCtrl::SetFocus()
274 // NB: We have to implement SetFocus a little differently because kControlFocusNextPart
275 // leads to setting the focus on the search icon rather than the text area.
276 // We get around this by explicitly telling the control to set focus to the
278 if ( !AcceptsFocus() )
281 wxWindow
* former
= FindFocus() ;
282 if ( former
== this )
285 // as we cannot rely on the control features to find out whether we are in full keyboard mode,
286 // we can only leave in case of an error
287 OSStatus err
= m_peer
->SetFocus( kControlEditTextPart
) ;
288 if ( err
== errCouldntSetFocus
)
291 SetUserFocusWindow( (WindowRef
)MacGetTopLevelWindowRef() );
294 // search control specific interfaces
295 // wxSearchCtrl owns menu after this call
296 void wxSearchCtrl::SetMenu( wxMenu
* menu
)
298 if ( menu
== m_menu
)
306 m_menu
->SetInvokingWindow( 0 );
314 m_menu
->SetInvokingWindow( this );
317 GetPeer()->SetSearchMenu( m_menu
);
320 wxMenu
* wxSearchCtrl::GetMenu()
325 void wxSearchCtrl::SetSearchButtonVisible( bool show
)
327 if ( GetSearchButtonVisible() == show
)
332 GetPeer()->SetSearchButtonVisible( show
);
335 bool wxSearchCtrl::GetSearchButtonVisible() const
337 return GetPeer()->GetSearchButtonVisible();
341 void wxSearchCtrl::SetCancelButtonVisible( bool show
)
343 if ( GetCancelButtonVisible() == show
)
348 GetPeer()->SetCancelButtonVisible( show
);
351 bool wxSearchCtrl::GetCancelButtonVisible() const
353 return GetPeer()->GetCancelButtonVisible();
356 wxInt32
wxSearchCtrl::MacSearchFieldSearchHit(WXEVENTHANDLERREF
WXUNUSED(handler
) , WXEVENTREF
WXUNUSED(event
) )
358 wxCommandEvent
event(wxEVT_COMMAND_SEARCHCTRL_SEARCH
, m_windowId
);
359 event
.SetEventObject(this);
360 ProcessCommand(event
);
361 return eventNotHandledErr
;
364 wxInt32
wxSearchCtrl::MacSearchFieldCancelHit(WXEVENTHANDLERREF
WXUNUSED(handler
) , WXEVENTREF
WXUNUSED(event
) )
366 wxCommandEvent
event(wxEVT_COMMAND_SEARCHCTRL_CANCEL
, m_windowId
);
367 event
.SetEventObject(this);
368 ProcessCommand(event
);
369 return eventNotHandledErr
;
373 void wxSearchCtrl::CreatePeer(
376 const wxSize
& size
, long style
)
379 #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
380 if ( UMAGetSystemVersion() >= 0x1030 )
382 m_peer
= new wxMacSearchFieldControl( this , str
, pos
, size
, style
);
388 wxTextCtrl::CreatePeer( str
, pos
, size
, style
);
392 #endif // USE_NATIVE_SEARCH_CONTROL
394 #endif // wxUSE_SEARCHCTRL