using System; //using System.Windows.Forms; using Gwen.ControlInternal; namespace Gwen.Control { public class VerticalSplitter : Base { private readonly SplitterBar m_HSplitter; private readonly Base[] m_Sections; private float m_HVal; // 0-1 private int m_BarSize; // pixels private int m_ZoomedSection; // 0-3 /// /// Invoked when one of the panels has been zoomed (maximized). /// public event GwenEventHandler PanelZoomed; /// /// Invoked when one of the panels has been unzoomed (restored). /// public event GwenEventHandler PanelUnZoomed; /// /// Invoked when the zoomed panel has been changed. /// public event GwenEventHandler ZoomChanged; /// /// Initializes a new instance of the class. /// /// Parent control. public VerticalSplitter(Base parent) : base(parent) { m_Sections = new Base[2]; m_HSplitter = new SplitterBar(this); m_HSplitter.SetPosition(128, 0); m_HSplitter.Dragged += OnHorizontalMoved; //m_HSplitter.Cursor = Cursors.SizeWE; m_HVal = 0.5f; SetPanel(0, null); SetPanel(1, null); SplitterSize = 5; SplittersVisible = false; m_ZoomedSection = -1; } /// /// Centers the panels so that they take even amount of space. /// public void CenterPanels() { m_HVal = 0.5f; Invalidate(); } public void SetHValue(float value) { if (value <= 1f || value >= 0) m_HVal = value; } /// /// Indicates whether any of the panels is zoomed. /// public bool IsZoomed { get { return m_ZoomedSection != -1; } } /// /// Gets or sets a value indicating whether splitters should be visible. /// public bool SplittersVisible { get { return m_HSplitter.ShouldDrawBackground; } set { m_HSplitter.ShouldDrawBackground = value; } } /// /// Gets or sets the size of the splitter. /// public int SplitterSize { get { return m_BarSize; } set { m_BarSize = value; } } private void UpdateHSplitter() { m_HSplitter.MoveTo((Width - m_HSplitter.Width) * (m_HVal), m_HSplitter.Y); } protected void OnHorizontalMoved(Base control) { m_HVal = CalculateValueHorizontal(); Invalidate(); } private float CalculateValueHorizontal() { return m_HSplitter.X / (float)(Width - m_HSplitter.Width); } /// /// Lays out the control's interior according to alignment, padding, dock etc. /// /// Skin to use. protected override void Layout(Skin.Base skin) { m_HSplitter.SetSize(m_BarSize, Height); UpdateHSplitter(); if (m_ZoomedSection == -1) { if (m_Sections[0] != null) m_Sections[0].SetBounds(0, 0, m_HSplitter.X, Height); if (m_Sections[1] != null) m_Sections[1].SetBounds(m_HSplitter.X + m_BarSize, 0, Width - (m_HSplitter.X + m_BarSize), Height); } else { //This should probably use Fill docking instead m_Sections[m_ZoomedSection].SetBounds(0, 0, Width, Height); } } /// /// Assigns a control to the specific inner section. /// /// Section index (0-3). /// Control to assign. public void SetPanel(int index, Base panel) { m_Sections[index] = panel; if (panel != null) { panel.Dock = Pos.None; panel.Parent = this; } Invalidate(); } /// /// Gets the specific inner section. /// /// Section index (0-3). /// Specified section. public Base GetPanel(int index) { return m_Sections[index]; } /// /// Internal handler for the zoom changed event. /// protected void OnZoomChanged() { if (ZoomChanged != null) ZoomChanged.Invoke(this); if (m_ZoomedSection == -1) { if (PanelUnZoomed != null) PanelUnZoomed.Invoke(this); } else { if (PanelZoomed != null) PanelZoomed.Invoke(this); } } /// /// Maximizes the specified panel so it fills the entire control. /// /// Panel index (0-3). public void Zoom(int section) { UnZoom(); if (m_Sections[section] != null) { for (int i = 0; i < 2; i++) { if (i != section && m_Sections[i] != null) m_Sections[i].IsHidden = true; } m_ZoomedSection = section; Invalidate(); } OnZoomChanged(); } /// /// Restores the control so all panels are visible. /// public void UnZoom() { m_ZoomedSection = -1; for (int i = 0; i < 2; i++) { if (m_Sections[i] != null) m_Sections[i].IsHidden = false; } Invalidate(); OnZoomChanged(); } } }