#region License Information
/* HeuristicLab
* Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
namespace HeuristicLab.Visualization {
public abstract class ChartMode {
protected readonly ChartControl chartControl;
protected readonly IList globalShortcuts = new List();
protected readonly IList localShortcuts;
protected Shortcut activeShortcut = null;
protected Cursor cursor;
protected Point buttonDownPoint;
protected Point previousLocation;
protected int mouseClickCount;
public abstract Image Image { get; }
public abstract string Text { get; }
public virtual string ToolTipText { get { return Text; } }
public virtual Cursor Cursor {
get { return cursor ?? (cursor = Cursors.Default); }
}
private bool selected;
public bool Selected {
get { return selected; }
set {
if (selected == value) return;
selected = value;
if (selected) {
chartControl.Mode = this;
OnSelected();
} else {
if (chartControl.Mode == this)
chartControl.Mode = null;
OnDeselected();
}
OnSelectedChanged();
}
}
public IEnumerable Shortcuts { get { return globalShortcuts; } }
protected ChartMode(ChartControl control) {
chartControl = control;
chartControl.ModeChanged += (sender, args) => Selected = chartControl.Mode == this;
localShortcuts = new List {
new Shortcut(Keys.Escape) { DownAction = Deselect }
};
}
public void Select() { Selected = true; }
public void Deselect() { Selected = false; }
protected virtual void OnSelected() { }
protected virtual void OnDeselected() { }
public virtual void HandleOnKeyDown(object sender, KeyEventArgs e) {
if (activeShortcut != null) return;
foreach (var shortcut in localShortcuts) {
if (e.KeyCode == shortcut.Key && e.Modifiers == shortcut.Modifiers) {
activeShortcut = shortcut;
var downAction = shortcut.DownAction;
if (downAction != null) downAction();
e.Handled = true;
break;
}
}
}
public virtual void HandleOnKeyUp(object sender, KeyEventArgs e) {
if (activeShortcut == null) return;
var modifiers = new List(from key in Enum.GetValues(typeof(Keys)).Cast()
where e.Modifiers.HasFlag(key)
select key);
if (activeShortcut.Key == e.KeyCode || activeShortcut.GetModifiers().Except(modifiers).Any()) {
var upAction = activeShortcut.UpAction;
if (upAction != null) upAction();
activeShortcut = null;
e.Handled = true;
}
}
public virtual void HandleOnMouseClick(object sender, MouseEventArgs e) { }
public virtual void HandleOnMouseDoubleClick(object sender, MouseEventArgs e) { }
public virtual void HandleOnMouseDown(object sender, MouseEventArgs e) {
buttonDownPoint = e.Location;
mouseClickCount = e.Clicks;
}
public virtual void HandleOnMouseUp(object sender, MouseEventArgs e) { }
public virtual void HandleOnMouseMove(object sender, MouseEventArgs e) {
previousLocation = e.Location;
}
public virtual void HandleOnMouseWheel(object sender, MouseEventArgs e) { }
public virtual void HandleOnMouseEnter(object sender, EventArgs e) { }
public virtual void HandleOnMouseLeave(object sender, EventArgs e) { }
public virtual void HandleOnClick(object sender, EventArgs e) { }
public event EventHandler SelectedChanged;
protected virtual void OnSelectedChanged() {
var handler = SelectedChanged;
if (handler != null) handler(this, EventArgs.Empty);
}
public class Shortcut {
public Keys Key { get; private set; }
public Keys Modifiers { get; private set; }
public Action DownAction { get; set; }
public Action UpAction { get; set; }
public Shortcut(Keys key, Keys modifiers = Keys.None) {
Key = key;
Modifiers = modifiers;
}
public IEnumerable GetModifiers() {
var keys = new List(from key in Enum.GetValues(typeof(Keys)).Cast()
where Modifiers.HasFlag(key)
select key);
return keys;
}
}
}
}