1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/carbon/srchctrl.cpp
3 // Purpose: implements mac carbon wxSearchCtrl
4 // Author: Vince Harron
6 // RCS-ID: $Id: srchctrl.cpp 54820 2008-07-29 20:04:11Z SC $
7 // Copyright: Vince Harron
8 // License: 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];
53 [self setTarget: self];
54 [self setAction: @selector(searchAction:)];
58 - (void) searchAction: (id) sender
61 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
64 wxSearchCtrl* wxpeer = dynamic_cast<wxSearchCtrl*>( impl->GetWXPeer() );
67 NSString *searchString = [self stringValue];
68 if ( searchString == nil )
70 wxpeer->HandleSearchFieldCancelHit();
74 wxpeer->HandleSearchFieldSearchHit();
82 // ============================================================================
83 // wxMacSearchFieldControl
84 // ============================================================================
86 class wxNSSearchFieldControl : public wxNSTextFieldControl, public wxSearchWidgetImpl
89 wxNSSearchFieldControl( wxTextCtrl *wxPeer, wxNSSearchField* w ) : wxNSTextFieldControl(wxPeer, w)
91 m_searchFieldCell = [w cell];
94 ~wxNSSearchFieldControl();
96 // search field options
97 virtual void ShowSearchButton( bool show )
100 [m_searchFieldCell resetSearchButtonCell];
102 [m_searchFieldCell setSearchButtonCell:nil];
103 [m_searchField setNeedsDisplay:YES];
106 virtual bool IsSearchButtonVisible() const
108 return [m_searchFieldCell searchButtonCell] != nil;
111 virtual void ShowCancelButton( bool show )
114 [m_searchFieldCell resetCancelButtonCell];
116 [m_searchFieldCell setCancelButtonCell:nil];
117 [m_searchField setNeedsDisplay:YES];
120 virtual bool IsCancelButtonVisible() const
122 return [m_searchFieldCell cancelButtonCell] != nil;
125 virtual void SetSearchMenu( wxMenu* menu )
128 [m_searchFieldCell setSearchMenuTemplate:menu->GetHMenu()];
130 [m_searchFieldCell setSearchMenuTemplate:nil];
131 [m_searchField setNeedsDisplay:YES];
134 virtual void SetDescriptiveText(const wxString& text)
136 [m_searchFieldCell setPlaceholderString:
137 wxCFStringRef( text , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
140 virtual bool SetFocus()
142 return wxNSTextFieldControl::SetFocus();
146 wxNSSearchField* m_searchField;
147 NSSearchFieldCell* m_searchFieldCell;
150 wxNSSearchFieldControl::~wxNSSearchFieldControl()
154 wxWidgetImplType* wxWidgetImpl::CreateSearchControl( wxTextCtrl* wxpeer,
155 wxWindowMac* WXUNUSED(parent),
156 wxWindowID WXUNUSED(id),
160 long WXUNUSED(style),
161 long WXUNUSED(extraStyle))
163 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
164 wxNSSearchField* v = [[wxNSSearchField alloc] initWithFrame:r];
165 [[v cell] setSendsWholeSearchString:YES];
166 // per wx default cancel is not shown
167 [[v cell] setCancelButtonCell:nil];
169 wxNSSearchFieldControl* c = new wxNSSearchFieldControl( wxpeer, v );
170 c->SetNeedsFrame( false );
171 c->SetStringValue( str );
175 #endif // wxUSE_NATIVE_SEARCH_CONTROL
177 #endif // wxUSE_SEARCHCTRL