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

« back to all changes in this revision

Viewing changes to external/xwt/Xwt.Gtk/Xwt.GtkBackend/GtkEngine.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
// GtkEngine.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
#define USE_PANGO
 
28
 
 
29
using System;
 
30
using Xwt.Engine;
 
31
using Xwt.Backends;
 
32
using Xwt.CairoBackend;
 
33
 
 
34
namespace Xwt.GtkBackend
 
35
{
 
36
        class GtkEngine: Xwt.Backends.EngineBackend
 
37
        {
 
38
                public override void InitializeApplication ()
 
39
                {
 
40
                        Gtk.Application.Init ();
 
41
                        
 
42
                        WidgetRegistry.RegisterBackend (typeof(Xwt.Widget), typeof(CustomWidgetBackend));
 
43
                        WidgetRegistry.RegisterBackend (typeof(Xwt.Window), typeof(WindowBackend));
 
44
                        WidgetRegistry.RegisterBackend (typeof(Xwt.Label), typeof(LabelBackend));
 
45
                        WidgetRegistry.RegisterBackend (typeof(Xwt.HBox), typeof(BoxBackend));
 
46
                        WidgetRegistry.RegisterBackend (typeof(Xwt.VBox), typeof(BoxBackend));
 
47
                        WidgetRegistry.RegisterBackend (typeof(Xwt.Button), typeof(ButtonBackend));
 
48
                        WidgetRegistry.RegisterBackend (typeof(Xwt.Notebook), typeof(NotebookBackend));
 
49
                        WidgetRegistry.RegisterBackend (typeof(Xwt.TreeView), typeof(TreeViewBackend));
 
50
                        WidgetRegistry.RegisterBackend (typeof(Xwt.TreeStore), typeof(TreeStoreBackend));
 
51
                        WidgetRegistry.RegisterBackend (typeof(Xwt.ListView), typeof(ListViewBackend));
 
52
                        WidgetRegistry.RegisterBackend (typeof(Xwt.ListStore), typeof(ListStoreBackend));
 
53
                        WidgetRegistry.RegisterBackend (typeof(Xwt.Canvas), typeof(CanvasBackend));
 
54
                        WidgetRegistry.RegisterBackend (typeof(Xwt.Drawing.Image), typeof(ImageHandler));
 
55
#if USE_PANGO
 
56
                        WidgetRegistry.RegisterBackend (typeof(Xwt.Drawing.Context), typeof(ContextBackendHandlerWithPango));
 
57
                        WidgetRegistry.RegisterBackend (typeof(Xwt.Drawing.TextLayout), typeof(TextLayoutBackendHandler));
 
58
#else
 
59
                        WidgetRegistry.RegisterBackend (typeof(Xwt.Drawing.Context), typeof(ContextBackendHandler));
 
60
                        WidgetRegistry.RegisterBackend (typeof(Xwt.Drawing.TextLayout), typeof(CairoTextLayoutBackendHandler));
 
61
#endif
 
62
                        WidgetRegistry.RegisterBackend (typeof(Xwt.Drawing.Gradient), typeof(CairoGradientBackendHandler));
 
63
                        WidgetRegistry.RegisterBackend (typeof(Xwt.Drawing.Font), typeof(FontBackendHandler));
 
64
                        WidgetRegistry.RegisterBackend (typeof(Xwt.Menu), typeof(MenuBackend));
 
65
                        WidgetRegistry.RegisterBackend (typeof(Xwt.MenuItem), typeof(MenuItemBackend));
 
66
                        WidgetRegistry.RegisterBackend (typeof(Xwt.CheckBoxMenuItem), typeof(CheckBoxMenuItemBackend));
 
67
                        WidgetRegistry.RegisterBackend (typeof(Xwt.RadioButtonMenuItem), typeof(RadioButtonMenuItemBackend));
 
68
                        WidgetRegistry.RegisterBackend (typeof(Xwt.SeparatorMenuItem), typeof(SeparatorMenuItemBackend));
 
69
                        WidgetRegistry.RegisterBackend (typeof(Xwt.ScrollView), typeof(ScrollViewBackend));
 
70
                        WidgetRegistry.RegisterBackend (typeof(Xwt.ComboBox), typeof(ComboBoxBackend));
 
71
                        WidgetRegistry.RegisterBackend (typeof(Xwt.Design.DesignerSurface), typeof(DesignerSurfaceBackend));
 
72
                        WidgetRegistry.RegisterBackend (typeof(Xwt.MenuButton), typeof(MenuButtonBackend));
 
73
                        WidgetRegistry.RegisterBackend (typeof(Xwt.TextEntry), typeof(TextEntryBackend));
 
74
                        WidgetRegistry.RegisterBackend (typeof(Xwt.ToggleButton), typeof(ToggleButtonBackend));
 
75
                        WidgetRegistry.RegisterBackend (typeof(Xwt.ImageView), typeof(ImageViewBackend));
 
76
                        WidgetRegistry.RegisterBackend (typeof(Xwt.Backends.IAlertDialogBackend), typeof(AlertDialogBackend));
 
77
                        WidgetRegistry.RegisterBackend (typeof(Xwt.Table), typeof(BoxBackend));
 
78
                        WidgetRegistry.RegisterBackend (typeof(Xwt.CheckBox), typeof(CheckBoxBackend));
 
79
                        WidgetRegistry.RegisterBackend (typeof(Xwt.Frame), typeof(FrameBackend));
 
80
                        WidgetRegistry.RegisterBackend (typeof(Xwt.VSeparator), typeof(SeparatorBackend));
 
81
                        WidgetRegistry.RegisterBackend (typeof(Xwt.HSeparator), typeof(SeparatorBackend));
 
82
                        WidgetRegistry.RegisterBackend (typeof(Xwt.Dialog), typeof(DialogBackend));
 
83
                        WidgetRegistry.RegisterBackend (typeof(Xwt.ComboBoxEntry), typeof(ComboBoxEntryBackend));
 
84
                        WidgetRegistry.RegisterBackend (typeof(Xwt.Clipboard), typeof(ClipboardBackend));
 
85
                        WidgetRegistry.RegisterBackend (typeof(Xwt.Drawing.ImagePattern), typeof(ImagePatternBackendHandler));
 
86
                        WidgetRegistry.RegisterBackend (typeof(Xwt.Drawing.ImageBuilder), typeof(ImageBuilderBackend));
 
87
                        WidgetRegistry.RegisterBackend (typeof(Xwt.ScrollAdjustment), typeof(ScrollAdjustmentBackend));
 
88
                        WidgetRegistry.RegisterBackend (typeof(Xwt.OpenFileDialog), typeof(OpenFileDialogBackend));
 
89
                        WidgetRegistry.RegisterBackend (typeof(Xwt.SaveFileDialog), typeof(SaveFileDialogBackend));
 
90
                        WidgetRegistry.RegisterBackend (typeof(Xwt.SelectFolderDialog), typeof(SelectFolderDialogBackend));
 
91
                        WidgetRegistry.RegisterBackend (typeof(Xwt.Paned), typeof(PanedBackend));
 
92
                        WidgetRegistry.RegisterBackend (typeof(Xwt.SelectColorDialog), typeof(SelectColorDialogBackend));
 
93
                        WidgetRegistry.RegisterBackend (typeof(Xwt.ListBox), typeof(ListBoxBackend));
 
94
                        WidgetRegistry.RegisterBackend (typeof(Xwt.StatusIcon), typeof(StatusIconBackend));
 
95
                        WidgetRegistry.RegisterBackend (typeof(Xwt.ProgressBar), typeof(ProgressBarBackend));
 
96
                        WidgetRegistry.RegisterBackend (typeof(Xwt.Popover), typeof (PopoverBackend));
 
97
                        WidgetRegistry.RegisterBackend (typeof(Xwt.SpinButton), typeof (SpinButtonBackend));
 
98
                        WidgetRegistry.RegisterBackend (typeof(Xwt.DatePicker), typeof (DatePickerBackend));
 
99
                        WidgetRegistry.RegisterBackend (typeof(Xwt.LinkLabel), typeof (LinkLabelBackend));
 
100
                        WidgetRegistry.RegisterBackend (typeof(Xwt.Placement), typeof (BoxBackend));
 
101
                        WidgetRegistry.RegisterBackend (typeof(Xwt.Spinner), typeof (SpinnerBackend));
 
102
                        WidgetRegistry.RegisterBackend (typeof(Xwt.RichTextView), typeof (RichTextViewBackend));
 
103
                        WidgetRegistry.RegisterBackend (typeof(Xwt.Expander), typeof (ExpanderBackend));
 
104
                }
 
105
 
 
106
                public override void RunApplication ()
 
107
                {
 
108
                        Gtk.Application.Run ();
 
109
                }
 
110
                
 
111
                public override void ExitApplication ()
 
112
                {
 
113
                        Gtk.Application.Quit ();
 
114
                }
 
115
                
 
116
                public override bool HandlesSizeNegotiation {
 
117
                        get {
 
118
                                return true;
 
119
                        }
 
120
                }
 
121
 
 
122
                public static void ReplaceChild (Gtk.Widget oldWidget, Gtk.Widget newWidget)
 
123
                {
 
124
                        Gtk.Container cont = oldWidget.Parent as Gtk.Container;
 
125
                        if (cont == null)
 
126
                                return;
 
127
                        
 
128
                        if (cont is IGtkContainer) {
 
129
                                ((IGtkContainer)cont).ReplaceChild (oldWidget, newWidget);
 
130
                        }
 
131
                        else if (cont is Gtk.Notebook) {
 
132
                                Gtk.Notebook notebook = (Gtk.Notebook) cont;
 
133
                                Gtk.Notebook.NotebookChild nc = (Gtk.Notebook.NotebookChild) notebook[oldWidget];
 
134
                                var detachable = nc.Detachable;
 
135
                                var pos = nc.Position;
 
136
                                var reorderable = nc.Reorderable;
 
137
                                var tabExpand = nc.TabExpand;
 
138
                                var tabFill = nc.TabFill;
 
139
                                var label = notebook.GetTabLabel (oldWidget);
 
140
                                notebook.Remove (oldWidget);
 
141
                                notebook.InsertPage (newWidget, label, pos);
 
142
                                
 
143
                                nc = (Gtk.Notebook.NotebookChild) notebook[newWidget];
 
144
                                nc.Detachable = detachable;
 
145
                                nc.Reorderable = reorderable;
 
146
                                nc.TabExpand = tabExpand;
 
147
                                nc.TabFill = tabFill;
 
148
                        }
 
149
                        else if (cont is Gtk.Bin) {
 
150
                                ((Gtk.Bin)cont).Remove (oldWidget);
 
151
                                ((Gtk.Bin)cont).Child  = newWidget;
 
152
                        }
 
153
                }
 
154
                
 
155
                public override void InvokeAsync (Action action)
 
156
                {
 
157
                        if (action == null)
 
158
                                throw new ArgumentNullException ("action");
 
159
 
 
160
                        Gtk.Application.Invoke (delegate {
 
161
                                action ();
 
162
                        });
 
163
                }
 
164
 
 
165
                public override object TimerInvoke (Func<bool> action, TimeSpan timeSpan)
 
166
                {
 
167
                        if (action == null)
 
168
                                throw new ArgumentNullException ("action");
 
169
                        if (timeSpan.TotalMilliseconds < 0)
 
170
                                throw new ArgumentException ("Timer period must be >=0", "timeSpan");
 
171
 
 
172
                        return GLib.Timeout.Add ((uint) timeSpan.TotalMilliseconds, delegate {
 
173
                                return action ();
 
174
                        });
 
175
                }
 
176
 
 
177
                public override void CancelTimerInvoke (object id)
 
178
                {
 
179
                        if (id == null)
 
180
                                throw new ArgumentNullException ("id");
 
181
 
 
182
                        GLib.Source.Remove ((uint)id);
 
183
                }
 
184
 
 
185
                public override object GetNativeWidget (Widget w)
 
186
                {
 
187
                        IGtkWidgetBackend wb = (IGtkWidgetBackend)WidgetRegistry.GetBackend (w);
 
188
                        return wb.Widget;
 
189
                }
 
190
                
 
191
                public override IWindowFrameBackend GetBackendForWindow (object nativeWindow)
 
192
                {
 
193
                        var win = new WindowFrameBackend ();
 
194
                        win.Window = (Gtk.Window) nativeWindow;
 
195
                        return win;
 
196
                }
 
197
                
 
198
                public override object GetNativeParentWindow (Widget w)
 
199
                {
 
200
                        IGtkWidgetBackend wb = (IGtkWidgetBackend)WidgetRegistry.GetBackend (w);
 
201
                        return wb.Widget.Toplevel as Gtk.Window;
 
202
                }
 
203
 
 
204
                public override bool HasNativeParent (Widget w)
 
205
                {
 
206
                        IGtkWidgetBackend wb = (IGtkWidgetBackend)WidgetRegistry.GetBackend (w);
 
207
                        return wb.Widget.Parent != null;
 
208
                }
 
209
 
 
210
                public override void DispatchPendingEvents ()
 
211
                {
 
212
                        // The loop is limited to 1000 iterations as a workaround for an issue that some users
 
213
                        // have experienced. Sometimes EventsPending starts return 'true' for all iterations,
 
214
                        // causing the loop to never end.
 
215
 
 
216
                        int n = 1000;
 
217
                        Gdk.Threads.Enter();
 
218
                        
 
219
                        while (Gtk.Application.EventsPending () && --n > 0) {
 
220
                                Gtk.Application.RunIteration (false);
 
221
                        }
 
222
                        
 
223
                        Gdk.Threads.Leave();
 
224
                }
 
225
        }
 
226
        
 
227
        public interface IGtkContainer
 
228
        {
 
229
                void ReplaceChild (Gtk.Widget oldWidget, Gtk.Widget newWidget);
 
230
        }
 
231
}
 
232