[Xfce4-commits] <xfce4-notes-plugin:master> Add a custom Cairo drawn close button icon

Mike Massonnet noreply at xfce.org
Sun Mar 14 02:36:01 CET 2010


Updating branch refs/heads/master
         to 3734297364bc646ce6aae6398cd69ebcd40315a9 (commit)
       from 4b1d5ab4f3f57c2c9dfc6ff51748b56ed7b26eb5 (commit)

commit 3734297364bc646ce6aae6398cd69ebcd40315a9
Author: Mike Massonnet <mmassonnet at xfce.org>
Date:   Sun Mar 14 02:33:39 2010 +0100

    Add a custom Cairo drawn close button icon

 ChangeLog            |    4 +
 lib/Makefile.am      |    1 +
 lib/icon-button.vala |  152 ++++++++++++++++++++++++++++++++++++++++++++++++++
 lib/window.vala      |   11 +---
 4 files changed, 159 insertions(+), 9 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 28af97e..bb9f457 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+2010-03-14  Mike Massonnet <mmassonnet at xfce.org>
+
+Add a custom Cairo drawn close button icon
+
 2010-03-11  Mike Massonnet <mmassonnet at xfce.org>
 
 Small modification on the scrollbars
diff --git a/lib/Makefile.am b/lib/Makefile.am
index 4d8281a..713440a 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -11,6 +11,7 @@ libnotes_la_VALAFLAGS =							\
 	--pkg=libxfce4util-1.0
 
 libnotes_la_SOURCES =							\
+	icon-button.vala						\
 	application.vala						\
 	hypertextview.vala						\
 	note.vala							\
diff --git a/lib/icon-button.vala b/lib/icon-button.vala
new file mode 100644
index 0000000..1a09d42
--- /dev/null
+++ b/lib/icon-button.vala
@@ -0,0 +1,152 @@
+/*
+ *  Notes - panel plugin for Xfce Desktop Environment
+ *  Copyright (c) 2010  Mike Massonnet <mmassonnet at xfce.org>
+ *
+ *  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 2 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 Library General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
+ */
+
+namespace Xnp {
+
+	public abstract class IconButton : Gtk.EventBox {
+
+		protected bool active = false;
+
+		public signal void clicked ();
+
+		construct {
+			set_visible_window (false);
+			set_above_child (true);
+			set_size_request (22, 22);
+			set_border_width (2);
+
+			enter_notify_event.connect (on_enter_notify_event);
+			leave_notify_event.connect (on_leave_notify_event);
+			button_release_event.connect (on_button_release_event);
+		}
+
+		protected abstract void draw_icon (Cairo.Context cr, int width, int height);
+
+		public override void add (Gtk.Widget widget) {
+			warning ("This object doesn't allow packing child widgets.");
+		}
+
+		public override bool expose_event (Gdk.EventExpose event) {
+			int width = allocation.width - (int)border_width * 2;
+			int height = allocation.height - (int)border_width * 2;
+			int x = allocation.width / 2 - width / 2 + allocation.x;
+			int y = allocation.height / 2 - height / 2 + allocation.y;
+
+			var cr = Gdk.cairo_create(window);
+			cr.rectangle (x, y, width, height);
+			cr.clip ();
+
+			var surface = new Cairo.ImageSurface (Cairo.Format.ARGB32, width, height);
+			var cr_ = new Cairo.Context (surface);
+			draw_icon (cr_, width, height);
+			cr.set_source_surface (surface, x, y);
+			cr.paint ();
+
+			return false;
+		}
+
+		private bool on_enter_notify_event (Gdk.EventCrossing event) {
+			active = true;
+			window.invalidate_rect (null, false);
+			return false;
+		}
+
+		private bool on_leave_notify_event (Gdk.EventCrossing event) {
+			active = false;
+			window.invalidate_rect (null, false);
+			return false;
+		}
+
+		private bool on_button_release_event (Gdk.EventButton event) {
+			if (event.button != 1)
+				return false;
+
+			int cur_x = (int)event.x;
+			int cur_y = (int)event.y;
+			int width, height;
+			get_size_request (out width, out height);
+
+			if (cur_x >= 0 && cur_x < width && cur_y >= 0 && cur_y < height)
+				clicked ();
+
+			return false;
+		}
+
+	}
+
+	public enum TitleBarButtonType {
+		EMPTY,
+		CLOSE,
+	}
+
+	public class TitleBarButton : IconButton {
+
+		public TitleBarButtonType icon_type { default = TitleBarButtonType.EMPTY; get; construct set; }
+
+		public TitleBarButton (TitleBarButtonType icon_type) {
+			Object (icon_type: icon_type);
+		}
+
+		private override void draw_icon (Cairo.Context cr, int width, int height) {
+			switch (icon_type) {
+				case TitleBarButtonType.CLOSE:
+					draw_close_button (cr, width, height);
+					break;
+				default:
+					break;
+			}
+		}
+
+		private void draw_close_button (Cairo.Context cr, int width, int height) {
+			int border = 4;
+			int x1 = border;
+			int x2 = width - border;
+			int y1 = border;
+			int y2 = height - border;
+			if (x2 <= x1 || y2 <= y1) {
+				return;
+			}
+
+			cr.set_line_cap (Cairo.LineCap.ROUND);
+
+			cr.set_source_rgba (1, 1, 1, active ? 0.4 : 0.2);
+			cr.set_line_width (4);
+			cr.move_to (x1, y1);
+			cr.line_to (x2, y2);
+			cr.move_to (x2, y1);
+			cr.line_to (x1, y2);
+
+			cr.stroke ();
+
+			Gdk.cairo_set_source_color (cr,
+					active ? style.base[Gtk.StateType.NORMAL]
+						  : style.fg[Gtk.StateType.INSENSITIVE]);
+			cr.set_line_width (2.66);
+			cr.move_to (x1, y1);
+			cr.line_to (x2, y2);
+			cr.move_to (x2, y1);
+			cr.line_to (x1, y2);
+
+			cr.stroke ();
+		}
+
+	}
+
+}
+
diff --git a/lib/window.vala b/lib/window.vala
index 7d81c80..60fbf36 100644
--- a/lib/window.vala
+++ b/lib/window.vala
@@ -1,6 +1,6 @@
 /*
  *  Notes - panel plugin for Xfce Desktop Environment
- *  Copyright (c) 2009  Mike Massonnet <mmassonnet at xfce.org>
+ *  Copyright (c) 2009-2010  Mike Massonnet <mmassonnet at xfce.org>
  *
  *  TODO:
  *  - Follow GNOME bug #551184 to change accelerators hexa values
@@ -238,18 +238,11 @@ namespace Xnp {
 			this.title_label.xalign = (float)0.0;
 			title_evbox.add (this.title_label);
 			title_box.pack_start (title_evbox, true, true, 6);
-			var close_box = new Gtk.Button ();
+			var close_box = new Xnp.TitleBarButton (Xnp.TitleBarButtonType.CLOSE);
 			close_box.tooltip_text = Gtk.accelerator_get_label (0xff1b, 0); // GDK_Escape
-			close_box.set_relief (Gtk.ReliefStyle.NONE);
-			close_box.can_focus = false;
-			var close_label = new Gtk.Label ("<b>X</b>");
-			close_label.use_markup = true;
-			close_box.add (close_label);
 			title_box.pack_start (close_box, false, false, 2);
 			title_box.show_all ();
 			vbox_frame.pack_start (title_box, false, false, 0);
-			if (close_box.allocation.width < 22)
-				close_box.set_size_request (22, -1);
 
 			/* Build content box */
 			this.content_box = new Gtk.VBox (false, 0);



More information about the Xfce4-commits mailing list