]> git.saurik.com Git - wxWidgets.git/blame - src/osx/cocoa/srchctrl.mm
support for hints, fixing textfield implementation on iOS
[wxWidgets.git] / src / osx / cocoa / srchctrl.mm
CommitLineData
524c47aa
SC
1///////////////////////////////////////////////////////////////////////////////
2// Name: src/osx/carbon/srchctrl.cpp
3// Purpose: implements mac carbon wxSearchCtrl
4// Author: Vince Harron
5// Created: 2006-02-19
6// RCS-ID: $Id: srchctrl.cpp 54820 2008-07-29 20:04:11Z SC $
7// Copyright: Vince Harron
526954c5 8// Licence: wxWindows licence
524c47aa
SC
9///////////////////////////////////////////////////////////////////////////////
10
11// For compilers that support precompilation, includes "wx.h".
12#include "wx/wxprec.h"
13
14#ifdef __BORLANDC__
15 #pragma hdrstop
16#endif
17
18#if wxUSE_SEARCHCTRL
19
20#include "wx/srchctrl.h"
21
22#ifndef WX_PRECOMP
23 #include "wx/menu.h"
24#endif //WX_PRECOMP
25
26#if wxUSE_NATIVE_SEARCH_CONTROL
27
1e181c7a
SC
28#include "wx/osx/private.h"
29#include "wx/osx/cocoa/private/textimpl.h"
524c47aa 30
524c47aa 31
1e181c7a 32@interface wxNSSearchField : NSSearchField
524c47aa 33{
524c47aa
SC
34}
35
1e181c7a 36@end
524c47aa 37
1e181c7a 38@implementation wxNSSearchField
524c47aa 39
4dd9fdf8
SC
40+ (void)initialize
41{
42 static BOOL initialized = NO;
03647350 43 if (!initialized)
4dd9fdf8
SC
44 {
45 initialized = YES;
46 wxOSXCocoaClassAddWXMethods( self );
47 }
48}
49
1e181c7a 50- (id)initWithFrame:(NSRect)frame
524c47aa 51{
1e181c7a 52 [super initWithFrame:frame];
1e181c7a
SC
53 [self setTarget: self];
54 [self setAction: @selector(searchAction:)];
55 return self;
524c47aa
SC
56}
57
1e181c7a 58- (void) searchAction: (id) sender
524c47aa 59{
6331c8c0 60 (void) sender;
4dd9fdf8 61 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
1e181c7a 62 if ( impl )
524c47aa 63 {
1e181c7a
SC
64 wxSearchCtrl* wxpeer = dynamic_cast<wxSearchCtrl*>( impl->GetWXPeer() );
65 if ( wxpeer )
66 {
67 NSString *searchString = [self stringValue];
68 if ( searchString == nil )
69 {
70 wxpeer->HandleSearchFieldCancelHit();
71 }
72 else
73 {
74 wxpeer->HandleSearchFieldSearchHit();
75 }
76 }
524c47aa
SC
77 }
78}
79
75a2c6a1
KO
80- (void)controlTextDidChange:(NSNotification *)aNotification
81{
82 wxUnusedVar(aNotification);
83 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
84 if ( impl )
85 impl->controlTextDidChange();
86}
87
1e181c7a 88@end
524c47aa
SC
89
90// ============================================================================
1e181c7a 91// wxMacSearchFieldControl
524c47aa
SC
92// ============================================================================
93
1e181c7a 94class wxNSSearchFieldControl : public wxNSTextFieldControl, public wxSearchWidgetImpl
524c47aa 95{
1e181c7a
SC
96public :
97 wxNSSearchFieldControl( wxTextCtrl *wxPeer, wxNSSearchField* w ) : wxNSTextFieldControl(wxPeer, w)
524c47aa 98 {
1e181c7a
SC
99 m_searchFieldCell = [w cell];
100 m_searchField = w;
524c47aa 101 }
1e181c7a 102 ~wxNSSearchFieldControl();
524c47aa 103
1e181c7a
SC
104 // search field options
105 virtual void ShowSearchButton( bool show )
524c47aa 106 {
1e181c7a
SC
107 if ( show )
108 [m_searchFieldCell resetSearchButtonCell];
109 else
110 [m_searchFieldCell setSearchButtonCell:nil];
111 [m_searchField setNeedsDisplay:YES];
524c47aa 112 }
03647350 113
1e181c7a 114 virtual bool IsSearchButtonVisible() const
524c47aa 115 {
1e181c7a 116 return [m_searchFieldCell searchButtonCell] != nil;
524c47aa
SC
117 }
118
1e181c7a 119 virtual void ShowCancelButton( bool show )
524c47aa 120 {
1e181c7a
SC
121 if ( show )
122 [m_searchFieldCell resetCancelButtonCell];
123 else
124 [m_searchFieldCell setCancelButtonCell:nil];
125 [m_searchField setNeedsDisplay:YES];
524c47aa 126 }
03647350 127
1e181c7a 128 virtual bool IsCancelButtonVisible() const
524c47aa 129 {
1e181c7a 130 return [m_searchFieldCell cancelButtonCell] != nil;
524c47aa
SC
131 }
132
1e181c7a 133 virtual void SetSearchMenu( wxMenu* menu )
524c47aa 134 {
1e181c7a
SC
135 if ( menu )
136 [m_searchFieldCell setSearchMenuTemplate:menu->GetHMenu()];
137 else
138 [m_searchFieldCell setSearchMenuTemplate:nil];
139 [m_searchField setNeedsDisplay:YES];
524c47aa 140 }
524c47aa 141
1e181c7a 142 virtual void SetDescriptiveText(const wxString& text)
524c47aa 143 {
1e181c7a
SC
144 [m_searchFieldCell setPlaceholderString:
145 wxCFStringRef( text , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
146 }
03647350 147
1e181c7a
SC
148 virtual bool SetFocus()
149 {
150 return wxNSTextFieldControl::SetFocus();
524c47aa 151 }
524c47aa 152
1e181c7a
SC
153private:
154 wxNSSearchField* m_searchField;
155 NSSearchFieldCell* m_searchFieldCell;
156} ;
524c47aa 157
1e181c7a 158wxNSSearchFieldControl::~wxNSSearchFieldControl()
524c47aa 159{
524c47aa
SC
160}
161
03647350
VZ
162wxWidgetImplType* wxWidgetImpl::CreateSearchControl( wxTextCtrl* wxpeer,
163 wxWindowMac* WXUNUSED(parent),
164 wxWindowID WXUNUSED(id),
1e181c7a 165 const wxString& str,
03647350 166 const wxPoint& pos,
1e181c7a 167 const wxSize& size,
03647350 168 long WXUNUSED(style),
6331c8c0 169 long WXUNUSED(extraStyle))
524c47aa 170{
1e181c7a
SC
171 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
172 wxNSSearchField* v = [[wxNSSearchField alloc] initWithFrame:r];
1e181c7a
SC
173 [[v cell] setSendsWholeSearchString:YES];
174 // per wx default cancel is not shown
175 [[v cell] setCancelButtonCell:nil];
176
177 wxNSSearchFieldControl* c = new wxNSSearchFieldControl( wxpeer, v );
b13d363b 178 c->SetNeedsFrame( false );
1e181c7a 179 c->SetStringValue( str );
1e181c7a 180 return c;
524c47aa
SC
181}
182
183#endif // wxUSE_NATIVE_SEARCH_CONTROL
184
185#endif // wxUSE_SEARCHCTRL