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