// ----------------------------------------------------------------------- // // Copyright 2014 MIT Licence. See licence.md for more information. // // ----------------------------------------------------------------------- namespace Perspex.Gtk { using System; using System.Threading.Tasks; using Perspex.Controls; using Perspex.Input.Raw; using Perspex.Platform; using Gtk = global::Gtk; public class WindowImpl : Gtk.Window, IWindowImpl { private TopLevel owner; private IPlatformHandle windowHandle; private Size clientSize; public WindowImpl() : base(Gtk.WindowType.Toplevel) { this.DefaultSize = new Gdk.Size(640, 480); this.Events = Gdk.EventMask.PointerMotionMask | Gdk.EventMask.ButtonPressMask | Gdk.EventMask.ButtonReleaseMask; this.windowHandle = new PlatformHandle(this.Handle, "GtkWindow"); } public Size ClientSize { get { return this.clientSize; } set { this.Resize((int)value.Width, (int)value.Height); } } IPlatformHandle ITopLevelImpl.Handle { get { return this.windowHandle; } } public Action Activated { get; set; } public Action Closed { get; set; } public Action Deactivated { get; set; } public Action Input { get; set; } public Action Paint { get; set; } public Action Resized { get; set; } public IPopupImpl CreatePopup() { throw new NotImplementedException(); } public void Invalidate(Rect rect) { this.QueueDraw(); } public Point PointToScreen(Point point) { // TODO: Implement. return new Point(); } public void SetOwner(TopLevel owner) { this.owner = owner; } public void SetTitle(string title) { this.Title = title; } public IDisposable ShowDialog() { throw new NotImplementedException(); } void ITopLevelImpl.Activate() { this.Activate(); } protected override bool OnButtonPressEvent(Gdk.EventButton evnt) { var e = new RawMouseEventArgs( GtkMouseDevice.Instance, evnt.Time, this.owner, RawMouseEventType.LeftButtonDown, new Point(evnt.X, evnt.Y)); this.Input(e); return true; } protected override bool OnButtonReleaseEvent(Gdk.EventButton evnt) { var e = new RawMouseEventArgs( GtkMouseDevice.Instance, evnt.Time, this.owner, RawMouseEventType.LeftButtonUp, new Point(evnt.X, evnt.Y)); this.Input(e); return true; } protected override bool OnConfigureEvent(Gdk.EventConfigure evnt) { var newSize = new Size(evnt.Width, evnt.Height); if (newSize != this.clientSize) { this.clientSize = newSize; this.Resized(this.clientSize); } return true; } protected override void OnDestroyed() { this.Closed(); } protected override bool OnKeyPressEvent(Gdk.EventKey evnt) { var e = new RawKeyEventArgs( GtkKeyboardDevice.Instance, evnt.Time, RawKeyEventType.KeyDown, GtkKeyboardDevice.ConvertKey(evnt.Key), new string((char)Gdk.Keyval.ToUnicode((uint)evnt.Key), 1)); this.Input(e); return true; } protected override bool OnExposeEvent(Gdk.EventExpose evnt) { this.Paint(evnt.Area.ToPerspex(), this.GetHandle(evnt.Window)); return true; } protected override void OnFocusActivated() { this.Activated(); } protected override bool OnMotionNotifyEvent(Gdk.EventMotion evnt) { var position = new Point(evnt.X, evnt.Y); GtkMouseDevice.Instance.SetClientPosition(position); var e = new RawMouseEventArgs( GtkMouseDevice.Instance, evnt.Time, this.owner, RawMouseEventType.Move, position); this.Input(e); return true; } private IPlatformHandle GetHandle(Gdk.Window window) { return new PlatformHandle(window.Handle, "GdkWindow"); } } }