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

« back to all changes in this revision

Viewing changes to external/xwt/Xwt.Gtk/Xwt.GtkBackend/Util.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
// Util.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.Drawing;
 
29
using Xwt.Engine;
 
30
using Xwt.Backends;
 
31
using System.Collections.Generic;
 
32
using System.Linq;
 
33
 
 
34
namespace Xwt.GtkBackend
 
35
{
 
36
        public static class Util
 
37
        {
 
38
                static uint targetIdCounter = 0;
 
39
                static Dictionary<TransferDataType, Gtk.TargetEntry[]> dragTargets = new Dictionary<TransferDataType, Gtk.TargetEntry[]> ();
 
40
                static Dictionary<string, TransferDataType> atomToType = new Dictionary<string, TransferDataType> ();
 
41
 
 
42
                public static void SetDragData (TransferDataSource data, Gtk.DragDataGetArgs args)
 
43
                {
 
44
                        foreach (var t in data.DataTypes) {
 
45
                                object val = data.GetValue (t);
 
46
                                SetSelectionData (args.SelectionData, t.Id, val);
 
47
                        }
 
48
                }
 
49
                
 
50
                public static void SetSelectionData (Gtk.SelectionData data, string atomType, object val)
 
51
                {
 
52
                        if (val == null)
 
53
                                return;
 
54
                        if (val is string)
 
55
                                data.Text = (string)val;
 
56
                        else if (val is Xwt.Drawing.Image)
 
57
                                data.SetPixbuf ((Gdk.Pixbuf) WidgetRegistry.GetBackend (val));
 
58
                        else {
 
59
                                var at = Gdk.Atom.Intern (atomType, false);
 
60
                                data.Set (at, 0, TransferDataSource.SerializeValue (val));
 
61
                        }
 
62
                }
 
63
                
 
64
                public static bool GetSelectionData (Gtk.SelectionData data, TransferDataStore target)
 
65
                {
 
66
                        TransferDataType type = Util.AtomToType (data.Target.Name);
 
67
                        if (type == null || data.Length <= 0)
 
68
                                return false;
 
69
 
 
70
                        if (type == TransferDataType.Text)
 
71
                                target.AddText (data.Text);
 
72
                        else if (data.TargetsIncludeImage (false))
 
73
                                target.AddImage (WidgetRegistry.CreateFrontend<Xwt.Drawing.Image> (data.Pixbuf));
 
74
                        else if (type == TransferDataType.Uri) {
 
75
                                var uris = System.Text.Encoding.UTF8.GetString (data.Data).Split ('\n').Where (u => !string.IsNullOrEmpty(u)).Select (u => new Uri (u)).ToArray ();
 
76
                                target.AddUris (uris);
 
77
                        }
 
78
                        else
 
79
                                target.AddValue (type, data.Data);
 
80
                        return true;
 
81
                }
 
82
                
 
83
                internal static TransferDataType AtomToType (string targetName)
 
84
                {
 
85
                        TransferDataType type;
 
86
                        atomToType.TryGetValue (targetName, out type);
 
87
                        return type;
 
88
                }
 
89
                
 
90
                internal static TransferDataType[] GetDragTypes (Gdk.Atom[] dropTypes)
 
91
                {
 
92
                        List<TransferDataType> types = new List<TransferDataType> ();
 
93
                        foreach (var dt in dropTypes) {
 
94
                                TransferDataType type;
 
95
                                if (atomToType.TryGetValue (dt.Name, out type))
 
96
                                        types.Add (type);
 
97
                        }
 
98
                        return types.ToArray ();
 
99
                }
 
100
                
 
101
                public static Gtk.TargetList BuildTargetTable (TransferDataType[] types)
 
102
                {
 
103
                        var tl = new Gtk.TargetList ();
 
104
                        foreach (var tt in types)
 
105
                                tl.AddTable (CreateTargetEntries (tt));
 
106
                        return tl;
 
107
                }
 
108
                
 
109
                static Gtk.TargetEntry[] CreateTargetEntries (TransferDataType type)
 
110
                {
 
111
                        lock (dragTargets) {
 
112
                                Gtk.TargetEntry[] entries;
 
113
                                if (dragTargets.TryGetValue (type, out entries))
 
114
                                        return entries;
 
115
                                
 
116
                                uint id = targetIdCounter++;
 
117
                                
 
118
                                if (type == TransferDataType.Uri) {
 
119
                                        Gtk.TargetList list = new Gtk.TargetList ();
 
120
                                        list.AddUriTargets (id);
 
121
                                        entries = (Gtk.TargetEntry[])list;
 
122
                                }
 
123
                                else if (type == TransferDataType.Text) {
 
124
                                        Gtk.TargetList list = new Gtk.TargetList ();
 
125
                                        list.AddTextTargets (id);
 
126
                                        //HACK: work around gtk_selection_data_set_text causing crashes on Mac w/ QuickSilver, Clipbard History etc.
 
127
                                        if (Platform.IsMac) {
 
128
                                                list.Remove ("COMPOUND_TEXT");
 
129
                                                list.Remove ("TEXT");
 
130
                                                list.Remove ("STRING");
 
131
                                        }
 
132
                                        entries = (Gtk.TargetEntry[])list;
 
133
                                }
 
134
                                else if (type == TransferDataType.Rtf) {
 
135
                                        Gdk.Atom atom;
 
136
                                        if (Platform.IsMac)
 
137
                                                atom = Gdk.Atom.Intern ("NSRTFPboardType", false); //TODO: use public.rtf when dep on MacOS 10.6
 
138
                                        else
 
139
                                                atom = Gdk.Atom.Intern ("text/rtf", false);
 
140
                                        entries = new Gtk.TargetEntry[] { new Gtk.TargetEntry (atom, 0, id) };
 
141
                                }
 
142
                                else {
 
143
                                        entries = new Gtk.TargetEntry[] { new Gtk.TargetEntry (Gdk.Atom.Intern ("application/" + type.Id, false), 0, id) };
 
144
                                }
 
145
                                
 
146
                                foreach (var a in entries.Select (e => e.Target))
 
147
                                        atomToType [a] = type;
 
148
                                return dragTargets [type] = entries;
 
149
                        }
 
150
                }       
 
151
                
 
152
                static Dictionary<string,string> icons;
 
153
 
 
154
                public static string ToGtkStock (string id)
 
155
                {
 
156
                        if (icons == null) {
 
157
                                icons = new Dictionary<string, string> ();
 
158
                                icons [StockIcons.ZoomIn] = Gtk.Stock.ZoomIn;
 
159
                                icons [StockIcons.ZoomOut] = Gtk.Stock.ZoomOut;
 
160
                                icons [StockIcons.Zoom100] = Gtk.Stock.Zoom100;
 
161
                                icons [StockIcons.ZoomFit] = Gtk.Stock.ZoomFit;
 
162
                                icons [StockIcons.OrientationPortrait] = Gtk.Stock.OrientationPortrait;
 
163
                                icons [StockIcons.OrientationLandscape] = Gtk.Stock.OrientationLandscape;
 
164
                                icons [StockIcons.Add] = Gtk.Stock.Add;
 
165
                                icons [StockIcons.Remove] = Gtk.Stock.Remove;
 
166
                                icons [StockIcons.Warning] = Gtk.Stock.DialogWarning;
 
167
                                icons [StockIcons.Error] = Gtk.Stock.DialogError;
 
168
                                icons [StockIcons.Information] = Gtk.Stock.DialogInfo;
 
169
                        }
 
170
                        string res;
 
171
                        icons.TryGetValue (id, out res);
 
172
                        return res;
 
173
                }
 
174
                
 
175
                public static Gtk.IconSize ToGtkSize (Xwt.IconSize size)
 
176
                {
 
177
                        switch (size) {
 
178
                        case IconSize.Small:
 
179
                                return Gtk.IconSize.Menu;
 
180
                        case IconSize.Medium:
 
181
                                return Gtk.IconSize.Button;
 
182
                        case IconSize.Large:
 
183
                                return Gtk.IconSize.Dialog;
 
184
                        }
 
185
                        return Gtk.IconSize.Dialog;
 
186
                }
 
187
                
 
188
                public static Gdk.Color ToGdkColor (this Xwt.Drawing.Color color)
 
189
                {
 
190
                        return new Gdk.Color ((byte)(color.Red * 255), (byte)(color.Green * 255), (byte)(color.Blue * 255));
 
191
                }
 
192
                
 
193
                public static Color ToXwtColor (this Gdk.Color color)
 
194
                {
 
195
                        return new Color ((double)color.Red / (double)ushort.MaxValue, (double)color.Green / (double)ushort.MaxValue, (double)color.Blue / (double)ushort.MaxValue);
 
196
                }
 
197
                
 
198
                public static ScrollPolicy ConvertScrollPolicy (Gtk.PolicyType p)
 
199
                {
 
200
                        switch (p) {
 
201
                        case Gtk.PolicyType.Always:
 
202
                                return ScrollPolicy.Always;
 
203
                        case Gtk.PolicyType.Automatic:
 
204
                                return ScrollPolicy.Automatic;
 
205
                        case Gtk.PolicyType.Never:
 
206
                                return ScrollPolicy.Never;
 
207
                        }
 
208
                        throw new InvalidOperationException ("Invalid policy value:" + p);
 
209
                }
 
210
                
 
211
                public static Gtk.PolicyType ConvertScrollPolicy (ScrollPolicy p)
 
212
                {
 
213
                        switch (p) {
 
214
                        case ScrollPolicy.Always:
 
215
                                return Gtk.PolicyType.Always;
 
216
                        case ScrollPolicy.Automatic:
 
217
                                return Gtk.PolicyType.Automatic;
 
218
                        case ScrollPolicy.Never:
 
219
                                return Gtk.PolicyType.Never;
 
220
                        }
 
221
                        throw new InvalidOperationException ("Invalid policy value:" + p);
 
222
                }
 
223
 
 
224
        public static ScrollDirection ConvertScrollDirection(Gdk.ScrollDirection d)
 
225
        {
 
226
            switch(d) {
 
227
            case Gdk.ScrollDirection.Up:
 
228
                return Xwt.ScrollDirection.Up;
 
229
            case Gdk.ScrollDirection.Down:
 
230
                return Xwt.ScrollDirection.Down;
 
231
            case Gdk.ScrollDirection.Left:
 
232
                return Xwt.ScrollDirection.Left;
 
233
            case Gdk.ScrollDirection.Right:
 
234
                return Xwt.ScrollDirection.Right;
 
235
            }
 
236
            throw new InvalidOperationException("Invalid mouse scroll direction value: " + d);
 
237
        }
 
238
 
 
239
        public static Gdk.ScrollDirection ConvertScrollDirection(ScrollDirection d)
 
240
        {
 
241
            switch (d) {
 
242
            case ScrollDirection.Up:
 
243
                return Gdk.ScrollDirection.Up;
 
244
            case ScrollDirection.Down:
 
245
                return Gdk.ScrollDirection.Down;
 
246
            case ScrollDirection.Left:
 
247
                return Gdk.ScrollDirection.Left;
 
248
            case ScrollDirection.Right:
 
249
                return Gdk.ScrollDirection.Right;
 
250
            }
 
251
            throw new InvalidOperationException("Invalid mouse scroll direction value: " + d);
 
252
        }
 
253
        }
 
254
}
 
255