]>
Commit | Line | Data |
---|---|---|
524c47aa SC |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: src/osx/carbon/srchctrl.cpp | |
3 | // Purpose: implements mac carbon wxSearchCtrl | |
4 | // Author: Vince Harron | |
5 | // Created: 2006-02-19 | |
6 | // RCS-ID: $Id: srchctrl.cpp 54820 2008-07-29 20:04:11Z SC $ | |
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 | ||
1e181c7a SC |
28 | #include "wx/osx/private.h" |
29 | #include "wx/osx/cocoa/private/textimpl.h" | |
524c47aa | 30 | |
524c47aa | 31 | |
1e181c7a | 32 | @interface wxNSSearchField : NSSearchField |
524c47aa | 33 | { |
1e181c7a | 34 | wxWidgetImpl* impl; |
524c47aa SC |
35 | } |
36 | ||
1e181c7a | 37 | @end |
524c47aa | 38 | |
1e181c7a | 39 | @implementation wxNSSearchField |
524c47aa | 40 | |
1e181c7a | 41 | - (id)initWithFrame:(NSRect)frame |
524c47aa | 42 | { |
1e181c7a SC |
43 | [super initWithFrame:frame]; |
44 | impl = NULL; | |
45 | [self setTarget: self]; | |
46 | [self setAction: @selector(searchAction:)]; | |
47 | return self; | |
524c47aa SC |
48 | } |
49 | ||
1e181c7a | 50 | - (void)setImplementation: (wxWidgetImpl *) theImplementation |
524c47aa | 51 | { |
1e181c7a | 52 | impl = theImplementation; |
524c47aa SC |
53 | } |
54 | ||
1e181c7a | 55 | - (wxWidgetImpl*) implementation |
524c47aa | 56 | { |
1e181c7a | 57 | return impl; |
524c47aa SC |
58 | } |
59 | ||
1e181c7a | 60 | - (BOOL) isFlipped |
524c47aa | 61 | { |
1e181c7a | 62 | return YES; |
524c47aa SC |
63 | } |
64 | ||
1e181c7a SC |
65 | // use our common calls |
66 | - (void) setTitle:(NSString *) title | |
524c47aa | 67 | { |
1e181c7a | 68 | [self setStringValue: title]; |
524c47aa SC |
69 | } |
70 | ||
1e181c7a | 71 | - (void) searchAction: (id) sender |
524c47aa | 72 | { |
1e181c7a | 73 | if ( impl ) |
524c47aa | 74 | { |
1e181c7a SC |
75 | wxSearchCtrl* wxpeer = dynamic_cast<wxSearchCtrl*>( impl->GetWXPeer() ); |
76 | if ( wxpeer ) | |
77 | { | |
78 | NSString *searchString = [self stringValue]; | |
79 | if ( searchString == nil ) | |
80 | { | |
81 | wxpeer->HandleSearchFieldCancelHit(); | |
82 | } | |
83 | else | |
84 | { | |
85 | wxpeer->HandleSearchFieldSearchHit(); | |
86 | } | |
87 | } | |
524c47aa SC |
88 | } |
89 | } | |
90 | ||
1e181c7a | 91 | @end |
524c47aa SC |
92 | |
93 | // ============================================================================ | |
1e181c7a | 94 | // wxMacSearchFieldControl |
524c47aa SC |
95 | // ============================================================================ |
96 | ||
1e181c7a | 97 | class wxNSSearchFieldControl : public wxNSTextFieldControl, public wxSearchWidgetImpl |
524c47aa | 98 | { |
1e181c7a SC |
99 | public : |
100 | wxNSSearchFieldControl( wxTextCtrl *wxPeer, wxNSSearchField* w ) : wxNSTextFieldControl(wxPeer, w) | |
524c47aa | 101 | { |
1e181c7a SC |
102 | m_searchFieldCell = [w cell]; |
103 | m_searchField = w; | |
524c47aa | 104 | } |
1e181c7a | 105 | ~wxNSSearchFieldControl(); |
524c47aa | 106 | |
1e181c7a SC |
107 | // search field options |
108 | virtual void ShowSearchButton( bool show ) | |
524c47aa | 109 | { |
1e181c7a SC |
110 | if ( show ) |
111 | [m_searchFieldCell resetSearchButtonCell]; | |
112 | else | |
113 | [m_searchFieldCell setSearchButtonCell:nil]; | |
114 | [m_searchField setNeedsDisplay:YES]; | |
524c47aa | 115 | } |
1e181c7a SC |
116 | |
117 | virtual bool IsSearchButtonVisible() const | |
524c47aa | 118 | { |
1e181c7a | 119 | return [m_searchFieldCell searchButtonCell] != nil; |
524c47aa SC |
120 | } |
121 | ||
1e181c7a | 122 | virtual void ShowCancelButton( bool show ) |
524c47aa | 123 | { |
1e181c7a SC |
124 | if ( show ) |
125 | [m_searchFieldCell resetCancelButtonCell]; | |
126 | else | |
127 | [m_searchFieldCell setCancelButtonCell:nil]; | |
128 | [m_searchField setNeedsDisplay:YES]; | |
524c47aa | 129 | } |
1e181c7a SC |
130 | |
131 | virtual bool IsCancelButtonVisible() const | |
524c47aa | 132 | { |
1e181c7a | 133 | return [m_searchFieldCell cancelButtonCell] != nil; |
524c47aa SC |
134 | } |
135 | ||
1e181c7a | 136 | virtual void SetSearchMenu( wxMenu* menu ) |
524c47aa | 137 | { |
1e181c7a SC |
138 | if ( menu ) |
139 | [m_searchFieldCell setSearchMenuTemplate:menu->GetHMenu()]; | |
140 | else | |
141 | [m_searchFieldCell setSearchMenuTemplate:nil]; | |
142 | [m_searchField setNeedsDisplay:YES]; | |
524c47aa | 143 | } |
524c47aa | 144 | |
1e181c7a | 145 | virtual void SetDescriptiveText(const wxString& text) |
524c47aa | 146 | { |
1e181c7a SC |
147 | [m_searchFieldCell setPlaceholderString: |
148 | wxCFStringRef( text , m_wxPeer->GetFont().GetEncoding() ).AsNSString()]; | |
149 | } | |
150 | ||
151 | virtual bool SetFocus() | |
152 | { | |
153 | return wxNSTextFieldControl::SetFocus(); | |
524c47aa | 154 | } |
524c47aa | 155 | |
1e181c7a SC |
156 | private: |
157 | wxNSSearchField* m_searchField; | |
158 | NSSearchFieldCell* m_searchFieldCell; | |
159 | } ; | |
524c47aa | 160 | |
1e181c7a | 161 | wxNSSearchFieldControl::~wxNSSearchFieldControl() |
524c47aa | 162 | { |
524c47aa SC |
163 | } |
164 | ||
1e181c7a SC |
165 | wxWidgetImplType* wxWidgetImpl::CreateSearchControl( wxTextCtrl* wxpeer, |
166 | wxWindowMac* parent, | |
167 | wxWindowID id, | |
168 | const wxString& str, | |
169 | const wxPoint& pos, | |
170 | const wxSize& size, | |
171 | long style, | |
172 | long extraStyle) | |
524c47aa | 173 | { |
1e181c7a SC |
174 | NSView* sv = (wxpeer->GetParent()->GetHandle() ); |
175 | ||
176 | NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ; | |
177 | wxNSSearchField* v = [[wxNSSearchField alloc] initWithFrame:r]; | |
178 | [sv addSubview:v]; | |
179 | [[v cell] setSendsWholeSearchString:YES]; | |
180 | // per wx default cancel is not shown | |
181 | [[v cell] setCancelButtonCell:nil]; | |
182 | ||
183 | wxNSSearchFieldControl* c = new wxNSSearchFieldControl( wxpeer, v ); | |
184 | c->SetStringValue( str ); | |
185 | [v setImplementation:c]; | |
186 | return c; | |
524c47aa SC |
187 | } |
188 | ||
189 | #endif // wxUSE_NATIVE_SEARCH_CONTROL | |
190 | ||
191 | #endif // wxUSE_SEARCHCTRL |