]>
Commit | Line | Data |
---|---|---|
489468fe | 1 | /////////////////////////////////////////////////////////////////////////////// |
524c47aa | 2 | // Name: src/osx/carbon/srchctrl.cpp |
489468fe SC |
3 | // Purpose: implements mac carbon wxSearchCtrl |
4 | // Author: Vince Harron | |
5 | // Created: 2006-02-19 | |
489468fe | 6 | // Copyright: Vince Harron |
526954c5 | 7 | // Licence: wxWindows licence |
489468fe SC |
8 | /////////////////////////////////////////////////////////////////////////////// |
9 | ||
10 | // For compilers that support precompilation, includes "wx.h". | |
11 | #include "wx/wxprec.h" | |
12 | ||
13 | #ifdef __BORLANDC__ | |
14 | #pragma hdrstop | |
15 | #endif | |
16 | ||
17 | #if wxUSE_SEARCHCTRL | |
18 | ||
19 | #include "wx/srchctrl.h" | |
20 | ||
21 | #ifndef WX_PRECOMP | |
22 | #include "wx/menu.h" | |
23 | #endif //WX_PRECOMP | |
24 | ||
25 | #if wxUSE_NATIVE_SEARCH_CONTROL | |
26 | ||
1f0c8f31 SC |
27 | #include "wx/osx/uma.h" |
28 | #include "wx/osx/carbon/private/mactext.h" | |
489468fe | 29 | |
489468fe SC |
30 | // ============================================================================ |
31 | // wxMacSearchFieldControl | |
32 | // ============================================================================ | |
33 | ||
1e181c7a | 34 | class wxMacSearchFieldControl : public wxMacUnicodeTextControl, public wxSearchWidgetImpl |
489468fe SC |
35 | { |
36 | public : | |
37 | wxMacSearchFieldControl( wxTextCtrl *wxPeer, | |
38 | const wxString& str, | |
39 | const wxPoint& pos, | |
1e181c7a | 40 | const wxSize& size, long style ) ; |
03647350 | 41 | |
489468fe SC |
42 | // search field options |
43 | virtual void ShowSearchButton( bool show ); | |
44 | virtual bool IsSearchButtonVisible() const; | |
45 | ||
46 | virtual void ShowCancelButton( bool show ); | |
47 | virtual bool IsCancelButtonVisible() const; | |
48 | ||
49 | virtual void SetSearchMenu( wxMenu* menu ); | |
489468fe SC |
50 | |
51 | virtual void SetDescriptiveText(const wxString& text); | |
03647350 | 52 | |
b2680ced | 53 | virtual bool SetFocus(); |
489468fe | 54 | |
489468fe | 55 | private: |
489468fe SC |
56 | } ; |
57 | ||
1e181c7a SC |
58 | static const EventTypeSpec eventList[] = |
59 | { | |
60 | { kEventClassSearchField, kEventSearchFieldCancelClicked } , | |
61 | { kEventClassSearchField, kEventSearchFieldSearchClicked } , | |
62 | }; | |
63 | ||
64 | // ============================================================================ | |
65 | // implementation | |
66 | // ============================================================================ | |
67 | ||
a4fec5b4 | 68 | static pascal OSStatus wxMacSearchControlEventHandler( EventHandlerCallRef WXUNUSED(handler) , EventRef event , void *data ) |
1e181c7a SC |
69 | { |
70 | OSStatus result = eventNotHandledErr ; | |
71 | ||
72 | wxMacCarbonEvent cEvent( event ) ; | |
73 | ||
74 | ControlRef controlRef ; | |
75 | wxSearchCtrl* thisWindow = (wxSearchCtrl*) data ; | |
76 | cEvent.GetParameter( kEventParamDirectObject , &controlRef ) ; | |
77 | ||
78 | switch( GetEventKind( event ) ) | |
79 | { | |
80 | case kEventSearchFieldCancelClicked : | |
81 | thisWindow->HandleSearchFieldCancelHit() ; | |
82 | break ; | |
83 | case kEventSearchFieldSearchClicked : | |
84 | thisWindow->HandleSearchFieldSearchHit() ; | |
85 | break ; | |
86 | } | |
87 | ||
88 | return result ; | |
89 | } | |
90 | ||
91 | DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacSearchControlEventHandler ) | |
92 | ||
93 | wxMacSearchFieldControl::wxMacSearchFieldControl( wxTextCtrl *wxPeer, | |
94 | const wxString& str, | |
95 | const wxPoint& pos, | |
96 | const wxSize& size, long style ) : wxMacUnicodeTextControl( wxPeer ) | |
489468fe | 97 | { |
1e181c7a SC |
98 | m_font = wxPeer->GetFont() ; |
99 | m_windowStyle = style ; | |
100 | m_selection.selStart = m_selection.selEnd = 0; | |
101 | Rect bounds = wxMacGetBoundsForControl( wxPeer , pos , size ) ; | |
102 | wxString st = str ; | |
103 | wxMacConvertNewlines10To13( &st ) ; | |
104 | wxCFStringRef cf(st , m_font.GetEncoding()) ; | |
105 | ||
106 | m_valueTag = kControlEditTextCFStringTag ; | |
107 | ||
489468fe SC |
108 | OptionBits attributes = kHISearchFieldAttributesSearchIcon; |
109 | ||
1e181c7a | 110 | HIRect hibounds = { { bounds.left, bounds.top }, { bounds.right-bounds.left, bounds.bottom-bounds.top } }; |
489468fe SC |
111 | verify_noerr( HISearchFieldCreate( |
112 | &hibounds, | |
113 | attributes, | |
114 | 0, // MenuRef | |
1e181c7a | 115 | CFSTR(""), |
489468fe SC |
116 | &m_controlRef |
117 | ) ); | |
118 | HIViewSetVisible (m_controlRef, true); | |
1e181c7a SC |
119 | |
120 | verify_noerr( SetData<CFStringRef>( 0, kControlEditTextCFStringTag , cf ) ) ; | |
121 | ||
122 | ::InstallControlEventHandler( m_controlRef, GetwxMacSearchControlEventHandlerUPP(), | |
123 | GetEventTypeCount(eventList), eventList, wxPeer, NULL); | |
2ea81701 | 124 | SetNeedsFrame(false); |
1e181c7a | 125 | wxMacUnicodeTextControl::InstallEventHandlers(); |
489468fe SC |
126 | } |
127 | ||
128 | // search field options | |
129 | void wxMacSearchFieldControl::ShowSearchButton( bool show ) | |
130 | { | |
131 | OptionBits set = 0; | |
132 | OptionBits clear = 0; | |
133 | if ( show ) | |
134 | { | |
135 | set |= kHISearchFieldAttributesSearchIcon; | |
136 | } | |
137 | else | |
138 | { | |
139 | clear |= kHISearchFieldAttributesSearchIcon; | |
140 | } | |
141 | HISearchFieldChangeAttributes( m_controlRef, set, clear ); | |
142 | } | |
143 | ||
144 | bool wxMacSearchFieldControl::IsSearchButtonVisible() const | |
145 | { | |
146 | OptionBits attributes = 0; | |
147 | verify_noerr( HISearchFieldGetAttributes( m_controlRef, &attributes ) ); | |
148 | return ( attributes & kHISearchFieldAttributesSearchIcon ) != 0; | |
149 | } | |
150 | ||
151 | void wxMacSearchFieldControl::ShowCancelButton( bool show ) | |
152 | { | |
153 | OptionBits set = 0; | |
154 | OptionBits clear = 0; | |
155 | if ( show ) | |
156 | { | |
157 | set |= kHISearchFieldAttributesCancel; | |
158 | } | |
159 | else | |
160 | { | |
161 | clear |= kHISearchFieldAttributesCancel; | |
162 | } | |
163 | HISearchFieldChangeAttributes( m_controlRef, set, clear ); | |
164 | } | |
165 | ||
166 | bool wxMacSearchFieldControl::IsCancelButtonVisible() const | |
167 | { | |
168 | OptionBits attributes = 0; | |
169 | verify_noerr( HISearchFieldGetAttributes( m_controlRef, &attributes ) ); | |
170 | return ( attributes & kHISearchFieldAttributesCancel ) != 0; | |
171 | } | |
172 | ||
173 | void wxMacSearchFieldControl::SetSearchMenu( wxMenu* menu ) | |
174 | { | |
1e181c7a | 175 | if ( menu ) |
489468fe | 176 | { |
1e181c7a | 177 | verify_noerr( HISearchFieldSetSearchMenu( m_controlRef, MAC_WXHMENU(menu->GetHMenu()) ) ); |
489468fe SC |
178 | } |
179 | else | |
180 | { | |
181 | verify_noerr( HISearchFieldSetSearchMenu( m_controlRef, 0 ) ); | |
182 | } | |
183 | } | |
184 | ||
489468fe SC |
185 | void wxMacSearchFieldControl::SetDescriptiveText(const wxString& text) |
186 | { | |
187 | verify_noerr( HISearchFieldSetDescriptiveText( | |
188 | m_controlRef, | |
189 | wxCFStringRef( text, wxFont::GetDefaultEncoding() ))); | |
190 | } | |
191 | ||
b2680ced SC |
192 | bool wxMacSearchFieldControl::SetFocus() |
193 | { | |
194 | // NB: We have to implement SetFocus a little differently because kControlFocusNextPart | |
195 | // leads to setting the focus on the search icon rather than the text area. | |
196 | // We get around this by explicitly telling the control to set focus to the | |
197 | // text area. | |
198 | ||
199 | OSStatus err = SetKeyboardFocus( GetControlOwner( m_controlRef ), m_controlRef, kControlEditTextPart ); | |
200 | if ( err == errCouldntSetFocus ) | |
201 | return false ; | |
202 | SetUserFocusWindow(GetControlOwner( m_controlRef ) ); | |
203 | return true; | |
204 | } | |
205 | ||
11f87a38 | 206 | wxWidgetImplType* wxWidgetImpl::CreateSearchControl( wxSearchCtrl* wxpeer, |
03647350 VZ |
207 | wxWindowMac* WXUNUSED(parent), |
208 | wxWindowID WXUNUSED(id), | |
1e181c7a | 209 | const wxString& str, |
03647350 | 210 | const wxPoint& pos, |
1e181c7a | 211 | const wxSize& size, |
03647350 | 212 | long style, |
a4fec5b4 | 213 | long WXUNUSED(extraStyle)) |
489468fe | 214 | { |
1e181c7a | 215 | wxMacControl* peer = new wxMacSearchFieldControl( wxpeer , str , pos , size , style ); |
489468fe | 216 | |
1e181c7a | 217 | return peer; |
489468fe SC |
218 | } |
219 | ||
220 | #endif // wxUSE_NATIVE_SEARCH_CONTROL | |
221 | ||
222 | #endif // wxUSE_SEARCHCTRL |