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 // 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];
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();
80 - (void)controlTextDidChange:(NSNotification *)aNotification
82 wxUnusedVar(aNotification);
83 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
85 impl->controlTextDidChange();
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();
154 wxNSSearchField* m_searchField;
155 NSSearchFieldCell* m_searchFieldCell;
158 wxNSSearchFieldControl::~wxNSSearchFieldControl()
162 wxWidgetImplType* wxWidgetImpl::CreateSearchControl( wxTextCtrl* wxpeer,
163 wxWindowMac* WXUNUSED(parent),
164 wxWindowID WXUNUSED(id),
168 long WXUNUSED(style),
169 long WXUNUSED(extraStyle))
171 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
172 wxNSSearchField* v = [[wxNSSearchField alloc] initWithFrame:r];
173 [[v cell] setSendsWholeSearchString:YES];
174 // per wx default cancel is not shown
175 [[v cell] setCancelButtonCell:nil];
177 wxNSSearchFieldControl* c = new wxNSSearchFieldControl( wxpeer, v );
178 c->SetNeedsFrame( false );
179 c->SetStringValue( str );
183 #endif // wxUSE_NATIVE_SEARCH_CONTROL
185 #endif // wxUSE_SEARCHCTRL