main
  1using System;
  2using System.ComponentModel;
  3using System.Linq;
  4using System.Windows.Forms;
  5using gorilla.commons.utility;
  6using momoney.presentation.views;
  7using MoMoney.Presentation.Views;
  8using MoMoney.Presentation.Winforms.Helpers;
  9using MoMoney.Presentation.Winforms.Resources;
 10using WeifenLuo.WinFormsUI.Docking;
 11
 12namespace MoMoney.Presentation.Winforms.Views
 13{
 14    public partial class ApplicationDockedWindow : DockContent, IApplicationDockedWindow
 15    {
 16        DockState dock_state;
 17
 18        public ApplicationDockedWindow()
 19        {
 20            InitializeComponent();
 21            Icon = ApplicationIcons.Application;
 22            dock_state = DockState.Document;
 23            HideOnClose = true;
 24
 25            activated = x => { };
 26            deactivated = x => { };
 27            closed = x => { };
 28            closing = x => { };
 29        }
 30
 31        protected override void OnActivated(EventArgs e)
 32        {
 33            base.OnActivated(e);
 34            activated(e);
 35        }
 36
 37        protected override void OnDeactivate(EventArgs e)
 38        {
 39            base.OnDeactivate(e);
 40            deactivated(e);
 41        }
 42
 43        protected override void OnClosing(CancelEventArgs e)
 44        {
 45            base.OnClosing(e);
 46            closing(e);
 47        }
 48
 49        protected override void OnClosed(EventArgs e)
 50        {
 51            base.OnClosed(e);
 52            closed(e);
 53        }
 54
 55        public ControlAction<EventArgs> activated { get; set; }
 56        public ControlAction<EventArgs> deactivated { get; set; }
 57        public ControlAction<EventArgs> closed { get; set; }
 58        public ControlAction<CancelEventArgs> closing { get; set; }
 59
 60        public IApplicationDockedWindow create_tool_tip_for(string title, string caption, Control control)
 61        {
 62            var tip = new ToolTip {IsBalloon = true, ToolTipTitle = title};
 63            tip.SetToolTip(control, caption);
 64            control.Controls.Add(new ControlAdapter(tip));
 65            return this;
 66        }
 67
 68        public IApplicationDockedWindow titled(string title, params object[] arguments)
 69        {
 70            TabText = title.formatted_using(arguments);
 71            return this;
 72        }
 73
 74        public IApplicationDockedWindow icon(ApplicationIcon icon)
 75        {
 76            Icon = icon;
 77            return this;
 78        }
 79
 80        public IApplicationDockedWindow cannot_be_closed()
 81        {
 82            CloseButton = false;
 83            CloseButtonVisible = false;
 84            return this;
 85        }
 86
 87        public IApplicationDockedWindow docked_to(DockState state)
 88        {
 89            dock_state = state;
 90            return this;
 91        }
 92
 93        public void add_to(DockPanel panel)
 94        {
 95            using (new SuspendLayout(panel))
 96            {
 97                if (window_is_already_contained_in(panel)) remove_from(panel);
 98                //else
 99                {
100                    Show(panel, dock_state);
101                }
102            }
103        }
104
105        void remove_from(DockPanel panel)
106        {
107            using (new SuspendLayout(panel))
108            {
109                var panel_to_remove = get_window_from(panel);
110                panel_to_remove.DockHandler.Close();
111                panel_to_remove.DockHandler.Dispose();
112            }
113        }
114
115        IDockContent get_window_from(DockPanel panel)
116        {
117            return panel.Documents.Single(matches);
118        }
119
120        bool window_is_already_contained_in(DockPanel panel)
121        {
122            return panel.Documents.Count(matches) > 0;
123        }
124
125        bool matches(IDockContent x)
126        {
127            return x.DockHandler.TabText.Equals(TabText);
128        }
129    }
130}