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