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
60 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
63 wxSearchCtrl* wxpeer = dynamic_cast<wxSearchCtrl*>( impl->GetWXPeer() );
66 NSString *searchString = [self stringValue];
67 if ( searchString == nil )
69 wxpeer->HandleSearchFieldCancelHit();
73 wxpeer->HandleSearchFieldSearchHit();
81 // ============================================================================
82 // wxMacSearchFieldControl
83 // ============================================================================
85 class wxNSSearchFieldControl : public wxNSTextFieldControl, public wxSearchWidgetImpl
88 wxNSSearchFieldControl( wxTextCtrl *wxPeer, wxNSSearchField* w ) : wxNSTextFieldControl(wxPeer, w)
90 m_searchFieldCell = [w cell];
93 ~wxNSSearchFieldControl();
95 // search field options
96 virtual void ShowSearchButton( bool show )
99 [m_searchFieldCell resetSearchButtonCell];
101 [m_searchFieldCell setSearchButtonCell:nil];
102 [m_searchField setNeedsDisplay:YES];
105 virtual bool IsSearchButtonVisible() const
107 return [m_searchFieldCell searchButtonCell] != nil;
110 virtual void ShowCancelButton( bool show )
113 [m_searchFieldCell resetCancelButtonCell];
115 [m_searchFieldCell setCancelButtonCell:nil];
116 [m_searchField setNeedsDisplay:YES];
119 virtual bool IsCancelButtonVisible() const
121 return [m_searchFieldCell cancelButtonCell] != nil;
124 virtual void SetSearchMenu( wxMenu* menu )
127 [m_searchFieldCell setSearchMenuTemplate:menu->GetHMenu()];
129 [m_searchFieldCell setSearchMenuTemplate:nil];
130 [m_searchField setNeedsDisplay:YES];
133 virtual void SetDescriptiveText(const wxString& text)
135 [m_searchFieldCell setPlaceholderString:
136 wxCFStringRef( text , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
139 virtual bool SetFocus()
141 return wxNSTextFieldControl::SetFocus();
145 wxNSSearchField* m_searchField;
146 NSSearchFieldCell* m_searchFieldCell;
149 wxNSSearchFieldControl::~wxNSSearchFieldControl()
153 wxWidgetImplType* wxWidgetImpl::CreateSearchControl( wxTextCtrl* wxpeer,
162 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
163 wxNSSearchField* v = [[wxNSSearchField alloc] initWithFrame:r];
164 [[v cell] setSendsWholeSearchString:YES];
165 // per wx default cancel is not shown
166 [[v cell] setCancelButtonCell:nil];
171 wxNSSearchFieldControl* c = new wxNSSearchFieldControl( wxpeer, v );
172 c->SetStringValue( str );
176 #endif // wxUSE_NATIVE_SEARCH_CONTROL
178 #endif // wxUSE_SEARCHCTRL