]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/srchctrl.cpp
Fix GetNextItem for virtual wxListCtrl, and improve behavior for wxLIST_NEXT_ALL.
[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;
71protected :
72 virtual void CreateControl( wxTextCtrl* peer, const Rect* bounds, CFStringRef crf );
73
74private:
75 wxMenu* m_menu;
76} ;
77
78void wxMacSearchFieldControl::CreateControl( wxTextCtrl* /*peer*/, const Rect* bounds, CFStringRef crf )
79{
80 OptionBits attributes = 0;
68fc5c80 81#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
3f7f284d
RD
82 if ( UMAGetSystemVersion() >= 0x1040 )
83 {
ec184e32
VZ
84 attributes = kHISearchFieldAttributesSearchIcon;
85 }
68fc5c80 86#endif
3f7f284d 87 HIRect hibounds = { { bounds->left, bounds->top }, { bounds->right-bounds->left, bounds->bottom-bounds->top } };
8bc333d7 88 verify_noerr( HISearchFieldCreate(
3f7f284d
RD
89 &hibounds,
90 attributes,
91 0, // MenuRef
92 CFSTR("Search"),
93 &m_controlRef
94 ) );
8bc333d7 95 HIViewSetVisible (m_controlRef, true);
3f7f284d
RD
96}
97
98// search field options
ef0abde6 99void wxMacSearchFieldControl::ShowSearchButton( bool show )
3f7f284d 100{
68fc5c80 101#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
3f7f284d
RD
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 }
68fc5c80 116#endif
3f7f284d
RD
117}
118
ef0abde6 119bool wxMacSearchFieldControl::IsSearchButtonVisible() const
8bc333d7 120{
68fc5c80 121#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
3f7f284d
RD
122 OptionBits attributes = 0;
123 verify_noerr( HISearchFieldGetAttributes( m_controlRef, &attributes ) );
124 return ( attributes & kHISearchFieldAttributesSearchIcon ) != 0;
68fc5c80
RD
125#else
126 return false;
127#endif
3f7f284d
RD
128}
129
ef0abde6 130void wxMacSearchFieldControl::ShowCancelButton( bool show )
3f7f284d
RD
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
ef0abde6 145bool wxMacSearchFieldControl::IsCancelButtonVisible() const
8bc333d7 146{
3f7f284d
RD
147 OptionBits attributes = 0;
148 verify_noerr( HISearchFieldGetAttributes( m_controlRef, &attributes ) );
149 return ( attributes & kHISearchFieldAttributesCancel ) != 0;
150}
151
152void 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
165wxMenu* wxMacSearchFieldControl::GetSearchMenu() const
166{
167 return m_menu;
168}
169
170#endif
171
172// ============================================================================
173// implementation
174// ============================================================================
175
176static 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 ;
3f7f284d
RD
184 cEvent.GetParameter( kEventParamDirectObject , &controlRef ) ;
185
186 switch( GetEventKind( event ) )
187 {
188 case kEventSearchFieldCancelClicked :
189 thisWindow->MacSearchFieldCancelHit( handler , event ) ;
190 break ;
68fc5c80 191#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
3f7f284d
RD
192 case kEventSearchFieldSearchClicked :
193 thisWindow->MacSearchFieldSearchHit( handler , event ) ;
194 break ;
68fc5c80 195#endif
3f7f284d
RD
196 }
197
198 return result ;
199}
200
201DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacSearchControlEventHandler )
202
203
204// ----------------------------------------------------------------------------
205// wxSearchCtrl creation
206// ----------------------------------------------------------------------------
207
208// creation
209// --------
210
211wxSearchCtrl::wxSearchCtrl()
8bc333d7 212{
3f7f284d
RD
213 Init();
214}
215
216wxSearchCtrl::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
229void wxSearchCtrl::Init()
230{
231 m_menu = 0;
232}
233
234bool 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
255wxSearchCtrl::~wxSearchCtrl()
256{
257 delete m_menu;
258}
259
260wxSize wxSearchCtrl::DoGetBestSize() const
261{
698db322
KO
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);
8bc333d7 266
698db322
KO
267 return size;
268}
269
270void 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() );
3f7f284d
RD
290}
291
292// search control specific interfaces
293// wxSearchCtrl owns menu after this call
294void 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
318wxMenu* wxSearchCtrl::GetMenu()
319{
320 return m_menu;
321}
322
ef0abde6 323void wxSearchCtrl::ShowSearchButton( bool show )
3f7f284d 324{
ef0abde6 325 if ( IsSearchButtonVisible() == show )
3f7f284d
RD
326 {
327 // no change
328 return;
329 }
ef0abde6 330 GetPeer()->ShowSearchButton( show );
3f7f284d
RD
331}
332
ef0abde6 333bool wxSearchCtrl::IsSearchButtonVisible() const
3f7f284d 334{
ef0abde6 335 return GetPeer()->IsSearchButtonVisible();
3f7f284d
RD
336}
337
338
ef0abde6 339void wxSearchCtrl::ShowCancelButton( bool show )
3f7f284d 340{
ef0abde6 341 if ( IsCancelButtonVisible() == show )
3f7f284d
RD
342 {
343 // no change
344 return;
345 }
ef0abde6 346 GetPeer()->ShowCancelButton( show );
3f7f284d
RD
347}
348
ef0abde6 349bool wxSearchCtrl::IsCancelButtonVisible() const
3f7f284d 350{
ef0abde6 351 return GetPeer()->IsCancelButtonVisible();
3f7f284d
RD
352}
353
354wxInt32 wxSearchCtrl::MacSearchFieldSearchHit(WXEVENTHANDLERREF WXUNUSED(handler) , WXEVENTREF WXUNUSED(event) )
355{
c60122bf 356 wxCommandEvent event(wxEVT_COMMAND_SEARCHCTRL_SEARCH_BTN, m_windowId );
3f7f284d
RD
357 event.SetEventObject(this);
358 ProcessCommand(event);
359 return eventNotHandledErr ;
360}
361
362wxInt32 wxSearchCtrl::MacSearchFieldCancelHit(WXEVENTHANDLERREF WXUNUSED(handler) , WXEVENTREF WXUNUSED(event) )
363{
c60122bf 364 wxCommandEvent event(wxEVT_COMMAND_SEARCHCTRL_CANCEL_BTN, m_windowId );
3f7f284d
RD
365 event.SetEventObject(this);
366 ProcessCommand(event);
367 return eventNotHandledErr ;
368}
369
370
371void 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
5b43c75c 390#endif // wxUSE_NATIVE_SEARCH_CONTROL
3f7f284d
RD
391
392#endif // wxUSE_SEARCHCTRL