]> git.saurik.com Git - wxWidgets.git/blob - src/osx/carbon/srchctrl.cpp
fixing focus, fixes #9985
[wxWidgets.git] / src / osx / carbon / srchctrl.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/carbon/srchctrl.cpp
3 // Purpose: implements mac carbon wxSearchCtrl
4 // Author: Vince Harron
5 // Created: 2006-02-19
6 // RCS-ID: $Id$
7 // Copyright: Vince Harron
8 // License: wxWindows licence
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
28 #include "wx/osx/uma.h"
29 #include "wx/osx/carbon/private/mactext.h"
30
31 // ============================================================================
32 // wxMacSearchFieldControl
33 // ============================================================================
34
35 class wxMacSearchFieldControl : public wxMacUnicodeTextControl, public wxSearchWidgetImpl
36 {
37 public :
38 wxMacSearchFieldControl( wxTextCtrl *wxPeer,
39 const wxString& str,
40 const wxPoint& pos,
41 const wxSize& size, long style ) ;
42
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 );
51
52 virtual void SetDescriptiveText(const wxString& text);
53
54 virtual bool SetFocus();
55
56 private:
57 } ;
58
59 static const EventTypeSpec eventList[] =
60 {
61 { kEventClassSearchField, kEventSearchFieldCancelClicked } ,
62 { kEventClassSearchField, kEventSearchFieldSearchClicked } ,
63 };
64
65 // ============================================================================
66 // implementation
67 // ============================================================================
68
69 static pascal OSStatus wxMacSearchControlEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
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
92 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacSearchControlEventHandler )
93
94 wxMacSearchFieldControl::wxMacSearchFieldControl( wxTextCtrl *wxPeer,
95 const wxString& str,
96 const wxPoint& pos,
97 const wxSize& size, long style ) : wxMacUnicodeTextControl( wxPeer )
98 {
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
109 OptionBits attributes = kHISearchFieldAttributesSearchIcon;
110
111 HIRect hibounds = { { bounds.left, bounds.top }, { bounds.right-bounds.left, bounds.bottom-bounds.top } };
112 verify_noerr( HISearchFieldCreate(
113 &hibounds,
114 attributes,
115 0, // MenuRef
116 CFSTR(""),
117 &m_controlRef
118 ) );
119 HIViewSetVisible (m_controlRef, true);
120
121 verify_noerr( SetData<CFStringRef>( 0, kControlEditTextCFStringTag , cf ) ) ;
122
123 ::InstallControlEventHandler( m_controlRef, GetwxMacSearchControlEventHandlerUPP(),
124 GetEventTypeCount(eventList), eventList, wxPeer, NULL);
125 wxMacUnicodeTextControl::InstallEventHandlers();
126 }
127
128 // search field options
129 void wxMacSearchFieldControl::ShowSearchButton( bool show )
130 {
131 OptionBits set = 0;
132 OptionBits clear = 0;
133 if ( show )
134 {
135 set |= kHISearchFieldAttributesSearchIcon;
136 }
137 else
138 {
139 clear |= kHISearchFieldAttributesSearchIcon;
140 }
141 HISearchFieldChangeAttributes( m_controlRef, set, clear );
142 }
143
144 bool wxMacSearchFieldControl::IsSearchButtonVisible() const
145 {
146 OptionBits attributes = 0;
147 verify_noerr( HISearchFieldGetAttributes( m_controlRef, &attributes ) );
148 return ( attributes & kHISearchFieldAttributesSearchIcon ) != 0;
149 }
150
151 void wxMacSearchFieldControl::ShowCancelButton( bool show )
152 {
153 OptionBits set = 0;
154 OptionBits clear = 0;
155 if ( show )
156 {
157 set |= kHISearchFieldAttributesCancel;
158 }
159 else
160 {
161 clear |= kHISearchFieldAttributesCancel;
162 }
163 HISearchFieldChangeAttributes( m_controlRef, set, clear );
164 }
165
166 bool wxMacSearchFieldControl::IsCancelButtonVisible() const
167 {
168 OptionBits attributes = 0;
169 verify_noerr( HISearchFieldGetAttributes( m_controlRef, &attributes ) );
170 return ( attributes & kHISearchFieldAttributesCancel ) != 0;
171 }
172
173 void wxMacSearchFieldControl::SetSearchMenu( wxMenu* menu )
174 {
175 if ( menu )
176 {
177 verify_noerr( HISearchFieldSetSearchMenu( m_controlRef, MAC_WXHMENU(menu->GetHMenu()) ) );
178 }
179 else
180 {
181 verify_noerr( HISearchFieldSetSearchMenu( m_controlRef, 0 ) );
182 }
183 }
184
185 void wxMacSearchFieldControl::SetDescriptiveText(const wxString& text)
186 {
187 verify_noerr( HISearchFieldSetDescriptiveText(
188 m_controlRef,
189 wxCFStringRef( text, wxFont::GetDefaultEncoding() )));
190 }
191
192 bool wxMacSearchFieldControl::SetFocus()
193 {
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
197 // text area.
198
199 OSStatus err = SetKeyboardFocus( GetControlOwner( m_controlRef ), m_controlRef, kControlEditTextPart );
200 if ( err == errCouldntSetFocus )
201 return false ;
202 SetUserFocusWindow(GetControlOwner( m_controlRef ) );
203 return true;
204 }
205
206 wxWidgetImplType* wxWidgetImpl::CreateSearchControl( wxTextCtrl* wxpeer,
207 wxWindowMac* parent,
208 wxWindowID id,
209 const wxString& str,
210 const wxPoint& pos,
211 const wxSize& size,
212 long style,
213 long extraStyle)
214 {
215 wxMacControl* peer = new wxMacSearchFieldControl( wxpeer , str , pos , size , style );
216
217 return peer;
218 }
219
220 #endif // wxUSE_NATIVE_SEARCH_CONTROL
221
222 #endif // wxUSE_SEARCHCTRL