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