diff options
| author | jpbruyere <jp.bruyere@hotmail.com> | 2015-06-02 10:44:23 +0200 |
|---|---|---|
| committer | jpbruyere <jp.bruyere@hotmail.com> | 2015-06-02 10:44:23 +0200 |
| commit | b56c22ad826a49b7ea0013c1ca78cf64169fc326 (patch) | |
| tree | 588a3bc9ae0ed826012096a072b138a94ded4e61 /src | |
| parent | 999f18b6daf7db54879a85f0f8b5c7b22f740ce7 (diff) | |
TemplatedControls, Picture objects for pixmap and svg raw data sharing
among controls
Diffstat (limited to 'src')
| -rw-r--r-- | src/BmpPicture.cs | 105 | ||||
| -rw-r--r-- | src/GraphicObjects/AnalogMeter.cs | 9 | ||||
| -rwxr-xr-x | src/GraphicObjects/Button.cs | 23 | ||||
| -rw-r--r-- | src/GraphicObjects/Checkbox.cs | 266 | ||||
| -rw-r--r-- | src/GraphicObjects/GenericStack.cs | 10 | ||||
| -rw-r--r-- | src/GraphicObjects/GraphicObject.cs | 28 | ||||
| -rwxr-xr-x | src/GraphicObjects/Image.cs | 131 | ||||
| -rwxr-xr-x | src/GraphicObjects/Label.cs | 2 | ||||
| -rw-r--r-- | src/GraphicObjects/ListBox.cs | 31 | ||||
| -rw-r--r-- | src/GraphicObjects/NumericControl.cs | 2 | ||||
| -rw-r--r-- | src/GraphicObjects/RadioButton.cs | 73 | ||||
| -rw-r--r-- | src/GraphicObjects/Scroller.cs | 17 | ||||
| -rw-r--r-- | src/GraphicObjects/Slider.cs | 23 | ||||
| -rw-r--r-- | src/GraphicObjects/Spinner.cs | 46 | ||||
| -rw-r--r-- | src/GraphicObjects/TemplatedControl.cs | 57 | ||||
| -rw-r--r-- | src/Interface.cs | 77 | ||||
| -rw-r--r-- | src/Picture.cs | 102 | ||||
| -rw-r--r-- | src/SvgPicture.cs | 89 |
18 files changed, 678 insertions, 413 deletions
diff --git a/src/BmpPicture.cs b/src/BmpPicture.cs new file mode 100644 index 00000000..6e8d1e52 --- /dev/null +++ b/src/BmpPicture.cs @@ -0,0 +1,105 @@ +// +// BmpPicture.cs +// +// Author: +// Jean-Philippe Bruyère <jp.bruyere@hotmail.com> +// +// Copyright (c) 2015 jp +// +// This program 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. +// +// This program 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 this program. If not, see <http://www.gnu.org/licenses/>. +using System; +using System.IO; +using Cairo; + +namespace go +{ + public class BmpPicture : Picture + { + byte[] image; + + public BmpPicture () + {} + public BmpPicture (string path) : base(path) + {} + + protected override void loadFromRessource (string resId) + { + Stream stream = null; + + //first, search for ressource in main executable assembly + stream = System.Reflection.Assembly.GetEntryAssembly().GetManifestResourceStream(resId); + if (stream == null)//try to find ressource in golib assembly + stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(resId); + if (stream == null) + return; + + using (MemoryStream ms = new MemoryStream ()) { + stream.CopyTo (ms); + loadBitmap (new System.Drawing.Bitmap (ms)); + } + } + + protected override void loadFromFile (string path) + { + if (!File.Exists(path)) + return; + + loadBitmap (new System.Drawing.Bitmap (path)); + } + + //load image via System.Drawing.Bitmap, cairo load png only + void loadBitmap (System.Drawing.Bitmap bitmap) + { + if (bitmap == null) + return; + + System.Drawing.Imaging.BitmapData data = bitmap.LockBits + (new System.Drawing.Rectangle (0, 0, bitmap.Width, bitmap.Height), + System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); + + Dimensions = new Size (bitmap.Width, bitmap.Height); + + int stride = data.Stride; + int bitmapSize = Math.Abs (data.Stride) * bitmap.Height; + + image = new byte[bitmapSize]; + System.Runtime.InteropServices.Marshal.Copy (data.Scan0, image, 0, bitmapSize); + + bitmap.UnlockBits (data); + } + + public override void Paint (Cairo.Context gr, Rectangle rect, string subPart = "") + { + float widthRatio = (float)rect.Width / Dimensions.Width; + float heightRatio = (float)rect.Height / Dimensions.Height; + float ratio = Math.Min (widthRatio, heightRatio); + + Rectangle rImg = rect; + gr.Save (); + + if (KeepProportions) + gr.Scale (ratio, ratio); + else + gr.Scale (widthRatio, heightRatio); + + using (ImageSurface imgSurf = new ImageSurface (image, Format.Argb32, + Dimensions.Width, Dimensions.Height, 4 * Dimensions.Width)) { + gr.SetSourceSurface (imgSurf, (int)(rImg.X / widthRatio), (int)(rImg.Y / heightRatio)); + gr.Paint (); + } + gr.Restore (); + } + } +} + diff --git a/src/GraphicObjects/AnalogMeter.cs b/src/GraphicObjects/AnalogMeter.cs index 5ef18090..e487189d 100644 --- a/src/GraphicObjects/AnalogMeter.cs +++ b/src/GraphicObjects/AnalogMeter.cs @@ -16,6 +16,15 @@ namespace go } #endregion + #region implemented abstract members of TemplatedControl + + protected override void loadTemplate () + { + throw new NotImplementedException (); + } + + #endregion + #region GraphicObject Overrides protected override void onDraw (Context gr) { diff --git a/src/GraphicObjects/Button.cs b/src/GraphicObjects/Button.cs index e4987fda..fcbdecd8 100755 --- a/src/GraphicObjects/Button.cs +++ b/src/GraphicObjects/Button.cs @@ -18,10 +18,10 @@ namespace go #region CTOR
public Button() : base()
{
- MouseEnter += delegate { Background = Color.Lion;};
- MouseLeave += delegate { Background = Color.Gray;};
- MouseButtonDown += delegate { Background = Color.Red;};
- MouseButtonUp += delegate { Background = Color.Gray;};
+ //MouseEnter += delegate { Background = Color.RedDevil;};
+ //MouseLeave += delegate { Background = Color.Transparent;};
+ MouseButtonDown += delegate { BackImgSub = "pressed"; registerForGraphicUpdate();};
+ MouseButtonUp += delegate { BackImgSub = "normal";registerForGraphicUpdate();};
}
#endregion
@@ -36,7 +36,7 @@ namespace go get { return base.Height; }
set { base.Height = value; }
}
- [XmlAttributeAttribute()][DefaultValue("Gray")]
+ [XmlAttributeAttribute()][DefaultValue("Transparent")]
public override Color Background {
get { return base.Background; }
set { base.Background = value; }
@@ -46,8 +46,17 @@ namespace go {
get { return base.Focusable; }
set { base.Focusable = value; }
- }
-
+ }
+ [XmlAttributeAttribute()][DefaultValue("#go.Images.Icons.buttest4.svg")]
+ public override Picture BackgroundImage {
+ get {
+ return base.BackgroundImage;
+ }
+ set {
+ base.BackgroundImage = value;
+ BackImgSub = "normal";
+ }
+ }
#endregion
[XmlAttributeAttribute][DefaultValue("Button")]
diff --git a/src/GraphicObjects/Checkbox.cs b/src/GraphicObjects/Checkbox.cs index 586855d9..07be76c5 100644 --- a/src/GraphicObjects/Checkbox.cs +++ b/src/GraphicObjects/Checkbox.cs @@ -12,255 +12,57 @@ using winColors = System.Drawing.Color; using System.Diagnostics;
using System.Xml.Serialization;
using OpenTK.Input;
+using System.ComponentModel;
namespace go
{
- public delegate void GOEvent(GraphicObject sender);
+ public class Checkbox : TemplatedControl
+ {
+ Label _caption;
+ Image _image;
- public enum ButtonStates
- {
- normal,
- mouseOver,
- mouseDown,
- Disable
- }
-
- public class Checkbox : Container, IXmlSerializable
- {
- public GOEvent Click;
-
- bool _isCheckable = false;
- bool _isChecked = false;
- bool _affectMultiSelectState = false;
- bool _border3D = false;
- Color _checkedColor;
- ButtonStates _CurrentState = ButtonStates.normal;
-
- public Checkbox() : base()
- {}
-
- [System.Xml.Serialization.XmlAttributeAttribute()]
- [System.ComponentModel.DefaultValue(true)]
- public override bool Focusable
- {
- get { return base.Focusable; }
- set { base.Focusable = value; }
- }
-
- [System.Xml.Serialization.XmlAttributeAttribute()]
- public Color CheckedColor
- {
- get { return _checkedColor; }
- set
- {
- _checkedColor = value;
- registerForGraphicUpdate();
- }
- }
+ public Checkbox() : base()
+ {
+ }
- [System.Xml.Serialization.XmlIgnore]
- public ButtonStates CurrentState
- {
- get { return _CurrentState; }
- set
- {
- if (value == _CurrentState)
- return;
- _CurrentState = value;
- RegisterForRedraw();
- }
- }
+ protected override void loadTemplate()
+ {
+ this.setChild (Interface.Load ("#go.Templates.Checkbox.goml"));
+ _caption = this.child.FindByName ("Caption") as Label;
+ _image = this.child.FindByName ("Image") as Image;
+ _image.SvgSub = "unchecked";
+ }
+
- [System.Xml.Serialization.XmlAttributeAttribute()]
- [System.ComponentModel.DefaultValue(false)]
+ [XmlAttributeAttribute()][DefaultValue("Checkbox")]
+ public string Caption {
+ get { return _caption.Text; }
+ set {
+ _caption.Text = value;
+ }
+ }
+ [XmlAttributeAttribute()][DefaultValue(false)]
public bool IsChecked
{
- get { return _isChecked; }
+ get { return _image == null ? false :_image.SvgSub == "checked"; }
set
{
- if (value == _isChecked)
+ if (value == IsChecked)
return;
- _isChecked = value;
-
- if (Click != null)
- Click(this);
-
+ if (value)
+ _image.SvgSub = "checked";
+ else
+ _image.SvgSub = "unchecked";
+
registerForGraphicUpdate();
}
}
- [System.Xml.Serialization.XmlAttributeAttribute()]
- [System.ComponentModel.DefaultValue(false)]
- public bool IsCheckable
- {
- get { return _isCheckable; }
- set { _isCheckable = value; }
- }
-
- [System.Xml.Serialization.XmlAttributeAttribute()]
- [System.ComponentModel.DefaultValue(false)]
- public bool AffectMultiSelectState
- {
- get { return _affectMultiSelectState; }
- set { _affectMultiSelectState = value; }
- }
-
- [System.Xml.Serialization.XmlAttributeAttribute()]
- [System.ComponentModel.DefaultValue(false)]
- public bool Border3d
- {
- get { return _border3D; }
- set { _border3D = value; }
- }
-
- public override void Paint(ref Context ctx, Rectangles clip = null)
- {
- base.Paint(ref ctx, clip);
-
- Rectangle r = Parent.ContextCoordinates (Slot);
-
- ctx.Save();
- //ctx.ResetClip();
-
- if (IsCheckable){
- if (IsChecked){
- ctx.Color = CheckedColor;
- ctx.Rectangle(r);
- ctx.Fill();
-
- if (Border3d)
- CairoHelpers.StrokeLoweredRectangle(ctx, r);
- }
- else{
- if (Border3d)
- CairoHelpers.StrokeRaisedRectangle(ctx, r);
- }
- }
-
- switch (CurrentState){
- case ButtonStates.normal:
- break;
- case ButtonStates.mouseOver:
- CairoHelpers.CairoRectangle(ctx,r,CornerRadius);
- ctx.Operator = Operator.Add;
- ctx.Color = new Color(0.2, 0.2, 0.2, 1.0);
- ctx.Fill();
- ctx.Operator = Operator.Over;
- break;
- case ButtonStates.mouseDown:
- CairoHelpers.CairoRectangle(ctx,r,CornerRadius);
- ctx.Operator = Operator.Add;
- ctx.Color = Color.Red;
- ctx.Fill();
- ctx.Operator = Operator.Over;
- break;
- case ButtonStates.Disable:
- CairoHelpers.CairoRectangle(ctx,r,CornerRadius);
- ctx.Color = new Color(0.2, 0.2, 0.2, 0.7);
- ctx.Fill();
- break;
-
- }
- ctx.Restore();
- }
-
-
- #region mouse handling
- public override void onMouseButtonDown (object sender,MouseButtonEventArgs e)
+ public override void onMouseClick (object sender, MouseButtonEventArgs e)
{
- if (CurrentState != ButtonStates.Disable)
- {
- if (IsCheckable)
- {
- if (IsChecked)
- IsChecked = false;
- else
- {
- Group wg = Parent as Group;
- if (wg != null)
- {
- if (AffectMultiSelectState && !wg.MultiSelect)
- {
- foreach (Checkbox but in wg.Children.OfType<Checkbox>())
- {
- if (but.IsCheckable)
- {
- if (but.IsChecked && but.AffectMultiSelectState)
- {
- but.IsChecked = false;
- break;
- }
- }
- }
- }
- }
- IsChecked = true;
- }
- }
- //else if (Click != null)
- // Click(this);
-
- CurrentState = ButtonStates.mouseDown;
- }
-
- base.onMouseButtonDown (sender, e);
+ IsChecked = !IsChecked;
+ base.onMouseClick (sender, e);
}
- public override void onMouseButtonUp (object sender, OpenTK.Input.MouseButtonEventArgs e)
- {
- if (CurrentState != ButtonStates.Disable) {
- if (MouseIsIn (e.Position))
- CurrentState = ButtonStates.mouseOver;
- else
- CurrentState = ButtonStates.normal;
-
- }
-
- base.onMouseButtonUp (sender, e);
- }
- public override void onMouseEnter (object sender, MouseMoveEventArgs e)
- {
- if (CurrentState == ButtonStates.normal)
- CurrentState = ButtonStates.mouseOver;
-
- base.onMouseEnter (sender, e);
- }
- public override void onMouseLeave (object sender, MouseMoveEventArgs e)
- {
- if (CurrentState == ButtonStates.mouseOver)
- CurrentState = ButtonStates.normal;
-
- base.onMouseLeave (sender, e);
- }
- #endregion
-
- #region IXmlSerializable
- public override System.Xml.Schema.XmlSchema GetSchema()
- {
- return null;
- }
- public override void ReadXml(System.Xml.XmlReader reader)
- {
- string handler = reader.GetAttribute("OnClick");
- Interface.EventsToResolve.Add(new DynAttribute
- {
- Source = this,
- Value = handler,
- MemberName = "OnClick"
- });
-
- //Container c = this as Container;
- base.ReadXml(reader);
- }
- public override void WriteXml(System.Xml.XmlWriter writer)
- {
- if (Click != null)
- {
- writer.WriteAttributeString("OnClick", Click.Method.Name);
- }
-
- base.WriteXml(writer);
- }
- #endregion
}
}
diff --git a/src/GraphicObjects/GenericStack.cs b/src/GraphicObjects/GenericStack.cs index f34e13d3..5759bac4 100644 --- a/src/GraphicObjects/GenericStack.cs +++ b/src/GraphicObjects/GenericStack.cs @@ -70,15 +70,19 @@ namespace go tmp.Width += c.Slot.Width + Spacing;
tmp.Height = Math.Max (tmp.Height, c.Slot.Bottom);
}
+ if (tmp.Width > 0)
+ tmp.Width -= Spacing;
} else {
foreach (GraphicObject c in Children) {
tmp.Width = Math.Max (tmp.Width, c.Slot.Right);
tmp.Height += c.Slot.Height + Spacing;
}
+ if (tmp.Height > 0)
+ tmp.Height -= Spacing;
}
- tmp.Width += 2*Margin;
- tmp.Height += 2*Margin;
+ tmp.Width += 2 * Margin;
+ tmp.Height += 2 * Margin;
return tmp;
}
@@ -89,11 +93,13 @@ namespace go foreach (GraphicObject c in Children) {
c.Slot.X = d;
d += c.Slot.Width + Spacing;
+ c.RegisterForLayouting ((int)LayoutingType.Y);
}
} else {
foreach (GraphicObject c in Children) {
c.Slot.Y = d;
d += c.Slot.Height + Spacing;
+ c.RegisterForLayouting ((int)LayoutingType.X);
}
}
}
diff --git a/src/GraphicObjects/GraphicObject.cs b/src/GraphicObjects/GraphicObject.cs index 094bc453..72fccfba 100644 --- a/src/GraphicObjects/GraphicObject.cs +++ b/src/GraphicObjects/GraphicObject.cs @@ -48,10 +48,8 @@ namespace go VerticalAlignment _verticalAlignment;
HorizontalAlignment _horizontalAlignment;
- bool hIsValid = false;
- bool wIsValid = false;
- bool xIsValid = false;
- bool yIsValid = false;
+ Picture _backgroundImage;
+ string _template;
#endregion
#region public fields
@@ -258,12 +256,21 @@ namespace go { get { return bmp == null ?
false :
true; } }
+ [XmlAttributeAttribute()][DefaultValue(null)]
+ public virtual Picture BackgroundImage {
+ get { return _backgroundImage; }
+ set {
+ _backgroundImage = value;
+ registerForGraphicUpdate ();
+ }
+ }
#endregion
+ public string BackImgSub = null;
/// <summary>
/// Loads the default values from XML attributes default
/// </summary>
- void loadDefaultValues()
+ protected virtual void loadDefaultValues()
{
foreach (PropertyInfo pi in this.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance)) {
if (pi.GetSetMethod () == null)
@@ -298,6 +305,8 @@ namespace go pi.SetValue (this, Color.Parse ((string)dv.Value), null);
else if (pi.PropertyType == typeof(Font))
pi.SetValue (this, Font.Parse ((string)dv.Value), null);
+ else if (pi.PropertyType == typeof(Picture))
+ pi.SetValue (this, Picture.Parse ((string)dv.Value), null);
else
pi.SetValue (this, dv.Value, null);
continue;
@@ -520,7 +529,13 @@ namespace go gr.Color = Background;
CairoHelpers.CairoRectangle(gr,rBack,_cornerRadius);
gr.Fill ();
+
+ if (BackgroundImage == null)
+ return;
+
+ BackgroundImage.Paint (gr, rBack, BackImgSub);
}
+
protected virtual void UpdateGraphic ()
{
int stride = 4 * Slot.Width;
@@ -711,6 +726,8 @@ namespace go pi.SetValue (this, Color.Parse ((string)defaultValue), null);
else if (pi.PropertyType == typeof(Font))
pi.SetValue (this, Font.Parse ((string)defaultValue), null);
+ else if (pi.PropertyType == typeof(Picture))
+ pi.SetValue (this, Picture.Parse ((string)defaultValue), null);
else
pi.SetValue (this, defaultValue, null);
} else {
@@ -725,6 +742,7 @@ namespace go MemberName = name,
Value = strBinding
});
+ continue;
}
if (pi.PropertyType == typeof(string)) {
diff --git a/src/GraphicObjects/Image.cs b/src/GraphicObjects/Image.cs index 863c95cc..2cb70446 100755 --- a/src/GraphicObjects/Image.cs +++ b/src/GraphicObjects/Image.cs @@ -12,18 +12,14 @@ namespace go {
public class Image : GraphicObject
{
- byte[] image;
- Rsvg.Handle hSVG;
- Size imgSize;
- string _imgPath;
+ Picture _pic;
string _svgSub;
-
+
[XmlAttributeAttribute("Path")]
public string ImagePath {
- get { return _imgPath; }
- set {
- _imgPath = value;
- LoadImage (_imgPath);
+ get { return _pic == null ? null : _pic.Path; }
+ set {
+ LoadImage (value);
}
}
@@ -40,127 +36,54 @@ namespace go public Image () : base()
{
}
- public Image (string ImagePath, Rectangle _bounds)
+ public Image (string _imgPath, Rectangle _bounds)
: base(_bounds)
- {
- _imgPath = ImagePath;
+ {
LoadImage(_imgPath);
}
- public Image (string ImagePath)
+ public Image (string _imgPath)
: base()
- {
- _imgPath = ImagePath;
+ {
LoadImage(_imgPath);
}
- public Image (System.Drawing.Bitmap _bitmap)
- : base()
- {
- LoadImage (_bitmap);
- }
+// public Image (System.Drawing.Bitmap _bitmap)
+// : base()
+// {
+// _pic = new BmpPicture ();
+//
+// LoadImage (_bitmap);
+// }
#endregion
#region Image Loading
- void loadFromRessource(string resId)
- {
- Stream stream = null;
-
- //first, search for ressource in main executable assembly
- stream = System.Reflection.Assembly.GetEntryAssembly().GetManifestResourceStream(resId);
- if (stream == null)//try to find ressource in golib assembly
- stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(resId);
- if (stream == null)
- return;
-
- using (MemoryStream ms = new MemoryStream ()) {
- stream.CopyTo (ms);
-
- if (resId.EndsWith (".svg", true, System.Globalization.CultureInfo.InvariantCulture)) {
- hSVG = new Rsvg.Handle (ms.ToArray ());
- imgSize = new Size (hSVG.Dimensions.Width, hSVG.Dimensions.Height);
- } else
- LoadImage (new System.Drawing.Bitmap (ms));
- }
- }
- void loadFromFile(string path)
- {
- if (!File.Exists(path))
- return;
-
- if (path.EndsWith (".svg", true, System.Globalization.CultureInfo.InvariantCulture)) {
- hSVG = new Rsvg.Handle (path);
- imgSize = new Size (hSVG.Dimensions.Width, hSVG.Dimensions.Height);
- }else
- LoadImage (new System.Drawing.Bitmap (path));
-
- }
public void LoadImage (string path)
{
- hSVG = null;
- image = null;
- if (path.StartsWith ("#"))
- loadFromRessource (path.Substring (1));
- else
- loadFromFile (path);
+ if (path.EndsWith (".svg", true, System.Globalization.CultureInfo.InvariantCulture))
+ _pic = new SvgPicture ();
+ else
+ _pic = new BmpPicture ();
- _imgPath = path;
- }
- //load image via System.Drawing.Bitmap, cairo load png only
- public void LoadImage (System.Drawing.Bitmap bitmap)
- {
- if (bitmap == null)
- return;
-
- System.Drawing.Imaging.BitmapData data = bitmap.LockBits
- (new System.Drawing.Rectangle (0, 0, bitmap.Width, bitmap.Height),
- System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
-
- imgSize = new Size (bitmap.Width, bitmap.Height);
-
- int stride = data.Stride;
- int bitmapSize = Math.Abs (data.Stride) * bitmap.Height;
-
- image = new byte[bitmapSize];
- System.Runtime.InteropServices.Marshal.Copy (data.Scan0, image, 0, bitmapSize);
-
- bitmap.UnlockBits (data);
+ _pic.LoadImage (path);
}
#endregion
#region GraphicObject overrides
protected override Size measureRawSize ()
{
- if (image == null && hSVG == null) {
- loadFromRessource ("go.Images.Icons.IconAlerte.svg");
- }
+// if (_pic == null)
+// _pic = "#go.Images.Icons.IconAlerte.svg";
- return imgSize + Margin * 2;
+ return _pic.Dimensions + Margin * 2;
}
protected override void onDraw (Context gr)
{
base.onDraw (gr);
- float widthRatio = (float)ClientRectangle.Width / imgSize.Width;
- float heightRatio = (float)ClientRectangle.Height / imgSize.Height;
- float ratio = Math.Min (widthRatio, heightRatio);
+ if (_pic == null)
+ return;
- Rectangle rImg = ClientRectangle;
- gr.Save ();
- gr.Scale (widthRatio, heightRatio);
- if (hSVG == null) {
- using (ImageSurface imgSurf = new ImageSurface (image, Format.Argb32,
- imgSize.Width, imgSize.Height, 4 * imgSize.Width)) {
- gr.SetSourceSurface (imgSurf, (int)(rImg.X / widthRatio), (int)(rImg.Y / heightRatio));
- gr.Paint ();
- }
- } else {
- gr.Translate (rImg.X/widthRatio, rImg.Y/heightRatio);
- if (string.IsNullOrEmpty (_svgSub))
- hSVG.RenderCairo (gr);
- else
- hSVG.RenderCairoSub (gr, "#" + _svgSub);
- }
- gr.Restore ();
+ _pic.Paint (gr, ClientRectangle, _svgSub);
}
#endregion
}
diff --git a/src/GraphicObjects/Label.cs b/src/GraphicObjects/Label.cs index 1f6048d1..c128929f 100755 --- a/src/GraphicObjects/Label.cs +++ b/src/GraphicObjects/Label.cs @@ -275,7 +275,7 @@ namespace go //gr.FontOptions.Antialias = Antialias.Subpixel;
//gr.FontOptions.HintMetrics = HintMetrics.On;
- rText = ClientRectangle;
+ rText = new Rectangle(measureRawSize());
widthRatio = 1f;
heightRatio = 1f;
diff --git a/src/GraphicObjects/ListBox.cs b/src/GraphicObjects/ListBox.cs index 970606c7..869eae2a 100644 --- a/src/GraphicObjects/ListBox.cs +++ b/src/GraphicObjects/ListBox.cs @@ -20,14 +20,31 @@ // along with this program. If not, see <http://www.gnu.org/licenses/>. using System; using System.Collections; +using System.Xml.Serialization; +using System.ComponentModel; namespace go { - public class ListBox : GraphicObject + public class ListBox : TemplatedControl { - IEnumerable data; + Group _list; - public IEnumerable Data { + public ListBox () : base() + { + } + + #region implemented abstract members of TemplatedControl + protected override void loadTemplate () + { + this.setChild (Interface.Load ("#go.Templates.Listbox.goml")); + _list = this.child.FindByName ("List") as Group; + } + #endregion + + IList data; + + [XmlAttributeAttribute()][DefaultValue(null)] + public IList Data { get { return data; } @@ -37,15 +54,13 @@ namespace go data = value; + foreach (var item in data) { - + _list.addChild(Interface.Load ("#go.Templates.ItemTemplate.goml", item)); + } } } - - public ListBox () - { - } } } diff --git a/src/GraphicObjects/NumericControl.cs b/src/GraphicObjects/NumericControl.cs index 2eb54bca..c904712b 100644 --- a/src/GraphicObjects/NumericControl.cs +++ b/src/GraphicObjects/NumericControl.cs @@ -4,7 +4,7 @@ using System.ComponentModel; namespace go { - public class NumericControl : GraphicObject + public abstract class NumericControl : TemplatedControl { #region CTOR public NumericControl () : base() diff --git a/src/GraphicObjects/RadioButton.cs b/src/GraphicObjects/RadioButton.cs new file mode 100644 index 00000000..9d6990db --- /dev/null +++ b/src/GraphicObjects/RadioButton.cs @@ -0,0 +1,73 @@ +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;
+using System.Xml.Serialization;
+using OpenTK.Input;
+using System.ComponentModel;
+
+namespace go
+{
+ public class RadioButton : TemplatedControl
+ {
+ Label _caption;
+ Image _image;
+
+ public RadioButton() : base()
+ {
+ }
+
+ protected override void loadTemplate()
+ {
+ this.setChild (Interface.Load ("#go.Templates.RadioButton.goml"));
+ _caption = this.child.FindByName ("Caption") as Label;
+ _image = this.child.FindByName ("Image") as Image;
+ _image.SvgSub = "unchecked";
+ }
+
+
+ [XmlAttributeAttribute()][DefaultValue("RadioButton")]
+ public string Caption {
+ get { return _caption.Text; }
+ set {
+ _caption.Text = value;
+ }
+ }
+ [XmlAttributeAttribute()][DefaultValue(false)]
+ public bool IsChecked
+ {
+ get { return _image == null ? false :_image.SvgSub == "checked"; }
+ set
+ {
+// if (value == IsChecked)
+// return;
+//
+ if (value)
+ _image.SvgSub = "checked";
+ else
+ _image.SvgSub = "unchecked";
+
+ registerForGraphicUpdate();
+ }
+ }
+
+ public override void onMouseClick (object sender, MouseButtonEventArgs e)
+ {
+ Group pg = Parent as Group;
+ if (pg != null) {
+ foreach (RadioButton c in pg.Children.OfType<RadioButton>())
+ c.IsChecked = (c == this);
+ }
+
+ base.onMouseClick (sender, e);
+ }
+ }
+}
diff --git a/src/GraphicObjects/Scroller.cs b/src/GraphicObjects/Scroller.cs index 19b93cd8..fa323f0d 100644 --- a/src/GraphicObjects/Scroller.cs +++ b/src/GraphicObjects/Scroller.cs @@ -115,22 +115,7 @@ namespace go {
TopContainer.redrawClip.AddRectangle (base.ScreenCoordinates(Slot));
}
-// public override void UpdateLayout (LayoutingType layoutType)
-// {
-// base.UpdateLayout ();
-//
-// if (!LayoutIsValid)
-// return;
-//
-// //override child positionning inside scroller in the
-// //scrolling direction
-// if (child == null)
-// return;
-// if (VerticalScrolling)
-// child.Slot.Y = 0;
-// if (HorizontalScrolling)
-// child.Slot.X = 0;
- //}
+
public override void Paint(ref Cairo.Context ctx, Rectangles clip = null)
{
if (!Visible)//check if necessary??
diff --git a/src/GraphicObjects/Slider.cs b/src/GraphicObjects/Slider.cs index 56c9dd0b..8c314377 100644 --- a/src/GraphicObjects/Slider.cs +++ b/src/GraphicObjects/Slider.cs @@ -5,6 +5,7 @@ using System.Text; using Cairo;
using System.Xml.Serialization;
using System.ComponentModel;
+using System.Diagnostics;
namespace go
{
@@ -19,6 +20,15 @@ namespace go }
#endregion
+ #region implemented abstract members of TemplatedControl
+
+ protected override void loadTemplate ()
+ {
+
+ }
+
+ #endregion
+
#region private fields
Rectangle cursor;
int _cursorWidth;
@@ -108,10 +118,10 @@ namespace go unity = (double)(r.Width - cursor.Width) / (Maximum - Minimum);
cursor.TopLeft = new Point(r.Left + (int)(Value * unity),
- (int)(p1.Y - cursor.Height / 2));
+ (int)(p1.Y - cursor.Height / 2));
+ Debug.WriteLine (cursor.TopLeft.ToString ());
}
-
#region mouse handling
public override void onMouseButtonDown (object sender, OpenTK.Input.MouseButtonEventArgs e)
{
@@ -133,13 +143,10 @@ namespace go }
public override void onMouseMove (object sender, OpenTK.Input.MouseMoveEventArgs e)
{
+ if (holdCursor)
+ Value += (double)e.XDelta / unity;
+
base.onMouseMove (sender, e);
-
- if (!holdCursor)
- return;
-
- Value += (double)e.XDelta / unity;
-
}
#endregion
}
diff --git a/src/GraphicObjects/Spinner.cs b/src/GraphicObjects/Spinner.cs index dcf0d3d4..46961966 100644 --- a/src/GraphicObjects/Spinner.cs +++ b/src/GraphicObjects/Spinner.cs @@ -19,25 +19,59 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see <http://www.gnu.org/licenses/>. using System; +using OpenTK.Input; namespace go { public class Spinner : NumericControl { +// Button butUp; +// Button butDown; + Label labCpt; + public Spinner (double minimum, double maximum, double step) : base (minimum, maximum, step) { - butUp = new Button (); - butUp.setChild (new Image ("go.Image.Icons.updown.svg")); +// butUp = new Button (); +// butUp.setChild (new Image ("go.Image.Icons.updown.svg")); } public Spinner () : base() { } - - Button butUp; - Button butDown; - Label labCpt; + #region implemented abstract members of TemplatedControl + + protected override void loadTemplate () + { + this.setChild (Interface.Load ("#go.Templates.Spinner.goml", this)); + labCpt = this.child.FindByName ("labCpt") as Label; + } + + #endregion + + void onUp (object sender, MouseButtonEventArgs e) + { +// decimal tmp = 0; +// if (!decimal.TryParse (labCpt.Text, out tmp)) +// return; + + Value += this.SmallIncrement; + labCpt.Text = Value.ToString (); + } + void onDown (object sender, MouseButtonEventArgs e) + { +// decimal tmp = 0; +// if (!decimal.TryParse (labCpt.Text, out tmp)) +// return; + + Value -= this.SmallIncrement; + labCpt.Text = Value.ToString (); + } + public override void onValueChanged (object sender, ValueChangeEventArgs e) + { + //labCpt.Text = e.NewValue.ToString (); + base.onValueChanged (sender, e); + } } } diff --git a/src/GraphicObjects/TemplatedControl.cs b/src/GraphicObjects/TemplatedControl.cs new file mode 100644 index 00000000..7b9db047 --- /dev/null +++ b/src/GraphicObjects/TemplatedControl.cs @@ -0,0 +1,57 @@ +// +// TemplatedControl.cs +// +// Author: +// Jean-Philippe Bruyère <jp.bruyere@hotmail.com> +// +// Copyright (c) 2015 jp +// +// This program 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. +// +// This program 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 this program. If not, see <http://www.gnu.org/licenses/>. +using System; +using System.Xml.Serialization; +using System.ComponentModel; + +namespace go +{ + public abstract class TemplatedControl : Container + { + public TemplatedControl () : base() + { + } + +// string _templatePath; +// [XmlAttributeAttribute()] +// public virtual string Template { +// get { return _templatePath; } +// set { _templatePath = value; } +// } + + + protected abstract void loadTemplate(); + + protected override void loadDefaultValues () + { + loadTemplate (); + base.loadDefaultValues (); + this.Focusable = true; + } + + public override GraphicObject FindByName (string nameToFind) + { + //prevent name searching in template + return nameToFind == this.Name ? this : null; + } + } +} + diff --git a/src/Interface.cs b/src/Interface.cs index 776dd9a6..a9da051a 100644 --- a/src/Interface.cs +++ b/src/Interface.cs @@ -60,25 +60,48 @@ namespace go xs.Serialize(s, graphicObject, xn); } } - public static GraphicObject Load(string path) + + public static Stream GetStreamFromPath(string path) + { + Stream stream = null; + + if (path.StartsWith ("#")) { + string resId = path.Substring (1); + stream = System.Reflection.Assembly.GetEntryAssembly().GetManifestResourceStream(resId); + if (stream == null)//try to find ressource in golib assembly + stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(resId); + if (stream == null) + return null; + } else { + if (!File.Exists (path)) + return null; + stream = new FileStream (path, FileMode.Open); + } + return stream; + } + public static GraphicObject Load(string path, object hostClass = null) { string root = "Object"; - using (Stream s = new FileStream (path, FileMode.Open)) { - using (XmlReader reader = XmlReader.Create (s)) { - while (reader.Read()) { - // first element is the root element - if (reader.NodeType == XmlNodeType.Element) { - root = reader.Name; - break; - } + + Stream stream = GetStreamFromPath (path); + using (XmlReader reader = XmlReader.Create (stream)) { + while (reader.Read()) { + // first element is the root element + if (reader.NodeType == XmlNodeType.Element) { + root = reader.Name; + break; } } } + Type t = Type.GetType("go." + root); //var go = Activator.CreateInstance(t); - return Load(path, t); + stream.Seek(0,SeekOrigin.Begin); + GraphicObject tmp = Load(stream, t, hostClass); + stream.Dispose (); + return tmp; } - public static void Load<T>(string file, out T result, object ClassContainingHandlers = null) + public static void Load<T>(string file, out T result, object hostClass = null) { EventsToResolve = new List<DynAttribute>(); Bindings = new List<DynAttribute> (); @@ -92,7 +115,7 @@ namespace go result = (T)xs.Deserialize(s); } - if (ClassContainingHandlers == null) + if (hostClass == null) return; foreach (DynAttribute es in EventsToResolve) @@ -103,7 +126,7 @@ namespace go if (es.Value.StartsWith ("{")) { CompilerServices.CompileEventSource (es); } else { - MethodInfo mi = ClassContainingHandlers.GetType ().GetMethod (es.Value, BindingFlags.NonPublic | BindingFlags.Public + MethodInfo mi = hostClass.GetType ().GetMethod (es.Value, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); if (mi == null) { @@ -112,7 +135,7 @@ namespace go } FieldInfo fi = CompilerServices.getEventHandlerField (es.Source.GetType (), es.MemberName); - Delegate del = Delegate.CreateDelegate(fi.FieldType, ClassContainingHandlers, mi); + Delegate del = Delegate.CreateDelegate(fi.FieldType, hostClass, mi); fi.SetValue(es.Source, del); } } @@ -124,10 +147,10 @@ namespace go // continue; // } //MemberInfo mi = binding.Source.GetType ().GetMember (binding.MemberName); - CompilerServices.CreateBinding (binding, ClassContainingHandlers); + CompilerServices.CreateBinding (binding, hostClass); } } - public static GraphicObject Load(string file, Type type, object ClassContainingHandlers = null) + public static GraphicObject Load(Stream stream, Type type, object hostClass = null) { GraphicObject result; EventsToResolve = new List<DynAttribute>(); @@ -136,12 +159,9 @@ namespace go xn.Add("", ""); XmlSerializer xs = new XmlSerializer(type); - using (Stream s = new FileStream(file, FileMode.Open)) - { - result = (GraphicObject)xs.Deserialize(s); - } + result = (GraphicObject)xs.Deserialize(stream); - if (ClassContainingHandlers == null) + if (hostClass == null) return result; foreach (DynAttribute es in EventsToResolve) @@ -152,7 +172,7 @@ namespace go if (es.Value.StartsWith ("{")) { CompilerServices.CompileEventSource (es); } else { - MethodInfo mi = ClassContainingHandlers.GetType ().GetMethod (es.Value, BindingFlags.NonPublic | BindingFlags.Public + MethodInfo mi = hostClass.GetType ().GetMethod (es.Value, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); if (mi == null) { @@ -161,10 +181,21 @@ namespace go } FieldInfo fi = CompilerServices.getEventHandlerField (es.Source.GetType (), es.MemberName); - Delegate del = Delegate.CreateDelegate(fi.FieldType, ClassContainingHandlers, mi); + Delegate del = Delegate.CreateDelegate(fi.FieldType, hostClass, mi); fi.SetValue(es.Source, del); } } + + foreach (DynAttribute binding in Bindings) { + // Type tSource = binding.Source.GetType (); + // if (!tSource.GetInterfaces ().Any (i => i.Name == "IValueChange")){ + // Debug.WriteLine ("Binding source does not implement IValueChange."); + // continue; + // } + //MemberInfo mi = binding.Source.GetType ().GetMember (binding.MemberName); + CompilerServices.CreateBinding (binding, hostClass); + } + return result; } diff --git a/src/Picture.cs b/src/Picture.cs new file mode 100644 index 00000000..df96733c --- /dev/null +++ b/src/Picture.cs @@ -0,0 +1,102 @@ +// +// Picture.cs +// +// Author: +// Jean-Philippe Bruyère <jp.bruyere@hotmail.com> +// +// Copyright (c) 2015 jp +// +// This program 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. +// +// This program 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 this program. If not, see <http://www.gnu.org/licenses/>. +using System; +using System.IO; +using Cairo; + +namespace go +{ + public abstract class Picture + { + public string Path; + public Size Dimensions; + public bool KeepProportions = false; + + public Picture () + { + } + public Picture (string path) + { + LoadImage (path); + } + + #region Image Loading + public void LoadImage (string path) + { + if (path.StartsWith ("#")) + loadFromRessource (path.Substring (1)); + else + loadFromFile (path); + + Path = path; + } + + protected abstract void loadFromRessource(string resId); + protected abstract void loadFromFile(string path); + #endregion + + public abstract void Paint(Context ctx, Rectangle rect, string subPart = ""); + + + public static implicit operator Picture(string path) + { + if (string.IsNullOrEmpty (path)) + return null; + + Picture _pic = null; + + if (path.EndsWith (".svg", true, System.Globalization.CultureInfo.InvariantCulture)) + _pic = new SvgPicture (); + else + _pic = new BmpPicture (); + + _pic.LoadImage (path); + + return _pic; + } + public static implicit operator string(Picture _pic) + { + return _pic == null ? null : _pic.Path; + } + + public static object Parse(string path) + { + if (string.IsNullOrEmpty (path)) + return null; + + Picture _pic = null; + + if (path.EndsWith (".svg", true, System.Globalization.CultureInfo.InvariantCulture)) + _pic = new SvgPicture (); + else + _pic = new BmpPicture (); + + _pic.LoadImage (path); + + return _pic; + } + public override string ToString () + { + return Path; + } + } +} + diff --git a/src/SvgPicture.cs b/src/SvgPicture.cs new file mode 100644 index 00000000..9fb3b8eb --- /dev/null +++ b/src/SvgPicture.cs @@ -0,0 +1,89 @@ +// +// SvgPicture.cs +// +// Author: +// Jean-Philippe Bruyère <jp.bruyere@hotmail.com> +// +// Copyright (c) 2015 jp +// +// This program 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. +// +// This program 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 this program. If not, see <http://www.gnu.org/licenses/>. +using System; +using System.IO; +using Cairo; + +namespace go +{ + public class SvgPicture : Picture + { + Rsvg.Handle hSVG; + string _subSvg; + + public SvgPicture () + {} + public SvgPicture (string path) : base(path) + {} + + protected override void loadFromRessource (string resId) + { + Stream stream = null; + + //first, search for ressource in main executable assembly + stream = System.Reflection.Assembly.GetEntryAssembly().GetManifestResourceStream(resId); + if (stream == null)//try to find ressource in golib assembly + stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(resId); + if (stream == null) + return; + + using (MemoryStream ms = new MemoryStream ()) { + stream.CopyTo (ms); + + hSVG = new Rsvg.Handle (ms.ToArray ()); + Dimensions = new Size (hSVG.Dimensions.Width, hSVG.Dimensions.Height); + } + } + + protected override void loadFromFile (string path) + { + if (!File.Exists(path)) + return; + + hSVG = new Rsvg.Handle (path); + Dimensions = new Size (hSVG.Dimensions.Width, hSVG.Dimensions.Height); + } + + public override void Paint (Cairo.Context gr, Rectangle rect, string subPart = "") + { + float widthRatio = (float)rect.Width / Dimensions.Width; + float heightRatio = (float)rect.Height / Dimensions.Height; + float ratio = Math.Min (widthRatio, heightRatio); + + Rectangle rImg = rect; + + gr.Save (); + + if (KeepProportions) + gr.Scale (ratio, ratio); + else + gr.Scale (widthRatio, heightRatio); + + gr.Translate (rImg.X/widthRatio, rImg.Y/heightRatio); + if (string.IsNullOrEmpty (subPart)) + hSVG.RenderCairo (gr); + else + hSVG.RenderCairoSub (gr, "#" + subPart); + gr.Restore (); + } + } +} + |