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