]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/srchctrl.cpp
89f2e20457ed035648a8c0bd40ace40ee6f18173
[wxWidgets.git] / src / mac / carbon / srchctrl.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/carbon/srchctrl.cpp
3 // Purpose: implements mac carbon wxSearchCtrl
4 // Author: Vince Harron
5 // Modified by:
6 // Created: 2006-02-19
7 // RCS-ID:
8 // Copyright: Vince Harron
9 // License: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "srchctrl.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #ifndef WX_PRECOMP
32 #include "wx/menu.h"
33 #endif //WX_PRECOMP
34
35 #if wxUSE_SEARCHCTRL
36
37 #include "wx/srchctrl.h"
38
39 #if USE_NATIVE_SEARCH_CONTROL
40
41 #include "wx/mac/uma.h"
42 #include "wx/mac/carbon/private/mactext.h"
43
44 BEGIN_EVENT_TABLE(wxSearchCtrl, wxSearchCtrlBase)
45 END_EVENT_TABLE()
46
47 IMPLEMENT_DYNAMIC_CLASS(wxSearchCtrl, wxSearchCtrlBase)
48
49 // ============================================================================
50 // wxMacSearchFieldControl
51 // ============================================================================
52
53 #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
54
55
56 static const EventTypeSpec eventList[] =
57 {
58 { kEventClassSearchField, kEventSearchFieldCancelClicked } ,
59 { kEventClassSearchField, kEventSearchFieldSearchClicked } ,
60 };
61
62 class wxMacSearchFieldControl : public wxMacUnicodeTextControl
63 {
64 public :
65 wxMacSearchFieldControl( wxTextCtrl *wxPeer,
66 const wxString& str,
67 const wxPoint& pos,
68 const wxSize& size, long style ) : wxMacUnicodeTextControl( wxPeer )
69 {
70 Create( wxPeer, str, pos, size, style );
71 }
72
73 // search field options
74 virtual void SetSearchButtonVisible( bool show );
75 virtual bool GetSearchButtonVisible() const;
76
77 virtual void SetCancelButtonVisible( bool show );
78 virtual bool GetCancelButtonVisible() const;
79
80 virtual void SetSearchMenu( wxMenu* menu );
81 virtual wxMenu* GetSearchMenu() const;
82 protected :
83 virtual void CreateControl( wxTextCtrl* peer, const Rect* bounds, CFStringRef crf );
84
85 private:
86 wxMenu* m_menu;
87 } ;
88
89 void wxMacSearchFieldControl::CreateControl( wxTextCtrl* /*peer*/, const Rect* bounds, CFStringRef crf )
90 {
91 OptionBits attributes = 0;
92 if ( UMAGetSystemVersion() >= 0x1040 )
93 {
94 attributes = kHISearchFieldAttributesSearchIcon;
95 }
96 HIRect hibounds = { { bounds->left, bounds->top }, { bounds->right-bounds->left, bounds->bottom-bounds->top } };
97 verify_noerr( HISearchFieldCreate(
98 &hibounds,
99 attributes,
100 0, // MenuRef
101 CFSTR("Search"),
102 &m_controlRef
103 ) );
104 HIViewSetVisible (m_controlRef, true);
105 }
106
107 // search field options
108 void wxMacSearchFieldControl::SetSearchButtonVisible( bool show )
109 {
110 if ( UMAGetSystemVersion() >= 0x1040 )
111 {
112 OptionBits set = 0;
113 OptionBits clear = 0;
114 if ( show )
115 {
116 set |= kHISearchFieldAttributesSearchIcon;
117 }
118 else
119 {
120 clear |= kHISearchFieldAttributesSearchIcon;
121 }
122 HISearchFieldChangeAttributes( m_controlRef, set, clear );
123 }
124 }
125
126 bool wxMacSearchFieldControl::GetSearchButtonVisible() const
127 {
128 OptionBits attributes = 0;
129 verify_noerr( HISearchFieldGetAttributes( m_controlRef, &attributes ) );
130 return ( attributes & kHISearchFieldAttributesSearchIcon ) != 0;
131 }
132
133 void wxMacSearchFieldControl::SetCancelButtonVisible( bool show )
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
148 bool wxMacSearchFieldControl::GetCancelButtonVisible() const
149 {
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
173 #endif
174
175 // ============================================================================
176 // implementation
177 // ============================================================================
178
179 static pascal OSStatus wxMacSearchControlEventHandler( EventHandlerCallRef handler , EventRef event , void *data )
180 {
181 OSStatus result = eventNotHandledErr ;
182
183 wxMacCarbonEvent cEvent( event ) ;
184
185 ControlRef controlRef ;
186 wxSearchCtrl* thisWindow = (wxSearchCtrl*) data ;
187 wxTextCtrl* textCtrl = wxDynamicCast( thisWindow , wxTextCtrl ) ;
188 cEvent.GetParameter( kEventParamDirectObject , &controlRef ) ;
189
190 switch( GetEventKind( event ) )
191 {
192 case kEventSearchFieldCancelClicked :
193 thisWindow->MacSearchFieldCancelHit( handler , event ) ;
194 break ;
195 case kEventSearchFieldSearchClicked :
196 thisWindow->MacSearchFieldSearchHit( handler , event ) ;
197 break ;
198 }
199
200 return result ;
201 }
202
203 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacSearchControlEventHandler )
204
205
206 // ----------------------------------------------------------------------------
207 // wxSearchCtrl creation
208 // ----------------------------------------------------------------------------
209
210 // creation
211 // --------
212
213 wxSearchCtrl::wxSearchCtrl()
214 {
215 Init();
216 }
217
218 wxSearchCtrl::wxSearchCtrl(wxWindow *parent, wxWindowID id,
219 const wxString& value,
220 const wxPoint& pos,
221 const wxSize& size,
222 long style,
223 const wxValidator& validator,
224 const wxString& name)
225 {
226 Init();
227
228 Create(parent, id, value, pos, size, style, validator, name);
229 }
230
231 void wxSearchCtrl::Init()
232 {
233 m_menu = 0;
234 }
235
236 bool wxSearchCtrl::Create(wxWindow *parent, wxWindowID id,
237 const wxString& value,
238 const wxPoint& pos,
239 const wxSize& size,
240 long style,
241 const wxValidator& validator,
242 const wxString& name)
243 {
244 if ( !wxTextCtrl::Create(parent, id, wxEmptyString, pos, size, wxBORDER_NONE | style, validator, name) )
245 {
246 return false;
247 }
248
249 EventHandlerRef searchEventHandler;
250 InstallControlEventHandler( m_peer->GetControlRef(), GetwxMacSearchControlEventHandlerUPP(),
251 GetEventTypeCount(eventList), eventList, this,
252 (EventHandlerRef *)&searchEventHandler);
253
254 return true;
255 }
256
257 wxSearchCtrl::~wxSearchCtrl()
258 {
259 delete m_menu;
260 }
261
262 wxSize wxSearchCtrl::DoGetBestSize() const
263 {
264 return wxWindow::DoGetBestSize();
265 }
266
267 // search control specific interfaces
268 // wxSearchCtrl owns menu after this call
269 void wxSearchCtrl::SetMenu( wxMenu* menu )
270 {
271 if ( menu == m_menu )
272 {
273 // no change
274 return;
275 }
276
277 if ( m_menu )
278 {
279 m_menu->SetInvokingWindow( 0 );
280 }
281
282 delete m_menu;
283 m_menu = menu;
284
285 if ( m_menu )
286 {
287 m_menu->SetInvokingWindow( this );
288 }
289
290 GetPeer()->SetSearchMenu( m_menu );
291 }
292
293 wxMenu* wxSearchCtrl::GetMenu()
294 {
295 return m_menu;
296 }
297
298 void wxSearchCtrl::SetSearchButtonVisible( bool show )
299 {
300 if ( GetSearchButtonVisible() == show )
301 {
302 // no change
303 return;
304 }
305 GetPeer()->SetSearchButtonVisible( show );
306 }
307
308 bool wxSearchCtrl::GetSearchButtonVisible() const
309 {
310 return GetPeer()->GetSearchButtonVisible();
311 }
312
313
314 void wxSearchCtrl::SetCancelButtonVisible( bool show )
315 {
316 if ( GetCancelButtonVisible() == show )
317 {
318 // no change
319 return;
320 }
321 GetPeer()->SetCancelButtonVisible( show );
322 }
323
324 bool wxSearchCtrl::GetCancelButtonVisible() const
325 {
326 return GetPeer()->GetCancelButtonVisible();
327 }
328
329 wxInt32 wxSearchCtrl::MacSearchFieldSearchHit(WXEVENTHANDLERREF WXUNUSED(handler) , WXEVENTREF WXUNUSED(event) )
330 {
331 wxCommandEvent event(wxEVT_COMMAND_SEARCHCTRL_SEARCH, m_windowId );
332 event.SetEventObject(this);
333 ProcessCommand(event);
334 return eventNotHandledErr ;
335 }
336
337 wxInt32 wxSearchCtrl::MacSearchFieldCancelHit(WXEVENTHANDLERREF WXUNUSED(handler) , WXEVENTREF WXUNUSED(event) )
338 {
339 wxCommandEvent event(wxEVT_COMMAND_SEARCHCTRL_CANCEL, m_windowId );
340 event.SetEventObject(this);
341 ProcessCommand(event);
342 return eventNotHandledErr ;
343 }
344
345
346 void wxSearchCtrl::CreatePeer(
347 const wxString& str,
348 const wxPoint& pos,
349 const wxSize& size, long style )
350 {
351 #ifdef __WXMAC_OSX__
352 #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
353 if ( UMAGetSystemVersion() >= 0x1030 )
354 {
355 m_peer = new wxMacSearchFieldControl( this , str , pos , size , style );
356 }
357 #endif
358 #endif
359 if ( !m_peer )
360 {
361 wxTextCtrl::CreatePeer( str, pos, size, style );
362 }
363 }
364
365 #endif // USE_NATIVE_SEARCH_CONTROL
366
367 #endif // wxUSE_SEARCHCTRL