]> git.saurik.com Git - wxWidgets.git/blame - src/osx/cocoa/srchctrl.mm
temporary workaround
[wxWidgets.git] / src / osx / cocoa / srchctrl.mm
CommitLineData
524c47aa 1///////////////////////////////////////////////////////////////////////////////
80fdcdb9 2// Name: src/osx/cocoa/srchctrl.mm
524c47aa
SC
3// Purpose: implements mac carbon wxSearchCtrl
4// Author: Vince Harron
5// Created: 2006-02-19
a9a4f229 6// RCS-ID: $Id$
524c47aa 7// Copyright: Vince Harron
526954c5 8// Licence: wxWindows licence
524c47aa
SC
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
1e181c7a
SC
28#include "wx/osx/private.h"
29#include "wx/osx/cocoa/private/textimpl.h"
524c47aa 30
524c47aa 31
1e181c7a 32@interface wxNSSearchField : NSSearchField
524c47aa 33{
524c47aa
SC
34}
35
1e181c7a 36@end
524c47aa 37
1e181c7a 38@implementation wxNSSearchField
524c47aa 39
4dd9fdf8
SC
40+ (void)initialize
41{
42 static BOOL initialized = NO;
03647350 43 if (!initialized)
4dd9fdf8
SC
44 {
45 initialized = YES;
46 wxOSXCocoaClassAddWXMethods( self );
47 }
48}
49
1e181c7a 50- (id)initWithFrame:(NSRect)frame
524c47aa 51{
b58dca4c 52 self = [super initWithFrame:frame];
1e181c7a 53 return self;
524c47aa 54}
11f87a38 55
75a2c6a1
KO
56- (void)controlTextDidChange:(NSNotification *)aNotification
57{
58 wxUnusedVar(aNotification);
59 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
60 if ( impl )
61 impl->controlTextDidChange();
62}
63
00ba1af9
SC
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
b58dca4c 73 // wxTextWidgetImpl* impl = (wxTextWidgetImpl* ) wxWidgetImpl::FindFromWXWidget( self );
00ba1af9
SC
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
1e181c7a 88@end
524c47aa
SC
89
90// ============================================================================
1e181c7a 91// wxMacSearchFieldControl
524c47aa
SC
92// ============================================================================
93
1e181c7a 94class wxNSSearchFieldControl : public wxNSTextFieldControl, public wxSearchWidgetImpl
524c47aa 95{
1e181c7a
SC
96public :
97 wxNSSearchFieldControl( wxTextCtrl *wxPeer, wxNSSearchField* w ) : wxNSTextFieldControl(wxPeer, w)
524c47aa 98 {
1e181c7a
SC
99 m_searchFieldCell = [w cell];
100 m_searchField = w;
524c47aa 101 }
1e181c7a 102 ~wxNSSearchFieldControl();
524c47aa 103
1e181c7a
SC
104 // search field options
105 virtual void ShowSearchButton( bool show )
524c47aa 106 {
1e181c7a
SC
107 if ( show )
108 [m_searchFieldCell resetSearchButtonCell];
109 else
110 [m_searchFieldCell setSearchButtonCell:nil];
111 [m_searchField setNeedsDisplay:YES];
524c47aa 112 }
03647350 113
1e181c7a 114 virtual bool IsSearchButtonVisible() const
524c47aa 115 {
1e181c7a 116 return [m_searchFieldCell searchButtonCell] != nil;
524c47aa
SC
117 }
118
1e181c7a 119 virtual void ShowCancelButton( bool show )
524c47aa 120 {
1e181c7a
SC
121 if ( show )
122 [m_searchFieldCell resetCancelButtonCell];
123 else
124 [m_searchFieldCell setCancelButtonCell:nil];
125 [m_searchField setNeedsDisplay:YES];
524c47aa 126 }
03647350 127
1e181c7a 128 virtual bool IsCancelButtonVisible() const
524c47aa 129 {
1e181c7a 130 return [m_searchFieldCell cancelButtonCell] != nil;
524c47aa
SC
131 }
132
1e181c7a 133 virtual void SetSearchMenu( wxMenu* menu )
524c47aa 134 {
1e181c7a
SC
135 if ( menu )
136 [m_searchFieldCell setSearchMenuTemplate:menu->GetHMenu()];
137 else
138 [m_searchFieldCell setSearchMenuTemplate:nil];
139 [m_searchField setNeedsDisplay:YES];
524c47aa 140 }
524c47aa 141
1e181c7a 142 virtual void SetDescriptiveText(const wxString& text)
524c47aa 143 {
1e181c7a
SC
144 [m_searchFieldCell setPlaceholderString:
145 wxCFStringRef( text , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
146 }
03647350 147
1e181c7a
SC
148 virtual bool SetFocus()
149 {
150 return wxNSTextFieldControl::SetFocus();
524c47aa 151 }
524c47aa 152
11f87a38
SC
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
1e181c7a
SC
170private:
171 wxNSSearchField* m_searchField;
172 NSSearchFieldCell* m_searchFieldCell;
173} ;
524c47aa 174
1e181c7a 175wxNSSearchFieldControl::~wxNSSearchFieldControl()
524c47aa 176{
524c47aa
SC
177}
178
11f87a38 179wxWidgetImplType* wxWidgetImpl::CreateSearchControl( wxSearchCtrl* wxpeer,
03647350
VZ
180 wxWindowMac* WXUNUSED(parent),
181 wxWindowID WXUNUSED(id),
1e181c7a 182 const wxString& str,
03647350 183 const wxPoint& pos,
1e181c7a 184 const wxSize& size,
03647350 185 long WXUNUSED(style),
6331c8c0 186 long WXUNUSED(extraStyle))
524c47aa 187{
1e181c7a
SC
188 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
189 wxNSSearchField* v = [[wxNSSearchField alloc] initWithFrame:r];
1e181c7a
SC
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 );
b13d363b 195 c->SetNeedsFrame( false );
1e181c7a 196 c->SetStringValue( str );
1e181c7a 197 return c;
524c47aa
SC
198}
199
200#endif // wxUSE_NATIVE_SEARCH_CONTROL
201
202#endif // wxUSE_SEARCHCTRL