Clone in a terminal:
Commit 40b867c
Johan Mattsson - 2015-03-14T19:56:31+00:00
40b867c5b5f9b90e5a8fcd00b8b2b89e3d82c053johan.mattsson.m@gmail.com

Platform independent tool tip

Changed files (6)

Diff

diff --git a/birdfont/GtkWindow.vala b/birdfont/GtkWindow.valaindex 8adbe2e..c4111ee 100644--- a/birdfont/GtkWindow.vala+++ b/birdfont/GtkWindow.vala@@ -521,52 +521,6 @@ public class GtkWindow : Gtk.Window, NativeWindow { 		return true; 	} 	-	// TODO: add the default tooltip style to the label-	public void show_tooltip (string tooltip, int x, int y) {-		Label tooltip_label;-		int parent_x, parent_y;-		int tool_box_x, tool_box_y;-		int posx, posy;-		Gtk.Allocation label_allocation;-		Gtk.Box box;-		-		box = new Gtk.Box (Gtk.Orientation.VERTICAL, 0);--		get_position (out parent_x, out parent_y);-		toolbox.translate_coordinates (toolbox.get_toplevel (), 0, 0, out tool_box_x, out tool_box_y);-		-		tooltip_window.hide ();-		-		tooltip_window = new Gtk.Window (Gtk.WindowType.POPUP);-		tooltip_label = new Label(tooltip);-		tooltip_label.margin = 0;-		-		box.pack_start (tooltip_label, true, true, 0);-		-		tooltip_window.add (box);-		tooltip_label.show();-		box.show ();-		-		posx = parent_x + tool_box_x + x;-		posy = parent_y + tool_box_y + y - 7;-		tooltip_window.move (posx, posy);--		tooltip_window.show();--		label_allocation = new Gtk.Allocation ();-		tooltip_label.size_allocate (label_allocation);--		// move label to the left if it is off screen-		if (posx + label_allocation.width > screen.get_width () - 20) {-			tooltip_window.move (screen.get_width () - label_allocation.width - 20, posy);-		}--	}--	public void hide_tooltip () {-		tooltip_window.hide ();-	}- 	public void run_background_thread (Task t) { 		unowned Thread<void*> bg; 		diff --git a/libbirdfont/NativeWindow.vala b/libbirdfont/NativeWindow.valaindex 51ae6cf..b2cce90 100644--- a/libbirdfont/NativeWindow.vala+++ b/libbirdfont/NativeWindow.vala@@ -39,12 +39,6 @@ public interface NativeWindow : GLib.Object {  	/** Convert an image to PNG format. */ 	public abstract bool convert_to_png (string from, string to);--	/** Show help text. */-	public abstract void show_tooltip (string tooltip, int x, int y);-	-	/** Hide help text. */-	public abstract void hide_tooltip (); 	 	/** Export fonts in a background thread. */ 	public abstract void export_font ();diff --git a/libbirdfont/Renderer/TextArea.vala b/libbirdfont/Renderer/TextArea.valaindex 1de3a7b..8a25540 100644--- a/libbirdfont/Renderer/TextArea.vala+++ b/libbirdfont/Renderer/TextArea.vala@@ -832,6 +832,10 @@ public class TextArea : Widget { 		tx = 0; 		ty = font_size; 		+		if (allocation.width <= 0 || allocation.height <= 0) {+			warning ("Parent widget allocation is not set.");+		}+		 		for (i = paragraphs.size - 1; i >= 0 && paragraphs.size > 1; i--) { 			if (unlikely (paragraphs.get (i).is_empty ())) { 				warning ("Empty paragraph.");diff --git a/libbirdfont/Tool.vala b/libbirdfont/Tool.valaindex b9fe7cd..e8ed1c9 100644--- a/libbirdfont/Tool.vala+++ b/libbirdfont/Tool.vala@@ -100,7 +100,7 @@ public class Tool : Widget { 		}); 		 		move_out_action.connect ((self) => {-			MainWindow.native_window.hide_tooltip ();+			MainWindow.get_toolbox ().hide_tooltip (); 			active_tooltip.showing_this_tooltip = false; 		}); 		@@ -158,13 +158,16 @@ public class Tool : Widget { 	 	public static void show_tooltip () { 		TimeoutSource timer_hide;+		Toolbox toolbox;+		+		toolbox = MainWindow.get_toolbox (); 		 		// hide tooltip label later 		if (!active_tooltip.showing_this_tooltip) { 			timer_hide = new TimeoutSource (1500); 			timer_hide.set_callback (() => { 				if (!active_tooltip.is_active ()) {-					MainWindow.native_window.hide_tooltip ();+					toolbox.hide_tooltip (); 					active_tooltip.showing_this_tooltip = false; 					active_tooltip = new Tool (); 				}				@@ -174,8 +177,9 @@ public class Tool : Widget { 		} 		 		active_tooltip.showing_this_tooltip = true;-		MainWindow.native_window.hide_tooltip ();-		MainWindow.native_window.show_tooltip (active_tooltip.tip, (int)active_tooltip.x, (int)active_tooltip.y);+			+		toolbox.hide_tooltip ();+		toolbox.show_tooltip (active_tooltip.tip, (int) active_tooltip.x, (int) active_tooltip.y); 	} 	 	public void set_icon (string name) {diff --git a/libbirdfont/Toolbox.vala b/libbirdfont/Toolbox.valaindex 44c4537..77d34e9 100644--- a/libbirdfont/Toolbox.vala+++ b/libbirdfont/Toolbox.vala@@ -46,8 +46,12 @@ public class Toolbox : GLib.Object  { 	public List<ToolCollection> tool_sets = new List<ToolCollection> (); 	 	static double scale = 1;++	string? tool_tip = null;+	double tool_tip_x = 0;+	double tool_tip_y = 0; 	-	public Toolbox (GlyphCanvas glyph_canvas, TabBar tab_bar) {			+	public Toolbox (GlyphCanvas glyph_canvas, TabBar tab_bar) { 		current_tool = new Tool ("no_icon"); 		press_tool = new Tool (null); @@ -465,9 +469,45 @@ public class Toolbox : GLib.Object  { 			draw_expanders (w, h, cr); 			 			cr.restore ();+			+			draw_tool_tip (cr);+		}+	}+	+	private void draw_tool_tip (Context cr) {+		TextArea t;+		+		if (tool_tip != null && tool_tip != "") {+			t = new TextArea (20 * get_scale ());+			t.allocation = new WidgetAllocation.for_area (0, 0, allocation_width, allocation_height);+			t.set_editable (false);+			t.set_text ((!) tool_tip);+			t.width = allocation_width - 20 * get_scale ();+			t.min_height = 17 * get_scale ();+			t.height = 17 * get_scale ();++			t.layout ();++			t.widget_x = 10 * get_scale ();+			t.widget_y = tool_tip_y - t.height - 5 * get_scale ();		++			t.draw (cr); 		} 	} 	+	public void hide_tooltip () {+		tool_tip = null;+		redraw_tool_box ();+	}+	+	public void show_tooltip (string tool_tip, double x, double y) {+		this.tool_tip = tool_tip;+		this.tool_tip_x = x;+		this.tool_tip_y = y;+		+		redraw_tool_box ();+	}+	 	public class EmptySet : ToolCollection  { 		 		public ImageSurface? background;diff --git a/libbirdfont/Widget.vala b/libbirdfont/Widget.valaindex 8c3667a..5cd70dd 100644--- a/libbirdfont/Widget.vala+++ b/libbirdfont/Widget.vala@@ -29,7 +29,7 @@ public abstract class Widget : GLib.Object { 	 	public WidgetAllocation allocation = new WidgetAllocation (); -	public void draw_rounded_rectangle (Context cr, double x, double y, double w, double h, double radius) {	+	public static void draw_rounded_rectangle (Context cr, double x, double y, double w, double h, double radius) {	 		// fixme radius is padding not margin 		cr.move_to (x, y + radius); 		cr.arc (x + radius, y + radius, radius, 2 * (PI / 2), 3 * (PI / 2));

Contact

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