2
// WindowFrameBackend.cs
5
// Carlos Alberto Cortez <calberto.cortez@gmail.com>
7
// Copyright (c) 2011 Carlos Alberto Cortez
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
28
using System.Collections.Generic;
36
namespace Xwt.WPFBackend
38
public class WindowFrameBackend : IWindowFrameBackend
40
System.Windows.Window window;
41
IWindowFrameEventSink eventSink;
44
public WindowFrameBackend ()
48
void IBackend.InitializeBackend (object frontend)
50
this.frontend = (WindowFrame) frontend;
53
void IWindowFrameBackend.Initialize (IWindowFrameEventSink eventSink)
55
this.eventSink = eventSink;
59
public virtual void Initialize ()
63
public virtual void Dispose ()
68
public System.Windows.Window Window {
69
get { return window; }
70
set { window = value; }
73
public virtual bool HasMenu {
77
protected WindowFrame Frontend {
78
get { return frontend; }
81
public IWindowFrameEventSink EventSink
83
get { return eventSink; }
86
bool IWindowFrameBackend.Decorated {
87
get { return window.WindowStyle != WindowStyle.None; }
88
set { window.WindowStyle = value ? WindowStyle.SingleBorderWindow : WindowStyle.None; }
91
bool IWindowFrameBackend.ShowInTaskbar {
92
get { return window.ShowInTaskbar; }
93
set { window.ShowInTaskbar = value; }
96
void IWindowFrameBackend.SetTransientFor (IWindowFrameBackend window)
98
this.Window.Owner = ((WindowFrameBackend) window).Window;
101
bool IWindowFrameBackend.Resizable {
103
return window.ResizeMode == ResizeMode.CanResize;
106
if (value != ((IWindowFrameBackend)this).Resizable) {
108
window.ResizeMode = value ? ResizeMode.CanResize : ResizeMode.NoResize;
109
if (window.IsLoaded && bounds != Bounds) {
110
// The size of the border of resizable windows is different from fixed windows.
111
// If we change the resize mode, the border size will change, and the client
112
// area will then change. Here, we restore the client area to the size it
113
// had before the mode change.
120
public void SetIcon (object imageBackend)
122
window.Icon = DataConverter.AsImageSource (imageBackend);
125
string IWindowFrameBackend.Title {
126
get { return window.Title; }
127
set { window.Title = value; }
130
bool IWindowFrameBackend.Visible
132
get { return window.Visibility == Visibility.Visible; }
133
set { window.Visibility = value ? Visibility.Visible : Visibility.Hidden; }
136
void IWindowFrameBackend.Present ()
141
public void Move (double x, double y)
143
var value = ToNonClientRect (new Rectangle (x, y, 1, 1));
144
window.Top = value.Top;
145
window.Left = value.Left;
146
Toolkit.Invoke (delegate
148
eventSink.OnBoundsChanged (Bounds);
152
public void Resize (double width, double height)
154
var value = ToNonClientRect (new Rectangle (0, 0, width, height));
155
window.Width = value.Width;
156
window.Height = value.Height;
157
Toolkit.Invoke (delegate
159
eventSink.OnBoundsChanged (Bounds);
163
public Rectangle Bounds {
165
double width = Double.IsNaN (window.Width) ? window.ActualWidth : window.Width;
166
double height = Double.IsNaN (window.Height) ? window.ActualHeight : window.Height;
167
return ToClientRect (new Rectangle (window.Left, window.Top, width, height));
170
value = ToNonClientRect (value);
171
window.Top = value.Top;
172
window.Left = value.Left;
173
window.Width = value.Width;
174
window.Height = value.Height;
175
Toolkit.Invoke (delegate {
176
eventSink.OnBoundsChanged (Bounds);
181
public virtual void EnableEvent (object eventId)
183
if (eventId is WindowFrameEvent) {
184
switch ((WindowFrameEvent)eventId) {
185
case WindowFrameEvent.BoundsChanged:
186
window.LocationChanged += BoundsChangedHandler;
187
window.SizeChanged += BoundsChangedHandler;
189
case WindowFrameEvent.Shown:
190
window.IsVisibleChanged += ShownHandler;
192
case WindowFrameEvent.Hidden:
193
window.IsVisibleChanged += HiddenHandler;
195
case WindowFrameEvent.CloseRequested:
196
window.Closing += ClosingHandler;
204
public virtual void DisableEvent (object eventId)
206
if (eventId is WindowFrameEvent) {
207
switch ((WindowFrameEvent)eventId) {
208
case WindowFrameEvent.BoundsChanged:
209
window.LocationChanged -= BoundsChangedHandler;
210
window.SizeChanged -= BoundsChangedHandler;
212
case WindowFrameEvent.Shown:
213
window.IsVisibleChanged -= ShownHandler;
215
case WindowFrameEvent.Hidden:
216
window.IsVisibleChanged -= HiddenHandler;
218
case WindowFrameEvent.CloseRequested:
219
window.Closing -= ClosingHandler;
225
void BoundsChangedHandler (object o, EventArgs args)
227
Toolkit.Invoke (delegate () {
228
eventSink.OnBoundsChanged (Bounds);
232
private void ShownHandler (object sender, DependencyPropertyChangedEventArgs e)
236
Toolkit.Invoke (delegate ()
238
eventSink.OnShown ();
243
private void HiddenHandler (object sender, DependencyPropertyChangedEventArgs e)
245
if((bool)e.NewValue == false)
247
Toolkit.Invoke (delegate ()
249
eventSink.OnHidden ();
254
private void ClosingHandler (object sender, System.ComponentModel.CancelEventArgs e)
256
Toolkit.Invoke (delegate ()
258
e.Cancel = eventSink.OnCloseRequested ();
262
Size GetBorderSize ()
264
if (window.ResizeMode == ResizeMode.CanResize)
265
return new Size (SystemParameters.ResizeFrameVerticalBorderWidth, SystemParameters.ResizeFrameHorizontalBorderHeight);
267
return new Size (SystemParameters.FixedFrameVerticalBorderWidth, SystemParameters.FixedFrameHorizontalBorderHeight);
270
protected Rectangle ToNonClientRect (Rectangle rect)
272
var size = rect.Size;
273
var loc = rect.Location;
275
var border = GetBorderSize ();
276
size.Height += border.Height * 2;
277
size.Width += border.Width * 2;
278
loc.X -= border.Width;
279
loc.Y -= border.Height;
281
if (((IWindowFrameBackend)this).Decorated) {
282
size.Height += SystemParameters.WindowCaptionHeight;
283
loc.Y -= SystemParameters.CaptionHeight;
286
size.Height += SystemParameters.MenuBarHeight;
287
loc.Y -= SystemParameters.MenuBarHeight;
290
return new Rectangle (loc, size);
293
protected Rectangle ToClientRect (Rectangle rect)
295
var size = rect.Size;
296
var loc = rect.Location;
298
var border = GetBorderSize ();
299
size.Height -= border.Height * 2;
300
size.Width -= border.Width * 2;
301
loc.X += border.Width;
302
loc.Y += border.Height;
304
if (((IWindowFrameBackend)this).Decorated) {
305
size.Height -= SystemParameters.CaptionHeight;
306
loc.Y += SystemParameters.CaptionHeight;
309
size.Height -= SystemParameters.MenuBarHeight;
310
loc.Y += SystemParameters.MenuBarHeight;
313
size.Width = Math.Max (0, size.Width);
314
size.Height = Math.Max (0, size.Height);
316
return new Rectangle (loc, size);