+ internal class TitleButton
+ {
+ public Rectangle Rectangle;
+ public ButtonState State;
+ public CaptionButton Caption;
+ private EventHandler Clicked;
+ public bool Visible;
+ bool entered;
+
+ public TitleButton (CaptionButton caption, EventHandler clicked)
+ {
+ Caption = caption;
+ Clicked = clicked;
+ }
+
+ public void OnClick ()
+ {
+ if (Clicked != null) {
+ Clicked (this, EventArgs.Empty);
+ }
+ }
+
+ public bool Entered {
+ get { return entered; }
+ set { entered = value; }
+ }
+ }
+
+ internal class TitleButtons : System.Collections.IEnumerable
+ {
+ public TitleButton MinimizeButton;
+ public TitleButton MaximizeButton;
+ public TitleButton RestoreButton;
+ public TitleButton CloseButton;
+ public TitleButton HelpButton;
+
+ public TitleButton [] AllButtons;
+ public bool Visible;
+
+ private ToolTip.ToolTipWindow tooltip;
+ private Timer tooltip_timer;
+ private TitleButton tooltip_hovered_button;
+ private TitleButton tooltip_hidden_button;
+ private const int tooltip_hide_interval = 3000;
+ private const int tooltip_show_interval = 1000;
+ private Form form;
+
+ public TitleButtons (Form frm)
+ {
+ this.form = frm;
+ this.Visible = true;
+
+ MinimizeButton = new TitleButton (CaptionButton.Minimize, new EventHandler (ClickHandler));
+ MaximizeButton = new TitleButton (CaptionButton.Maximize, new EventHandler (ClickHandler));
+ RestoreButton = new TitleButton (CaptionButton.Restore, new EventHandler (ClickHandler));
+ CloseButton = new TitleButton (CaptionButton.Close, new EventHandler (ClickHandler));
+ HelpButton = new TitleButton (CaptionButton.Help, new EventHandler (ClickHandler));
+
+ AllButtons = new TitleButton [] { MinimizeButton, MaximizeButton, RestoreButton, CloseButton, HelpButton };
+ }
+
+ private void ClickHandler (object sender, EventArgs e)
+ {
+ if (!Visible) {
+ return;
+ }
+
+ TitleButton button = (TitleButton) sender;
+
+ switch (button.Caption) {
+ case CaptionButton.Close:
+ form.Close ();
+ break;
+ case CaptionButton.Help:
+ Console.WriteLine ("Help not implemented.");
+ break;
+ case CaptionButton.Maximize:
+ form.WindowState = FormWindowState.Maximized;
+ break;
+ case CaptionButton.Minimize:
+ form.WindowState = FormWindowState.Minimized;
+ break;
+ case CaptionButton.Restore:
+ form.WindowState = FormWindowState.Normal;
+ break;
+ }
+ }
+
+ public TitleButton FindButton (int x, int y)
+ {
+ if (!Visible) {
+ return null;
+ }
+
+ foreach (TitleButton button in AllButtons) {
+ if (button.Visible && button.Rectangle.Contains (x, y)) {
+ return button;
+ }
+ }
+ return null;
+ }
+
+ public bool AnyPushedTitleButtons {
+ get {
+ if (!Visible) {
+ return false;
+ }
+
+ foreach (TitleButton button in AllButtons) {
+ if (button.Visible && button.State == ButtonState.Pushed) {
+ return true;
+ }
+ }
+ return false;
+ }
+ }
+
+ #region IEnumerable Members
+
+ public System.Collections.IEnumerator GetEnumerator ()
+ {
+ return AllButtons.GetEnumerator ();
+ }
+ #endregion
+
+ #region ToolTip helpers
+ // Called from MouseMove if mouse is over a button
+ public void ToolTipStart (TitleButton button)
+ {
+ tooltip_hovered_button = button;
+
+ if (tooltip_hovered_button == tooltip_hidden_button)
+ return;
+ tooltip_hidden_button = null;
+
+ if (tooltip != null && tooltip.Visible)
+ ToolTipShow (true);
+
+ if (tooltip_timer == null) {
+
+ tooltip_timer = new Timer ();
+ tooltip_timer.Tick += new EventHandler (ToolTipTimerTick);
+ }
+
+ tooltip_timer.Interval = tooltip_show_interval;
+ tooltip_timer.Start ();
+ tooltip_hovered_button = button;
+ }
+
+ public void ToolTipTimerTick (object sender, EventArgs e)
+ {
+ if (tooltip_timer.Interval == tooltip_hide_interval) {
+ tooltip_hidden_button = tooltip_hovered_button;
+ ToolTipHide (false);
+ } else {
+ ToolTipShow (false);
+ }
+ }
+ // Called from timer (with only_refresh = false)
+ // Called from ToolTipStart if tooltip is already shown (with only_refresh = true)
+ public void ToolTipShow (bool only_refresh)
+ {
+ if (!form.Visible)
+ return;
+
+ string text = Locale.GetText (tooltip_hovered_button.Caption.ToString ());
+
+ tooltip_timer.Interval = tooltip_hide_interval;
+ tooltip_timer.Enabled = true;
+
+ if (only_refresh && (tooltip == null || !tooltip.Visible)) {
+ return;
+ }
+
+ if (tooltip == null)
+ tooltip = new ToolTip.ToolTipWindow ();
+ else if (tooltip.Text == text && tooltip.Visible)
+ return;
+ else if (tooltip.Visible)
+ tooltip.Visible = false;
+
+ if (form.WindowState == FormWindowState.Maximized && form.MdiParent != null)
+ tooltip.Present (form.MdiParent, text);
+ else
+ tooltip.Present (form, text);
+
+ }
+
+ // Called from MouseLeave (with reset_hidden_button = true)
+ // Called from MouseDown (with reset_hidden_button = false)
+ // Called from MouseMove if mouse isn't over any button (with reset_hidden_button = false)
+ // Called from Timer if hiding (with reset_hidden_button = false)
+ public void ToolTipHide (bool reset_hidden_button)
+ {
+ if (tooltip_timer != null)
+ tooltip_timer.Enabled = false;
+ if (tooltip != null && tooltip.Visible)
+ tooltip.Visible = false;
+ if (reset_hidden_button)
+ tooltip_hidden_button = null;
+ }
+ #endregion
+
+ public bool MouseMove (int x, int y)
+ {
+ if (!Visible) {
+ return false;
+ }
+
+ bool any_change = false;
+ bool any_pushed_buttons = AnyPushedTitleButtons;
+ bool any_tooltip = false;
+ TitleButton over_button = FindButton (x, y);
+
+ foreach (TitleButton button in this) {
+ if (button == null)
+ continue;
+
+ if (button.State == ButtonState.Inactive)
+ continue;
+
+ if (button == over_button) {
+ if (any_pushed_buttons) {
+ any_change |= button.State != ButtonState.Pushed;
+ button.State = ButtonState.Pushed;
+ }
+ ToolTipStart (button);
+ any_tooltip = true;
+ if (!button.Entered) {
+ button.Entered = true;
+ if (ThemeEngine.Current.ManagedWindowTitleButtonHasHotElementStyle (button, form))
+ any_change = true;
+ }
+ } else {
+ if (any_pushed_buttons) {
+ any_change |= button.State != ButtonState.Normal;
+ button.State = ButtonState.Normal;
+ }
+ if (button.Entered) {
+ button.Entered = false;
+ if (ThemeEngine.Current.ManagedWindowTitleButtonHasHotElementStyle (button, form))
+ any_change = true;
+ }
+ }
+ }
+
+ if (!any_tooltip)
+ ToolTipHide (false);
+
+ return any_change;
+ }
+
+ public void MouseDown (int x, int y)
+ {
+ if (!Visible) {
+ return;
+ }
+
+ ToolTipHide (false);
+
+ foreach (TitleButton button in this) {
+ if (button != null && button.State != ButtonState.Inactive) {
+ button.State = ButtonState.Normal;
+ }
+ }
+ TitleButton clicked_button = FindButton (x, y);
+ if (clicked_button != null && clicked_button.State != ButtonState.Inactive) {
+ clicked_button.State = ButtonState.Pushed;
+ }
+ }
+
+ public void MouseUp (int x, int y)
+ {
+ if (!Visible) {
+ return;
+ }
+
+ TitleButton clicked_button = FindButton (x, y);
+ if (clicked_button != null && clicked_button.State != ButtonState.Inactive) {
+ clicked_button.OnClick ();
+ }
+
+ foreach (TitleButton button in this) {
+ if (button == null || button.State == ButtonState.Inactive)
+ continue;
+
+ button.State = ButtonState.Normal;
+ }
+
+ if (clicked_button == CloseButton && !form.closing)
+ XplatUI.InvalidateNC (form.Handle);
+
+ ToolTipHide (true);
+ }
+
+ internal void MouseLeave (int x, int y)
+ {
+ if (!Visible) {
+ return;
+ }
+
+ foreach (TitleButton button in this) {
+ if (button == null || button.State == ButtonState.Inactive)
+ continue;
+
+ button.State = ButtonState.Normal;
+ }
+
+ ToolTipHide (true);
+ }
+ }