]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/cocoa/button.mm
add support for GDK_SCROLL_SMOOTH mouse wheel event, introduced in GTK+ 3.4
[wxWidgets.git] / src / cocoa / button.mm
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/cocoa/button.mm
3// Purpose: wxButton
4// Author: David Elliott
5// Modified by:
6// Created: 2002/12/30
7// Copyright: (c) 2002 David Elliott
8// Licence: wxWindows licence
9/////////////////////////////////////////////////////////////////////////////
10
11#include "wx/wxprec.h"
12
13#include "wx/button.h"
14
15#ifndef WX_PRECOMP
16 #include "wx/log.h"
17#endif
18
19#include "wx/stockitem.h"
20#include "wx/cocoa/autorelease.h"
21#include "wx/cocoa/string.h"
22
23#import <AppKit/NSButton.h>
24#import <math.h>
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,
32 const wxString& lbl, const wxPoint& pos,
33 const wxSize& size, long style,
34 const wxValidator& validator, const wxString& name)
35{
36 wxString label((lbl.empty() && wxIsStockID(winid))?wxGetStockLabel(winid):lbl);
37
38 wxAutoNSAutoreleasePool pool;
39 wxLogTrace(wxTRACE_COCOA,wxT("Creating control with id=%d"),winid);
40 if(!CreateControl(parent,winid,pos,size,style,validator,name))
41 return false;
42 wxLogTrace(wxTRACE_COCOA,wxT("Created control with id=%d"),GetId());
43 m_cocoaNSView = NULL;
44 SetNSButton([[NSButton alloc] initWithFrame: MakeDefaultNSRect(size)]);
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];
50 CocoaSetLabelForObject(label, GetNSButton());
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
64 [GetNSControl() sizeToFit];
65
66 if(m_parent)
67 m_parent->CocoaAddChild(this);
68 SetInitialFrameRect(pos,size);
69
70 return true;
71}
72
73wxButton::~wxButton()
74{
75 DisassociateNSButton(GetNSButton());
76}
77
78void wxButton::Cocoa_wxNSButtonAction(void)
79{
80 wxLogTrace(wxTRACE_COCOA,wxT("YAY!"));
81 wxCommandEvent event(wxEVT_BUTTON, GetId());
82 InitCommandEvent(event); // event.SetEventObject(this);
83 Command(event);
84}
85
86wxString wxButton::GetLabel() const
87{
88 return wxStringWithNSString([GetNSButton() title]);
89}
90
91void wxButton::SetLabel(const wxString& label)
92{
93 CocoaSetLabelForObject(label, GetNSButton());
94}
95
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)];
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];
116}
117
118wxSize wxButtonBase::GetDefaultSize()
119{
120 static NSRect cocoaRect = MakeNSButtonDefaultRect();
121 // Apple HIG says OK/Cancel buttons have default width of 68.
122 return wxSize(68,(int)ceil(cocoaRect.size.height));
123}