]> git.saurik.com Git - wxWidgets.git/blob - src/osx/cocoa/srchctrl.mm
moving OnInit back
[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 - (void)controlTextDidChange:(NSNotification *)aNotification
81 {
82 wxUnusedVar(aNotification);
83 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
84 if ( impl )
85 impl->controlTextDidChange();
86 }
87
88 @end
89
90 // ============================================================================
91 // wxMacSearchFieldControl
92 // ============================================================================
93
94 class wxNSSearchFieldControl : public wxNSTextFieldControl, public wxSearchWidgetImpl
95 {
96 public :
97 wxNSSearchFieldControl( wxTextCtrl *wxPeer, wxNSSearchField* w ) : wxNSTextFieldControl(wxPeer, w)
98 {
99 m_searchFieldCell = [w cell];
100 m_searchField = w;
101 }
102 ~wxNSSearchFieldControl();
103
104 // search field options
105 virtual void ShowSearchButton( bool show )
106 {
107 if ( show )
108 [m_searchFieldCell resetSearchButtonCell];
109 else
110 [m_searchFieldCell setSearchButtonCell:nil];
111 [m_searchField setNeedsDisplay:YES];
112 }
113
114 virtual bool IsSearchButtonVisible() const
115 {
116 return [m_searchFieldCell searchButtonCell] != nil;
117 }
118
119 virtual void ShowCancelButton( bool show )
120 {
121 if ( show )
122 [m_searchFieldCell resetCancelButtonCell];
123 else
124 [m_searchFieldCell setCancelButtonCell:nil];
125 [m_searchField setNeedsDisplay:YES];
126 }
127
128 virtual bool IsCancelButtonVisible() const
129 {
130 return [m_searchFieldCell cancelButtonCell] != nil;
131 }
132
133 virtual void SetSearchMenu( wxMenu* menu )
134 {
135 if ( menu )
136 [m_searchFieldCell setSearchMenuTemplate:menu->GetHMenu()];
137 else
138 [m_searchFieldCell setSearchMenuTemplate:nil];
139 [m_searchField setNeedsDisplay:YES];
140 }
141
142 virtual void SetDescriptiveText(const wxString& text)
143 {
144 [m_searchFieldCell setPlaceholderString:
145 wxCFStringRef( text , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
146 }
147
148 virtual bool SetFocus()
149 {
150 return wxNSTextFieldControl::SetFocus();
151 }
152
153 private:
154 wxNSSearchField* m_searchField;
155 NSSearchFieldCell* m_searchFieldCell;
156 } ;
157
158 wxNSSearchFieldControl::~wxNSSearchFieldControl()
159 {
160 }
161
162 wxWidgetImplType* wxWidgetImpl::CreateSearchControl( wxTextCtrl* wxpeer,
163 wxWindowMac* WXUNUSED(parent),
164 wxWindowID WXUNUSED(id),
165 const wxString& str,
166 const wxPoint& pos,
167 const wxSize& size,
168 long WXUNUSED(style),
169 long WXUNUSED(extraStyle))
170 {
171 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
172 wxNSSearchField* v = [[wxNSSearchField alloc] initWithFrame:r];
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 );
178 c->SetNeedsFrame( false );
179 c->SetStringValue( str );
180 return c;
181 }
182
183 #endif // wxUSE_NATIVE_SEARCH_CONTROL
184
185 #endif // wxUSE_SEARCHCTRL