]> git.saurik.com Git - wxWidgets.git/blame - src/osx/carbon/srchctrl.cpp
Merge in from trunk r68626 - r68683
[wxWidgets.git] / src / osx / carbon / srchctrl.cpp
CommitLineData
489468fe 1///////////////////////////////////////////////////////////////////////////////
524c47aa 2// Name: src/osx/carbon/srchctrl.cpp
489468fe
SC
3// Purpose: implements mac carbon wxSearchCtrl
4// Author: Vince Harron
5// Created: 2006-02-19
6// RCS-ID: $Id$
7// Copyright: Vince Harron
526954c5 8// Licence: wxWindows licence
489468fe
SC
9///////////////////////////////////////////////////////////////////////////////
10
11// For compilers that support precompilation, includes "wx.h".
12#include "wx/wxprec.h"
13
14#ifdef __BORLANDC__
15 #pragma hdrstop
16#endif
17
18#if wxUSE_SEARCHCTRL
19
20#include "wx/srchctrl.h"
21
22#ifndef WX_PRECOMP
23 #include "wx/menu.h"
24#endif //WX_PRECOMP
25
26#if wxUSE_NATIVE_SEARCH_CONTROL
27
1f0c8f31
SC
28#include "wx/osx/uma.h"
29#include "wx/osx/carbon/private/mactext.h"
489468fe 30
489468fe
SC
31// ============================================================================
32// wxMacSearchFieldControl
33// ============================================================================
34
1e181c7a 35class wxMacSearchFieldControl : public wxMacUnicodeTextControl, public wxSearchWidgetImpl
489468fe
SC
36{
37public :
38 wxMacSearchFieldControl( wxTextCtrl *wxPeer,
39 const wxString& str,
40 const wxPoint& pos,
1e181c7a 41 const wxSize& size, long style ) ;
03647350 42
489468fe
SC
43 // search field options
44 virtual void ShowSearchButton( bool show );
45 virtual bool IsSearchButtonVisible() const;
46
47 virtual void ShowCancelButton( bool show );
48 virtual bool IsCancelButtonVisible() const;
49
50 virtual void SetSearchMenu( wxMenu* menu );
489468fe
SC
51
52 virtual void SetDescriptiveText(const wxString& text);
03647350 53
b2680ced 54 virtual bool SetFocus();
489468fe 55
489468fe 56private:
489468fe
SC
57} ;
58
1e181c7a
SC
59static const EventTypeSpec eventList[] =
60{
61 { kEventClassSearchField, kEventSearchFieldCancelClicked } ,
62 { kEventClassSearchField, kEventSearchFieldSearchClicked } ,
63};
64
65// ============================================================================
66// implementation
67// ============================================================================
68
a4fec5b4 69static pascal OSStatus wxMacSearchControlEventHandler( EventHandlerCallRef WXUNUSED(handler) , EventRef event , void *data )
1e181c7a
SC
70{
71 OSStatus result = eventNotHandledErr ;
72
73 wxMacCarbonEvent cEvent( event ) ;
74
75 ControlRef controlRef ;
76 wxSearchCtrl* thisWindow = (wxSearchCtrl*) data ;
77 cEvent.GetParameter( kEventParamDirectObject , &controlRef ) ;
78
79 switch( GetEventKind( event ) )
80 {
81 case kEventSearchFieldCancelClicked :
82 thisWindow->HandleSearchFieldCancelHit() ;
83 break ;
84 case kEventSearchFieldSearchClicked :
85 thisWindow->HandleSearchFieldSearchHit() ;
86 break ;
87 }
88
89 return result ;
90}
91
92DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacSearchControlEventHandler )
93
94wxMacSearchFieldControl::wxMacSearchFieldControl( wxTextCtrl *wxPeer,
95 const wxString& str,
96 const wxPoint& pos,
97 const wxSize& size, long style ) : wxMacUnicodeTextControl( wxPeer )
489468fe 98{
1e181c7a
SC
99 m_font = wxPeer->GetFont() ;
100 m_windowStyle = style ;
101 m_selection.selStart = m_selection.selEnd = 0;
102 Rect bounds = wxMacGetBoundsForControl( wxPeer , pos , size ) ;
103 wxString st = str ;
104 wxMacConvertNewlines10To13( &st ) ;
105 wxCFStringRef cf(st , m_font.GetEncoding()) ;
106
107 m_valueTag = kControlEditTextCFStringTag ;
108
489468fe
SC
109 OptionBits attributes = kHISearchFieldAttributesSearchIcon;
110
1e181c7a 111 HIRect hibounds = { { bounds.left, bounds.top }, { bounds.right-bounds.left, bounds.bottom-bounds.top } };
489468fe
SC
112 verify_noerr( HISearchFieldCreate(
113 &hibounds,
114 attributes,
115 0, // MenuRef
1e181c7a 116 CFSTR(""),
489468fe
SC
117 &m_controlRef
118 ) );
119 HIViewSetVisible (m_controlRef, true);
1e181c7a
SC
120
121 verify_noerr( SetData<CFStringRef>( 0, kControlEditTextCFStringTag , cf ) ) ;
122
123 ::InstallControlEventHandler( m_controlRef, GetwxMacSearchControlEventHandlerUPP(),
124 GetEventTypeCount(eventList), eventList, wxPeer, NULL);
2ea81701 125 SetNeedsFrame(false);
1e181c7a 126 wxMacUnicodeTextControl::InstallEventHandlers();
489468fe
SC
127}
128
129// search field options
130void wxMacSearchFieldControl::ShowSearchButton( bool show )
131{
132 OptionBits set = 0;
133 OptionBits clear = 0;
134 if ( show )
135 {
136 set |= kHISearchFieldAttributesSearchIcon;
137 }
138 else
139 {
140 clear |= kHISearchFieldAttributesSearchIcon;
141 }
142 HISearchFieldChangeAttributes( m_controlRef, set, clear );
143}
144
145bool wxMacSearchFieldControl::IsSearchButtonVisible() const
146{
147 OptionBits attributes = 0;
148 verify_noerr( HISearchFieldGetAttributes( m_controlRef, &attributes ) );
149 return ( attributes & kHISearchFieldAttributesSearchIcon ) != 0;
150}
151
152void wxMacSearchFieldControl::ShowCancelButton( bool show )
153{
154 OptionBits set = 0;
155 OptionBits clear = 0;
156 if ( show )
157 {
158 set |= kHISearchFieldAttributesCancel;
159 }
160 else
161 {
162 clear |= kHISearchFieldAttributesCancel;
163 }
164 HISearchFieldChangeAttributes( m_controlRef, set, clear );
165}
166
167bool wxMacSearchFieldControl::IsCancelButtonVisible() const
168{
169 OptionBits attributes = 0;
170 verify_noerr( HISearchFieldGetAttributes( m_controlRef, &attributes ) );
171 return ( attributes & kHISearchFieldAttributesCancel ) != 0;
172}
173
174void wxMacSearchFieldControl::SetSearchMenu( wxMenu* menu )
175{
1e181c7a 176 if ( menu )
489468fe 177 {
1e181c7a 178 verify_noerr( HISearchFieldSetSearchMenu( m_controlRef, MAC_WXHMENU(menu->GetHMenu()) ) );
489468fe
SC
179 }
180 else
181 {
182 verify_noerr( HISearchFieldSetSearchMenu( m_controlRef, 0 ) );
183 }
184}
185
489468fe
SC
186void wxMacSearchFieldControl::SetDescriptiveText(const wxString& text)
187{
188 verify_noerr( HISearchFieldSetDescriptiveText(
189 m_controlRef,
190 wxCFStringRef( text, wxFont::GetDefaultEncoding() )));
191}
192
b2680ced
SC
193bool wxMacSearchFieldControl::SetFocus()
194{
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
198 // text area.
199
200 OSStatus err = SetKeyboardFocus( GetControlOwner( m_controlRef ), m_controlRef, kControlEditTextPart );
201 if ( err == errCouldntSetFocus )
202 return false ;
203 SetUserFocusWindow(GetControlOwner( m_controlRef ) );
204 return true;
205}
206
11f87a38 207wxWidgetImplType* wxWidgetImpl::CreateSearchControl( wxSearchCtrl* wxpeer,
03647350
VZ
208 wxWindowMac* WXUNUSED(parent),
209 wxWindowID WXUNUSED(id),
1e181c7a 210 const wxString& str,
03647350 211 const wxPoint& pos,
1e181c7a 212 const wxSize& size,
03647350 213 long style,
a4fec5b4 214 long WXUNUSED(extraStyle))
489468fe 215{
1e181c7a 216 wxMacControl* peer = new wxMacSearchFieldControl( wxpeer , str , pos , size , style );
489468fe 217
1e181c7a 218 return peer;
489468fe
SC
219}
220
221#endif // wxUSE_NATIVE_SEARCH_CONTROL
222
223#endif // wxUSE_SEARCHCTRL