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