| 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 | wxWidgetImpl* impl; |
| 35 | } |
| 36 | |
| 37 | @end |
| 38 | |
| 39 | @implementation wxNSSearchField |
| 40 | |
| 41 | - (id)initWithFrame:(NSRect)frame |
| 42 | { |
| 43 | [super initWithFrame:frame]; |
| 44 | impl = NULL; |
| 45 | [self setTarget: self]; |
| 46 | [self setAction: @selector(searchAction:)]; |
| 47 | return self; |
| 48 | } |
| 49 | |
| 50 | - (void)setImplementation: (wxWidgetImpl *) theImplementation |
| 51 | { |
| 52 | impl = theImplementation; |
| 53 | } |
| 54 | |
| 55 | - (wxWidgetImpl*) implementation |
| 56 | { |
| 57 | return impl; |
| 58 | } |
| 59 | |
| 60 | - (BOOL) isFlipped |
| 61 | { |
| 62 | return YES; |
| 63 | } |
| 64 | |
| 65 | // use our common calls |
| 66 | - (void) setTitle:(NSString *) title |
| 67 | { |
| 68 | [self setStringValue: title]; |
| 69 | } |
| 70 | |
| 71 | - (void) searchAction: (id) sender |
| 72 | { |
| 73 | if ( impl ) |
| 74 | { |
| 75 | wxSearchCtrl* wxpeer = dynamic_cast<wxSearchCtrl*>( impl->GetWXPeer() ); |
| 76 | if ( wxpeer ) |
| 77 | { |
| 78 | NSString *searchString = [self stringValue]; |
| 79 | if ( searchString == nil ) |
| 80 | { |
| 81 | wxpeer->HandleSearchFieldCancelHit(); |
| 82 | } |
| 83 | else |
| 84 | { |
| 85 | wxpeer->HandleSearchFieldSearchHit(); |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | @end |
| 92 | |
| 93 | // ============================================================================ |
| 94 | // wxMacSearchFieldControl |
| 95 | // ============================================================================ |
| 96 | |
| 97 | class wxNSSearchFieldControl : public wxNSTextFieldControl, public wxSearchWidgetImpl |
| 98 | { |
| 99 | public : |
| 100 | wxNSSearchFieldControl( wxTextCtrl *wxPeer, wxNSSearchField* w ) : wxNSTextFieldControl(wxPeer, w) |
| 101 | { |
| 102 | m_searchFieldCell = [w cell]; |
| 103 | m_searchField = w; |
| 104 | } |
| 105 | ~wxNSSearchFieldControl(); |
| 106 | |
| 107 | // search field options |
| 108 | virtual void ShowSearchButton( bool show ) |
| 109 | { |
| 110 | if ( show ) |
| 111 | [m_searchFieldCell resetSearchButtonCell]; |
| 112 | else |
| 113 | [m_searchFieldCell setSearchButtonCell:nil]; |
| 114 | [m_searchField setNeedsDisplay:YES]; |
| 115 | } |
| 116 | |
| 117 | virtual bool IsSearchButtonVisible() const |
| 118 | { |
| 119 | return [m_searchFieldCell searchButtonCell] != nil; |
| 120 | } |
| 121 | |
| 122 | virtual void ShowCancelButton( bool show ) |
| 123 | { |
| 124 | if ( show ) |
| 125 | [m_searchFieldCell resetCancelButtonCell]; |
| 126 | else |
| 127 | [m_searchFieldCell setCancelButtonCell:nil]; |
| 128 | [m_searchField setNeedsDisplay:YES]; |
| 129 | } |
| 130 | |
| 131 | virtual bool IsCancelButtonVisible() const |
| 132 | { |
| 133 | return [m_searchFieldCell cancelButtonCell] != nil; |
| 134 | } |
| 135 | |
| 136 | virtual void SetSearchMenu( wxMenu* menu ) |
| 137 | { |
| 138 | if ( menu ) |
| 139 | [m_searchFieldCell setSearchMenuTemplate:menu->GetHMenu()]; |
| 140 | else |
| 141 | [m_searchFieldCell setSearchMenuTemplate:nil]; |
| 142 | [m_searchField setNeedsDisplay:YES]; |
| 143 | } |
| 144 | |
| 145 | virtual void SetDescriptiveText(const wxString& text) |
| 146 | { |
| 147 | [m_searchFieldCell setPlaceholderString: |
| 148 | wxCFStringRef( text , m_wxPeer->GetFont().GetEncoding() ).AsNSString()]; |
| 149 | } |
| 150 | |
| 151 | virtual bool SetFocus() |
| 152 | { |
| 153 | return wxNSTextFieldControl::SetFocus(); |
| 154 | } |
| 155 | |
| 156 | private: |
| 157 | wxNSSearchField* m_searchField; |
| 158 | NSSearchFieldCell* m_searchFieldCell; |
| 159 | } ; |
| 160 | |
| 161 | wxNSSearchFieldControl::~wxNSSearchFieldControl() |
| 162 | { |
| 163 | } |
| 164 | |
| 165 | wxWidgetImplType* wxWidgetImpl::CreateSearchControl( wxTextCtrl* wxpeer, |
| 166 | wxWindowMac* parent, |
| 167 | wxWindowID id, |
| 168 | const wxString& str, |
| 169 | const wxPoint& pos, |
| 170 | const wxSize& size, |
| 171 | long style, |
| 172 | long extraStyle) |
| 173 | { |
| 174 | NSView* sv = (wxpeer->GetParent()->GetHandle() ); |
| 175 | |
| 176 | NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ; |
| 177 | wxNSSearchField* v = [[wxNSSearchField alloc] initWithFrame:r]; |
| 178 | [sv addSubview:v]; |
| 179 | [[v cell] setSendsWholeSearchString:YES]; |
| 180 | // per wx default cancel is not shown |
| 181 | [[v cell] setCancelButtonCell:nil]; |
| 182 | |
| 183 | wxNSSearchFieldControl* c = new wxNSSearchFieldControl( wxpeer, v ); |
| 184 | c->SetStringValue( str ); |
| 185 | [v setImplementation:c]; |
| 186 | return c; |
| 187 | } |
| 188 | |
| 189 | #endif // wxUSE_NATIVE_SEARCH_CONTROL |
| 190 | |
| 191 | #endif // wxUSE_SEARCHCTRL |