]>
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 | { |
524c47aa SC |
34 | } |
35 | ||
1e181c7a | 36 | @end |
524c47aa | 37 | |
1e181c7a | 38 | @implementation wxNSSearchField |
524c47aa | 39 | |
4dd9fdf8 SC |
40 | + (void)initialize |
41 | { | |
42 | static BOOL initialized = NO; | |
43 | if (!initialized) | |
44 | { | |
45 | initialized = YES; | |
46 | wxOSXCocoaClassAddWXMethods( self ); | |
47 | } | |
48 | } | |
49 | ||
1e181c7a | 50 | - (id)initWithFrame:(NSRect)frame |
524c47aa | 51 | { |
1e181c7a | 52 | [super initWithFrame:frame]; |
1e181c7a SC |
53 | [self setTarget: self]; |
54 | [self setAction: @selector(searchAction:)]; | |
55 | return self; | |
524c47aa SC |
56 | } |
57 | ||
1e181c7a SC |
58 | // use our common calls |
59 | - (void) setTitle:(NSString *) title | |
524c47aa | 60 | { |
1e181c7a | 61 | [self setStringValue: title]; |
524c47aa SC |
62 | } |
63 | ||
1e181c7a | 64 | - (void) searchAction: (id) sender |
524c47aa | 65 | { |
4dd9fdf8 | 66 | wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); |
1e181c7a | 67 | if ( impl ) |
524c47aa | 68 | { |
1e181c7a SC |
69 | wxSearchCtrl* wxpeer = dynamic_cast<wxSearchCtrl*>( impl->GetWXPeer() ); |
70 | if ( wxpeer ) | |
71 | { | |
72 | NSString *searchString = [self stringValue]; | |
73 | if ( searchString == nil ) | |
74 | { | |
75 | wxpeer->HandleSearchFieldCancelHit(); | |
76 | } | |
77 | else | |
78 | { | |
79 | wxpeer->HandleSearchFieldSearchHit(); | |
80 | } | |
81 | } | |
524c47aa SC |
82 | } |
83 | } | |
84 | ||
1e181c7a | 85 | @end |
524c47aa SC |
86 | |
87 | // ============================================================================ | |
1e181c7a | 88 | // wxMacSearchFieldControl |
524c47aa SC |
89 | // ============================================================================ |
90 | ||
1e181c7a | 91 | class wxNSSearchFieldControl : public wxNSTextFieldControl, public wxSearchWidgetImpl |
524c47aa | 92 | { |
1e181c7a SC |
93 | public : |
94 | wxNSSearchFieldControl( wxTextCtrl *wxPeer, wxNSSearchField* w ) : wxNSTextFieldControl(wxPeer, w) | |
524c47aa | 95 | { |
1e181c7a SC |
96 | m_searchFieldCell = [w cell]; |
97 | m_searchField = w; | |
524c47aa | 98 | } |
1e181c7a | 99 | ~wxNSSearchFieldControl(); |
524c47aa | 100 | |
1e181c7a SC |
101 | // search field options |
102 | virtual void ShowSearchButton( bool show ) | |
524c47aa | 103 | { |
1e181c7a SC |
104 | if ( show ) |
105 | [m_searchFieldCell resetSearchButtonCell]; | |
106 | else | |
107 | [m_searchFieldCell setSearchButtonCell:nil]; | |
108 | [m_searchField setNeedsDisplay:YES]; | |
524c47aa | 109 | } |
1e181c7a SC |
110 | |
111 | virtual bool IsSearchButtonVisible() const | |
524c47aa | 112 | { |
1e181c7a | 113 | return [m_searchFieldCell searchButtonCell] != nil; |
524c47aa SC |
114 | } |
115 | ||
1e181c7a | 116 | virtual void ShowCancelButton( bool show ) |
524c47aa | 117 | { |
1e181c7a SC |
118 | if ( show ) |
119 | [m_searchFieldCell resetCancelButtonCell]; | |
120 | else | |
121 | [m_searchFieldCell setCancelButtonCell:nil]; | |
122 | [m_searchField setNeedsDisplay:YES]; | |
524c47aa | 123 | } |
1e181c7a SC |
124 | |
125 | virtual bool IsCancelButtonVisible() const | |
524c47aa | 126 | { |
1e181c7a | 127 | return [m_searchFieldCell cancelButtonCell] != nil; |
524c47aa SC |
128 | } |
129 | ||
1e181c7a | 130 | virtual void SetSearchMenu( wxMenu* menu ) |
524c47aa | 131 | { |
1e181c7a SC |
132 | if ( menu ) |
133 | [m_searchFieldCell setSearchMenuTemplate:menu->GetHMenu()]; | |
134 | else | |
135 | [m_searchFieldCell setSearchMenuTemplate:nil]; | |
136 | [m_searchField setNeedsDisplay:YES]; | |
524c47aa | 137 | } |
524c47aa | 138 | |
1e181c7a | 139 | virtual void SetDescriptiveText(const wxString& text) |
524c47aa | 140 | { |
1e181c7a SC |
141 | [m_searchFieldCell setPlaceholderString: |
142 | wxCFStringRef( text , m_wxPeer->GetFont().GetEncoding() ).AsNSString()]; | |
143 | } | |
144 | ||
145 | virtual bool SetFocus() | |
146 | { | |
147 | return wxNSTextFieldControl::SetFocus(); | |
524c47aa | 148 | } |
524c47aa | 149 | |
1e181c7a SC |
150 | private: |
151 | wxNSSearchField* m_searchField; | |
152 | NSSearchFieldCell* m_searchFieldCell; | |
153 | } ; | |
524c47aa | 154 | |
1e181c7a | 155 | wxNSSearchFieldControl::~wxNSSearchFieldControl() |
524c47aa | 156 | { |
524c47aa SC |
157 | } |
158 | ||
1e181c7a SC |
159 | wxWidgetImplType* wxWidgetImpl::CreateSearchControl( wxTextCtrl* wxpeer, |
160 | wxWindowMac* parent, | |
161 | wxWindowID id, | |
162 | const wxString& str, | |
163 | const wxPoint& pos, | |
164 | const wxSize& size, | |
165 | long style, | |
166 | long extraStyle) | |
524c47aa | 167 | { |
1e181c7a SC |
168 | NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ; |
169 | wxNSSearchField* v = [[wxNSSearchField alloc] initWithFrame:r]; | |
1e181c7a SC |
170 | [[v cell] setSendsWholeSearchString:YES]; |
171 | // per wx default cancel is not shown | |
172 | [[v cell] setCancelButtonCell:nil]; | |
173 | ||
174 | wxNSSearchFieldControl* c = new wxNSSearchFieldControl( wxpeer, v ); | |
175 | c->SetStringValue( str ); | |
1e181c7a | 176 | return c; |
524c47aa SC |
177 | } |
178 | ||
179 | #endif // wxUSE_NATIVE_SEARCH_CONTROL | |
180 | ||
181 | #endif // wxUSE_SEARCHCTRL |