From dac6093ddbfe896217e6bd05e618c1f360027c23 Mon Sep 17 00:00:00 2001
From: sebastiii <sebastiii@team-mediaportal.com>
Date: Wed, 30 Sep 2015 20:39:21 +0200
Subject: [PATCH] Fork : Restore MP windows from last position.

---
 mediaportal/MediaPortal.Application/D3D.cs         | 13 +++++-
 mediaportal/MediaPortal.Application/MediaPortal.cs | 51 +++++++++++++++++-----
 2 files changed, 51 insertions(+), 13 deletions(-)

diff --git a/mediaportal/MediaPortal.Application/D3D.cs b/mediaportal/MediaPortal.Application/D3D.cs
index 0214219..403edcf 100644
--- a/mediaportal/MediaPortal.Application/D3D.cs
+++ b/mediaportal/MediaPortal.Application/D3D.cs
@@ -2545,7 +2545,18 @@ namespace MediaPortal
     /// Clean up any resources
     /// </summary>
     protected override void Dispose(bool disposing)
-    {
+    {
+      // Store MP Windowed
+      using (var xmlWriter = new MPSettings())
+      {
+        xmlWriter.SetValue("gui", "lastrecttop", Location.Y);
+        xmlWriter.SetValue("gui", "lastrectleft", Location.X);
+        xmlWriter.SetValue("gui", "lastrectbottom", Size.Height);
+        xmlWriter.SetValue("gui", "lastrectright", Size.Width);
+        xmlWriter.SetValue("gui", "lastlocationx", Location.X);
+        xmlWriter.SetValue("gui", "lastlocationy", Location.Y);
+      }
+
       CleanupEnvironment();
 
       if (_notifyIcon != null)
diff --git a/mediaportal/MediaPortal.Application/MediaPortal.cs b/mediaportal/MediaPortal.Application/MediaPortal.cs
index c22bc9a..fc70f7e 100644
--- a/mediaportal/MediaPortal.Application/MediaPortal.cs
+++ b/mediaportal/MediaPortal.Application/MediaPortal.cs
@@ -134,6 +134,13 @@ public class MediaPortalApp : D3D, IRender
   private bool                  _delayedResume;
   private readonly Object       _delayedResumeLock = new Object();
   private bool                  _keepstartfullscreen;
+  private int                   _lastRectTop;
+  private int                   _lastRectLeft;
+  private int                   _lastRectBottom;
+  private int                   _lastRectRight;
+  private int                   _locationX;
+  private int                   _locationY;
+  private bool                  _firstRestoreScreen = true;
 
   // ReSharper disable InconsistentNaming
   private const int WM_SYSCOMMAND            = 0x0112; // http://msdn.microsoft.com/en-us/library/windows/desktop/ms646360(v=vs.85).aspx
@@ -2562,6 +2569,12 @@ public class MediaPortalApp : D3D, IRender
               x + border.Width, y + border.Height, x + border.Width, height + border.Height, x, height);
             ClientSize = new Size(x, height);
           }
+          // Restore last MP windows
+          if (_firstRestoreScreen)
+          {
+            Location = new Point(_locationX, _locationY);
+            _firstRestoreScreen = false;
+          }
         }
         else
         {
@@ -3338,16 +3351,37 @@ public class MediaPortalApp : D3D, IRender
     Utils.FileExistsInCache(Thumbs.Videos + "\\dummy.png");
     Utils.FileExistsInCache(Thumbs.MusicFolder + "\\dummy.png");
 
+    using (Settings xmlreader = new MPSettings())
+    {
+      _useLongDateFormat = xmlreader.GetValueAsBool("home", "LongTimeFormat", false);
+      _startWithBasicHome = xmlreader.GetValueAsBool("gui", "startbasichome", false);
+      _useOnlyOneHome = xmlreader.GetValueAsBool("gui", "useonlyonehome", false);
+      _lastRectTop = xmlreader.GetValueAsInt("gui", "lastrecttop", 0);
+      _lastRectLeft = xmlreader.GetValueAsInt("gui", "lastrectleft", 0);
+      _lastRectBottom = xmlreader.GetValueAsInt("gui", "lastrectbottom", 0);
+      _lastRectRight = xmlreader.GetValueAsInt("gui", "lastrectright", 0);
+      _locationX = xmlreader.GetValueAsInt("gui", "lastlocationx", 0);
+      _locationY = xmlreader.GetValueAsInt("gui", "lastlocationy", 0);
+    }
+
     // Resize Form
     if (Windowed)
     {
       Log.Debug("Startup: Resizing form to Skin Dimensions");
       ClientSize      = CalcMaxClientArea();
-      LastRect.top    = Location.Y;
-      LastRect.left   = Location.X;
-      LastRect.bottom = Size.Height;
-      LastRect.right  = Size.Width;
-      Location = new Point(GUIGraphicsContext.currentScreen.Bounds.X, GUIGraphicsContext.currentScreen.Bounds.Y);
+
+      // Restore last position
+      LastRect.top = _lastRectTop;
+      LastRect.left = _lastRectLeft;
+      LastRect.bottom = _lastRectBottom;
+      LastRect.right = _lastRectRight;
+      Location = new Point(_locationX, _locationY);
+
+      //  LastRect.top = Location.Y;
+      //  LastRect.left = Location.X;
+      //  LastRect.bottom = Size.Height;
+      //  LastRect.right = Size.Width;
+      //  Location = new Point(GUIGraphicsContext.currentScreen.Bounds.X, GUIGraphicsContext.currentScreen.Bounds.Y);
     }
     else
     {
@@ -3377,13 +3411,6 @@ public class MediaPortalApp : D3D, IRender
     GUIGraphicsContext.Load();
     GUIWindowManager.Initialize();
 
-    using (Settings xmlreader = new MPSettings())
-    {
-      _useLongDateFormat = xmlreader.GetValueAsBool("home", "LongTimeFormat", false);
-      _startWithBasicHome = xmlreader.GetValueAsBool("gui", "startbasichome", false);
-      _useOnlyOneHome = xmlreader.GetValueAsBool("gui", "useonlyonehome", false);
-    }
-
     Log.Info("Startup: Starting Window Manager");
     GUIWindowManager.PreInit();
     GUIGraphicsContext.CurrentState = GUIGraphicsContext.State.RUNNING;
-- 
1.9.5.msysgit.0

