1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/carbon/srchctrl.cpp
3 // Purpose: implements mac carbon wxSearchCtrl
4 // Author: Vince Harron
6 // Copyright: Vince Harron
7 // Licence: wxWindows licence
8 ///////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
19 #include "wx/srchctrl.h"
25 #if wxUSE_NATIVE_SEARCH_CONTROL
27 #include "wx/osx/uma.h"
28 #include "wx/osx/carbon/private/mactext.h"
30 // ============================================================================
31 // wxMacSearchFieldControl
32 // ============================================================================
34 class wxMacSearchFieldControl
: public wxMacUnicodeTextControl
, public wxSearchWidgetImpl
37 wxMacSearchFieldControl( wxTextCtrl
*wxPeer
,
40 const wxSize
& size
, long style
) ;
42 // search field options
43 virtual void ShowSearchButton( bool show
);
44 virtual bool IsSearchButtonVisible() const;
46 virtual void ShowCancelButton( bool show
);
47 virtual bool IsCancelButtonVisible() const;
49 virtual void SetSearchMenu( wxMenu
* menu
);
51 virtual void SetDescriptiveText(const wxString
& text
);
53 virtual bool SetFocus();
58 static const EventTypeSpec eventList
[] =
60 { kEventClassSearchField
, kEventSearchFieldCancelClicked
} ,
61 { kEventClassSearchField
, kEventSearchFieldSearchClicked
} ,
64 // ============================================================================
66 // ============================================================================
68 static pascal OSStatus
wxMacSearchControlEventHandler( EventHandlerCallRef
WXUNUSED(handler
) , EventRef event
, void *data
)
70 OSStatus result
= eventNotHandledErr
;
72 wxMacCarbonEvent
cEvent( event
) ;
74 ControlRef controlRef
;
75 wxSearchCtrl
* thisWindow
= (wxSearchCtrl
*) data
;
76 cEvent
.GetParameter( kEventParamDirectObject
, &controlRef
) ;
78 switch( GetEventKind( event
) )
80 case kEventSearchFieldCancelClicked
:
81 thisWindow
->HandleSearchFieldCancelHit() ;
83 case kEventSearchFieldSearchClicked
:
84 thisWindow
->HandleSearchFieldSearchHit() ;
91 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacSearchControlEventHandler
)
93 wxMacSearchFieldControl::wxMacSearchFieldControl( wxTextCtrl
*wxPeer
,
96 const wxSize
& size
, long style
) : wxMacUnicodeTextControl( wxPeer
)
98 m_font
= wxPeer
->GetFont() ;
99 m_windowStyle
= style
;
100 m_selection
.selStart
= m_selection
.selEnd
= 0;
101 Rect bounds
= wxMacGetBoundsForControl( wxPeer
, pos
, size
) ;
103 wxMacConvertNewlines10To13( &st
) ;
104 wxCFStringRef
cf(st
, m_font
.GetEncoding()) ;
106 m_valueTag
= kControlEditTextCFStringTag
;
108 OptionBits attributes
= kHISearchFieldAttributesSearchIcon
;
110 HIRect hibounds
= { { bounds
.left
, bounds
.top
}, { bounds
.right
-bounds
.left
, bounds
.bottom
-bounds
.top
} };
111 verify_noerr( HISearchFieldCreate(
118 HIViewSetVisible (m_controlRef
, true);
120 verify_noerr( SetData
<CFStringRef
>( 0, kControlEditTextCFStringTag
, cf
) ) ;
122 ::InstallControlEventHandler( m_controlRef
, GetwxMacSearchControlEventHandlerUPP(),
123 GetEventTypeCount(eventList
), eventList
, wxPeer
, NULL
);
124 SetNeedsFrame(false);
125 wxMacUnicodeTextControl::InstallEventHandlers();
128 // search field options
129 void wxMacSearchFieldControl::ShowSearchButton( bool show
)
132 OptionBits clear
= 0;
135 set
|= kHISearchFieldAttributesSearchIcon
;
139 clear
|= kHISearchFieldAttributesSearchIcon
;
141 HISearchFieldChangeAttributes( m_controlRef
, set
, clear
);
144 bool wxMacSearchFieldControl::IsSearchButtonVisible() const
146 OptionBits attributes
= 0;
147 verify_noerr( HISearchFieldGetAttributes( m_controlRef
, &attributes
) );
148 return ( attributes
& kHISearchFieldAttributesSearchIcon
) != 0;
151 void wxMacSearchFieldControl::ShowCancelButton( bool show
)
154 OptionBits clear
= 0;
157 set
|= kHISearchFieldAttributesCancel
;
161 clear
|= kHISearchFieldAttributesCancel
;
163 HISearchFieldChangeAttributes( m_controlRef
, set
, clear
);
166 bool wxMacSearchFieldControl::IsCancelButtonVisible() const
168 OptionBits attributes
= 0;
169 verify_noerr( HISearchFieldGetAttributes( m_controlRef
, &attributes
) );
170 return ( attributes
& kHISearchFieldAttributesCancel
) != 0;
173 void wxMacSearchFieldControl::SetSearchMenu( wxMenu
* menu
)
177 verify_noerr( HISearchFieldSetSearchMenu( m_controlRef
, MAC_WXHMENU(menu
->GetHMenu()) ) );
181 verify_noerr( HISearchFieldSetSearchMenu( m_controlRef
, 0 ) );
185 void wxMacSearchFieldControl::SetDescriptiveText(const wxString
& text
)
187 verify_noerr( HISearchFieldSetDescriptiveText(
189 wxCFStringRef( text
, wxFont::GetDefaultEncoding() )));
192 bool wxMacSearchFieldControl::SetFocus()
194 // NB: We have to implement SetFocus a little differently because kControlFocusNextPart
195 // leads to setting the focus on the search icon rather than the text area.
196 // We get around this by explicitly telling the control to set focus to the
199 OSStatus err
= SetKeyboardFocus( GetControlOwner( m_controlRef
), m_controlRef
, kControlEditTextPart
);
200 if ( err
== errCouldntSetFocus
)
202 SetUserFocusWindow(GetControlOwner( m_controlRef
) );
206 wxWidgetImplType
* wxWidgetImpl::CreateSearchControl( wxSearchCtrl
* wxpeer
,
207 wxWindowMac
* WXUNUSED(parent
),
208 wxWindowID
WXUNUSED(id
),
213 long WXUNUSED(extraStyle
))
215 wxMacControl
* peer
= new wxMacSearchFieldControl( wxpeer
, str
, pos
, size
, style
);
220 #endif // wxUSE_NATIVE_SEARCH_CONTROL
222 #endif // wxUSE_SEARCHCTRL