Commit ea0adca
delete button in version menu
Changed files (3)
modifiedlibbirdfont/DropMenu.valamodifiedlibbirdfont/MenuAction.valamodifiedlibbirdfont/VersionList.vala
Diff
@@ -41,12 +41,31 @@ public class DropMenu : GLib.Object { ImageSurface? icon = null; + public signal void signal_delete_item (int item_index);+ public DropMenu (string icon_file = "") { if (icon_file != "") { icon = Icons.get_icon (@"$icon_file.png"); } } + public void recreate_index () {+ int i = -1;+ foreach (MenuAction a in actions) {+ a.index = i;+ i++;+ }+ }+ + public MenuAction get_action_no2 () {+ if (actions.length () < 2) {+ warning ("No such action");+ return new MenuAction ("NULL");+ }+ + return actions.nth (1).data;+ }+ public void deselect_all () { foreach (MenuAction m in actions) { m.set_selected (false);@@ -82,14 +101,40 @@ public class DropMenu : GLib.Object { public bool menu_item_action (double px, double py) { MenuAction? action;-+ MenuAction a;+ unowned List <MenuAction> ma;+ int index;+ if (menu_visible) { action = get_menu_action_at (px, py); if (action != null) {- ((!) action).action ((!)action);- selected (this);- menu_visible = false;+ a = (!) action;+ if (a.has_delete_button && x - 5 < px < x + 13 && actions.length () > 2) { // over delete button+ + index = 0;+ ma = actions.first ();+ while (true) {+ if (a == ma.data) {+ actions.remove_link (ma);+ signal_delete_item (index);+ break;+ }+ + if (ma == actions.last ()) {+ break;+ } else {+ ma = ma.next;+ index++;+ }+ }+ return false;+ } else {+ a.action (a);+ selected (this);+ menu_visible = false;+ }+ return true; } }@@ -143,15 +188,29 @@ public class DropMenu : GLib.Object { cr.set_line_join (LineJoin.ROUND); cr.set_line_width (12); - if (direction == MenuDirection.DROP_DOWN)+ if (direction == MenuDirection.DROP_DOWN) { cr.rectangle (x - 100 + 18, y + 18, 88, actions.length () * item_height - 12);- else+ } else { cr.rectangle (x - 100 + 18, y + 6 - actions.length () * item_height, 88, actions.length () * item_height - 12);- + }+ cr.fill_preserve (); cr.stroke (); cr.restore (); + // fill the lower right corner+ if (direction != MenuDirection.DROP_DOWN) {+ cr.save ();+ cr.set_source_rgba (122/255.0, 150/255.0, 169/255.0, 1);+ cr.set_line_width (0); + + cr.rectangle (x, y - 7, 12, 7);++ cr.fill_preserve ();+ cr.stroke ();+ cr.restore ();+ }+ cr.save (); n = 0;@@ -24,10 +24,17 @@ public class MenuAction : GLib.Object { public DropMenu.Selected action; public DropMenu? parent = null; public int index = -1;+ public bool has_delete_button = true;+ bool selected = false;+ static ImageSurface? delete_button = null; public MenuAction (string label) { this.label = label;+ + if (delete_button == null) {+ delete_button = Icons.get_icon ("delete_menu_item.png");+ } } public void set_selected (bool s) {@@ -35,6 +42,8 @@ public class MenuAction : GLib.Object { } public virtual void draw (double x, double y, Context cr) {+ ImageSurface img;+ if (selected) { cr.save (); cr.set_line_join (LineJoin.ROUND);@@ -45,6 +54,14 @@ public class MenuAction : GLib.Object { cr.stroke (); cr.restore (); }++ if (has_delete_button && delete_button != null) {+ img = (!) delete_button;+ cr.save ();+ cr.set_source_surface (img, x - img.get_width () / 2 + 82, y - img.get_height () / 2 - 5);+ cr.paint ();+ cr.restore ();+ } cr.save (); cr.set_source_rgba (1, 1, 1, 1);@@ -30,6 +30,7 @@ public class VersionList : DropMenu { set_direction (MenuDirection.POP_UP); MenuAction ma = add_item (_("New version"));+ ma.has_delete_button = false; ma.action = (self) => { return_if_fail (self.parent != null); return_if_fail (glyphs.length () > 0);@@ -40,6 +41,22 @@ public class VersionList : DropMenu { current_version = (int) glyphs.length () - 1; }; + signal_delete_item.connect ((index) => {+ unowned List<Glyph> gl;+ index--; // first item is the add new action+ return_if_fail (0 <= index < glyphs.length ());+ gl = glyphs.nth (index);+ glyphs.remove_link (gl);+ + recreate_index ();+ + if (index == current_version) {+ set_selected_item (get_action_no2 ());+ } else if (index < current_version) {+ current_version--;+ }+ });+ if (g != null) { add_glyph ((!) g); }