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