1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/cocoa/srchctrl.mm
3 // Purpose: implements mac carbon wxSearchCtrl
4 // Author: Vince Harron
6 // Copyright: Vince Harron
7 // Licence: wxWindows licence
8 ///////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
19 #include "wx/srchctrl.h"
25 #if wxUSE_NATIVE_SEARCH_CONTROL
27 #include "wx/osx/private.h"
28 #include "wx/osx/cocoa/private/textimpl.h"
31 @interface wxNSSearchField : NSSearchField
37 @implementation wxNSSearchField
41 static BOOL initialized = NO;
45 wxOSXCocoaClassAddWXMethods( self );
49 - (id)initWithFrame:(NSRect)frame
51 self = [super initWithFrame:frame];
55 - (void)controlTextDidChange:(NSNotification *)aNotification
57 wxUnusedVar(aNotification);
58 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
60 impl->controlTextDidChange();
63 - (NSArray *)control:(NSControl *)control textView:(NSTextView *)textView completions:(NSArray *)words
64 forPartialWordRange:(NSRange)charRange indexOfSelectedItem:(int*)index
66 NSMutableArray* matches = NULL;
67 NSString* partialString;
69 partialString = [[textView string] substringWithRange:charRange];
70 matches = [NSMutableArray array];
72 // wxTextWidgetImpl* impl = (wxTextWidgetImpl* ) wxWidgetImpl::FindFromWXWidget( self );
73 wxArrayString completions;
75 // adapt to whatever strategy we have for getting the strings
76 // impl->GetTextEntry()->GetCompletions(wxCFStringRef::AsString(partialString), completions);
78 for (size_t i = 0; i < completions.GetCount(); ++i )
79 [matches addObject: wxCFStringRef(completions[i]).AsNSString()];
81 // [matches sortUsingSelector:@selector(compare:)];
89 // ============================================================================
90 // wxMacSearchFieldControl
91 // ============================================================================
93 class wxNSSearchFieldControl : public wxNSTextFieldControl, public wxSearchWidgetImpl
96 wxNSSearchFieldControl( wxTextCtrl *wxPeer, wxNSSearchField* w ) : wxNSTextFieldControl(wxPeer, w)
98 m_searchFieldCell = [w cell];
101 ~wxNSSearchFieldControl();
103 // search field options
104 virtual void ShowSearchButton( bool show )
107 [m_searchFieldCell resetSearchButtonCell];
109 [m_searchFieldCell setSearchButtonCell:nil];
110 [m_searchField setNeedsDisplay:YES];
113 virtual bool IsSearchButtonVisible() const
115 return [m_searchFieldCell searchButtonCell] != nil;
118 virtual void ShowCancelButton( bool show )
121 [m_searchFieldCell resetCancelButtonCell];
123 [m_searchFieldCell setCancelButtonCell:nil];
124 [m_searchField setNeedsDisplay:YES];
127 virtual bool IsCancelButtonVisible() const
129 return [m_searchFieldCell cancelButtonCell] != nil;
132 virtual void SetSearchMenu( wxMenu* menu )
135 [m_searchFieldCell setSearchMenuTemplate:menu->GetHMenu()];
137 [m_searchFieldCell setSearchMenuTemplate:nil];
138 [m_searchField setNeedsDisplay:YES];
141 virtual void SetDescriptiveText(const wxString& text)
143 [m_searchFieldCell setPlaceholderString:
144 wxCFStringRef( text , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
147 virtual bool SetFocus()
149 return wxNSTextFieldControl::SetFocus();
152 void controlAction( WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd), void *WXUNUSED(sender))
154 wxSearchCtrl* wxpeer = (wxSearchCtrl*) GetWXPeer();
157 NSString *searchString = [m_searchField stringValue];
158 if ( searchString == nil )
160 wxpeer->HandleSearchFieldCancelHit();
164 wxpeer->HandleSearchFieldSearchHit();
170 wxNSSearchField* m_searchField;
171 NSSearchFieldCell* m_searchFieldCell;
174 wxNSSearchFieldControl::~wxNSSearchFieldControl()
178 wxWidgetImplType* wxWidgetImpl::CreateSearchControl( wxSearchCtrl* wxpeer,
179 wxWindowMac* WXUNUSED(parent),
180 wxWindowID WXUNUSED(id),
184 long WXUNUSED(style),
185 long WXUNUSED(extraStyle))
187 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
188 wxNSSearchField* v = [[wxNSSearchField alloc] initWithFrame:r];
189 [[v cell] setSendsWholeSearchString:YES];
190 // per wx default cancel is not shown
191 [[v cell] setCancelButtonCell:nil];
193 wxNSSearchFieldControl* c = new wxNSSearchFieldControl( wxpeer, v );
194 c->SetNeedsFrame( false );
195 c->SetStringValue( str );
199 #endif // wxUSE_NATIVE_SEARCH_CONTROL
201 #endif // wxUSE_SEARCHCTRL