]>
Commit | Line | Data |
---|---|---|
489468fe SC |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: src/mac/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/mac/uma.h" | |
29 | #include "wx/mac/carbon/private/mactext.h" | |
30 | ||
31 | BEGIN_EVENT_TABLE(wxSearchCtrl, wxSearchCtrlBase) | |
32 | END_EVENT_TABLE() | |
33 | ||
34 | IMPLEMENT_DYNAMIC_CLASS(wxSearchCtrl, wxSearchCtrlBase) | |
35 | ||
36 | // ============================================================================ | |
37 | // wxMacSearchFieldControl | |
38 | // ============================================================================ | |
39 | ||
40 | static const EventTypeSpec eventList[] = | |
41 | { | |
42 | { kEventClassSearchField, kEventSearchFieldCancelClicked } , | |
43 | { kEventClassSearchField, kEventSearchFieldSearchClicked } , | |
44 | }; | |
45 | ||
46 | class wxMacSearchFieldControl : public wxMacUnicodeTextControl | |
47 | { | |
48 | public : | |
49 | wxMacSearchFieldControl( wxTextCtrl *wxPeer, | |
50 | const wxString& str, | |
51 | const wxPoint& pos, | |
52 | const wxSize& size, long style ) : wxMacUnicodeTextControl( wxPeer ) | |
53 | { | |
54 | Create( wxPeer, str, pos, size, style ); | |
55 | } | |
56 | ||
57 | // search field options | |
58 | virtual void ShowSearchButton( bool show ); | |
59 | virtual bool IsSearchButtonVisible() const; | |
60 | ||
61 | virtual void ShowCancelButton( bool show ); | |
62 | virtual bool IsCancelButtonVisible() const; | |
63 | ||
64 | virtual void SetSearchMenu( wxMenu* menu ); | |
65 | virtual wxMenu* GetSearchMenu() const; | |
66 | ||
67 | virtual void SetDescriptiveText(const wxString& text); | |
68 | virtual wxString GetDescriptiveText() const; | |
69 | ||
70 | protected : | |
71 | virtual void CreateControl( wxTextCtrl* peer, const Rect* bounds, CFStringRef crf ); | |
72 | ||
73 | private: | |
74 | wxMenu* m_menu; | |
75 | } ; | |
76 | ||
77 | void wxMacSearchFieldControl::CreateControl(wxTextCtrl* WXUNUSED(peer), | |
78 | const Rect* bounds, | |
79 | CFStringRef WXUNUSED(crf)) | |
80 | { | |
81 | OptionBits attributes = kHISearchFieldAttributesSearchIcon; | |
82 | ||
83 | HIRect hibounds = { { bounds->left, bounds->top }, { bounds->right-bounds->left, bounds->bottom-bounds->top } }; | |
84 | verify_noerr( HISearchFieldCreate( | |
85 | &hibounds, | |
86 | attributes, | |
87 | 0, // MenuRef | |
88 | CFSTR("Search"), | |
89 | &m_controlRef | |
90 | ) ); | |
91 | HIViewSetVisible (m_controlRef, true); | |
92 | } | |
93 | ||
94 | // search field options | |
95 | void wxMacSearchFieldControl::ShowSearchButton( bool show ) | |
96 | { | |
97 | OptionBits set = 0; | |
98 | OptionBits clear = 0; | |
99 | if ( show ) | |
100 | { | |
101 | set |= kHISearchFieldAttributesSearchIcon; | |
102 | } | |
103 | else | |
104 | { | |
105 | clear |= kHISearchFieldAttributesSearchIcon; | |
106 | } | |
107 | HISearchFieldChangeAttributes( m_controlRef, set, clear ); | |
108 | } | |
109 | ||
110 | bool wxMacSearchFieldControl::IsSearchButtonVisible() const | |
111 | { | |
112 | OptionBits attributes = 0; | |
113 | verify_noerr( HISearchFieldGetAttributes( m_controlRef, &attributes ) ); | |
114 | return ( attributes & kHISearchFieldAttributesSearchIcon ) != 0; | |
115 | } | |
116 | ||
117 | void wxMacSearchFieldControl::ShowCancelButton( bool show ) | |
118 | { | |
119 | OptionBits set = 0; | |
120 | OptionBits clear = 0; | |
121 | if ( show ) | |
122 | { | |
123 | set |= kHISearchFieldAttributesCancel; | |
124 | } | |
125 | else | |
126 | { | |
127 | clear |= kHISearchFieldAttributesCancel; | |
128 | } | |
129 | HISearchFieldChangeAttributes( m_controlRef, set, clear ); | |
130 | } | |
131 | ||
132 | bool wxMacSearchFieldControl::IsCancelButtonVisible() const | |
133 | { | |
134 | OptionBits attributes = 0; | |
135 | verify_noerr( HISearchFieldGetAttributes( m_controlRef, &attributes ) ); | |
136 | return ( attributes & kHISearchFieldAttributesCancel ) != 0; | |
137 | } | |
138 | ||
139 | void wxMacSearchFieldControl::SetSearchMenu( wxMenu* menu ) | |
140 | { | |
141 | m_menu = menu; | |
142 | if ( m_menu ) | |
143 | { | |
144 | verify_noerr( HISearchFieldSetSearchMenu( m_controlRef, MAC_WXHMENU(m_menu->GetHMenu()) ) ); | |
145 | } | |
146 | else | |
147 | { | |
148 | verify_noerr( HISearchFieldSetSearchMenu( m_controlRef, 0 ) ); | |
149 | } | |
150 | } | |
151 | ||
152 | wxMenu* wxMacSearchFieldControl::GetSearchMenu() const | |
153 | { | |
154 | return m_menu; | |
155 | } | |
156 | ||
157 | ||
158 | void wxMacSearchFieldControl::SetDescriptiveText(const wxString& text) | |
159 | { | |
160 | verify_noerr( HISearchFieldSetDescriptiveText( | |
161 | m_controlRef, | |
162 | wxCFStringRef( text, wxFont::GetDefaultEncoding() ))); | |
163 | } | |
164 | ||
165 | wxString wxMacSearchFieldControl::GetDescriptiveText() const | |
166 | { | |
167 | CFStringRef cfStr; | |
168 | verify_noerr( HISearchFieldCopyDescriptiveText( m_controlRef, &cfStr )); | |
169 | if ( cfStr ) | |
170 | { | |
171 | return wxCFStringRef(cfStr).AsString(); | |
172 | } | |
173 | else | |
174 | { | |
175 | return wxEmptyString; | |
176 | } | |
177 | } | |
178 | ||
179 | // ============================================================================ | |
180 | // implementation | |
181 | // ============================================================================ | |
182 | ||
183 | static pascal OSStatus wxMacSearchControlEventHandler( EventHandlerCallRef handler , EventRef event , void *data ) | |
184 | { | |
185 | OSStatus result = eventNotHandledErr ; | |
186 | ||
187 | wxMacCarbonEvent cEvent( event ) ; | |
188 | ||
189 | ControlRef controlRef ; | |
190 | wxSearchCtrl* thisWindow = (wxSearchCtrl*) data ; | |
191 | cEvent.GetParameter( kEventParamDirectObject , &controlRef ) ; | |
192 | ||
193 | switch( GetEventKind( event ) ) | |
194 | { | |
195 | case kEventSearchFieldCancelClicked : | |
196 | thisWindow->MacSearchFieldCancelHit( handler , event ) ; | |
197 | break ; | |
198 | case kEventSearchFieldSearchClicked : | |
199 | thisWindow->MacSearchFieldSearchHit( handler , event ) ; | |
200 | break ; | |
201 | } | |
202 | ||
203 | return result ; | |
204 | } | |
205 | ||
206 | DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacSearchControlEventHandler ) | |
207 | ||
208 | ||
209 | // ---------------------------------------------------------------------------- | |
210 | // wxSearchCtrl creation | |
211 | // ---------------------------------------------------------------------------- | |
212 | ||
213 | // creation | |
214 | // -------- | |
215 | ||
216 | wxSearchCtrl::wxSearchCtrl() | |
217 | { | |
218 | Init(); | |
219 | } | |
220 | ||
221 | wxSearchCtrl::wxSearchCtrl(wxWindow *parent, wxWindowID id, | |
222 | const wxString& value, | |
223 | const wxPoint& pos, | |
224 | const wxSize& size, | |
225 | long style, | |
226 | const wxValidator& validator, | |
227 | const wxString& name) | |
228 | { | |
229 | Init(); | |
230 | ||
231 | Create(parent, id, value, pos, size, style, validator, name); | |
232 | } | |
233 | ||
234 | void wxSearchCtrl::Init() | |
235 | { | |
236 | m_menu = 0; | |
237 | } | |
238 | ||
239 | bool wxSearchCtrl::Create(wxWindow *parent, wxWindowID id, | |
240 | const wxString& value, | |
241 | const wxPoint& pos, | |
242 | const wxSize& size, | |
243 | long style, | |
244 | const wxValidator& validator, | |
245 | const wxString& name) | |
246 | { | |
247 | if ( !wxTextCtrl::Create(parent, id, wxEmptyString, pos, size, wxBORDER_NONE | style, validator, name) ) | |
248 | { | |
249 | return false; | |
250 | } | |
251 | ||
252 | EventHandlerRef searchEventHandler; | |
253 | InstallControlEventHandler( m_peer->GetControlRef(), GetwxMacSearchControlEventHandlerUPP(), | |
254 | GetEventTypeCount(eventList), eventList, this, | |
255 | (EventHandlerRef *)&searchEventHandler); | |
256 | ||
257 | SetValue(value); | |
258 | ||
259 | return true; | |
260 | } | |
261 | ||
262 | wxSearchCtrl::~wxSearchCtrl() | |
263 | { | |
264 | delete m_menu; | |
265 | } | |
266 | ||
267 | wxSize wxSearchCtrl::DoGetBestSize() const | |
268 | { | |
269 | wxSize size = wxWindow::DoGetBestSize(); | |
270 | // it seems to return a default width of about 16, which is way too small here. | |
271 | if (size.GetWidth() < 100) | |
272 | size.SetWidth(100); | |
273 | ||
274 | return size; | |
275 | } | |
276 | ||
277 | void wxSearchCtrl::SetFocus() | |
278 | { | |
279 | // NB: We have to implement SetFocus a little differently because kControlFocusNextPart | |
280 | // leads to setting the focus on the search icon rather than the text area. | |
281 | // We get around this by explicitly telling the control to set focus to the | |
282 | // text area. | |
283 | if ( !AcceptsFocus() ) | |
284 | return ; | |
285 | ||
286 | wxWindow* former = FindFocus() ; | |
287 | if ( former == this ) | |
288 | return ; | |
289 | ||
290 | // as we cannot rely on the control features to find out whether we are in full keyboard mode, | |
291 | // we can only leave in case of an error | |
292 | OSStatus err = m_peer->SetFocus( kControlEditTextPart ) ; | |
293 | if ( err == errCouldntSetFocus ) | |
294 | return ; | |
295 | ||
296 | SetUserFocusWindow( (WindowRef)MacGetTopLevelWindowRef() ); | |
297 | } | |
298 | ||
299 | // search control specific interfaces | |
300 | // wxSearchCtrl owns menu after this call | |
301 | void wxSearchCtrl::SetMenu( wxMenu* menu ) | |
302 | { | |
303 | if ( menu == m_menu ) | |
304 | { | |
305 | // no change | |
306 | return; | |
307 | } | |
308 | ||
309 | if ( m_menu ) | |
310 | { | |
311 | m_menu->SetInvokingWindow( 0 ); | |
312 | } | |
313 | ||
314 | delete m_menu; | |
315 | m_menu = menu; | |
316 | ||
317 | if ( m_menu ) | |
318 | { | |
319 | m_menu->SetInvokingWindow( this ); | |
320 | } | |
321 | ||
322 | GetPeer()->SetSearchMenu( m_menu ); | |
323 | } | |
324 | ||
325 | wxMenu* wxSearchCtrl::GetMenu() | |
326 | { | |
327 | return m_menu; | |
328 | } | |
329 | ||
330 | void wxSearchCtrl::ShowSearchButton( bool show ) | |
331 | { | |
332 | if ( IsSearchButtonVisible() == show ) | |
333 | { | |
334 | // no change | |
335 | return; | |
336 | } | |
337 | GetPeer()->ShowSearchButton( show ); | |
338 | } | |
339 | ||
340 | bool wxSearchCtrl::IsSearchButtonVisible() const | |
341 | { | |
342 | return GetPeer()->IsSearchButtonVisible(); | |
343 | } | |
344 | ||
345 | ||
346 | void wxSearchCtrl::ShowCancelButton( bool show ) | |
347 | { | |
348 | if ( IsCancelButtonVisible() == show ) | |
349 | { | |
350 | // no change | |
351 | return; | |
352 | } | |
353 | GetPeer()->ShowCancelButton( show ); | |
354 | } | |
355 | ||
356 | bool wxSearchCtrl::IsCancelButtonVisible() const | |
357 | { | |
358 | return GetPeer()->IsCancelButtonVisible(); | |
359 | } | |
360 | ||
361 | void wxSearchCtrl::SetDescriptiveText(const wxString& text) | |
362 | { | |
363 | GetPeer()->SetDescriptiveText(text); | |
364 | } | |
365 | ||
366 | wxString wxSearchCtrl::GetDescriptiveText() const | |
367 | { | |
368 | return GetPeer()->GetDescriptiveText(); | |
369 | } | |
370 | ||
371 | wxInt32 wxSearchCtrl::MacSearchFieldSearchHit(WXEVENTHANDLERREF WXUNUSED(handler) , WXEVENTREF WXUNUSED(event) ) | |
372 | { | |
373 | wxCommandEvent event(wxEVT_COMMAND_SEARCHCTRL_SEARCH_BTN, m_windowId ); | |
374 | event.SetEventObject(this); | |
375 | ProcessCommand(event); | |
376 | return eventNotHandledErr ; | |
377 | } | |
378 | ||
379 | wxInt32 wxSearchCtrl::MacSearchFieldCancelHit(WXEVENTHANDLERREF WXUNUSED(handler) , WXEVENTREF WXUNUSED(event) ) | |
380 | { | |
381 | wxCommandEvent event(wxEVT_COMMAND_SEARCHCTRL_CANCEL_BTN, m_windowId ); | |
382 | event.SetEventObject(this); | |
383 | ProcessCommand(event); | |
384 | return eventNotHandledErr ; | |
385 | } | |
386 | ||
387 | ||
388 | void wxSearchCtrl::CreatePeer( | |
389 | const wxString& str, | |
390 | const wxPoint& pos, | |
391 | const wxSize& size, long style ) | |
392 | { | |
393 | m_peer = new wxMacSearchFieldControl( this , str , pos , size , style ); | |
394 | } | |
395 | ||
396 | #endif // wxUSE_NATIVE_SEARCH_CONTROL | |
397 | ||
398 | #endif // wxUSE_SEARCHCTRL |