]> git.saurik.com Git - wxWidgets.git/blob - src/osx/cocoa/srchctrl.mm
Allow creating initially hidden controls in wxOSX/Cocoa.
[wxWidgets.git] / src / osx / cocoa / srchctrl.mm
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
8 // License: wxWindows licence
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
28 #include "wx/osx/private.h"
29 #include "wx/osx/cocoa/private/textimpl.h"
30
31
32 @interface wxNSSearchField : NSSearchField
33 {
34 }
35
36 @end
37
38 @implementation wxNSSearchField
39
40 + (void)initialize
41 {
42 static BOOL initialized = NO;
43 if (!initialized)
44 {
45 initialized = YES;
46 wxOSXCocoaClassAddWXMethods( self );
47 }
48 }
49
50 - (id)initWithFrame:(NSRect)frame
51 {
52 [super initWithFrame:frame];
53 [self setTarget: self];
54 [self setAction: @selector(searchAction:)];
55 return self;
56 }
57
58 - (void) searchAction: (id) sender
59 {
60 (void) sender;
61 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
62 if ( impl )
63 {
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 }
77 }
78 }
79
80 @end
81
82 // ============================================================================
83 // wxMacSearchFieldControl
84 // ============================================================================
85
86 class wxNSSearchFieldControl : public wxNSTextFieldControl, public wxSearchWidgetImpl
87 {
88 public :
89 wxNSSearchFieldControl( wxTextCtrl *wxPeer, wxNSSearchField* w ) : wxNSTextFieldControl(wxPeer, w)
90 {
91 m_searchFieldCell = [w cell];
92 m_searchField = w;
93 }
94 ~wxNSSearchFieldControl();
95
96 // search field options
97 virtual void ShowSearchButton( bool show )
98 {
99 if ( show )
100 [m_searchFieldCell resetSearchButtonCell];
101 else
102 [m_searchFieldCell setSearchButtonCell:nil];
103 [m_searchField setNeedsDisplay:YES];
104 }
105
106 virtual bool IsSearchButtonVisible() const
107 {
108 return [m_searchFieldCell searchButtonCell] != nil;
109 }
110
111 virtual void ShowCancelButton( bool show )
112 {
113 if ( show )
114 [m_searchFieldCell resetCancelButtonCell];
115 else
116 [m_searchFieldCell setCancelButtonCell:nil];
117 [m_searchField setNeedsDisplay:YES];
118 }
119
120 virtual bool IsCancelButtonVisible() const
121 {
122 return [m_searchFieldCell cancelButtonCell] != nil;
123 }
124
125 virtual void SetSearchMenu( wxMenu* menu )
126 {
127 if ( menu )
128 [m_searchFieldCell setSearchMenuTemplate:menu->GetHMenu()];
129 else
130 [m_searchFieldCell setSearchMenuTemplate:nil];
131 [m_searchField setNeedsDisplay:YES];
132 }
133
134 virtual void SetDescriptiveText(const wxString& text)
135 {
136 [m_searchFieldCell setPlaceholderString:
137 wxCFStringRef( text , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
138 }
139
140 virtual bool SetFocus()
141 {
142 return wxNSTextFieldControl::SetFocus();
143 }
144
145 private:
146 wxNSSearchField* m_searchField;
147 NSSearchFieldCell* m_searchFieldCell;
148 } ;
149
150 wxNSSearchFieldControl::~wxNSSearchFieldControl()
151 {
152 }
153
154 wxWidgetImplType* wxWidgetImpl::CreateSearchControl( wxTextCtrl* wxpeer,
155 wxWindowMac* WXUNUSED(parent),
156 wxWindowID WXUNUSED(id),
157 const wxString& str,
158 const wxPoint& pos,
159 const wxSize& size,
160 long WXUNUSED(style),
161 long WXUNUSED(extraStyle))
162 {
163 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
164 wxNSSearchField* v = [[wxNSSearchField alloc] initWithFrame:r];
165 [[v cell] setSendsWholeSearchString:YES];
166 // per wx default cancel is not shown
167 [[v cell] setCancelButtonCell:nil];
168
169 wxNSSearchFieldControl* c = new wxNSSearchFieldControl( wxpeer, v );
170 c->SetNeedsFrame( false );
171 c->SetStringValue( str );
172 return c;
173 }
174
175 #endif // wxUSE_NATIVE_SEARCH_CONTROL
176
177 #endif // wxUSE_SEARCHCTRL