5
// Lluis Sanchez <lluis@xamarin.com>
7
// Copyright (c) 2011 Xamarin Inc
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:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
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
32
using Xwt.CairoBackend;
34
namespace Xwt.GtkBackend
36
class GtkEngine: Xwt.Backends.EngineBackend
38
public override void InitializeApplication ()
40
Gtk.Application.Init ();
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));
56
WidgetRegistry.RegisterBackend (typeof(Xwt.Drawing.Context), typeof(ContextBackendHandlerWithPango));
57
WidgetRegistry.RegisterBackend (typeof(Xwt.Drawing.TextLayout), typeof(TextLayoutBackendHandler));
59
WidgetRegistry.RegisterBackend (typeof(Xwt.Drawing.Context), typeof(ContextBackendHandler));
60
WidgetRegistry.RegisterBackend (typeof(Xwt.Drawing.TextLayout), typeof(CairoTextLayoutBackendHandler));
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));
106
public override void RunApplication ()
108
Gtk.Application.Run ();
111
public override void ExitApplication ()
113
Gtk.Application.Quit ();
116
public override bool HandlesSizeNegotiation {
122
public static void ReplaceChild (Gtk.Widget oldWidget, Gtk.Widget newWidget)
124
Gtk.Container cont = oldWidget.Parent as Gtk.Container;
128
if (cont is IGtkContainer) {
129
((IGtkContainer)cont).ReplaceChild (oldWidget, newWidget);
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);
143
nc = (Gtk.Notebook.NotebookChild) notebook[newWidget];
144
nc.Detachable = detachable;
145
nc.Reorderable = reorderable;
146
nc.TabExpand = tabExpand;
147
nc.TabFill = tabFill;
149
else if (cont is Gtk.Bin) {
150
((Gtk.Bin)cont).Remove (oldWidget);
151
((Gtk.Bin)cont).Child = newWidget;
155
public override void InvokeAsync (Action action)
158
throw new ArgumentNullException ("action");
160
Gtk.Application.Invoke (delegate {
165
public override object TimerInvoke (Func<bool> action, TimeSpan timeSpan)
168
throw new ArgumentNullException ("action");
169
if (timeSpan.TotalMilliseconds < 0)
170
throw new ArgumentException ("Timer period must be >=0", "timeSpan");
172
return GLib.Timeout.Add ((uint) timeSpan.TotalMilliseconds, delegate {
177
public override void CancelTimerInvoke (object id)
180
throw new ArgumentNullException ("id");
182
GLib.Source.Remove ((uint)id);
185
public override object GetNativeWidget (Widget w)
187
IGtkWidgetBackend wb = (IGtkWidgetBackend)WidgetRegistry.GetBackend (w);
191
public override IWindowFrameBackend GetBackendForWindow (object nativeWindow)
193
var win = new WindowFrameBackend ();
194
win.Window = (Gtk.Window) nativeWindow;
198
public override object GetNativeParentWindow (Widget w)
200
IGtkWidgetBackend wb = (IGtkWidgetBackend)WidgetRegistry.GetBackend (w);
201
return wb.Widget.Toplevel as Gtk.Window;
204
public override bool HasNativeParent (Widget w)
206
IGtkWidgetBackend wb = (IGtkWidgetBackend)WidgetRegistry.GetBackend (w);
207
return wb.Widget.Parent != null;
210
public override void DispatchPendingEvents ()
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.
219
while (Gtk.Application.EventsPending () && --n > 0) {
220
Gtk.Application.RunIteration (false);
227
public interface IGtkContainer
229
void ReplaceChild (Gtk.Widget oldWidget, Gtk.Widget newWidget);