]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/button.mm
Fix install_name_tool calls in OS X "make install".
[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
fb896a32 7// Copyright: (c) 2002 David Elliott
526954c5 8// Licence: wxWindows licence
fb896a32
DE
9/////////////////////////////////////////////////////////////////////////////
10
11#include "wx/wxprec.h"
8898456d 12
f1e01716
WS
13#include "wx/button.h"
14
fb896a32 15#ifndef WX_PRECOMP
fb896a32
DE
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];
525007cf 50 CocoaSetLabelForObject(label, GetNSButton());
fe8402e6
DE
51
52 do
53 {
54 NSTextAlignment mode;
55 if ((style & wxBU_LEFT) && !(style & wxBU_RIGHT))
56 mode = NSLeftTextAlignment;
57 else if ((style & wxBU_RIGHT) && !(style & wxBU_LEFT))
58 mode = NSRightTextAlignment;
59 else
60 break;
61 [GetNSControl() setAlignment:mode];
62 } while(0);
63
fb896a32
DE
64 [GetNSControl() sizeToFit];
65
66 if(m_parent)
67 m_parent->CocoaAddChild(this);
8d656ea9 68 SetInitialFrameRect(pos,size);
fb896a32
DE
69
70 return true;
71}
72
73wxButton::~wxButton()
74{
911e17c6 75 DisassociateNSButton(GetNSButton());
fb896a32
DE
76}
77
78void wxButton::Cocoa_wxNSButtonAction(void)
79{
48580976 80 wxLogTrace(wxTRACE_COCOA,wxT("YAY!"));
ce7fe42e 81 wxCommandEvent event(wxEVT_BUTTON, GetId());
fb896a32
DE
82 InitCommandEvent(event); // event.SetEventObject(this);
83 Command(event);
84}
85
ce319b6d
DE
86wxString wxButton::GetLabel() const
87{
b0c0a393 88 return wxStringWithNSString([GetNSButton() title]);
ce319b6d
DE
89}
90
91void wxButton::SetLabel(const wxString& label)
92{
525007cf 93 CocoaSetLabelForObject(label, GetNSButton());
ce319b6d
DE
94}
95
635f0147
DE
96wxSize wxButton::DoGetBestSize() const
97{
98 wxSize size = wxButtonBase::DoGetBestSize();
99 if(!HasFlag(wxBU_EXACTFIT))
100 {
101 if(size.x<68)
102 size.x = 68;
103 }
104 return size;
105}
106
107static NSRect MakeNSButtonDefaultRect()
108{
109 // create at (10.0,10.0) with size 20.0x20.0 (just bogus values)
110 wxObjcAutoRefFromAlloc<NSButton*> defaultButton = [[NSButton alloc]
111 initWithFrame:NSMakeRect(10.0,10.0,20.0,20.0)];
5447d1e1
DE
112 [static_cast<NSButton*>(defaultButton) setBezelStyle:NSRoundedBezelStyle];
113 [static_cast<NSButton*>(defaultButton) setTitle:@""];
114 [static_cast<NSButton*>(defaultButton) sizeToFit];
115 return [static_cast<NSButton*>(defaultButton) frame];
635f0147
DE
116}
117
83437648
DE
118wxSize wxButtonBase::GetDefaultSize()
119{
635f0147
DE
120 static NSRect cocoaRect = MakeNSButtonDefaultRect();
121 // Apple HIG says OK/Cancel buttons have default width of 68.
c05c7cb5 122 return wxSize(68,(int)ceil(cocoaRect.size.height));
83437648 123}