1///////////////////////////////////////////////////////////////////////////// 2// Name: src/cocoa/button.mm 3// Purpose: wxButton 4// Author: David Elliott 5// Modified by: 6// Created: 2002/12/30 7// RCS-ID: $Id: button.mm 48529 2007-09-03 17:17:35Z DE $ 8// Copyright: (c) 2002 David Elliott 9// Licence: wxWidgets licence 10///////////////////////////////////////////////////////////////////////////// 11 12#include "wx/wxprec.h" 13 14#include "wx/button.h" 15 16#ifndef WX_PRECOMP 17 #include "wx/log.h" 18 #include "wx/toplevel.h" 19#endif 20 21#include "wx/stockitem.h" 22#include "wx/cocoa/autorelease.h" 23#include "wx/cocoa/string.h" 24 25#import <AppKit/NSButton.h> 26#import <math.h> 27 28IMPLEMENT_DYNAMIC_CLASS(wxButton, wxControl) 29BEGIN_EVENT_TABLE(wxButton, wxButtonBase) 30END_EVENT_TABLE() 31WX_IMPLEMENT_COCOA_OWNER(wxButton,NSButton,NSControl,NSView) 32 33bool wxButton::Create(wxWindow *parent, wxWindowID winid, 34 const wxString& lbl, const wxPoint& pos, 35 const wxSize& size, long style, 36 const wxValidator& validator, const wxString& name) 37{ 38 wxString label((lbl.empty() && wxIsStockID(winid))?wxGetStockLabel(winid):lbl); 39 40 wxAutoNSAutoreleasePool pool; 41 wxLogTrace(wxTRACE_COCOA,wxT("Creating control with id=%d"),winid); 42 if(!CreateControl(parent,winid,pos,size,style,validator,name)) 43 return false; 44 wxLogTrace(wxTRACE_COCOA,wxT("Created control with id=%d"),GetId()); 45 m_cocoaNSView = NULL; 46 SetNSButton([[NSButton alloc] initWithFrame: MakeDefaultNSRect(size)]); 47 // NOTE: YES we want to release this (to match the alloc). 48 // DoAddChild(this) will retain us again since addSubView doesn't. 49 [m_cocoaNSView release]; 50 51 [GetNSButton() setBezelStyle:NSRoundedBezelStyle]; 52 CocoaSetLabelForObject(label, GetNSButton()); 53 54 do 55 { 56 NSTextAlignment mode; 57 if ((style & wxBU_LEFT) && !(style & wxBU_RIGHT)) 58 mode = NSLeftTextAlignment; 59 else if ((style & wxBU_RIGHT) && !(style & wxBU_LEFT)) 60 mode = NSRightTextAlignment; 61 else 62 break; 63 [GetNSControl() setAlignment:mode]; 64 } while(0); 65 66 [GetNSControl() sizeToFit]; 67 68 if(m_parent) 69 m_parent->CocoaAddChild(this); 70 SetInitialFrameRect(pos,size); 71 72 return true; 73} 74 75wxButton::~wxButton() 76{ 77 DisassociateNSButton(GetNSButton()); 78} 79 80void wxButton::Cocoa_wxNSButtonAction(void) 81{ 82 wxLogTrace(wxTRACE_COCOA,wxT("YAY!")); 83 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, GetId()); 84 InitCommandEvent(event); // event.SetEventObject(this); 85 Command(event); 86} 87 88wxString wxButton::GetLabel() const 89{ 90 return wxStringWithNSString([GetNSButton() title]); 91} 92 93void wxButton::SetLabel(const wxString& label) 94{ 95 CocoaSetLabelForObject(label, GetNSButton()); 96} 97 98wxSize wxButton::DoGetBestSize() const 99{ 100 wxSize size = wxButtonBase::DoGetBestSize(); 101 if(!HasFlag(wxBU_EXACTFIT)) 102 { 103 if(size.x<68) 104 size.x = 68; 105 } 106 return size; 107} 108 109void wxButton::SetDefault() 110{ 111 wxTopLevelWindow * const 112 tlw = wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow); 113 114 wxCHECK_RET( tlw != NULL, _T("button without top level window?") ); 115 116 tlw->SetDefaultItem(this); 117} 118 119static NSRect MakeNSButtonDefaultRect() 120{ 121 // create at (10.0,10.0) with size 20.0x20.0 (just bogus values) 122 wxObjcAutoRefFromAlloc<NSButton*> defaultButton = [[NSButton alloc] 123 initWithFrame:NSMakeRect(10.0,10.0,20.0,20.0)]; 124 [static_cast<NSButton*>(defaultButton) setBezelStyle:NSRoundedBezelStyle]; 125 [static_cast<NSButton*>(defaultButton) setTitle:@""]; 126 [static_cast<NSButton*>(defaultButton) sizeToFit]; 127 return [static_cast<NSButton*>(defaultButton) frame]; 128} 129 130wxSize wxButtonBase::GetDefaultSize() 131{ 132 static NSRect cocoaRect = MakeNSButtonDefaultRect(); 133 // Apple HIG says OK/Cancel buttons have default width of 68. 134 return wxSize(68,(int)ceil(cocoaRect.size.height)); 135} 136