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