]>
Commit | Line | Data |
---|---|---|
524c47aa | 1 | /////////////////////////////////////////////////////////////////////////////// |
80fdcdb9 | 2 | // Name: src/osx/cocoa/srchctrl.mm |
524c47aa SC |
3 | // Purpose: implements mac carbon wxSearchCtrl |
4 | // Author: Vince Harron | |
5 | // Created: 2006-02-19 | |
524c47aa | 6 | // Copyright: Vince Harron |
526954c5 | 7 | // Licence: wxWindows licence |
524c47aa 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 | ||
1e181c7a SC |
27 | #include "wx/osx/private.h" |
28 | #include "wx/osx/cocoa/private/textimpl.h" | |
524c47aa | 29 | |
524c47aa | 30 | |
1e181c7a | 31 | @interface wxNSSearchField : NSSearchField |
524c47aa | 32 | { |
524c47aa SC |
33 | } |
34 | ||
1e181c7a | 35 | @end |
524c47aa | 36 | |
1e181c7a | 37 | @implementation wxNSSearchField |
524c47aa | 38 | |
4dd9fdf8 SC |
39 | + (void)initialize |
40 | { | |
41 | static BOOL initialized = NO; | |
03647350 | 42 | if (!initialized) |
4dd9fdf8 SC |
43 | { |
44 | initialized = YES; | |
45 | wxOSXCocoaClassAddWXMethods( self ); | |
46 | } | |
47 | } | |
48 | ||
1e181c7a | 49 | - (id)initWithFrame:(NSRect)frame |
524c47aa | 50 | { |
b58dca4c | 51 | self = [super initWithFrame:frame]; |
1e181c7a | 52 | return self; |
524c47aa | 53 | } |
11f87a38 | 54 | |
75a2c6a1 KO |
55 | - (void)controlTextDidChange:(NSNotification *)aNotification |
56 | { | |
57 | wxUnusedVar(aNotification); | |
58 | wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); | |
59 | if ( impl ) | |
60 | impl->controlTextDidChange(); | |
61 | } | |
62 | ||
00ba1af9 SC |
63 | - (NSArray *)control:(NSControl *)control textView:(NSTextView *)textView completions:(NSArray *)words |
64 | forPartialWordRange:(NSRange)charRange indexOfSelectedItem:(int*)index | |
65 | { | |
66 | NSMutableArray* matches = NULL; | |
67 | NSString* partialString; | |
68 | ||
69 | partialString = [[textView string] substringWithRange:charRange]; | |
70 | matches = [NSMutableArray array]; | |
71 | ||
b58dca4c | 72 | // wxTextWidgetImpl* impl = (wxTextWidgetImpl* ) wxWidgetImpl::FindFromWXWidget( self ); |
00ba1af9 SC |
73 | wxArrayString completions; |
74 | ||
75 | // adapt to whatever strategy we have for getting the strings | |
76 | // impl->GetTextEntry()->GetCompletions(wxCFStringRef::AsString(partialString), completions); | |
77 | ||
78 | for (size_t i = 0; i < completions.GetCount(); ++i ) | |
79 | [matches addObject: wxCFStringRef(completions[i]).AsNSString()]; | |
80 | ||
81 | // [matches sortUsingSelector:@selector(compare:)]; | |
82 | ||
83 | ||
84 | return matches; | |
85 | } | |
86 | ||
1e181c7a | 87 | @end |
524c47aa SC |
88 | |
89 | // ============================================================================ | |
1e181c7a | 90 | // wxMacSearchFieldControl |
524c47aa SC |
91 | // ============================================================================ |
92 | ||
1e181c7a | 93 | class wxNSSearchFieldControl : public wxNSTextFieldControl, public wxSearchWidgetImpl |
524c47aa | 94 | { |
1e181c7a SC |
95 | public : |
96 | wxNSSearchFieldControl( wxTextCtrl *wxPeer, wxNSSearchField* w ) : wxNSTextFieldControl(wxPeer, w) | |
524c47aa | 97 | { |
1e181c7a SC |
98 | m_searchFieldCell = [w cell]; |
99 | m_searchField = w; | |
524c47aa | 100 | } |
1e181c7a | 101 | ~wxNSSearchFieldControl(); |
524c47aa | 102 | |
1e181c7a SC |
103 | // search field options |
104 | virtual void ShowSearchButton( bool show ) | |
524c47aa | 105 | { |
1e181c7a SC |
106 | if ( show ) |
107 | [m_searchFieldCell resetSearchButtonCell]; | |
108 | else | |
109 | [m_searchFieldCell setSearchButtonCell:nil]; | |
110 | [m_searchField setNeedsDisplay:YES]; | |
524c47aa | 111 | } |
03647350 | 112 | |
1e181c7a | 113 | virtual bool IsSearchButtonVisible() const |
524c47aa | 114 | { |
1e181c7a | 115 | return [m_searchFieldCell searchButtonCell] != nil; |
524c47aa SC |
116 | } |
117 | ||
1e181c7a | 118 | virtual void ShowCancelButton( bool show ) |
524c47aa | 119 | { |
1e181c7a SC |
120 | if ( show ) |
121 | [m_searchFieldCell resetCancelButtonCell]; | |
122 | else | |
123 | [m_searchFieldCell setCancelButtonCell:nil]; | |
124 | [m_searchField setNeedsDisplay:YES]; | |
524c47aa | 125 | } |
03647350 | 126 | |
1e181c7a | 127 | virtual bool IsCancelButtonVisible() const |
524c47aa | 128 | { |
1e181c7a | 129 | return [m_searchFieldCell cancelButtonCell] != nil; |
524c47aa SC |
130 | } |
131 | ||
1e181c7a | 132 | virtual void SetSearchMenu( wxMenu* menu ) |
524c47aa | 133 | { |
1e181c7a SC |
134 | if ( menu ) |
135 | [m_searchFieldCell setSearchMenuTemplate:menu->GetHMenu()]; | |
136 | else | |
137 | [m_searchFieldCell setSearchMenuTemplate:nil]; | |
138 | [m_searchField setNeedsDisplay:YES]; | |
524c47aa | 139 | } |
524c47aa | 140 | |
1e181c7a | 141 | virtual void SetDescriptiveText(const wxString& text) |
524c47aa | 142 | { |
1e181c7a SC |
143 | [m_searchFieldCell setPlaceholderString: |
144 | wxCFStringRef( text , m_wxPeer->GetFont().GetEncoding() ).AsNSString()]; | |
145 | } | |
03647350 | 146 | |
1e181c7a SC |
147 | virtual bool SetFocus() |
148 | { | |
149 | return wxNSTextFieldControl::SetFocus(); | |
524c47aa | 150 | } |
524c47aa | 151 | |
11f87a38 SC |
152 | void controlAction( WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd), void *WXUNUSED(sender)) |
153 | { | |
154 | wxSearchCtrl* wxpeer = (wxSearchCtrl*) GetWXPeer(); | |
155 | if ( wxpeer ) | |
156 | { | |
157 | NSString *searchString = [m_searchField stringValue]; | |
158 | if ( searchString == nil ) | |
159 | { | |
160 | wxpeer->HandleSearchFieldCancelHit(); | |
161 | } | |
162 | else | |
163 | { | |
164 | wxpeer->HandleSearchFieldSearchHit(); | |
165 | } | |
166 | } | |
167 | } | |
168 | ||
1e181c7a SC |
169 | private: |
170 | wxNSSearchField* m_searchField; | |
171 | NSSearchFieldCell* m_searchFieldCell; | |
172 | } ; | |
524c47aa | 173 | |
1e181c7a | 174 | wxNSSearchFieldControl::~wxNSSearchFieldControl() |
524c47aa | 175 | { |
524c47aa SC |
176 | } |
177 | ||
11f87a38 | 178 | wxWidgetImplType* wxWidgetImpl::CreateSearchControl( wxSearchCtrl* wxpeer, |
03647350 VZ |
179 | wxWindowMac* WXUNUSED(parent), |
180 | wxWindowID WXUNUSED(id), | |
1e181c7a | 181 | const wxString& str, |
03647350 | 182 | const wxPoint& pos, |
1e181c7a | 183 | const wxSize& size, |
03647350 | 184 | long WXUNUSED(style), |
6331c8c0 | 185 | long WXUNUSED(extraStyle)) |
524c47aa | 186 | { |
1e181c7a SC |
187 | NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ; |
188 | wxNSSearchField* v = [[wxNSSearchField alloc] initWithFrame:r]; | |
1e181c7a SC |
189 | [[v cell] setSendsWholeSearchString:YES]; |
190 | // per wx default cancel is not shown | |
191 | [[v cell] setCancelButtonCell:nil]; | |
192 | ||
193 | wxNSSearchFieldControl* c = new wxNSSearchFieldControl( wxpeer, v ); | |
b13d363b | 194 | c->SetNeedsFrame( false ); |
1e181c7a | 195 | c->SetStringValue( str ); |
1e181c7a | 196 | return c; |
524c47aa SC |
197 | } |
198 | ||
199 | #endif // wxUSE_NATIVE_SEARCH_CONTROL | |
200 | ||
201 | #endif // wxUSE_SEARCHCTRL |