From dab0f2befeda33bd1c8e59ec41a2e1768712086b Mon Sep 17 00:00:00 2001 From: jpbruyere Date: Sat, 16 Nov 2013 09:16:43 +0100 Subject: first commit --- src/Button.cs | 460 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 460 insertions(+) create mode 100644 src/Button.cs (limited to 'src/Button.cs') diff --git a/src/Button.cs b/src/Button.cs new file mode 100644 index 00000000..d29cfbc0 --- /dev/null +++ b/src/Button.cs @@ -0,0 +1,460 @@ +using System; + + +using System.Collections.Generic; +using System.Linq; +using System.Text; +//using OpenTK.Graphics.OpenGL; + +using Cairo; + +using winColors = System.Drawing.Color; +using System.Diagnostics; + +namespace go +{ + public class Button : Group + { + public static class Theme + { + public static Color background = Color.Gray; + public static Color borderColor = Color.Black; + public static int margin = 1; + public static int borderWidth = 1; + public static VerticalAlignment verticalAlignment = VerticalAlignment.None; + public static HorizontalAlignment horizontalAlignment = HorizontalAlignment.None; + public static bool sizeToContent = false; + } + + public Button(ButtonWidgetClick _clickEvent, string _text, bool _checkable = false, int width = 0, int height = 0) + : base(width, height) + { + initButtonWidget(_clickEvent, _checkable); + label = new Label(_text); + sizeToContent = true; + label.fontColor = Color.Black; + label.fontSize = 14; + label.horizontalAlignment = HorizontalAlignment.Stretch; + label.verticalAlignment = VerticalAlignment.Stretch; + label.textAlignment = Alignment.Center; + + } + public Button(System.Drawing.Bitmap iconBitmap, ButtonWidgetClick _clickEvent, bool _checkable, int width, int height) + : base(width, height) + { + icon = new Image(iconBitmap); + initButtonWidget(_clickEvent, _checkable); + } + public Button(string iconFile, ButtonWidgetClick _clickEvent = null, bool _checkable = false, bool _affectMultiSelectState = true) + : base() + { + affectMultiSelectState = _affectMultiSelectState; + icon = new Image(iconFile); + initButtonWidget(_clickEvent, _checkable); + sizeToContent = true; + } + public Button(string iconFile, ButtonWidgetClick _clickEvent, bool _checkable, int width, int height, bool _affectMultiSelectState = true) + : base(width, height) + { + affectMultiSelectState = _affectMultiSelectState; + icon = new Image(iconFile); + initButtonWidget(_clickEvent, _checkable); + } + public Button(string iconFile, string checkedIconFile, ButtonWidgetClick _clickEvent, bool _checkable = false, int width = 30, int height = 30, bool _affectMultiSelectState = true) + : base(width, height) + { + affectMultiSelectState = _affectMultiSelectState; + icon = new Image(iconFile); + checkedIcon = new Image(checkedIconFile); + checkedIcon.isVisible = false; + + initButtonWidget(_clickEvent, _checkable); + } + + void initButtonWidget(ButtonWidgetClick _clickEvent = null, bool _checkable = false) + { + focusable = true; + IsCheckable = _checkable; + clickEvent = _clickEvent; + + background = Theme.background; + borderColor = Theme.borderColor; + borderWidth = Theme.borderWidth; + margin = Theme.margin; + horizontalAlignment = Theme.horizontalAlignment; + verticalAlignment = Theme.verticalAlignment; + sizeToContent = Theme.sizeToContent; + } + + public enum ButtonStates + { + normal, + mouseOver, + mouseDown, + Disable + } + + public static int maxIconSize = 22; + + ButtonStates _CurrentState = ButtonStates.normal; + public ButtonStates CurrentState + { + get { return _CurrentState; } + set + { + if (value == _CurrentState) + return; + _CurrentState = value; + registerForRedraw(); + //registerForGraphicUpdate(); + //needGraphicalUpdate = true; + } + } + + public ButtonWidgetClick clickEvent; + + bool _isChecked = false; + public bool IsChecked + { + get { return _isChecked; } + set + { + if (value == _isChecked) + return; + + _isChecked = value; + + if (clickEvent != null) + clickEvent(this); + + registerForGraphicUpdate(); + } + } + + public bool IsCheckable = false; + public bool affectMultiSelectState = true; + public bool border3D = false; + + Image _icon; + Image _checkedIcon; + Label _label; + + public Image icon + { + get { return _icon; } + set + { + if (_icon != null) + removeChild(_icon); + + if (value == null) + _icon = null; + else + { + _icon = addChild(value) as Image; + _icon.horizontalAlignment = go.HorizontalAlignment.Stretch; + _icon.verticalAlignment = go.VerticalAlignment.Stretch; + _icon.background = Color.Transparent; + putWidgetOnBottom(_icon); + //_icon.borderWidth = 5; + } + } + } + public Image checkedIcon + { + get { return _checkedIcon; } + set + { + if (_checkedIcon != null) + removeChild(_checkedIcon); + + if (value == null) + _checkedIcon = null; + else + { + _checkedIcon = addChild(value) as Image; + _checkedIcon.horizontalAlignment = go.HorizontalAlignment.Stretch; + _checkedIcon.verticalAlignment = go.VerticalAlignment.Stretch; + _checkedIcon.background = Color.Transparent; + + putWidgetOnBottom(_checkedIcon); + } + } + } + public Label label + { + get { return _label; } + set + { + if (_label != null) + removeChild(_label); + + if (value == null) + _label = null; + else + { + _label = addChild(value) as Label; + _label.horizontalAlignment = go.HorizontalAlignment.Stretch; + _label.verticalAlignment = go.VerticalAlignment.Stretch; + _label.textAlignment = Alignment.VerticalStretch; + putWidgetOnTop(_label); + } + } + } + + public string text + { + get { return label.text; } + set + { + label = new Label(value); + } + } + + //public ButtonWidget(string iconFile, int _x, int _y, bool _checkable = false):base() + //{ + // IsCheckable = _checkable; + // x = _x; + // y = _y; + + // icon = new ImageWidget(iconFile); + // icon.Parent = this; + //} + + + + public override bool ProcessMousePosition(Point mousePos) + { + if (CurrentState == ButtonStates.Disable) + return false; + + bool result = base.ProcessMousePosition(mousePos); + + if (result && CurrentState != ButtonStates.mouseDown) + CurrentState = ButtonStates.mouseOver; + else + CurrentState = ButtonStates.normal; + + return result; + + } + public override void ProcessMouseDown(Point mousePos) + { + //base.ProcessMouseDown(mousePos); + + + if (CurrentState != Button.ButtonStates.Disable) + { + Interface.activeWidget = this; + + if (IsCheckable) + { + if (IsChecked) + IsChecked = false; + else + { + Group wg = Parent as Group; + if (wg != null) + { + if (affectMultiSelectState && !multiSelect) + { + foreach (Button but in wg.Children.OfType