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