]> git.saurik.com Git - wxWidgets.git/blob - src/osx/cocoa/srchctrl.mm
non-pch build fix
[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 // use our common calls
59 - (void) setTitle:(NSString *) title
60 {
61 [self setStringValue: title];
62 }
63
64 - (void) searchAction: (id) sender
65 {
66 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
67 if ( impl )
68 {
69 wxSearchCtrl* wxpeer = dynamic_cast<wxSearchCtrl*>( impl->GetWXPeer() );
70 if ( wxpeer )
71 {
72 NSString *searchString = [self stringValue];
73 if ( searchString == nil )
74 {
75 wxpeer->HandleSearchFieldCancelHit();
76 }
77 else
78 {
79 wxpeer->HandleSearchFieldSearchHit();
80 }
81 }
82 }
83 }
84
85 @end
86
87 // ============================================================================
88 // wxMacSearchFieldControl
89 // ============================================================================
90
91 class wxNSSearchFieldControl : public wxNSTextFieldControl, public wxSearchWidgetImpl
92 {
93 public :
94 wxNSSearchFieldControl( wxTextCtrl *wxPeer, wxNSSearchField* w ) : wxNSTextFieldControl(wxPeer, w)
95 {
96 m_searchFieldCell = [w cell];
97 m_searchField = w;
98 }
99 ~wxNSSearchFieldControl();
100
101 // search field options
102 virtual void ShowSearchButton( bool show )
103 {
104 if ( show )
105 [m_searchFieldCell resetSearchButtonCell];
106 else
107 [m_searchFieldCell setSearchButtonCell:nil];
108 [m_searchField setNeedsDisplay:YES];
109 }
110
111 virtual bool IsSearchButtonVisible() const
112 {
113 return [m_searchFieldCell searchButtonCell] != nil;
114 }
115
116 virtual void ShowCancelButton( bool show )
117 {
118 if ( show )
119 [m_searchFieldCell resetCancelButtonCell];
120 else
121 [m_searchFieldCell setCancelButtonCell:nil];
122 [m_searchField setNeedsDisplay:YES];
123 }
124
125 virtual bool IsCancelButtonVisible() const
126 {
127 return [m_searchFieldCell cancelButtonCell] != nil;
128 }
129
130 virtual void SetSearchMenu( wxMenu* menu )
131 {
132 if ( menu )
133 [m_searchFieldCell setSearchMenuTemplate:menu->GetHMenu()];
134 else
135 [m_searchFieldCell setSearchMenuTemplate:nil];
136 [m_searchField setNeedsDisplay:YES];
137 }
138
139 virtual void SetDescriptiveText(const wxString& text)
140 {
141 [m_searchFieldCell setPlaceholderString:
142 wxCFStringRef( text , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
143 }
144
145 virtual bool SetFocus()
146 {
147 return wxNSTextFieldControl::SetFocus();
148 }
149
150 private:
151 wxNSSearchField* m_searchField;
152 NSSearchFieldCell* m_searchFieldCell;
153 } ;
154
155 wxNSSearchFieldControl::~wxNSSearchFieldControl()
156 {
157 }
158
159 wxWidgetImplType* wxWidgetImpl::CreateSearchControl( wxTextCtrl* wxpeer,
160 wxWindowMac* parent,
161 wxWindowID id,
162 const wxString& str,
163 const wxPoint& pos,
164 const wxSize& size,
165 long style,
166 long extraStyle)
167 {
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];
173
174 wxNSSearchFieldControl* c = new wxNSSearchFieldControl( wxpeer, v );
175 c->SetStringValue( str );
176 return c;
177 }
178
179 #endif // wxUSE_NATIVE_SEARCH_CONTROL
180
181 #endif // wxUSE_SEARCHCTRL