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