| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: button.cpp |
| 3 | // Purpose: wxButton |
| 4 | // Author: Stefan Csomor |
| 5 | // Modified by: |
| 6 | // Created: 1998-01-01 |
| 7 | // RCS-ID: $Id$ |
| 8 | // Copyright: (c) Stefan Csomor |
| 9 | // Licence: wxWindows licence |
| 10 | ///////////////////////////////////////////////////////////////////////////// |
| 11 | |
| 12 | #ifdef __GNUG__ |
| 13 | #pragma implementation "button.h" |
| 14 | #endif |
| 15 | |
| 16 | #include "wx/defs.h" |
| 17 | |
| 18 | #include "wx/button.h" |
| 19 | #include "wx/panel.h" |
| 20 | |
| 21 | #if !USE_SHARED_LIBRARY |
| 22 | IMPLEMENT_DYNAMIC_CLASS(wxButton, wxControl) |
| 23 | #endif |
| 24 | |
| 25 | #include "wx/mac/uma.h" |
| 26 | // Button |
| 27 | |
| 28 | bool wxButton::Create(wxWindow *parent, wxWindowID id, const wxString& label, |
| 29 | const wxPoint& pos, |
| 30 | const wxSize& size, long style, |
| 31 | const wxValidator& validator, |
| 32 | const wxString& name) |
| 33 | { |
| 34 | m_macIsUserPane = FALSE ; |
| 35 | |
| 36 | if ( !wxButtonBase::Create(parent, id, pos, size, style, validator, name) ) |
| 37 | return false; |
| 38 | |
| 39 | m_label = label ; |
| 40 | |
| 41 | Rect bounds = wxMacGetBoundsForControl( this , pos , size ) ; |
| 42 | m_peer = new wxMacControl() ; |
| 43 | if ( label.Find('\n' ) == wxNOT_FOUND && label.Find('\r' ) == wxNOT_FOUND) |
| 44 | { |
| 45 | verify_noerr ( CreatePushButtonControl( MAC_WXHWND(parent->MacGetTopLevelWindowRef()) , &bounds , CFSTR("") , *m_peer ) ); |
| 46 | } |
| 47 | else |
| 48 | { |
| 49 | ControlButtonContentInfo info ; |
| 50 | info.contentType = kControlNoContent ; |
| 51 | verify_noerr(CreateBevelButtonControl( MAC_WXHWND(parent->MacGetTopLevelWindowRef()) , &bounds,CFSTR(""), |
| 52 | kControlBevelButtonLargeBevel , kControlBehaviorPushbutton , &info , 0 , 0 , 0 , *m_peer ) ); |
| 53 | } |
| 54 | |
| 55 | wxASSERT_MSG( m_peer != NULL && m_peer->Ok() , wxT("No valid mac control") ) ; |
| 56 | |
| 57 | MacPostControlCreate(pos,size) ; |
| 58 | |
| 59 | return TRUE; |
| 60 | } |
| 61 | |
| 62 | void wxButton::SetDefault() |
| 63 | { |
| 64 | wxWindow *parent = GetParent(); |
| 65 | wxButton *btnOldDefault = NULL; |
| 66 | if ( parent ) |
| 67 | { |
| 68 | btnOldDefault = wxDynamicCast(parent->GetDefaultItem(), |
| 69 | wxButton); |
| 70 | parent->SetDefaultItem(this); |
| 71 | } |
| 72 | |
| 73 | if ( btnOldDefault ) |
| 74 | btnOldDefault->m_peer->SetData(kControlButtonPart , kControlPushButtonDefaultTag , (Boolean) 0 ) ; |
| 75 | m_peer->SetData(kControlButtonPart , kControlPushButtonDefaultTag , (Boolean) 1 ) ; |
| 76 | } |
| 77 | |
| 78 | wxSize wxButton::DoGetBestSize() const |
| 79 | { |
| 80 | wxSize sz = GetDefaultSize() ; |
| 81 | |
| 82 | int charspace = 8 ; |
| 83 | if ( GetWindowVariant() == wxWINDOW_VARIANT_NORMAL || GetWindowVariant() == wxWINDOW_VARIANT_LARGE ) |
| 84 | { |
| 85 | sz.y = 20 ; |
| 86 | charspace = 10 ; |
| 87 | } |
| 88 | else if ( GetWindowVariant() == wxWINDOW_VARIANT_SMALL ) |
| 89 | { |
| 90 | sz.y = 17 ; |
| 91 | charspace = 8 ; |
| 92 | } |
| 93 | else if ( GetWindowVariant() == wxWINDOW_VARIANT_MINI ) |
| 94 | { |
| 95 | sz.y = 15 ; |
| 96 | charspace = 8 ; |
| 97 | } |
| 98 | |
| 99 | Rect bestsize = { 0 , 0 , 0 , 0 } ; |
| 100 | short baselineoffset ; |
| 101 | ::GetBestControlRect( *m_peer , &bestsize , &baselineoffset ) ; |
| 102 | |
| 103 | int wBtn = 0 ; |
| 104 | if ( EmptyRect( &bestsize ) ) |
| 105 | { |
| 106 | int wBtn = m_label.Length() * charspace + 12 ; |
| 107 | } |
| 108 | else |
| 109 | { |
| 110 | sz.x = bestsize.right - bestsize.left ; |
| 111 | sz.y = bestsize.bottom - bestsize.top ; |
| 112 | } |
| 113 | |
| 114 | if (wBtn > sz.x || ( GetWindowStyle() & wxBU_EXACTFIT) ) |
| 115 | sz.x = wBtn; |
| 116 | |
| 117 | return sz ; |
| 118 | } |
| 119 | |
| 120 | wxSize wxButton::GetDefaultSize() |
| 121 | { |
| 122 | int wBtn = 70 ; |
| 123 | int hBtn = 20 ; |
| 124 | |
| 125 | return wxSize(wBtn, hBtn); |
| 126 | } |
| 127 | |
| 128 | void wxButton::Command (wxCommandEvent & event) |
| 129 | { |
| 130 | m_peer->Flash(kControlButtonPart) ; |
| 131 | ProcessCommand (event); |
| 132 | } |
| 133 | |
| 134 | wxInt32 wxButton::MacControlHit(WXEVENTHANDLERREF WXUNUSED(handler) , WXEVENTREF WXUNUSED(event) ) |
| 135 | { |
| 136 | wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, m_windowId ); |
| 137 | event.SetEventObject(this); |
| 138 | ProcessCommand(event); |
| 139 | return noErr ; |
| 140 | } |
| 141 | |