1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/cocoa/srchctrl.mm
3 // Purpose: implements mac carbon wxSearchCtrl
4 // Author: Vince Harron
7 // Copyright: Vince Harron
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
20 #include "wx/srchctrl.h"
26 #if wxUSE_NATIVE_SEARCH_CONTROL
28 #include "wx/osx/private.h"
29 #include "wx/osx/cocoa/private/textimpl.h"
32 @interface wxNSSearchField : NSSearchField
38 @implementation wxNSSearchField
42 static BOOL initialized = NO;
46 wxOSXCocoaClassAddWXMethods( self );
50 - (id)initWithFrame:(NSRect)frame
52 self = [super initWithFrame:frame];
56 - (void)controlTextDidChange:(NSNotification *)aNotification
58 wxUnusedVar(aNotification);
59 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
61 impl->controlTextDidChange();
64 - (NSArray *)control:(NSControl *)control textView:(NSTextView *)textView completions:(NSArray *)words
65 forPartialWordRange:(NSRange)charRange indexOfSelectedItem:(int*)index
67 NSMutableArray* matches = NULL;
68 NSString* partialString;
70 partialString = [[textView string] substringWithRange:charRange];
71 matches = [NSMutableArray array];
73 // wxTextWidgetImpl* impl = (wxTextWidgetImpl* ) wxWidgetImpl::FindFromWXWidget( self );
74 wxArrayString completions;
76 // adapt to whatever strategy we have for getting the strings
77 // impl->GetTextEntry()->GetCompletions(wxCFStringRef::AsString(partialString), completions);
79 for (size_t i = 0; i < completions.GetCount(); ++i )
80 [matches addObject: wxCFStringRef(completions[i]).AsNSString()];
82 // [matches sortUsingSelector:@selector(compare:)];
90 // ============================================================================
91 // wxMacSearchFieldControl
92 // ============================================================================
94 class wxNSSearchFieldControl : public wxNSTextFieldControl, public wxSearchWidgetImpl
97 wxNSSearchFieldControl( wxTextCtrl *wxPeer, wxNSSearchField* w ) : wxNSTextFieldControl(wxPeer, w)
99 m_searchFieldCell = [w cell];
102 ~wxNSSearchFieldControl();
104 // search field options
105 virtual void ShowSearchButton( bool show )
108 [m_searchFieldCell resetSearchButtonCell];
110 [m_searchFieldCell setSearchButtonCell:nil];
111 [m_searchField setNeedsDisplay:YES];
114 virtual bool IsSearchButtonVisible() const
116 return [m_searchFieldCell searchButtonCell] != nil;
119 virtual void ShowCancelButton( bool show )
122 [m_searchFieldCell resetCancelButtonCell];
124 [m_searchFieldCell setCancelButtonCell:nil];
125 [m_searchField setNeedsDisplay:YES];
128 virtual bool IsCancelButtonVisible() const
130 return [m_searchFieldCell cancelButtonCell] != nil;
133 virtual void SetSearchMenu( wxMenu* menu )
136 [m_searchFieldCell setSearchMenuTemplate:menu->GetHMenu()];
138 [m_searchFieldCell setSearchMenuTemplate:nil];
139 [m_searchField setNeedsDisplay:YES];
142 virtual void SetDescriptiveText(const wxString& text)
144 [m_searchFieldCell setPlaceholderString:
145 wxCFStringRef( text , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
148 virtual bool SetFocus()
150 return wxNSTextFieldControl::SetFocus();
153 void controlAction( WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd), void *WXUNUSED(sender))
155 wxSearchCtrl* wxpeer = (wxSearchCtrl*) GetWXPeer();
158 NSString *searchString = [m_searchField stringValue];
159 if ( searchString == nil )
161 wxpeer->HandleSearchFieldCancelHit();
165 wxpeer->HandleSearchFieldSearchHit();
171 wxNSSearchField* m_searchField;
172 NSSearchFieldCell* m_searchFieldCell;
175 wxNSSearchFieldControl::~wxNSSearchFieldControl()
179 wxWidgetImplType* wxWidgetImpl::CreateSearchControl( wxSearchCtrl* wxpeer,
180 wxWindowMac* WXUNUSED(parent),
181 wxWindowID WXUNUSED(id),
185 long WXUNUSED(style),
186 long WXUNUSED(extraStyle))
188 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
189 wxNSSearchField* v = [[wxNSSearchField alloc] initWithFrame:r];
190 [[v cell] setSendsWholeSearchString:YES];
191 // per wx default cancel is not shown
192 [[v cell] setCancelButtonCell:nil];
194 wxNSSearchFieldControl* c = new wxNSSearchFieldControl( wxpeer, v );
195 c->SetNeedsFrame( false );
196 c->SetStringValue( str );
200 #endif // wxUSE_NATIVE_SEARCH_CONTROL
202 #endif // wxUSE_SEARCHCTRL