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 return wxWindow::DoGetBestSize();
267 // search control specific interfaces
268 // wxSearchCtrl owns menu after this call
269 void wxSearchCtrl::SetMenu( wxMenu
* menu
)
271 if ( menu
== m_menu
)
279 m_menu
->SetInvokingWindow( 0 );
287 m_menu
->SetInvokingWindow( this );
290 GetPeer()->SetSearchMenu( m_menu
);
293 wxMenu
* wxSearchCtrl::GetMenu()
298 void wxSearchCtrl::SetSearchButtonVisible( bool show
)
300 if ( GetSearchButtonVisible() == show
)
305 GetPeer()->SetSearchButtonVisible( show
);
308 bool wxSearchCtrl::GetSearchButtonVisible() const
310 return GetPeer()->GetSearchButtonVisible();
314 void wxSearchCtrl::SetCancelButtonVisible( bool show
)
316 if ( GetCancelButtonVisible() == show
)
321 GetPeer()->SetCancelButtonVisible( show
);
324 bool wxSearchCtrl::GetCancelButtonVisible() const
326 return GetPeer()->GetCancelButtonVisible();
329 wxInt32
wxSearchCtrl::MacSearchFieldSearchHit(WXEVENTHANDLERREF
WXUNUSED(handler
) , WXEVENTREF
WXUNUSED(event
) )
331 wxCommandEvent
event(wxEVT_COMMAND_SEARCHCTRL_SEARCH
, m_windowId
);
332 event
.SetEventObject(this);
333 ProcessCommand(event
);
334 return eventNotHandledErr
;
337 wxInt32
wxSearchCtrl::MacSearchFieldCancelHit(WXEVENTHANDLERREF
WXUNUSED(handler
) , WXEVENTREF
WXUNUSED(event
) )
339 wxCommandEvent
event(wxEVT_COMMAND_SEARCHCTRL_CANCEL
, m_windowId
);
340 event
.SetEventObject(this);
341 ProcessCommand(event
);
342 return eventNotHandledErr
;
346 void wxSearchCtrl::CreatePeer(
349 const wxSize
& size
, long style
)
352 #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
353 if ( UMAGetSystemVersion() >= 0x1030 )
355 m_peer
= new wxMacSearchFieldControl( this , str
, pos
, size
, style
);
361 wxTextCtrl::CreatePeer( str
, pos
, size
, style
);
365 #endif // USE_NATIVE_SEARCH_CONTROL
367 #endif // wxUSE_SEARCHCTRL