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

« back to all changes in this revision

Viewing changes to external/xwt/Xwt.Mac/Xwt.Mac/LabelBackend.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
// LabelBackend.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 MonoMac.AppKit;
 
29
using Xwt.Backends;
 
30
 
 
31
namespace Xwt.Mac
 
32
{
 
33
        public class LabelBackend: ViewBackend<NSTextField,IWidgetEventSink>, ILabelBackend
 
34
        {
 
35
                public LabelBackend ()
 
36
                        : this (new TextFieldView ())
 
37
                {
 
38
                }
 
39
 
 
40
                protected LabelBackend (IViewObject viewObject)
 
41
                {
 
42
                        ViewObject = viewObject;
 
43
                        Widget.Editable = false;
 
44
                        Widget.Bezeled = false;
 
45
                        Widget.DrawsBackground = false;
 
46
                        Widget.SizeToFit ();
 
47
                }
 
48
 
 
49
                public virtual string Text {
 
50
                        get {
 
51
                                return Widget.StringValue;
 
52
                        }
 
53
                        set {
 
54
                                Widget.StringValue = value;
 
55
                                Widget.SizeToFit ();
 
56
                        }
 
57
                }
 
58
 
 
59
                public Xwt.Drawing.Color TextColor {
 
60
                        get { return Widget.TextColor.ToXwtColor (); }
 
61
                        set { Widget.TextColor = value.ToNSColor (); }
 
62
                }
 
63
                
 
64
                public Alignment TextAlignment {
 
65
                        get {
 
66
                                switch (Widget.Alignment) {
 
67
                                case NSTextAlignment.Left: return Alignment.Start;
 
68
                                case NSTextAlignment.Center: return Alignment.Center;
 
69
                                case NSTextAlignment.Right: return Alignment.End;
 
70
                                }
 
71
                                return Alignment.Start;
 
72
                        }
 
73
                        set {
 
74
                                switch (value) {
 
75
                                case Alignment.Start: Widget.Alignment = NSTextAlignment.Left; break;
 
76
                                case Alignment.Center: Widget.Alignment = NSTextAlignment.Center; break;
 
77
                                case Alignment.End: Widget.Alignment = NSTextAlignment.Right; break;
 
78
                                }
 
79
                        }
 
80
                }
 
81
                
 
82
                public EllipsizeMode Ellipsize {
 
83
                        get {
 
84
                                switch (Widget.Cell.LineBreakMode) {
 
85
                                case NSLineBreakMode.TruncatingHead: return Xwt.EllipsizeMode.Start;
 
86
                                case NSLineBreakMode.TruncatingMiddle: return Xwt.EllipsizeMode.Middle;
 
87
                                case NSLineBreakMode.TruncatingTail: return Xwt.EllipsizeMode.End;
 
88
                                default: return Xwt.EllipsizeMode.None;
 
89
                                }
 
90
                        }
 
91
                        set {
 
92
                                switch (value) {
 
93
                                case Xwt.EllipsizeMode.None: Widget.Cell.LineBreakMode = NSLineBreakMode.Clipping; break;
 
94
                                case Xwt.EllipsizeMode.Start: Widget.Cell.LineBreakMode = NSLineBreakMode.TruncatingHead; break;
 
95
                                case Xwt.EllipsizeMode.Middle: Widget.Cell.LineBreakMode = NSLineBreakMode.TruncatingMiddle; break;
 
96
                                case Xwt.EllipsizeMode.End: Widget.Cell.LineBreakMode = NSLineBreakMode.TruncatingTail; break;
 
97
                                }
 
98
                        }
 
99
                }
 
100
 
 
101
                public override Xwt.Drawing.Color BackgroundColor {
 
102
                        get { return Widget.BackgroundColor.ToXwtColor (); }
 
103
                        set {
 
104
                                Widget.BackgroundColor = value.ToNSColor ();
 
105
                                Widget.DrawsBackground = true;
 
106
                        }
 
107
                }
 
108
 
 
109
                // FIXME: technically not possible if we keep NSTextField
 
110
                // as the backend because it's one-line only
 
111
                public WrapMode Wrap {
 
112
                        get {
 
113
                                if (!Widget.Cell.Wraps)
 
114
                                        return WrapMode.None;
 
115
                                switch (Widget.Cell.LineBreakMode) {
 
116
                                case NSLineBreakMode.ByWordWrapping:
 
117
                                        return WrapMode.Word;
 
118
                                case NSLineBreakMode.CharWrapping:
 
119
                                        return WrapMode.Character;
 
120
                                default:
 
121
                                        return WrapMode.None;
 
122
                                }
 
123
                        }
 
124
                        set {
 
125
                                if (value == WrapMode.None) {
 
126
                                        Widget.Cell.Wraps = false;
 
127
                                } else {
 
128
                                        Widget.Cell.Wraps = true;
 
129
                                        switch (value) {
 
130
                                        case WrapMode.Word:
 
131
                                        case WrapMode.WordAndCharacter:
 
132
                                                Widget.Cell.LineBreakMode = NSLineBreakMode.ByWordWrapping;
 
133
                                                break;
 
134
                                        case WrapMode.Character:
 
135
                                                Widget.Cell.LineBreakMode = NSLineBreakMode.CharWrapping;
 
136
                                                break;
 
137
                                        }
 
138
                                }
 
139
                        }
 
140
                }
 
141
        }
 
142
        
 
143
        class TextFieldView: NSTextField, IViewObject
 
144
        {
 
145
                public Widget Frontend { get; set; }
 
146
                public NSView View {
 
147
                        get { return this; }
 
148
                }
 
149
        }
 
150
}
 
151