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
39 @implementation wxNSSearchField
41 - (id)initWithFrame:(NSRect)frame
43 [super initWithFrame:frame];
45 [self setTarget: self];
46 [self setAction: @selector(searchAction:)];
50 - (void)setImplementation: (wxWidgetImpl *) theImplementation
52 impl = theImplementation;
55 - (wxWidgetImpl*) implementation
65 // use our common calls
66 - (void) setTitle:(NSString *) title
68 [self setStringValue: title];
71 - (void) searchAction: (id) sender
75 wxSearchCtrl* wxpeer = dynamic_cast<wxSearchCtrl*>( impl->GetWXPeer() );
78 NSString *searchString = [self stringValue];
79 if ( searchString == nil )
81 wxpeer->HandleSearchFieldCancelHit();
85 wxpeer->HandleSearchFieldSearchHit();
93 // ============================================================================
94 // wxMacSearchFieldControl
95 // ============================================================================
97 class wxNSSearchFieldControl : public wxNSTextFieldControl, public wxSearchWidgetImpl
100 wxNSSearchFieldControl( wxTextCtrl *wxPeer, wxNSSearchField* w ) : wxNSTextFieldControl(wxPeer, w)
102 m_searchFieldCell = [w cell];
105 ~wxNSSearchFieldControl();
107 // search field options
108 virtual void ShowSearchButton( bool show )
111 [m_searchFieldCell resetSearchButtonCell];
113 [m_searchFieldCell setSearchButtonCell:nil];
114 [m_searchField setNeedsDisplay:YES];
117 virtual bool IsSearchButtonVisible() const
119 return [m_searchFieldCell searchButtonCell] != nil;
122 virtual void ShowCancelButton( bool show )
125 [m_searchFieldCell resetCancelButtonCell];
127 [m_searchFieldCell setCancelButtonCell:nil];
128 [m_searchField setNeedsDisplay:YES];
131 virtual bool IsCancelButtonVisible() const
133 return [m_searchFieldCell cancelButtonCell] != nil;
136 virtual void SetSearchMenu( wxMenu* menu )
139 [m_searchFieldCell setSearchMenuTemplate:menu->GetHMenu()];
141 [m_searchFieldCell setSearchMenuTemplate:nil];
142 [m_searchField setNeedsDisplay:YES];
145 virtual void SetDescriptiveText(const wxString& text)
147 [m_searchFieldCell setPlaceholderString:
148 wxCFStringRef( text , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
151 virtual bool SetFocus()
153 return wxNSTextFieldControl::SetFocus();
157 wxNSSearchField* m_searchField;
158 NSSearchFieldCell* m_searchFieldCell;
161 wxNSSearchFieldControl::~wxNSSearchFieldControl()
165 wxWidgetImplType* wxWidgetImpl::CreateSearchControl( wxTextCtrl* wxpeer,
174 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
175 wxNSSearchField* v = [[wxNSSearchField alloc] initWithFrame:r];
176 [[v cell] setSendsWholeSearchString:YES];
177 // per wx default cancel is not shown
178 [[v cell] setCancelButtonCell:nil];
180 wxNSSearchFieldControl* c = new wxNSSearchFieldControl( wxpeer, v );
181 c->SetStringValue( str );
182 [v setImplementation:c];
186 #endif // wxUSE_NATIVE_SEARCH_CONTROL
188 #endif // wxUSE_SEARCHCTRL