1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/carbon/srchctrl.cpp
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 [super initWithFrame:frame];
56 - (void)controlTextDidChange:(NSNotification *)aNotification
58 wxUnusedVar(aNotification);
59 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
61 impl->controlTextDidChange();
66 // ============================================================================
67 // wxMacSearchFieldControl
68 // ============================================================================
70 class wxNSSearchFieldControl : public wxNSTextFieldControl, public wxSearchWidgetImpl
73 wxNSSearchFieldControl( wxTextCtrl *wxPeer, wxNSSearchField* w ) : wxNSTextFieldControl(wxPeer, w)
75 m_searchFieldCell = [w cell];
78 ~wxNSSearchFieldControl();
80 // search field options
81 virtual void ShowSearchButton( bool show )
84 [m_searchFieldCell resetSearchButtonCell];
86 [m_searchFieldCell setSearchButtonCell:nil];
87 [m_searchField setNeedsDisplay:YES];
90 virtual bool IsSearchButtonVisible() const
92 return [m_searchFieldCell searchButtonCell] != nil;
95 virtual void ShowCancelButton( bool show )
98 [m_searchFieldCell resetCancelButtonCell];
100 [m_searchFieldCell setCancelButtonCell:nil];
101 [m_searchField setNeedsDisplay:YES];
104 virtual bool IsCancelButtonVisible() const
106 return [m_searchFieldCell cancelButtonCell] != nil;
109 virtual void SetSearchMenu( wxMenu* menu )
112 [m_searchFieldCell setSearchMenuTemplate:menu->GetHMenu()];
114 [m_searchFieldCell setSearchMenuTemplate:nil];
115 [m_searchField setNeedsDisplay:YES];
118 virtual void SetDescriptiveText(const wxString& text)
120 [m_searchFieldCell setPlaceholderString:
121 wxCFStringRef( text , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
124 virtual bool SetFocus()
126 return wxNSTextFieldControl::SetFocus();
129 void controlAction( WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd), void *WXUNUSED(sender))
131 wxSearchCtrl* wxpeer = (wxSearchCtrl*) GetWXPeer();
134 NSString *searchString = [m_searchField stringValue];
135 if ( searchString == nil )
137 wxpeer->HandleSearchFieldCancelHit();
141 wxpeer->HandleSearchFieldSearchHit();
147 wxNSSearchField* m_searchField;
148 NSSearchFieldCell* m_searchFieldCell;
151 wxNSSearchFieldControl::~wxNSSearchFieldControl()
155 wxWidgetImplType* wxWidgetImpl::CreateSearchControl( wxSearchCtrl* wxpeer,
156 wxWindowMac* WXUNUSED(parent),
157 wxWindowID WXUNUSED(id),
161 long WXUNUSED(style),
162 long WXUNUSED(extraStyle))
164 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
165 wxNSSearchField* v = [[wxNSSearchField alloc] initWithFrame:r];
166 [[v cell] setSendsWholeSearchString:YES];
167 // per wx default cancel is not shown
168 [[v cell] setCancelButtonCell:nil];
170 wxNSSearchFieldControl* c = new wxNSSearchFieldControl( wxpeer, v );
171 c->SetNeedsFrame( false );
172 c->SetStringValue( str );
176 #endif // wxUSE_NATIVE_SEARCH_CONTROL
178 #endif // wxUSE_SEARCHCTRL