1
///////////////////////////////////////////////////////////////////////////////
2
// Name: src/osx/carbon/srchctrl.cpp
3
// Purpose: implements mac carbon wxSearchCtrl
4
// Author: Vince Harron
6
// RCS-ID: $Id: srchctrl.cpp 66826 2011-02-02 07:55:57Z SC $
7
// Copyright: Vince Harron
8
// Licence: 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
// ============================================================================
32
// wxMacSearchFieldControl
33
// ============================================================================
35
class wxMacSearchFieldControl : public wxMacUnicodeTextControl, public wxSearchWidgetImpl
38
wxMacSearchFieldControl( wxTextCtrl *wxPeer,
41
const wxSize& size, long style ) ;
43
// search field options
44
virtual void ShowSearchButton( bool show );
45
virtual bool IsSearchButtonVisible() const;
47
virtual void ShowCancelButton( bool show );
48
virtual bool IsCancelButtonVisible() const;
50
virtual void SetSearchMenu( wxMenu* menu );
52
virtual void SetDescriptiveText(const wxString& text);
54
virtual bool SetFocus();
59
static const EventTypeSpec eventList[] =
61
{ kEventClassSearchField, kEventSearchFieldCancelClicked } ,
62
{ kEventClassSearchField, kEventSearchFieldSearchClicked } ,
65
// ============================================================================
67
// ============================================================================
69
static pascal OSStatus wxMacSearchControlEventHandler( EventHandlerCallRef WXUNUSED(handler) , EventRef event , void *data )
71
OSStatus result = eventNotHandledErr ;
73
wxMacCarbonEvent cEvent( event ) ;
75
ControlRef controlRef ;
76
wxSearchCtrl* thisWindow = (wxSearchCtrl*) data ;
77
cEvent.GetParameter( kEventParamDirectObject , &controlRef ) ;
79
switch( GetEventKind( event ) )
81
case kEventSearchFieldCancelClicked :
82
thisWindow->HandleSearchFieldCancelHit() ;
84
case kEventSearchFieldSearchClicked :
85
thisWindow->HandleSearchFieldSearchHit() ;
92
DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacSearchControlEventHandler )
94
wxMacSearchFieldControl::wxMacSearchFieldControl( wxTextCtrl *wxPeer,
97
const wxSize& size, long style ) : wxMacUnicodeTextControl( wxPeer )
99
m_font = wxPeer->GetFont() ;
100
m_windowStyle = style ;
101
m_selection.selStart = m_selection.selEnd = 0;
102
Rect bounds = wxMacGetBoundsForControl( wxPeer , pos , size ) ;
104
wxMacConvertNewlines10To13( &st ) ;
105
wxCFStringRef cf(st , m_font.GetEncoding()) ;
107
m_valueTag = kControlEditTextCFStringTag ;
109
OptionBits attributes = kHISearchFieldAttributesSearchIcon;
111
HIRect hibounds = { { bounds.left, bounds.top }, { bounds.right-bounds.left, bounds.bottom-bounds.top } };
112
verify_noerr( HISearchFieldCreate(
119
HIViewSetVisible (m_controlRef, true);
121
verify_noerr( SetData<CFStringRef>( 0, kControlEditTextCFStringTag , cf ) ) ;
123
::InstallControlEventHandler( m_controlRef, GetwxMacSearchControlEventHandlerUPP(),
124
GetEventTypeCount(eventList), eventList, wxPeer, NULL);
125
SetNeedsFrame(false);
126
wxMacUnicodeTextControl::InstallEventHandlers();
129
// search field options
130
void wxMacSearchFieldControl::ShowSearchButton( bool show )
133
OptionBits clear = 0;
136
set |= kHISearchFieldAttributesSearchIcon;
140
clear |= kHISearchFieldAttributesSearchIcon;
142
HISearchFieldChangeAttributes( m_controlRef, set, clear );
145
bool wxMacSearchFieldControl::IsSearchButtonVisible() const
147
OptionBits attributes = 0;
148
verify_noerr( HISearchFieldGetAttributes( m_controlRef, &attributes ) );
149
return ( attributes & kHISearchFieldAttributesSearchIcon ) != 0;
152
void wxMacSearchFieldControl::ShowCancelButton( bool show )
155
OptionBits clear = 0;
158
set |= kHISearchFieldAttributesCancel;
162
clear |= kHISearchFieldAttributesCancel;
164
HISearchFieldChangeAttributes( m_controlRef, set, clear );
167
bool wxMacSearchFieldControl::IsCancelButtonVisible() const
169
OptionBits attributes = 0;
170
verify_noerr( HISearchFieldGetAttributes( m_controlRef, &attributes ) );
171
return ( attributes & kHISearchFieldAttributesCancel ) != 0;
174
void wxMacSearchFieldControl::SetSearchMenu( wxMenu* menu )
178
verify_noerr( HISearchFieldSetSearchMenu( m_controlRef, MAC_WXHMENU(menu->GetHMenu()) ) );
182
verify_noerr( HISearchFieldSetSearchMenu( m_controlRef, 0 ) );
186
void wxMacSearchFieldControl::SetDescriptiveText(const wxString& text)
188
verify_noerr( HISearchFieldSetDescriptiveText(
190
wxCFStringRef( text, wxFont::GetDefaultEncoding() )));
193
bool wxMacSearchFieldControl::SetFocus()
195
// NB: We have to implement SetFocus a little differently because kControlFocusNextPart
196
// leads to setting the focus on the search icon rather than the text area.
197
// We get around this by explicitly telling the control to set focus to the
200
OSStatus err = SetKeyboardFocus( GetControlOwner( m_controlRef ), m_controlRef, kControlEditTextPart );
201
if ( err == errCouldntSetFocus )
203
SetUserFocusWindow(GetControlOwner( m_controlRef ) );
207
wxWidgetImplType* wxWidgetImpl::CreateSearchControl( wxSearchCtrl* wxpeer,
208
wxWindowMac* WXUNUSED(parent),
209
wxWindowID WXUNUSED(id),
214
long WXUNUSED(extraStyle))
216
wxMacControl* peer = new wxMacSearchFieldControl( wxpeer , str , pos , size , style );
221
#endif // wxUSE_NATIVE_SEARCH_CONTROL
223
#endif // wxUSE_SEARCHCTRL