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