using System; using Orvid.Graphics; using System.Collections.Generic; using Orvid.Graphics.FontSupport; namespace OForms.Windows { /// /// The class that represents a WindowManager. /// public class WindowManager { /// /// The height of the taskbar. /// internal const int TaskBarHeight = Taskbar.TaskBarHeight; //internal static Font WindowFont = FontManager.Instance.LoadFont(1, new System.IO.MemoryStream(EmbeddedFiles.Fonts.Vera10_bdf)); /// /// The taskbar. /// private Taskbar Taskbar; /// /// The location of the mouse. /// public Vec2 MouseLocation = Vec2.Zero; /// /// The X location of the mouse. /// public int MouseX { get { return MouseLocation.X; } } /// /// The Y location of the mouse. /// public int MouseY { get { return MouseLocation.Y; } } /// /// Is true when all the windows need to be re-drawn, /// in other-words, is true if a window has been moved, /// resized, added, or removed. /// internal bool NeedToRedrawAll = false; /// /// An array containing all of the active windows /// in the current window manager instance. /// public Window[] ActiveWindows; /// /// The currently active window. Beware, /// there is no array bounds check. /// public Window ActiveWindow { get { return ActiveWindows[0]; } set { BringWindowToFront(value); } } /// /// The size of the screen. /// public Vec2 Size; /// /// The default constructor. /// public WindowManager(Vec2 size) { ActiveWindows = new Window[0]; this.Size = size; this.Taskbar = new Taskbar(this); } /// /// Draws all the windows on the specified image. /// /// The image to draw the windows on. public void Draw(Image i) { if (NeedToRedrawAll) { i.Clear(Colors.White); if (ActiveWindows.Length > 0 && ActiveWindows[0].CurrentState == WindowState.Maximized) { ActiveWindows[0].Draw(i); } else { for (int ind = ActiveWindows.Length - 1; ind >= 0; ind--) { if (ActiveWindows[ind].CurrentState != WindowState.Minimized) { ActiveWindows[ind].Draw(i); } } } NeedToRedrawAll = false; } else { if (ActiveWindows.Length > 0) { if (ActiveWindows[0].CurrentState != WindowState.Minimized) { ActiveWindows[0].Draw(i); } } } Taskbar.Draw(i); } /// /// Adds a window at the front. /// /// The window to add. public void AddWindow(Window w) { InternalAddWindow(w); Taskbar.AddWindow(w); } /// /// Adds the specified window without modifying the taskbar. /// /// The window to add. private void InternalAddWindow(Window w) { w.Parent = this; w.IsActiveWindow = true; if (ActiveWindows.Length > 0) { ActiveWindows[0].IsActiveWindow = false; } Window[] tmp = new Window[ActiveWindows.Length + 1]; Array.Copy(ActiveWindows, 0, tmp, 1, ActiveWindows.Length); tmp[0] = w; ActiveWindows = tmp; NeedToRedrawAll = true; } /// /// Maximize the specified window. /// /// The window to maximize. public void MaximizeWindow(Window w) { w.CurrentState = WindowState.Maximized; } /// /// Restore a window to the Normal state. /// /// The window to restore. public void RestoreWindow(Window w) { w.CurrentState = WindowState.Normal; } /// /// Minimizes the specified window. /// /// The window to minimize. public void MinimizeWindow(Window w) { if (w.IsActiveWindow) { this.SendWindowToBack(w); } w.CurrentState = WindowState.Minimized; } /// /// Sends the specified window to the back. /// /// The window to send to the back. public void SendWindowToBack(Window w) { if (w.IsActiveWindow) { w.IsActiveWindow = false; } for (int i = 0; i < ActiveWindows.Length; i++) { if (ActiveWindows[i] == w) { RemoveWindow(i); NeedToRedrawAll = true; Window[] winds = new Window[ActiveWindows.Length + 1]; Array.Copy(ActiveWindows, winds, ActiveWindows.Length); winds[winds.Length - 1] = w; ActiveWindows = winds; ActiveWindows[0].IsActiveWindow = true; Taskbar.Modified = true; return; } } throw new Exception("Unable to find the specified window."); } /// /// Bring the specified window to the front. /// /// The window to move to the front. public void BringWindowToFront(Window w) { if (ActiveWindows.Length > 0) { ActiveWindows[0].IsActiveWindow = false; } w.IsActiveWindow = true; for (int i = 0; i < ActiveWindows.Length; i++) { if (ActiveWindows[i] == w) { RemoveWindow(i); InternalAddWindow(w); NeedToRedrawAll = true; Taskbar.Modified = true; return; } } throw new Exception("Specified Window not found!"); } /// /// Removes the window at the specified index. /// /// The index of the window to remove. private void RemoveWindow(int indx) { Window[] tmp = new Window[ActiveWindows.Length - 1]; Array.Copy(ActiveWindows, tmp, indx); Array.Copy(ActiveWindows, indx + 1, tmp, indx, ActiveWindows.Length - indx - 1); ActiveWindows = tmp; if (ActiveWindows.Length > 0) { ActiveWindows[0].IsActiveWindow = true; } NeedToRedrawAll = true; } /// /// Closes the specified window. /// /// The window to close. public void CloseWindow(Window w) { for (int i = 0; i < ActiveWindows.Length; i++) { if (ActiveWindows[i] == w) { RemoveWindow(i); NeedToRedrawAll = true; w.DoClose(); Taskbar.RemoveWindow(w); return; } } } #region Handle Events #region Mouse Click /// /// Handles a MouseClick event. /// /// The location of the mouse. /// The MouseButtons that are pressed. /// The image to draw to. public void HandleMouseClick(Vec2 loc, MouseButtons buttons, Image i) { MouseLocation = loc; if (Taskbar.Bounds.IsInBounds(loc)) { Taskbar.DoClick(loc, buttons); } else { if (Taskbar.WasOverButton) { Taskbar.UndrawOverButton(Taskbar.WindowButtonBounds[Taskbar.overButtonIndx], Taskbar.Windows[Taskbar.overButtonIndx]); } foreach (Window w in ActiveWindows) { if (w.Bounds.IsInBounds(loc)) { w.DoClick(loc, OForms.MouseButtons.Left); break; } } } this.Draw(i); } #endregion #region Mouse Move /// /// Processes a MouseMove event. /// /// The location of the mouse. /// The buttons of the mouse that are pressed. /// The image to draw to. public void HandleMouseMove(Vec2 loc, MouseButtons buttons, Image i) { MouseLocation = loc; if (Taskbar.Bounds.IsInBounds(loc)) { Taskbar.DoMouseMove(loc); } else { if (Taskbar.WasOverButton) { Taskbar.UndrawOverButton(Taskbar.WindowButtonBounds[Taskbar.overButtonIndx], Taskbar.Windows[Taskbar.overButtonIndx]); } if (ActiveWindows.Length > 0) { ActiveWindow.DoMouseMove(loc, buttons); } } this.Draw(i); } #endregion #region Mouse Down /// /// Processes a MouseDown event. /// /// The location of the mouse. /// The MouseButtons that are pressed. /// The Image to draw to. public void HandleMouseDown(Vec2 loc, MouseButtons buttons, Image i) { MouseLocation = loc; if (ActiveWindows.Length > 0) { if (ActiveWindow.Bounds.IsInBounds(loc)) { ActiveWindow.DoMouseDown(loc, buttons); this.Draw(i); } } } #endregion #region Mouse Up /// /// Processes a MouseUp event. /// /// The location of the mouse. /// The MouseButtons that are still pressed. /// The Image to draw to. public void HandleMouseUp(Vec2 loc, MouseButtons buttons, Image i) { MouseLocation = loc; if (ActiveWindows.Length > 0) { ActiveWindow.DoMouseUp(loc, buttons); this.Draw(i); } } #endregion #endregion } }