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 // use our common calls
59 - (void) setTitle:(NSString *) title
61 [self setStringValue: title];
64 - (void) searchAction: (id) sender
66 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
69 wxSearchCtrl* wxpeer = dynamic_cast<wxSearchCtrl*>( impl->GetWXPeer() );
72 NSString *searchString = [self stringValue];
73 if ( searchString == nil )
75 wxpeer->HandleSearchFieldCancelHit();
79 wxpeer->HandleSearchFieldSearchHit();
87 // ============================================================================
88 // wxMacSearchFieldControl
89 // ============================================================================
91 class wxNSSearchFieldControl : public wxNSTextFieldControl, public wxSearchWidgetImpl
94 wxNSSearchFieldControl( wxTextCtrl *wxPeer, wxNSSearchField* w ) : wxNSTextFieldControl(wxPeer, w)
96 m_searchFieldCell = [w cell];
99 ~wxNSSearchFieldControl();
101 // search field options
102 virtual void ShowSearchButton( bool show )
105 [m_searchFieldCell resetSearchButtonCell];
107 [m_searchFieldCell setSearchButtonCell:nil];
108 [m_searchField setNeedsDisplay:YES];
111 virtual bool IsSearchButtonVisible() const
113 return [m_searchFieldCell searchButtonCell] != nil;
116 virtual void ShowCancelButton( bool show )
119 [m_searchFieldCell resetCancelButtonCell];
121 [m_searchFieldCell setCancelButtonCell:nil];
122 [m_searchField setNeedsDisplay:YES];
125 virtual bool IsCancelButtonVisible() const
127 return [m_searchFieldCell cancelButtonCell] != nil;
130 virtual void SetSearchMenu( wxMenu* menu )
133 [m_searchFieldCell setSearchMenuTemplate:menu->GetHMenu()];
135 [m_searchFieldCell setSearchMenuTemplate:nil];
136 [m_searchField setNeedsDisplay:YES];
139 virtual void SetDescriptiveText(const wxString& text)
141 [m_searchFieldCell setPlaceholderString:
142 wxCFStringRef( text , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
145 virtual bool SetFocus()
147 return wxNSTextFieldControl::SetFocus();
151 wxNSSearchField* m_searchField;
152 NSSearchFieldCell* m_searchFieldCell;
155 wxNSSearchFieldControl::~wxNSSearchFieldControl()
159 wxWidgetImplType* wxWidgetImpl::CreateSearchControl( wxTextCtrl* wxpeer,
168 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
169 wxNSSearchField* v = [[wxNSSearchField alloc] initWithFrame:r];
170 [[v cell] setSendsWholeSearchString:YES];
171 // per wx default cancel is not shown
172 [[v cell] setCancelButtonCell:nil];
174 wxNSSearchFieldControl* c = new wxNSSearchFieldControl( wxpeer, v );
175 c->SetStringValue( str );
179 #endif // wxUSE_NATIVE_SEARCH_CONTROL
181 #endif // wxUSE_SEARCHCTRL