Clone in a terminal:
Commit 8dd2ef3
Johan Mattsson - 2016-01-15T11:17:07+00:00
8dd2ef3be86e5c55bc8382c346b56a56f1677ea6johan.mattsson.m@gmail.com

Use max-height and max-width properties in CSS

Changed files (11)

Diff

diff --git a/birdui/AbsoluteLayout.vala b/birdui/AbsoluteLayout.valaindex d8aaa1f..0b022d2 100644--- a/birdui/AbsoluteLayout.vala+++ b/birdui/AbsoluteLayout.vala@@ -20,11 +20,8 @@ namespace Bird {  class AbsoluteLayout : Component { -	public AbsoluteLayout () {-	}--	public AbsoluteLayout.for_tag (XmlElement layout_tag) {-		parse (layout_tag);+	public AbsoluteLayout (XmlElement layout_tag, Defs defs) {+		base (layout_tag, defs); 	}  	public override string to_string () {diff --git a/birdui/BoxLayout.vala b/birdui/BoxLayout.valaindex fe147ee..3d91105 100644--- a/birdui/BoxLayout.vala+++ b/birdui/BoxLayout.vala@@ -27,48 +27,32 @@ class BoxLayout : Component {  	public BoxOrientation orientation { get; private set; } 	-	public BoxLayout (BoxOrientation orientation) {-		base ();+	public BoxLayout (XmlElement layout_tag, BoxOrientation orientation, Defs defs) {+		base (layout_tag, defs); 		this.orientation = orientation; 	}--	public BoxLayout.for_tag (XmlElement layout_tag, BoxOrientation orientation) {-		base.for_tag (layout_tag);-		this.orientation = orientation;-	}-		-	public override void layout () {-		switch (orientation) {-		case BoxOrientation.HORIZONTAL:-			layout_horizontal ();-			break;-		case BoxOrientation.VERTICAL:-			layout_vertical ();-			break;+	+	void set_max_size (Component component) {+		string? max_width = style.get_css_property ("max-width");+		if (max_width != null) {+			double w = SvgFile.parse_number (max_width);+			+			if (component.width > w) {+				component.width = w;+			} 		}-	} -	void layout_horizontal () {-		double child_x = 0;-		double child_y = 0;-		-		foreach (Component component in components) {-			component.x = child_x;-			component.y = child_y;-			component.layout ();-			component.apply_padding ();-			-			child_x += component.padded_width;+		string? max_height = style.get_css_property ("max-height");+		if (max_height != null) {+			double h = SvgFile.parse_number (max_height); 			-			if (component.height > height) {-				height = component.height;+			if (component.height > h) {+				component.height = h; 			}-		}-		-		width = child_x;+		}	 	}--	void layout_vertical () {+	+	public override void layout () { 		double child_x = 0; 		double child_y = 0; 		@@ -77,15 +61,28 @@ class BoxLayout : Component { 			component.y = child_y; 			component.layout (); 			component.apply_padding ();+			set_max_size (component); -			child_y += component.padded_height;-						-			if (component.width > width) {-				width = component.width;+			if (orientation == BoxOrientation.HORIZONTAL) {+				child_x += component.padded_width;+				+				if (component.height > height) {+					height = component.height;+				}+			} else {+				child_y += component.padded_height;+							+				if (component.width > width) {+					width = component.width;+				}				 			} 		} 		-		height = child_y;+		if (orientation == BoxOrientation.HORIZONTAL) {+			width = child_x;+		} else {+			height = child_y;+		} 	}  	public override string to_string () {diff --git a/birdui/Component.vala b/birdui/Component.valaindex 878da2a..672f94d 100644--- a/birdui/Component.vala+++ b/birdui/Component.vala@@ -44,45 +44,44 @@ class Component : GLib.Object { 	/** The parts this component is made of. */ 	protected ArrayList<Component> components = new ArrayList<Component> (); -	XmlElement? component_tag = null;+	XmlElement component_tag; 	 	/** Style sheet and other SVG definitions. */-	Defs? defs = null;-	SvgStyle style = new SvgStyle ();+	Defs defs = new Defs ();+	protected SvgStyle style = new SvgStyle ();  	string? css_class = null; 	string? id = null; -	public Component () {-	}--	public Component.for_tag (XmlElement component_tag) {+	public Component (XmlElement component_tag, Defs defs) {+		this.defs = defs; 		this.component_tag = component_tag; 		parse (component_tag); 	} -	private void inherit_style (Defs? defs) {+	public Component.embedded (XmlElement component_tag, Defs defs) { 		this.defs = defs;+		this.component_tag = component_tag;+	}+	+	public Component.load (string file_name) {+		load_file (file_name); 	} -	protected void apply_padding () {	+	private void inherit_style (Defs defs) {+		this.defs = defs; 	} -	protected void load_svg_file (string file_name) {-		SvgComponent svg = new SvgComponent.for_file (file_name);-		add_component (svg);		+	protected void apply_padding () {	 	}  	protected void parse_style (XmlElement style_tag) { 		Defs definitions = new Defs ();		-		StyleSheet style_sheet = StyleSheet.parse (definitions, style_tag);-		-		if (defs != null) {-			Defs subscope_definitions = ((!) defs).shallow_copy ();-			subscope_definitions.style_sheet.merge (style_sheet);-		} else {-			defs = definitions;-		}+		definitions.style_sheet = StyleSheet.parse (definitions, style_tag);++		Defs subscope_definitions = defs.shallow_copy ();+		subscope_definitions.style_sheet.merge (definitions.style_sheet);+		defs = subscope_definitions; 	} 	 	protected void parse_svg (XmlElement svg_tag) {@@ -90,7 +89,9 @@ class Component : GLib.Object { 			string attribute_name = attribute.get_name (); 			 			if (attribute_name == "file") {-				load_svg_file (attribute.get_content ());+				string file_name = attribute.get_content ();+				SvgComponent svg = new SvgComponent (component_tag, defs, file_name);+				add_component (svg);	 			} 		} 	}@@ -98,22 +99,21 @@ class Component : GLib.Object { 	protected void add_component (Component component) { 		components.add (component); 		component.inherit_style (defs);-		-		if (component.component_tag != null) {-			style = SvgStyle.parse (defs, style, (!) component.component_tag);-		}+		component.style = SvgStyle.parse (defs, style, (!) component.component_tag); 	}  	protected void parse_layout (XmlElement layout_tag) {+		bool parse_style = false;+		 		foreach (Attribute attribute in layout_tag.get_attributes ()) { 			string attribute_name = attribute.get_name (); 			 			if (attribute_name == "type") { 				if (attribute.get_content () == "hbox") {-					HBox hbox = new HBox.for_tag (layout_tag);+					HBox hbox = new HBox (layout_tag, defs); 					add_component (hbox); 				} else if (attribute.get_content () == "vbox") {-					VBox hbox = new VBox.for_tag (layout_tag);+					VBox hbox = new VBox (layout_tag, defs); 					add_component (hbox); 				} else { 					warning ("Layout of type " + attribute.get_content ()@@ -124,7 +124,7 @@ class Component : GLib.Object { 			} else if (attribute_name == "class") { 				css_class = attribute.get_content (); 			} else if (attribute_name == "style") {-				// style will be parsed later+				// style will be parsed later in add_component 			} else { 				unused_attribute (attribute_name); 			}@@ -136,8 +136,7 @@ class Component : GLib.Object { 			string attribute_name = attribute.get_name (); 			 			if (attribute_name == "file") {-				Component component = new Component ();-				component.load (attribute.get_content ());+				Component component = new Component (component_tag, defs); 				add_component (component); 			} else { 				unused_attribute (attribute_name);@@ -173,11 +172,12 @@ class Component : GLib.Object { 		warning ("The tag " + tag_name + " is not known in this version."); 	} -	public void load (string file_name) {+	public void load_file (string file_name) { 		if (file_name.has_suffix (".ui")) { 			load_layout (file_name); 		} else if (file_name.has_suffix (".svg")) {-			load_svg_file (file_name);+			SvgComponent svg = new SvgComponent.for_file (file_name);+			add_component (svg);	 		} else { 			warning (file_name + " is not a ui file or svg file."); 		}@@ -202,8 +202,11 @@ class Component : GLib.Object { 		}  		XmlTree xml_parser = new XmlTree (xml_data);-		Component component = new Component.for_tag (xml_parser.get_root ());-		add_component (component);+		+		component_tag = xml_parser.get_root ();+		defs = new Defs ();+		parse (component_tag);+		 		layout (); 	} 	@@ -246,13 +249,7 @@ class Component : GLib.Object { 	} 	 	public virtual string to_string () {-		if (component_tag != null) {-			XmlElement tag = (!) component_tag;-			return @"Component $(tag.get_name ())";-		} else {-			-			return "Component";-		}+		return @"Component $(component_tag.get_name ())"; 	} 	 	public void print_tree () {diff --git a/birdui/GtkWindow.vala b/birdui/GtkWindow.valaindex a32bd4e..8f4375d 100644--- a/birdui/GtkWindow.vala+++ b/birdui/GtkWindow.vala@@ -22,8 +22,7 @@ int main (string[] args) { 	window.set_title ("UI Bird"); 	window.destroy.connect (Gtk.main_quit); 	-	Component layout = new Component ();-	layout.load ("test.ui");+	Component layout = new Component.load ("test.ui"); 	 	Bird.Widget primary_layout = new Bird.Widget (layout); diff --git a/birdui/HBox.vala b/birdui/HBox.valaindex e26102d..d7af576 100644--- a/birdui/HBox.vala+++ b/birdui/HBox.vala@@ -14,16 +14,13 @@  using Gee; using B;+using SvgBird;  namespace Bird {  class HBox : BoxLayout {-	public HBox () {-		base (BoxOrientation.HORIZONTAL);-	}--	public HBox.for_tag (XmlElement layout) {-		base.for_tag (layout, BoxOrientation.HORIZONTAL);+	public HBox (XmlElement layout, Defs defs) {+		base (layout, BoxOrientation.HORIZONTAL, defs); 	} 	 	public override string to_string () {diff --git a/birdui/SvgComponent.vala b/birdui/SvgComponent.valaindex 106a853..b0ebd3a 100644--- a/birdui/SvgComponent.vala+++ b/birdui/SvgComponent.vala@@ -24,16 +24,13 @@ class SvgComponent : Component { 	string file_name = ""; 	SvgDrawing? drawing = null; 	-	public SvgComponent () {-		base ();-	}--	public SvgComponent.for_tag (XmlElement svg_component_tag) {-		base ();+	public SvgComponent (XmlElement svg_component_tag, Defs defs, string svg_file) {+		base.embedded (svg_component_tag, defs);+		load_svg (svg_file); 	}  	public SvgComponent.for_file (string svg_file) {-		base ();+		base.embedded (new XmlElement.empty (), new Defs ()); 		load_svg (svg_file); 	} diff --git a/birdui/VBox.vala b/birdui/VBox.valaindex a9a1e03..d3d81cb 100644--- a/birdui/VBox.vala+++ b/birdui/VBox.vala@@ -14,16 +14,13 @@  using Gee; using B;+using SvgBird;  namespace Bird { 	 class VBox : BoxLayout {-	public VBox () {-		base (BoxOrientation.VERTICAL);-	}--	public VBox.for_tag (XmlElement layout) {-		base.for_tag (layout, BoxOrientation.VERTICAL);+	public VBox (XmlElement layout, Defs defs) {+		base (layout, BoxOrientation.VERTICAL, defs); 	} 	 	public override string to_string () {diff --git a/libsvgbird/SelectorPattern.vala b/libsvgbird/SelectorPattern.valaindex e8ec52a..d4021ce 100644--- a/libsvgbird/SelectorPattern.vala+++ b/libsvgbird/SelectorPattern.vala@@ -51,7 +51,6 @@ public class SelectorPattern : GLib.Object { 						 			if (pattern.name == ">") { 				if (i - 1 < 0) {-					 					return false; 				} 				diff --git a/libsvgbird/SelectorTag.vala b/libsvgbird/SelectorTag.valaindex 8329c65..2d34463 100644--- a/libsvgbird/SelectorTag.vala+++ b/libsvgbird/SelectorTag.vala@@ -146,7 +146,7 @@ public class SelectorTag : GLib.Object { 			if (id == null) { 				return false; 			} -			+ 			if (((!) this.id) != ((!) id)) { 				return false; 			}diff --git a/libsvgbird/StyleSheet.vala b/libsvgbird/StyleSheet.valaindex 2062300..986a61f 100644--- a/libsvgbird/StyleSheet.vala+++ b/libsvgbird/StyleSheet.vala@@ -47,7 +47,7 @@ public class StyleSheet : GLib.Object { 		return style_sheet; 	} 	-	public void inherit_style (XmlElement tag, SvgStyle style) {+	public void apply_style (XmlElement tag, SvgStyle style) { 		string? id = null; 		string? css_class = null; 		@@ -63,7 +63,7 @@ public class StyleSheet : GLib.Object { 		 		foreach (Selector selector in styles) { 			if (selector.match (tag, id, css_class)) {-				style.inherit (selector.style);+				style.apply (selector.style); 			} 		} 	}diff --git a/libsvgbird/SvgStyle.vala b/libsvgbird/SvgStyle.valaindex bc42b6f..7e8575a 100644--- a/libsvgbird/SvgStyle.vala+++ b/libsvgbird/SvgStyle.vala@@ -28,39 +28,54 @@ public class SvgStyle : GLib.Object { 	public double stroke_width = 0;  	private static Gee.HashMap<string, string>? inheritance;+	private static Mutex inheritance_mutex = new Mutex (); 	 	public SvgStyle () { 	} +	public bool has_css_property (string property) {+		return style.has_key (property);+	}++	public string? get_css_property (string property) {+		string p = property.down ();+		+		if (!has_css_property (p)) {+			return null;+		}+		+		return style.get (p);+	}+	 	public static bool is_inherited (string property) {-		lock (inheritace) {-			if (unlikely (inheritance == null)) {-				create_inheritance_table ();-			}-			-			Gee.HashMap<string, string> inheritance = (!) inheritance;-			string? inherited = inherited.get (property);-			-			if (inherited == null) {-				return true;-			}+		inheritance_mutex.lock ();+		if (unlikely (inheritance == null)) {+			create_inheritance_table ();+		}+		+		Gee.HashMap<string, string> inheritance = (!) inheritance;+		string? inherited = inheritance.get (property);+		inheritance_mutex.unlock (); -			string inherited_property = (!) inherited;-			return inherited_property == "yes";+		if (inherited == null) {+			return false; 		}+		+		string inherited_property = (!) inherited;+		return inherited_property == "yes"; 	}  	/** Specify inheritance for a CSS property. */-	public static bool set_inheritance (string property, bool inherit) {-		lock (inheritace) {-			if (unlikely (inheritance == null)) {-				create_inheritance_table ();-			}-			-			Gee.HashMap<string, string> inheritance = (!) inheritance;-			string inherit_property = inherit ? "yes" : "no";-			inheritance.set (property, inherit_property);+	public static void set_inheritance (string property, bool inherit) {+		inheritance_mutex.lock ();+		if (unlikely (inheritance == null)) {+			create_inheritance_table (); 		}+		+		Gee.HashMap<string, string> inheritance = (!) inheritance;+		string inherit_property = inherit ? "yes" : "no";+		inheritance.set (property, inherit_property);+		inheritance_mutex.unlock (); 	}  	public SvgStyle.for_properties (Defs? defs, string style) {@@ -68,10 +83,6 @@ public class SvgStyle : GLib.Object { 		set_style_properties (defs, this); 	} -	public string? get_css_property (string key) {-		return style.get (key.down ());-	}- 	public SvgStyle copy () { 		SvgStyle style = new SvgStyle (); 		style.inherit (this);@@ -110,6 +121,21 @@ public class SvgStyle : GLib.Object { 		return stroke_width; 	} 	+	public string to_string () {+		StringBuilder description = new StringBuilder ();+		+		description.append ("SvgStyle: ");+		+		foreach (string key in style.keys) {+			description.append (key);+			description.append (": ");+			description.append (style.get (key));+			description.append ("; ");+		}+		+		return description.str.strip ();+	}+	 	public void inherit (SvgStyle inherited) { 		foreach (string key in inherited.style.keys) { 			if (is_inherited (key)) {@@ -117,6 +143,12 @@ public class SvgStyle : GLib.Object { 			} 		} 	}++	public void apply (SvgStyle inherited) {+		foreach (string key in inherited.style.keys) {+			style.set (key, inherited.style.get (key));+		}+	} 	 	public static SvgStyle parse (Defs? d, SvgStyle inherited, XmlElement tag) { 		SvgStyle s = new SvgStyle ();@@ -127,7 +159,7 @@ public class SvgStyle : GLib.Object { 		 		if (d != null) { 			StyleSheet style_sheet = ((!) d).style_sheet;-			style_sheet.inherit_style (tag, s);+			style_sheet.apply_style (tag, s); 		}  		foreach (Attribute a in attributes) {@@ -242,8 +274,8 @@ public class SvgStyle : GLib.Object { 		} 	} -	private static create_inheritance_table () {-		inheritance = Gee.HashMap<string, string> ();+	private static void create_inheritance_table () {+		inheritance = new Gee.HashMap<string, string> (); 		 		Gee.HashMap<string, string> inherited = (!) inheritance; 		

Contact

Email: johan.mattsson.m@gmail.com
Mastodon: typo.social/@birdfont
GitHub: johanmattssonm
LinkedIn: johan-mattsson-2ab324112