]> git.saurik.com Git - wxWidgets.git/blob - src/osx/cocoa/srchctrl.mm
allow NULL parent menu
[wxWidgets.git] / src / osx / cocoa / srchctrl.mm
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/cocoa/srchctrl.mm
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 self = [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 - (NSArray *)control:(NSControl *)control textView:(NSTextView *)textView completions:(NSArray *)words
65 forPartialWordRange:(NSRange)charRange indexOfSelectedItem:(int*)index
66 {
67 NSMutableArray* matches = NULL;
68 NSString* partialString;
69
70 partialString = [[textView string] substringWithRange:charRange];
71 matches = [NSMutableArray array];
72
73 // wxTextWidgetImpl* impl = (wxTextWidgetImpl* ) wxWidgetImpl::FindFromWXWidget( self );
74 wxArrayString completions;
75
76 // adapt to whatever strategy we have for getting the strings
77 // impl->GetTextEntry()->GetCompletions(wxCFStringRef::AsString(partialString), completions);
78
79 for (size_t i = 0; i < completions.GetCount(); ++i )
80 [matches addObject: wxCFStringRef(completions[i]).AsNSString()];
81
82 // [matches sortUsingSelector:@selector(compare:)];
83
84
85 return matches;
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 void controlAction( WXWidget WXUNUSED(slf), void *WXUNUSED(_cmd), void *WXUNUSED(sender))
154 {
155 wxSearchCtrl* wxpeer = (wxSearchCtrl*) GetWXPeer();
156 if ( wxpeer )
157 {
158 NSString *searchString = [m_searchField stringValue];
159 if ( searchString == nil )
160 {
161 wxpeer->HandleSearchFieldCancelHit();
162 }
163 else
164 {
165 wxpeer->HandleSearchFieldSearchHit();
166 }
167 }
168 }
169
170 private:
171 wxNSSearchField* m_searchField;
172 NSSearchFieldCell* m_searchFieldCell;
173 } ;
174
175 wxNSSearchFieldControl::~wxNSSearchFieldControl()
176 {
177 }
178
179 wxWidgetImplType* wxWidgetImpl::CreateSearchControl( wxSearchCtrl* wxpeer,
180 wxWindowMac* WXUNUSED(parent),
181 wxWindowID WXUNUSED(id),
182 const wxString& str,
183 const wxPoint& pos,
184 const wxSize& size,
185 long WXUNUSED(style),
186 long WXUNUSED(extraStyle))
187 {
188 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
189 wxNSSearchField* v = [[wxNSSearchField alloc] initWithFrame:r];
190 [[v cell] setSendsWholeSearchString:YES];
191 // per wx default cancel is not shown
192 [[v cell] setCancelButtonCell:nil];
193
194 wxNSSearchFieldControl* c = new wxNSSearchFieldControl( wxpeer, v );
195 c->SetNeedsFrame( false );
196 c->SetStringValue( str );
197 return c;
198 }
199
200 #endif // wxUSE_NATIVE_SEARCH_CONTROL
201
202 #endif // wxUSE_SEARCHCTRL