1 /////////////////////////////////////////////////////////////////////////////// 
   2 // Name:        src/mac/carbon/srchctrl.cpp 
   3 // Purpose:     implements mac carbon wxSearchCtrl 
   4 // Author:      Vince Harron 
   7 // Copyright:   Vince Harron 
   8 // License:     wxWindows licence 
   9 /////////////////////////////////////////////////////////////////////////////// 
  11 // For compilers that support precompilation, includes "wx.h". 
  12 #include "wx/wxprec.h" 
  20 #include "wx/srchctrl.h" 
  26 #if wxUSE_NATIVE_SEARCH_CONTROL 
  28 #include "wx/mac/uma.h" 
  29 #include "wx/mac/carbon/private/mactext.h" 
  31 BEGIN_EVENT_TABLE(wxSearchCtrl
, wxSearchCtrlBase
) 
  34 IMPLEMENT_DYNAMIC_CLASS(wxSearchCtrl
, wxSearchCtrlBase
) 
  36 // ============================================================================ 
  37 // wxMacSearchFieldControl 
  38 // ============================================================================ 
  40 static const EventTypeSpec eventList
[] = 
  42     { kEventClassSearchField
, kEventSearchFieldCancelClicked 
} , 
  43     { kEventClassSearchField
, kEventSearchFieldSearchClicked 
} , 
  46 class wxMacSearchFieldControl 
: public wxMacUnicodeTextControl
 
  49     wxMacSearchFieldControl( wxTextCtrl 
*wxPeer
, 
  52                              const wxSize
& size
, long style 
) : wxMacUnicodeTextControl( wxPeer 
) 
  54         Create( wxPeer
, str
, pos
, size
, style 
); 
  57     // search field options 
  58     virtual void ShowSearchButton( bool show 
); 
  59     virtual bool IsSearchButtonVisible() const; 
  61     virtual void ShowCancelButton( bool show 
); 
  62     virtual bool IsCancelButtonVisible() const; 
  64     virtual void SetSearchMenu( wxMenu
* menu 
); 
  65     virtual wxMenu
* GetSearchMenu() const; 
  67     virtual void SetDescriptiveText(const wxString
& text
); 
  68     virtual wxString 
GetDescriptiveText() const; 
  71     virtual void CreateControl( wxTextCtrl
* peer
, const Rect
* bounds
, CFStringRef crf 
); 
  77 void wxMacSearchFieldControl::CreateControl(wxTextCtrl
* WXUNUSED(peer
), 
  79                                             CFStringRef 
WXUNUSED(crf
)) 
  81     OptionBits attributes 
= kHISearchFieldAttributesSearchIcon
; 
  83     HIRect hibounds 
= { { bounds
->left
, bounds
->top 
}, { bounds
->right
-bounds
->left
, bounds
->bottom
-bounds
->top 
} }; 
  84     verify_noerr( HISearchFieldCreate( 
  91     HIViewSetVisible (m_controlRef
, true); 
  94 // search field options 
  95 void wxMacSearchFieldControl::ShowSearchButton( bool show 
) 
 101         set 
|= kHISearchFieldAttributesSearchIcon
; 
 105         clear 
|= kHISearchFieldAttributesSearchIcon
; 
 107     HISearchFieldChangeAttributes( m_controlRef
, set
, clear 
); 
 110 bool wxMacSearchFieldControl::IsSearchButtonVisible() const 
 112     OptionBits attributes 
= 0; 
 113     verify_noerr( HISearchFieldGetAttributes( m_controlRef
, &attributes 
) ); 
 114     return ( attributes 
& kHISearchFieldAttributesSearchIcon 
) != 0; 
 117 void wxMacSearchFieldControl::ShowCancelButton( bool show 
) 
 120     OptionBits clear 
= 0; 
 123         set 
|= kHISearchFieldAttributesCancel
; 
 127         clear 
|= kHISearchFieldAttributesCancel
; 
 129     HISearchFieldChangeAttributes( m_controlRef
, set
, clear 
); 
 132 bool wxMacSearchFieldControl::IsCancelButtonVisible() const 
 134     OptionBits attributes 
= 0; 
 135     verify_noerr( HISearchFieldGetAttributes( m_controlRef
, &attributes 
) ); 
 136     return ( attributes 
& kHISearchFieldAttributesCancel 
) != 0; 
 139 void wxMacSearchFieldControl::SetSearchMenu( wxMenu
* menu 
) 
 144         verify_noerr( HISearchFieldSetSearchMenu( m_controlRef
, MAC_WXHMENU(m_menu
->GetHMenu()) ) ); 
 148         verify_noerr( HISearchFieldSetSearchMenu( m_controlRef
, 0 ) ); 
 152 wxMenu
* wxMacSearchFieldControl::GetSearchMenu() const 
 158 void wxMacSearchFieldControl::SetDescriptiveText(const wxString
& text
) 
 160     verify_noerr( HISearchFieldSetDescriptiveText( 
 162                       wxCFStringRef( text
, wxFont::GetDefaultEncoding() ))); 
 165 wxString 
wxMacSearchFieldControl::GetDescriptiveText() const 
 168     verify_noerr( HISearchFieldCopyDescriptiveText( m_controlRef
, &cfStr 
)); 
 171         return wxCFStringRef(cfStr
).AsString(); 
 175         return wxEmptyString
; 
 179 // ============================================================================ 
 181 // ============================================================================ 
 183 static pascal OSStatus 
wxMacSearchControlEventHandler( EventHandlerCallRef handler 
, EventRef event 
, void *data 
) 
 185     OSStatus result 
= eventNotHandledErr 
; 
 187     wxMacCarbonEvent 
cEvent( event 
) ; 
 189     ControlRef controlRef 
; 
 190     wxSearchCtrl
* thisWindow 
= (wxSearchCtrl
*) data 
; 
 191     cEvent
.GetParameter( kEventParamDirectObject 
, &controlRef 
) ; 
 193     switch( GetEventKind( event 
) ) 
 195         case kEventSearchFieldCancelClicked 
: 
 196             thisWindow
->MacSearchFieldCancelHit( handler 
, event 
) ; 
 198         case kEventSearchFieldSearchClicked 
: 
 199             thisWindow
->MacSearchFieldSearchHit( handler 
, event 
) ; 
 206 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacSearchControlEventHandler 
) 
 209 // ---------------------------------------------------------------------------- 
 210 // wxSearchCtrl creation 
 211 // ---------------------------------------------------------------------------- 
 216 wxSearchCtrl::wxSearchCtrl() 
 221 wxSearchCtrl::wxSearchCtrl(wxWindow 
*parent
, wxWindowID id
, 
 222            const wxString
& value
, 
 226            const wxValidator
& validator
, 
 227            const wxString
& name
) 
 231     Create(parent
, id
, value
, pos
, size
, style
, validator
, name
); 
 234 void wxSearchCtrl::Init() 
 239 bool wxSearchCtrl::Create(wxWindow 
*parent
, wxWindowID id
, 
 240             const wxString
& value
, 
 244             const wxValidator
& validator
, 
 245             const wxString
& name
) 
 247     if ( !wxTextCtrl::Create(parent
, id
, wxEmptyString
, pos
, size
, wxBORDER_NONE 
| style
, validator
, name
) ) 
 252     EventHandlerRef searchEventHandler
; 
 253     InstallControlEventHandler( m_peer
->GetControlRef(), GetwxMacSearchControlEventHandlerUPP(), 
 254         GetEventTypeCount(eventList
), eventList
, this, 
 255         (EventHandlerRef 
*)&searchEventHandler
); 
 262 wxSearchCtrl::~wxSearchCtrl() 
 267 wxSize 
wxSearchCtrl::DoGetBestSize() const 
 269     wxSize size 
= wxWindow::DoGetBestSize(); 
 270     // it seems to return a default width of about 16, which is way too small here. 
 271     if (size
.GetWidth() < 100) 
 277 void wxSearchCtrl::SetFocus() 
 279     // NB: We have to implement SetFocus a little differently because kControlFocusNextPart 
 280     // leads to setting the focus on the search icon rather than the text area. 
 281     // We get around this by explicitly telling the control to set focus to the 
 283     if ( !AcceptsFocus() ) 
 286     wxWindow
* former 
= FindFocus() ; 
 287     if ( former 
== this ) 
 290     // as we cannot rely on the control features to find out whether we are in full keyboard mode, 
 291     // we can only leave in case of an error 
 292     OSStatus err 
= m_peer
->SetFocus( kControlEditTextPart 
) ; 
 293     if ( err 
== errCouldntSetFocus 
) 
 296     SetUserFocusWindow( (WindowRef
)MacGetTopLevelWindowRef() ); 
 299 // search control specific interfaces 
 300 // wxSearchCtrl owns menu after this call 
 301 void wxSearchCtrl::SetMenu( wxMenu
* menu 
) 
 303     if ( menu 
== m_menu 
) 
 311         m_menu
->SetInvokingWindow( 0 ); 
 319         m_menu
->SetInvokingWindow( this ); 
 322     GetPeer()->SetSearchMenu( m_menu 
); 
 325 wxMenu
* wxSearchCtrl::GetMenu() 
 330 void wxSearchCtrl::ShowSearchButton( bool show 
) 
 332     if ( IsSearchButtonVisible() == show 
) 
 337     GetPeer()->ShowSearchButton( show 
); 
 340 bool wxSearchCtrl::IsSearchButtonVisible() const 
 342     return GetPeer()->IsSearchButtonVisible(); 
 346 void wxSearchCtrl::ShowCancelButton( bool show 
) 
 348     if ( IsCancelButtonVisible() == show 
) 
 353     GetPeer()->ShowCancelButton( show 
); 
 356 bool wxSearchCtrl::IsCancelButtonVisible() const 
 358     return GetPeer()->IsCancelButtonVisible(); 
 361 void wxSearchCtrl::SetDescriptiveText(const wxString
& text
) 
 363     GetPeer()->SetDescriptiveText(text
); 
 366 wxString 
wxSearchCtrl::GetDescriptiveText() const 
 368     return GetPeer()->GetDescriptiveText(); 
 371 wxInt32 
wxSearchCtrl::MacSearchFieldSearchHit(WXEVENTHANDLERREF 
WXUNUSED(handler
) , WXEVENTREF 
WXUNUSED(event
) ) 
 373     wxCommandEvent 
event(wxEVT_COMMAND_SEARCHCTRL_SEARCH_BTN
, m_windowId 
); 
 374     event
.SetEventObject(this); 
 375     ProcessCommand(event
); 
 376     return eventNotHandledErr 
; 
 379 wxInt32 
wxSearchCtrl::MacSearchFieldCancelHit(WXEVENTHANDLERREF 
WXUNUSED(handler
) , WXEVENTREF 
WXUNUSED(event
) ) 
 381     wxCommandEvent 
event(wxEVT_COMMAND_SEARCHCTRL_CANCEL_BTN
, m_windowId 
); 
 382     event
.SetEventObject(this); 
 383     ProcessCommand(event
); 
 384     return eventNotHandledErr 
; 
 388 void wxSearchCtrl::CreatePeer( 
 391            const wxSize
& size
, long style 
) 
 393     m_peer 
= new wxMacSearchFieldControl( this , str 
, pos 
, size 
, style 
); 
 396 #endif // wxUSE_NATIVE_SEARCH_CONTROL 
 398 #endif // wxUSE_SEARCHCTRL