1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/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/osx/uma.h"
29 #include "wx/osx/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 { kEventClassSearchField
, kEventSearchFieldSearchClicked
} ,
46 class wxMacSearchFieldControl
: public wxMacUnicodeTextControl
49 wxMacSearchFieldControl( wxTextCtrl
*wxPeer
,
52 const wxSize
& size
, long style
) : wxMacUnicodeTextControl( wxPeer
)
54 Create( wxPeer
, str
, pos
, size
, style
);
57 // search field options
58 virtual void ShowSearchButton( bool show
);
59 virtual bool IsSearchButtonVisible() const;
61 virtual void ShowCancelButton( bool show
);
62 virtual bool IsCancelButtonVisible() const;
64 virtual void SetSearchMenu( wxMenu
* menu
);
65 virtual wxMenu
* GetSearchMenu() const;
67 virtual void SetDescriptiveText(const wxString
& text
);
68 virtual wxString
GetDescriptiveText() const;
70 virtual bool SetFocus();
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
= kHISearchFieldAttributesSearchIcon
;
85 HIRect hibounds
= { { bounds
->left
, bounds
->top
}, { bounds
->right
-bounds
->left
, bounds
->bottom
-bounds
->top
} };
86 verify_noerr( HISearchFieldCreate(
93 HIViewSetVisible (m_controlRef
, true);
96 // search field options
97 void wxMacSearchFieldControl::ShowSearchButton( bool show
)
100 OptionBits clear
= 0;
103 set
|= kHISearchFieldAttributesSearchIcon
;
107 clear
|= kHISearchFieldAttributesSearchIcon
;
109 HISearchFieldChangeAttributes( m_controlRef
, set
, clear
);
112 bool wxMacSearchFieldControl::IsSearchButtonVisible() const
114 OptionBits attributes
= 0;
115 verify_noerr( HISearchFieldGetAttributes( m_controlRef
, &attributes
) );
116 return ( attributes
& kHISearchFieldAttributesSearchIcon
) != 0;
119 void wxMacSearchFieldControl::ShowCancelButton( bool show
)
122 OptionBits clear
= 0;
125 set
|= kHISearchFieldAttributesCancel
;
129 clear
|= kHISearchFieldAttributesCancel
;
131 HISearchFieldChangeAttributes( m_controlRef
, set
, clear
);
134 bool wxMacSearchFieldControl::IsCancelButtonVisible() const
136 OptionBits attributes
= 0;
137 verify_noerr( HISearchFieldGetAttributes( m_controlRef
, &attributes
) );
138 return ( attributes
& kHISearchFieldAttributesCancel
) != 0;
141 void wxMacSearchFieldControl::SetSearchMenu( wxMenu
* menu
)
146 verify_noerr( HISearchFieldSetSearchMenu( m_controlRef
, MAC_WXHMENU(m_menu
->GetHMenu()) ) );
150 verify_noerr( HISearchFieldSetSearchMenu( m_controlRef
, 0 ) );
154 wxMenu
* wxMacSearchFieldControl::GetSearchMenu() const
160 void wxMacSearchFieldControl::SetDescriptiveText(const wxString
& text
)
162 verify_noerr( HISearchFieldSetDescriptiveText(
164 wxCFStringRef( text
, wxFont::GetDefaultEncoding() )));
167 wxString
wxMacSearchFieldControl::GetDescriptiveText() const
170 verify_noerr( HISearchFieldCopyDescriptiveText( m_controlRef
, &cfStr
));
173 return wxCFStringRef(cfStr
).AsString();
177 return wxEmptyString
;
181 bool wxMacSearchFieldControl::SetFocus()
183 // NB: We have to implement SetFocus a little differently because kControlFocusNextPart
184 // leads to setting the focus on the search icon rather than the text area.
185 // We get around this by explicitly telling the control to set focus to the
188 OSStatus err
= SetKeyboardFocus( GetControlOwner( m_controlRef
), m_controlRef
, kControlEditTextPart
);
189 if ( err
== errCouldntSetFocus
)
191 SetUserFocusWindow(GetControlOwner( m_controlRef
) );
196 // ============================================================================
198 // ============================================================================
200 static pascal OSStatus
wxMacSearchControlEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
202 OSStatus result
= eventNotHandledErr
;
204 wxMacCarbonEvent
cEvent( event
) ;
206 ControlRef controlRef
;
207 wxSearchCtrl
* thisWindow
= (wxSearchCtrl
*) data
;
208 cEvent
.GetParameter( kEventParamDirectObject
, &controlRef
) ;
210 switch( GetEventKind( event
) )
212 case kEventSearchFieldCancelClicked
:
213 thisWindow
->MacSearchFieldCancelHit( handler
, event
) ;
215 case kEventSearchFieldSearchClicked
:
216 thisWindow
->MacSearchFieldSearchHit( handler
, event
) ;
223 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacSearchControlEventHandler
)
226 // ----------------------------------------------------------------------------
227 // wxSearchCtrl creation
228 // ----------------------------------------------------------------------------
233 wxSearchCtrl::wxSearchCtrl()
238 wxSearchCtrl::wxSearchCtrl(wxWindow
*parent
, wxWindowID id
,
239 const wxString
& value
,
243 const wxValidator
& validator
,
244 const wxString
& name
)
248 Create(parent
, id
, value
, pos
, size
, style
, validator
, name
);
251 void wxSearchCtrl::Init()
256 bool wxSearchCtrl::Create(wxWindow
*parent
, wxWindowID id
,
257 const wxString
& value
,
261 const wxValidator
& validator
,
262 const wxString
& name
)
264 if ( !wxTextCtrl::Create(parent
, id
, wxEmptyString
, pos
, size
, wxBORDER_NONE
| style
, validator
, name
) )
269 EventHandlerRef searchEventHandler
;
270 InstallControlEventHandler( m_peer
->GetControlRef(), GetwxMacSearchControlEventHandlerUPP(),
271 GetEventTypeCount(eventList
), eventList
, this,
272 (EventHandlerRef
*)&searchEventHandler
);
279 wxSearchCtrl::~wxSearchCtrl()
284 wxSize
wxSearchCtrl::DoGetBestSize() const
286 wxSize size
= wxWindow::DoGetBestSize();
287 // it seems to return a default width of about 16, which is way too small here.
288 if (size
.GetWidth() < 100)
295 // search control specific interfaces
296 // wxSearchCtrl owns menu after this call
297 void wxSearchCtrl::SetMenu( wxMenu
* menu
)
299 if ( menu
== m_menu
)
307 m_menu
->SetInvokingWindow( 0 );
315 m_menu
->SetInvokingWindow( this );
318 GetPeer()->SetSearchMenu( m_menu
);
321 wxMenu
* wxSearchCtrl::GetMenu()
326 void wxSearchCtrl::ShowSearchButton( bool show
)
328 if ( IsSearchButtonVisible() == show
)
333 GetPeer()->ShowSearchButton( show
);
336 bool wxSearchCtrl::IsSearchButtonVisible() const
338 return GetPeer()->IsSearchButtonVisible();
342 void wxSearchCtrl::ShowCancelButton( bool show
)
344 if ( IsCancelButtonVisible() == show
)
349 GetPeer()->ShowCancelButton( show
);
352 bool wxSearchCtrl::IsCancelButtonVisible() const
354 return GetPeer()->IsCancelButtonVisible();
357 void wxSearchCtrl::SetDescriptiveText(const wxString
& text
)
359 GetPeer()->SetDescriptiveText(text
);
362 wxString
wxSearchCtrl::GetDescriptiveText() const
364 return GetPeer()->GetDescriptiveText();
367 wxInt32
wxSearchCtrl::MacSearchFieldSearchHit(WXEVENTHANDLERREF
WXUNUSED(handler
) , WXEVENTREF
WXUNUSED(event
) )
369 wxCommandEvent
event(wxEVT_COMMAND_SEARCHCTRL_SEARCH_BTN
, m_windowId
);
370 event
.SetEventObject(this);
371 ProcessCommand(event
);
372 return eventNotHandledErr
;
375 wxInt32
wxSearchCtrl::MacSearchFieldCancelHit(WXEVENTHANDLERREF
WXUNUSED(handler
) , WXEVENTREF
WXUNUSED(event
) )
377 wxCommandEvent
event(wxEVT_COMMAND_SEARCHCTRL_CANCEL_BTN
, m_windowId
);
378 event
.SetEventObject(this);
379 ProcessCommand(event
);
380 return eventNotHandledErr
;
384 void wxSearchCtrl::CreatePeer(
387 const wxSize
& size
, long style
)
389 m_peer
= new wxMacSearchFieldControl( this , str
, pos
, size
, style
);
392 #endif // wxUSE_NATIVE_SEARCH_CONTROL
394 #endif // wxUSE_SEARCHCTRL