~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/xwt/Xwt.Mac/Xwt.Mac/ButtonBackend.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// 
 
2
// ButtonBackend.cs
 
3
//  
 
4
// Author:
 
5
//       Lluis Sanchez <lluis@xamarin.com>
 
6
// 
 
7
// Copyright (c) 2011 Xamarin Inc
 
8
// 
 
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
 
10
// of this software and associated documentation files (the "Software"), to deal
 
11
// in the Software without restriction, including without limitation the rights
 
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 
13
// copies of the Software, and to permit persons to whom the Software is
 
14
// furnished to do so, subject to the following conditions:
 
15
// 
 
16
// The above copyright notice and this permission notice shall be included in
 
17
// all copies or substantial portions of the Software.
 
18
// 
 
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 
25
// THE SOFTWARE.
 
26
 
 
27
using System;
 
28
using Xwt.Backends;
 
29
using MonoMac.AppKit;
 
30
using MonoMac.Foundation;
 
31
using Xwt.Engine;
 
32
using MonoMac.ObjCRuntime;
 
33
 
 
34
namespace Xwt.Mac
 
35
{
 
36
        public class ButtonBackend: ViewBackend<NSButton,IButtonEventSink>, IButtonBackend
 
37
        {
 
38
                public ButtonBackend ()
 
39
                {
 
40
                }
 
41
 
 
42
                #region IButtonBackend implementation
 
43
                public override void Initialize ()
 
44
                {
 
45
                        ViewObject = new MacButton (EventSink);
 
46
                        Widget.SizeToFit ();
 
47
                }
 
48
 
 
49
                public void EnableEvent (Xwt.Backends.ButtonEvent ev)
 
50
                {
 
51
                        ((MacButton)Widget).EnableEvent (ev);
 
52
                }
 
53
 
 
54
                public void DisableEvent (Xwt.Backends.ButtonEvent ev)
 
55
                {
 
56
                        ((MacButton)Widget).DisableEvent (ev);
 
57
                }
 
58
                
 
59
                public void SetContent (string label, object imageBackend, ContentPosition imagePosition)
 
60
                {
 
61
                        Widget.Title = label ?? "";
 
62
                        if (string.IsNullOrEmpty (label))
 
63
                                imagePosition = ContentPosition.Center;
 
64
                        if (imageBackend != null) {
 
65
                                Widget.Image = (NSImage)imageBackend;
 
66
                                switch (imagePosition) {
 
67
                                case ContentPosition.Bottom: Widget.ImagePosition = NSCellImagePosition.ImageBelow; break;
 
68
                                case ContentPosition.Left: Widget.ImagePosition = NSCellImagePosition.ImageLeft; break;
 
69
                                case ContentPosition.Right: Widget.ImagePosition = NSCellImagePosition.ImageRight; break;
 
70
                                case ContentPosition.Top: Widget.ImagePosition = NSCellImagePosition.ImageAbove; break;
 
71
                                case ContentPosition.Center: Widget.ImagePosition = NSCellImagePosition.ImageOverlaps; break;
 
72
                                }
 
73
                        }
 
74
                        Widget.SizeToFit ();
 
75
                }
 
76
                
 
77
                public void SetButtonStyle (ButtonStyle style)
 
78
                {
 
79
                        switch (style) {
 
80
                        case ButtonStyle.Normal:
 
81
                                Widget.BezelStyle = NSBezelStyle.RoundRect;
 
82
                                Widget.SetButtonType (NSButtonType.MomentaryPushIn);
 
83
                                Messaging.void_objc_msgSend_bool (Widget.Handle, selSetShowsBorderOnlyWhileMouseInside.Handle, false);
 
84
                                break;
 
85
                        case ButtonStyle.Flat:
 
86
                                Widget.BezelStyle = NSBezelStyle.RoundRect;
 
87
                                Messaging.void_objc_msgSend_bool (Widget.Handle, selSetShowsBorderOnlyWhileMouseInside.Handle, true);
 
88
                                break;
 
89
                        }
 
90
                }
 
91
                
 
92
                static Selector selSetShowsBorderOnlyWhileMouseInside = new Selector ("setShowsBorderOnlyWhileMouseInside:");
 
93
                
 
94
                public void SetButtonType (ButtonType type)
 
95
                {
 
96
                        switch (type) {
 
97
                        case ButtonType.Disclosure: Widget.BezelStyle = NSBezelStyle.Disclosure; break;
 
98
                        default: Widget.BezelStyle = NSBezelStyle.RoundRect; break;
 
99
                        }
 
100
                }
 
101
                
 
102
                #endregion
 
103
        }
 
104
        
 
105
        class MacButton: NSButton, IViewObject
 
106
        {
 
107
                public MacButton (IntPtr p): base (p)
 
108
                {
 
109
                }
 
110
                
 
111
                public MacButton (IButtonEventSink eventSink)
 
112
                {
 
113
                        BezelStyle = NSBezelStyle.Rounded;
 
114
                        Activated += delegate {
 
115
                                Toolkit.Invoke (delegate {
 
116
                                        eventSink.OnClicked ();
 
117
                                });
 
118
                        };
 
119
                }
 
120
                
 
121
                public MacButton (ICheckBoxEventSink eventSink)
 
122
                {
 
123
                        Activated += delegate {
 
124
                                Toolkit.Invoke (delegate {
 
125
                                        eventSink.OnClicked ();
 
126
                                });
 
127
                        };
 
128
                }
 
129
                
 
130
                public Widget Frontend { get; set; }
 
131
                
 
132
                public NSView View {
 
133
                        get { return this; }
 
134
                }
 
135
                
 
136
                public void EnableEvent (Xwt.Backends.ButtonEvent ev)
 
137
                {
 
138
                }
 
139
 
 
140
                public void DisableEvent (Xwt.Backends.ButtonEvent ev)
 
141
                {
 
142
                }
 
143
        }
 
144
}
 
145