diff options
author | Alessandro Ghedini <al3xbio@gmail.com> | 2011-11-20 15:50:38 +0100 |
---|---|---|
committer | Alessandro Ghedini <al3xbio@gmail.com> | 2011-11-20 15:51:50 +0100 |
commit | 3997b71e281a5f43cc8c1892e85de30728df7b2d (patch) | |
tree | 96f2be7c48af03b7c540eba9dd6680eb270f23b8 /src/utilities | |
parent | 902829c62f552f35517783570025b479745de3c5 (diff) |
Imported Upstream version 0.3.1
Diffstat (limited to 'src/utilities')
-rw-r--r-- | src/utilities/bindingManager.vala | 191 | ||||
-rw-r--r-- | src/utilities/config.vala | 16 | ||||
-rw-r--r-- | src/utilities/focusGrabber.vala | 74 | ||||
-rw-r--r-- | src/utilities/icon.vala | 102 | ||||
-rw-r--r-- | src/utilities/image.vala | 163 | ||||
-rw-r--r-- | src/utilities/renderedText.vala | 110 | ||||
-rw-r--r-- | src/utilities/themedIcon.vala | 161 | ||||
-rw-r--r-- | src/utilities/trigger.vala | 255 |
8 files changed, 485 insertions, 587 deletions
diff --git a/src/utilities/bindingManager.vala b/src/utilities/bindingManager.vala index 8795124..437f4c1 100644 --- a/src/utilities/bindingManager.vala +++ b/src/utilities/bindingManager.vala @@ -53,6 +53,10 @@ public class BindingManager : GLib.Object { Gdk.ModifierType.LOCK_MASK|Gdk.ModifierType.MOD5_MASK, Gdk.ModifierType.MOD2_MASK|Gdk.ModifierType.LOCK_MASK|Gdk.ModifierType.MOD5_MASK }; + + private uint32 delayed_count = 0; + private X.Event? delayed_event = null; + private Keybinding? delayed_binding = null; ///////////////////////////////////////////////////////////////////// /// Helper class to store keybinding @@ -60,16 +64,12 @@ public class BindingManager : GLib.Object { private class Keybinding { - public Keybinding(string accelerator, int keycode, Gdk.ModifierType modifiers, string id) { - this.accelerator = accelerator; - this.keycode = keycode; - this.modifiers = modifiers; + public Keybinding(Trigger trigger, string id) { + this.trigger = trigger; this.id = id; } - public string accelerator { get; set; } - public int keycode { get; set; } - public Gdk.ModifierType modifiers { get; set; } + public Trigger trigger { get; set; } public string id { get; set; } } @@ -89,32 +89,30 @@ public class BindingManager : GLib.Object { /// Binds the ID to the given accelerator. ///////////////////////////////////////////////////////////////////// - public void bind(string accelerator, string id) { - uint keysym; - Gdk.ModifierType modifiers; - Gtk.accelerator_parse(accelerator, out keysym, out modifiers); + public void bind(Trigger trigger, string id) { + if(trigger.key_code != 0) { + Gdk.Window rootwin = Gdk.get_default_root_window(); + X.Display display = Gdk.x11_drawable_get_xdisplay(rootwin); + X.ID xid = Gdk.x11_drawable_get_xid(rootwin); - if (keysym == 0) { - warning("Invalid keystroke: " + accelerator); - return; - } - - Gdk.Window rootwin = Gdk.get_default_root_window(); - X.Display display = Gdk.x11_drawable_get_xdisplay(rootwin); - X.ID xid = Gdk.x11_drawable_get_xid(rootwin); - int keycode = display.keysym_to_keycode(keysym); - - if(keycode != 0) { Gdk.error_trap_push(); foreach(uint lock_modifier in lock_modifiers) { - display.grab_key(keycode, modifiers|lock_modifier, xid, false, X.GrabMode.Async, X.GrabMode.Async); + if (trigger.with_mouse) { + display.grab_button(trigger.key_code, trigger.modifiers|lock_modifier, xid, false, + X.EventMask.ButtonPressMask | X.EventMask.ButtonReleaseMask, + X.GrabMode.Async, X.GrabMode.Async, xid, 0); + } else { + display.grab_key(trigger.key_code, trigger.modifiers|lock_modifier, + xid, false, X.GrabMode.Async, X.GrabMode.Async); + } } Gdk.flush(); - Keybinding binding = new Keybinding(accelerator, keycode, modifiers, id); + Keybinding binding = new Keybinding(trigger, id); bindings.add(binding); + display.flush(); } } @@ -130,13 +128,18 @@ public class BindingManager : GLib.Object { foreach(var binding in bindings) { if(id == binding.id) { foreach(uint lock_modifier in lock_modifiers) { - display.ungrab_key(binding.keycode, binding.modifiers, xid); + if (binding.trigger.with_mouse) { + display.ungrab_button(binding.trigger.key_code, binding.trigger.modifiers|lock_modifier, xid); + } else { + display.ungrab_key(binding.trigger.key_code, binding.trigger.modifiers|lock_modifier, xid); + } } remove_bindings.add(binding); } } bindings.remove_all(remove_bindings); + display.flush(); } ///////////////////////////////////////////////////////////////////// @@ -144,15 +147,13 @@ public class BindingManager : GLib.Object { ///////////////////////////////////////////////////////////////////// public string get_accelerator_label_of(string id) { - string accelerator = this.get_accelerator_of(id); - - if (accelerator == "") - return _("Not bound"); + foreach (var binding in bindings) { + if (binding.id == id) { + return binding.trigger.label_with_specials; + } + } - uint key = 0; - Gdk.ModifierType mods; - Gtk.accelerator_parse(accelerator, out key, out mods); - return Gtk.accelerator_get_label(key, mods); + return _("Not bound"); } ///////////////////////////////////////////////////////////////////// @@ -162,7 +163,38 @@ public class BindingManager : GLib.Object { public string get_accelerator_of(string id) { foreach (var binding in bindings) { if (binding.id == id) { - return binding.accelerator; + return binding.trigger.name; + } + } + + return ""; + } + + ///////////////////////////////////////////////////////////////////// + /// Returns whether the pie with the given ID is in turbo mode. + ///////////////////////////////////////////////////////////////////// + + public bool get_is_turbo(string id) { + foreach (var binding in bindings) { + if (binding.id == id) { + return binding.trigger.turbo; + } + } + + return false; + } + + ///////////////////////////////////////////////////////////////////// + /// Returns the name ID of the Pie bound to the given Trigger. + /// Returns "" if there is nothing bound to this trigger. + ///////////////////////////////////////////////////////////////////// + + public string get_assigned_id(Trigger trigger) { + foreach (var binding in bindings) { + var first = binding.trigger.name.replace("[turbo]", "").replace("[delayed]", ""); + var second = trigger.name.replace("[turbo]", "").replace("[delayed]", ""); + if (first == second) { + return binding.id; } } @@ -170,12 +202,10 @@ public class BindingManager : GLib.Object { } ///////////////////////////////////////////////////////////////////// - /// Event filter method needed to fetch X.Events + /// Event filter method needed to fetch X.Events. ///////////////////////////////////////////////////////////////////// - private Gdk.FilterReturn event_filter(Gdk.XEvent gdk_xevent, Gdk.Event gdk_event) { - Gdk.FilterReturn filter_return = Gdk.FilterReturn.CONTINUE; - + private Gdk.FilterReturn event_filter(Gdk.XEvent gdk_xevent, Gdk.Event gdk_event) { void* pointer = &gdk_xevent; X.Event* xevent = (X.Event*) pointer; @@ -183,13 +213,92 @@ public class BindingManager : GLib.Object { foreach(var binding in bindings) { // remove NumLock, CapsLock and ScrollLock from key state uint event_mods = xevent.xkey.state & ~ (lock_modifiers[7]); - if(xevent->xkey.keycode == binding.keycode && event_mods == binding.modifiers) { - on_press(binding.id); + if(xevent->xkey.keycode == binding.trigger.key_code && event_mods == binding.trigger.modifiers) { + if (binding.trigger.delayed) { + this.activate_delayed(binding, *xevent); + } else { + on_press(binding.id); + } } } } + else if(xevent->type == X.EventType.ButtonPress) { + foreach(var binding in bindings) { + // remove NumLock, CapsLock and ScrollLock from key state + uint event_mods = xevent.xbutton.state & ~ (lock_modifiers[7]); + if(xevent->xbutton.button == binding.trigger.key_code && event_mods == binding.trigger.modifiers) { + if (binding.trigger.delayed) { + this.activate_delayed(binding, *xevent); + } else { + on_press(binding.id); + } + } + } + } + else if(xevent->type == X.EventType.ButtonRelease || xevent->type == X.EventType.KeyRelease) { + this.activate_delayed(null, *xevent); + } - return filter_return; + return Gdk.FilterReturn.CONTINUE; + } + + ///////////////////////////////////////////////////////////////////// + /// This method is always called when a trigger is activated which is + /// delayed. Therefore on_press() is only emitted, when this method + /// is not called again within 300 milliseconds. Else a fake event is + /// sent in order to simulate the actual key which has been pressed. + ///////////////////////////////////////////////////////////////////// + + private void activate_delayed(Keybinding? binding , X.Event event) { + // increase event count, so any waiting event will realize that + // something happened in the meantime + var current_count = ++this.delayed_count; + + if (binding == null && this.delayed_event != null) { + // if the trigger is released and an event is currently waiting + // simulate that the trigger has been pressed without any inter- + // ference of Gnome-Pie + Gdk.Window rootwin = Gdk.get_default_root_window(); + X.Display display = Gdk.x11_drawable_get_xdisplay(rootwin); + + // unbind the trigger, else we'll capture that event again ;) + unbind(delayed_binding.id); + + if (this.delayed_binding.trigger.with_mouse) { + // simulate mouse click + X.Test.fake_button_event(display, this.delayed_event.xbutton.button, true, 0); + display.flush(); + + X.Test.fake_button_event(display, this.delayed_event.xbutton.button, false, 0); + display.flush(); + + } else { + // simulate key press + X.Test.fake_key_event(display, this.delayed_event.xkey.keycode, true, 0); + display.flush(); + + X.Test.fake_key_event(display, this.delayed_event.xkey.keycode, false, 0); + display.flush(); + } + + // bind it again + bind(delayed_binding.trigger, delayed_binding.id); + } else if (binding != null) { + // if the trigger has been pressed, store it and wait for any interuption + // within the next 300 milliseconds + this.delayed_event = event; + this.delayed_binding = binding; + + Timeout.add(300, () => { + // if nothing has been pressed in the meantime + if (current_count == this.delayed_count) { + this.delayed_binding = null; + this.delayed_event = null; + on_press(binding.id); + } + return false; + }); + } } } diff --git a/src/utilities/config.vala b/src/utilities/config.vala index c5dedd5..cf4311d 100644 --- a/src/utilities/config.vala +++ b/src/utilities/config.vala @@ -56,7 +56,6 @@ public class Config : GLib.Object { public double global_scale { get; set; default = 1.0; } public bool show_indicator { get; set; default = true; } public bool open_at_mouse { get; set; default = true; } - public bool turbo_mode { get; set; default = false; } public bool auto_start { get; set; default = false; } public Gee.ArrayList<Theme?> themes { get; private set; } @@ -73,7 +72,6 @@ public class Config : GLib.Object { writer.write_attribute("global_scale", global_scale.to_string()); writer.write_attribute("show_indicator", show_indicator ? "true" : "false"); writer.write_attribute("open_at_mouse", open_at_mouse ? "true" : "false"); - writer.write_attribute("turbo_mode", turbo_mode ? "true" : "false"); writer.end_element(); writer.end_document(); } @@ -119,9 +117,6 @@ public class Config : GLib.Object { case "open_at_mouse": open_at_mouse = bool.parse(attr_content); break; - case "turbo_mode": - turbo_mode = bool.parse(attr_content); - break; default: warning("Invalid setting \"" + attr_name + "\" in gnome-pie.conf!"); break; @@ -160,16 +155,17 @@ public class Config : GLib.Object { // load global themes var d = Dir.open(Paths.global_themes); while ((name = d.read_name()) != null) { - var theme = new Theme(Paths.global_themes + "/" + name); - if (theme != null) - themes.add(theme); + var theme = new Theme(Paths.global_themes + "/" + name); + + if (theme.load()) + themes.add(theme); } // load local themes d = Dir.open(Paths.local_themes); while ((name = d.read_name()) != null) { var theme = new Theme(Paths.local_themes + "/" + name); - if (theme != null) + if (theme.load()) themes.add(theme); } @@ -185,7 +181,6 @@ public class Config : GLib.Object { foreach (var t in themes) { if (t.name == current) { theme = t; - theme.load_images(); break; } } @@ -193,6 +188,7 @@ public class Config : GLib.Object { theme = themes[0]; warning("Theme \"" + current + "\" not found! Using fallback..."); } + theme.load_images(); } else error("No theme found!"); } diff --git a/src/utilities/focusGrabber.vala b/src/utilities/focusGrabber.vala new file mode 100644 index 0000000..0e07b39 --- /dev/null +++ b/src/utilities/focusGrabber.vala @@ -0,0 +1,74 @@ +/* +Copyright (c) 2011 by Simon Schneegans + +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/>. +*/ + +namespace GnomePie { + +///////////////////////////////////////////////////////////////////////// +/// Some helper methods which focus the input on a given Gtk.Window. +///////////////////////////////////////////////////////////////////////// + +public class FocusGrabber : GLib.Object { + + ///////////////////////////////////////////////////////////////////// + /// Utilities for grabbing focus. + /// Code from Gnome-Do/Synapse. + ///////////////////////////////////////////////////////////////////// + + public static void grab(Gtk.Window window) { + window.present_with_time(Gdk.CURRENT_TIME); + window.get_window().raise(); + window.get_window().focus(Gdk.CURRENT_TIME); + + int i = 0; + Timeout.add(100, () => { + if (++i >= 100) return false; + return !try_grab_window(window); + }); + } + + ///////////////////////////////////////////////////////////////////// + /// Code from Gnome-Do/Synapse. + ///////////////////////////////////////////////////////////////////// + + public static void ungrab(Gtk.Window window) { + Gdk.pointer_ungrab(Gdk.CURRENT_TIME); + Gdk.keyboard_ungrab(Gdk.CURRENT_TIME); + Gtk.grab_remove(window); + } + + ///////////////////////////////////////////////////////////////////// + /// Code from Gnome-Do/Synapse. + ///////////////////////////////////////////////////////////////////// + + private static bool try_grab_window(Gtk.Window window) { + if (Gdk.pointer_grab(window.get_window(), true, Gdk.EventMask.BUTTON_PRESS_MASK | + Gdk.EventMask.BUTTON_RELEASE_MASK | Gdk.EventMask.POINTER_MOTION_MASK, + null, null, Gdk.CURRENT_TIME) == Gdk.GrabStatus.SUCCESS) { + + if (Gdk.keyboard_grab(window.get_window(), true, Gdk.CURRENT_TIME) == Gdk.GrabStatus.SUCCESS) { + Gtk.grab_add(window); + return true; + } else { + Gdk.pointer_ungrab(Gdk.CURRENT_TIME); + return false; + } + } + return false; + } +} + +} diff --git a/src/utilities/icon.vala b/src/utilities/icon.vala deleted file mode 100644 index 1c8a9f4..0000000 --- a/src/utilities/icon.vala +++ /dev/null @@ -1,102 +0,0 @@ -/* -Copyright (c) 2011 by Simon Schneegans - -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/>. -*/ - -namespace GnomePie { - -///////////////////////////////////////////////////////////////////////// -/// A class representing a square-shaped icon, loaded from the users -/// icon theme. -///////////////////////////////////////////////////////////////////////// - -public class Icon : Image { - - ///////////////////////////////////////////////////////////////////// - /// A cache which stores loaded icon. It is cleared when the icon - /// theme of the user changes. The key is in form <filename>@<size>. - ///////////////////////////////////////////////////////////////////// - - private static Gee.HashMap<string, Cairo.ImageSurface?> cache { private get; private set; } - - ///////////////////////////////////////////////////////////////////// - /// Initializes the cache. - ///////////////////////////////////////////////////////////////////// - - public static void init() { - clear_cache(); - - Gtk.IconTheme.get_default().changed.connect(() => { - clear_cache(); - }); - } - - ///////////////////////////////////////////////////////////////////// - /// Clears the cache. - ///////////////////////////////////////////////////////////////////// - - public static void clear_cache() { - cache = new Gee.HashMap<string, Cairo.ImageSurface?>(); - } - - ///////////////////////////////////////////////////////////////////// - /// Loads an icon from the current icon theme of the user. - ///////////////////////////////////////////////////////////////////// - - public Icon(string icon_name, int size) { - var cached = this.cache.get("%s@%u".printf(icon_name, size)); - - if (cached == null) { - this.load_file_at_size(this.get_icon_file(icon_name, size), size, size); - this.cache.set("%s@%u".printf(icon_name, size), this.surface); - } else { - this.surface = cached; - } - } - - ///////////////////////////////////////////////////////////////////// - /// Returns the size of the icon in pixels. Greetings to Liskov. - ///////////////////////////////////////////////////////////////////// - - public int size() { - return base.width(); - } - - ///////////////////////////////////////////////////////////////////// - /// Returns the filename for a given system icon. - ///////////////////////////////////////////////////////////////////// - - public static string get_icon_file(string icon_name, int size) { - string result = ""; - - var icon_theme = Gtk.IconTheme.get_default(); - var file = icon_theme.lookup_icon(icon_name, size, 0); - if (file != null) result = file.get_filename(); - - if (result == "") { - warning("Icon \"" + icon_name + "\" not found! Using default icon..."); - icon_name = "application-default-icon"; - file = icon_theme.lookup_icon(icon_name, size, 0); - if (file != null) result = file.get_filename(); - } - - if (result == "") - warning("Icon \"" + icon_name + "\" not found! Will be ugly..."); - - return result; - } -} - -} diff --git a/src/utilities/image.vala b/src/utilities/image.vala deleted file mode 100644 index 836e4e2..0000000 --- a/src/utilities/image.vala +++ /dev/null @@ -1,163 +0,0 @@ -/* -Copyright (c) 2011 by Simon Schneegans - -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/>. -*/ - -namespace GnomePie { - -///////////////////////////////////////////////////////////////////////// -/// A class which loads image files. It can load image files in various -/// formats, including jpeg, png and svg. -///////////////////////////////////////////////////////////////////////// - -public class Image : GLib.Object { - - ///////////////////////////////////////////////////////////////////// - /// The internally used surface. - ///////////////////////////////////////////////////////////////////// - - public Cairo.ImageSurface surface { public get; protected set; default=null; } - - ///////////////////////////////////////////////////////////////////// - /// Creates an empty Image. - ///////////////////////////////////////////////////////////////////// - - public Image.empty(int width, int height, Color? color = null) { - this.surface = new Cairo.ImageSurface(Cairo.Format.ARGB32, width, height); - - if (color != null) { - var ctx = this.context(); - ctx.set_source_rgb(color.r, color.g, color.b); - ctx.paint(); - } - } - - ///////////////////////////////////////////////////////////////////// - /// Creates an image from the the given filename. - ///////////////////////////////////////////////////////////////////// - - public Image.from_file(string filename) { - this.load_file(filename); - } - - ///////////////////////////////////////////////////////////////////// - /// Creates an image from the the given filename at a given size. - ///////////////////////////////////////////////////////////////////// - - public Image.from_file_at_size(string filename, int width, int height) { - this.load_file_at_size(filename, width, height); - } - - ///////////////////////////////////////////////////////////////////// - /// Creates an image from the the given Gdk.Pixbuf. - ///////////////////////////////////////////////////////////////////// - - public Image.from_pixbuf(Gdk.Pixbuf pixbuf) { - this.load_pixbuf(pixbuf); - } - - public Image.capture_screen(int posx, int posy, int width, int height) { - Gdk.Window root = Gdk.get_default_root_window(); - Gdk.Pixbuf pixbuf = Gdk.pixbuf_get_from_drawable(null, root, null, posx, posy, 0, 0, width, height); - - this.load_pixbuf(pixbuf); - } - - ///////////////////////////////////////////////////////////////////// - /// Loads an image from the the given filename. - ///////////////////////////////////////////////////////////////////// - - public void load_file(string filename) { - try { - var pixbuf = new Gdk.Pixbuf.from_file(filename); - - if (pixbuf != null) { - this.load_pixbuf(pixbuf); - } else { - warning("Failed to load " + filename + "!"); - } - } catch (GLib.Error e) { - message("Error loading image file: %s", e.message); - this.surface = new Cairo.ImageSurface(Cairo.Format.ARGB32, 1, 1); - } - } - - ///////////////////////////////////////////////////////////////////// - /// Loads an image from the the given filename at a given size. - ///////////////////////////////////////////////////////////////////// - - public void load_file_at_size(string filename, int width, int height) { - try { - var pixbuf = new Gdk.Pixbuf.from_file_at_size(filename, width, height); - - if (pixbuf != null) { - this.load_pixbuf(pixbuf); - } else { - warning("Failed to load " + filename + "!"); - } - } catch (GLib.Error e) { - message("Error loading image file: %s", e.message); - this.surface = new Cairo.ImageSurface(Cairo.Format.ARGB32, width, height); - } - } - - ///////////////////////////////////////////////////////////////////// - /// Loads an image from the the given Gdk.Pixbuf. - ///////////////////////////////////////////////////////////////////// - - public void load_pixbuf(Gdk.Pixbuf pixbuf) { - this.surface = new Cairo.ImageSurface(Cairo.Format.ARGB32, pixbuf.width, pixbuf.height); - - var ctx = this.context(); - Gdk.cairo_set_source_pixbuf(ctx, pixbuf, 1.0, 1.0); - ctx.paint(); - } - - ///////////////////////////////////////////////////////////////////// - /// Paints the image onto the given Cairo.Context - ///////////////////////////////////////////////////////////////////// - - public void paint_on(Cairo.Context ctx, double alpha = 1.0) { - ctx.set_source_surface(this.surface, -0.5*this.width()-1, -0.5*this.height()-1); - if (alpha >= 1.0) ctx.paint(); - else ctx.paint_with_alpha(alpha); - } - - ///////////////////////////////////////////////////////////////////// - /// Returns a Cairo.Context for the Image. - ///////////////////////////////////////////////////////////////////// - - public Cairo.Context context() { - return new Cairo.Context(this.surface);; - } - - ///////////////////////////////////////////////////////////////////// - /// Returns the width of the image in pixels. - ///////////////////////////////////////////////////////////////////// - - public int width() { - return this.surface.get_width(); - } - - ///////////////////////////////////////////////////////////////////// - /// Returns the height of the image in pixels. - ///////////////////////////////////////////////////////////////////// - - public int height() { - return this.surface.get_height(); - } -} - -} diff --git a/src/utilities/renderedText.vala b/src/utilities/renderedText.vala deleted file mode 100644 index 924742a..0000000 --- a/src/utilities/renderedText.vala +++ /dev/null @@ -1,110 +0,0 @@ -/* -Copyright (c) 2011 by Simon Schneegans - -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/>. -*/ - -namespace GnomePie { - -///////////////////////////////////////////////////////////////////////// -/// A class representing string, rendered on an Image. -///////////////////////////////////////////////////////////////////////// - -public class RenderedText : Image { - - ///////////////////////////////////////////////////////////////////// - /// A cache which stores images. It is cleared when the theme of - /// Gnome-Pie changes. - /// The key is in form <string>@<width>x<height>:<font>. - ///////////////////////////////////////////////////////////////////// - - private static Gee.HashMap<string, Cairo.ImageSurface?> cache { private get; private set; } - - ///////////////////////////////////////////////////////////////////// - /// Initializes the cache. - ///////////////////////////////////////////////////////////////////// - - public static void init() { - clear_cache(); - - Config.global.notify["theme"].connect(() => { - clear_cache(); - }); - } - - ///////////////////////////////////////////////////////////////////// - /// Clears the cache. - ///////////////////////////////////////////////////////////////////// - - static void clear_cache() { - cache = new Gee.HashMap<string, Cairo.ImageSurface?>(); - } - - ///////////////////////////////////////////////////////////////////// - /// C'tor, creates a new image representation of a string. - ///////////////////////////////////////////////////////////////////// - - public RenderedText(string text, int width, int height, string font) { - var cached = this.cache.get("%s@%ux%u:%s".printf(text, width, height, font)); - - if (cached == null) { - this.render_text(text, width, height, font); - this.cache.set("%s@%ux%u:%s".printf(text, width, height, font), this.surface); - } else { - this.surface = cached; - } - } - - ///////////////////////////////////////////////////////////////////// - /// Creates a new transparent image, with text written onto. - ///////////////////////////////////////////////////////////////////// - - public void render_text(string text, int width, int height, string font) { - this.surface = new Cairo.ImageSurface(Cairo.Format.ARGB32, width, height); - - var ctx = this.context(); - - // set the color as specified in the current theme - Color color = Config.global.theme.caption_color; - ctx.set_source_rgb(color.r, color.g, color.g); - - var layout = Pango.cairo_create_layout(ctx); - layout.set_width(Pango.units_from_double(width)); - - var font_description = Pango.FontDescription.from_string(font); - font_description.set_size((int)(font_description.get_size() * Config.global.global_scale)); - - layout.set_font_description(font_description); - layout.set_text(text, -1); - - // add newlines at the end of each line, in order to allow ellipsizing - string broken_string = ""; - foreach (var line in layout.get_lines()) { - broken_string = broken_string.concat(text.substring(line.start_index, line.length), "\n"); - } - layout.set_text(broken_string, broken_string.length-1); - - layout.set_ellipsize(Pango.EllipsizeMode.END); - layout.set_alignment(Pango.Alignment.CENTER); - - Pango.Rectangle extents; - layout.get_pixel_extents(null, out extents); - ctx.move_to(0, (int)(0.5*(height - extents.height))); - - Pango.cairo_update_layout(ctx, layout); - Pango.cairo_show_layout(ctx, layout); - } -} - -} diff --git a/src/utilities/themedIcon.vala b/src/utilities/themedIcon.vala deleted file mode 100644 index 29ae380..0000000 --- a/src/utilities/themedIcon.vala +++ /dev/null @@ -1,161 +0,0 @@ -/* -Copyright (c) 2011 by Simon Schneegans - -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/>. -*/ - -namespace GnomePie { - -///////////////////////////////////////////////////////////////////////// -/// A class representing a square-shaped icon, themed according to the -/// current theme of Gnome-Pie. -///////////////////////////////////////////////////////////////////////// - -public class ThemedIcon : Image { - - ///////////////////////////////////////////////////////////////////// - /// A cache which stores loaded icon. The key is the icon name. When - /// the users icon theme or the theme of Gnome-Pie changes, these - /// cahces are cleared. - ///////////////////////////////////////////////////////////////////// - - private static Gee.HashMap<string, Cairo.ImageSurface?> active_cache { private get; private set; } - private static Gee.HashMap<string, Cairo.ImageSurface?> inactive_cache { private get; private set; } - - ///////////////////////////////////////////////////////////////////// - /// Initializes the caches. - ///////////////////////////////////////////////////////////////////// - - public static void init() { - clear_cache(); - - Config.global.notify["theme"].connect(() => { - clear_cache(); - }); - - Gtk.IconTheme.get_default().changed.connect(() => { - clear_cache(); - }); - } - - ///////////////////////////////////////////////////////////////////// - /// Clears the cache. - ///////////////////////////////////////////////////////////////////// - - public static void clear_cache() { - active_cache = new Gee.HashMap<string, Cairo.ImageSurface?>(); - inactive_cache = new Gee.HashMap<string, Cairo.ImageSurface?>(); - } - - ///////////////////////////////////////////////////////////////////// - /// Paint a slice icon according to the current theme. - ///////////////////////////////////////////////////////////////////// - - public ThemedIcon(string icon_name, bool active) { - // check cache - var current_cache = active ? active_cache : inactive_cache; - var cached = current_cache.get(icon_name); - - if (cached != null) { - this.surface = cached; - return; - } - - // get layers for the desired slice type - var layers = active ? Config.global.theme.active_slice_layers : Config.global.theme.inactive_slice_layers; - - // get max size - int size = 0; - foreach (var layer in layers) { - if (layer.image.width() > size) size = layer.image.width(); - } - - this.surface = new Cairo.ImageSurface(Cairo.Format.ARGB32, size, size); - - // get size of icon layer - int icon_size = size; - foreach (var layer in layers) { - if (layer.is_icon) icon_size = layer.image.width(); - } - - Image icon; - if (icon_name.contains("/")) - icon = new Image.from_file_at_size(icon_name, icon_size, icon_size); - else - icon = new Icon(icon_name, icon_size); - - var color = new Color.from_icon(icon); - var ctx = this.context(); - - ctx.translate(size/2, size/2); - ctx.set_operator(Cairo.Operator.OVER); - - // now render all layers on top of each other - foreach (var layer in layers) { - - if (layer.colorize) { - ctx.push_group(); - } - - if (layer.is_icon) { - - ctx.push_group(); - - layer.image.paint_on(ctx); - - ctx.set_operator(Cairo.Operator.IN); - - if (layer.image.width() != icon_size) { - if (icon_name.contains("/")) - icon = new Image.from_file_at_size(icon_name, layer.image.width(), layer.image.width()); - else - icon = new Icon(icon_name,layer.image.width()); - } - - icon.paint_on(ctx); - - ctx.pop_group_to_source(); - ctx.paint(); - ctx.set_operator(Cairo.Operator.OVER); - - } else { - layer.image.paint_on(ctx); - } - - // colorize the whole layer if neccasary - if (layer.colorize) { - ctx.set_operator(Cairo.Operator.ATOP); - ctx.set_source_rgb(color.r, color.g, color.b); - ctx.paint(); - - ctx.set_operator(Cairo.Operator.OVER); - ctx.pop_group_to_source(); - ctx.paint(); - } - } - - // store the surface in cache - current_cache.set(icon_name, this.surface); - } - - ///////////////////////////////////////////////////////////////////// - /// Returns the size of the icon in pixels. Greetings to Liskov. - ///////////////////////////////////////////////////////////////////// - - public int size() { - return base.width(); - } -} - -} diff --git a/src/utilities/trigger.vala b/src/utilities/trigger.vala new file mode 100644 index 0000000..1f6fcfe --- /dev/null +++ b/src/utilities/trigger.vala @@ -0,0 +1,255 @@ +/* +Copyright (c) 2011 by Simon Schneegans + +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/>. +*/ + +namespace GnomePie { + +///////////////////////////////////////////////////////////////////////// +/// This class represents a hotkey, used to open pies. It supports any +/// combination of modifier keys with keyboard and mouse buttons. +///////////////////////////////////////////////////////////////////////// + +public class Trigger : GLib.Object { + + ///////////////////////////////////////////////////////////////////// + /// Returns a human-readable version of this Trigger. + ///////////////////////////////////////////////////////////////////// + + public string label { get; private set; default=""; } + + ///////////////////////////////////////////////////////////////////// + /// Returns a human-readable version of this Trigger. Small + /// identifiers for turbo mode and delayed mode are added. + ///////////////////////////////////////////////////////////////////// + + public string label_with_specials { get; private set; default=""; } + + ///////////////////////////////////////////////////////////////////// + /// The Trigger string. Like [delayed]<Control>button3 + ///////////////////////////////////////////////////////////////////// + + public string name { get; private set; default=""; } + + ///////////////////////////////////////////////////////////////////// + /// The key code of the hotkey or the button number of the mouse. + ///////////////////////////////////////////////////////////////////// + + public int key_code { get; private set; default=0; } + + ///////////////////////////////////////////////////////////////////// + /// The keysym of the hotkey or the button number of the mouse. + ///////////////////////////////////////////////////////////////////// + + public uint key_sym { get; private set; default=0; } + + ///////////////////////////////////////////////////////////////////// + /// Modifier keys pressed for this hotkey. + ///////////////////////////////////////////////////////////////////// + + public Gdk.ModifierType modifiers { get; private set; default=0; } + + ///////////////////////////////////////////////////////////////////// + /// True if this hotkey involves the mouse. + ///////////////////////////////////////////////////////////////////// + + public bool with_mouse { get; private set; default=false; } + + ///////////////////////////////////////////////////////////////////// + /// True if the pie closes when the trigger hotkey is released. + ///////////////////////////////////////////////////////////////////// + + public bool turbo { get; private set; default=false; } + + ///////////////////////////////////////////////////////////////////// + /// True if the trigger should wait a short delay before being + /// triggered. + ///////////////////////////////////////////////////////////////////// + + public bool delayed { get; private set; default=false; } + + ///////////////////////////////////////////////////////////////////// + /// C'tor, creates a new, "unbound" Trigger. + ///////////////////////////////////////////////////////////////////// + + public Trigger() { + this.set_unbound(); + } + + ///////////////////////////////////////////////////////////////////// + /// C'tor, creates a new Trigger from a given Trigger string. This is + /// in this format: "[option(s)]<modifier(s)>button" where + /// "<modifier>" is something like "<Alt>" or "<Control>", "button" + /// something like "s", "F4" or "button0" and "[option]" is either + /// "[turbo]" or "["delayed"]". + ///////////////////////////////////////////////////////////////////// + + public Trigger.from_string(string trigger) { + this.parse_string(trigger); + } + + ///////////////////////////////////////////////////////////////////// + /// C'tor, creates a new Trigger from the key values. + ///////////////////////////////////////////////////////////////////// + + public Trigger.from_values(uint key_sym, Gdk.ModifierType modifiers, + bool with_mouse, bool turbo, bool delayed ) { + + string trigger = (turbo ? "[turbo]" : "") + (delayed ? "[delayed]" : ""); + + if (with_mouse) { + trigger += Gtk.accelerator_name(0, modifiers) + "button%u".printf(key_sym); + } else { + trigger += Gtk.accelerator_name(key_sym, modifiers); + } + + this.parse_string(trigger); + } + + ///////////////////////////////////////////////////////////////////// + /// Parses a Trigger string. This is + /// in this format: "[option(s)]<modifier(s)>button" where + /// "<modifier>" is something like "<Alt>" or "<Control>", "button" + /// something like "s", "F4" or "button0" and "[option]" is either + /// "[turbo]" or "["delayed"]". + ///////////////////////////////////////////////////////////////////// + + public void parse_string(string trigger) { + if (this.is_valid(trigger)) { + // copy string + string check_string = trigger; + + this.name = check_string; + + this.turbo = check_string.contains("[turbo]"); + this.delayed = check_string.contains("[delayed]"); + + // remove optional arguments + check_string = check_string.replace("[turbo]", ""); + check_string = check_string.replace("[delayed]", ""); + + int button = this.get_mouse_button(check_string); + if (button > 0) { + this.with_mouse = true; + this.key_code = button; + this.key_sym = button; + + Gtk.accelerator_parse(check_string, null, out this._modifiers); + this.label = Gtk.accelerator_get_label(0, this.modifiers); + + string button_text = _("Button %i").printf(this.key_code); + + if (this.key_code == 1) + button_text = _("LeftButton"); + else if (this.key_code == 3) + button_text = _("RightButton"); + else if (this.key_code == 2) + button_text = _("MiddleButton"); + + this.label += button_text; + } else { + this.with_mouse = false; + + var display = new X.Display(); + + uint keysym = 0; + Gtk.accelerator_parse(check_string, out keysym, out this._modifiers); + this.key_code = display.keysym_to_keycode(keysym); + this.key_sym = keysym; + this.label = Gtk.accelerator_get_label(keysym, this.modifiers); + } + + this.label = this.label.replace("<", "<"); + this.label = this.label.replace(">", ">"); + this.label = this.label.replace("&", "&"); + + this.label_with_specials = this.label; + + if (this.turbo && this.delayed) + this.label_with_specials += ("\n<small><span weight='light'>" + _("Turbo") + " | " + _("Delayed") + "</span></small>"); + else if (this.turbo) + this.label_with_specials += ("\n<small><span weight='light'>" + _("Turbo") + "</span></small>"); + else if (this.delayed) + this.label_with_specials += ("\n<small><span weight='light'>" + _("Delayed") + "</span></small>"); + + } else { + this.set_unbound(); + } + } + + ///////////////////////////////////////////////////////////////////// + /// Resets all member variables to their defaults. + ///////////////////////////////////////////////////////////////////// + + private void set_unbound() { + this.label = _("Not bound"); + this.label_with_specials = _("Not bound"); + this.name = ""; + this.key_code = 0; + this.key_sym = 0; + this.modifiers = 0; + this.turbo = false; + this.delayed = false; + this.with_mouse = false; + } + + ///////////////////////////////////////////////////////////////////// + /// Returns true, if the trigger string is in a valid format. + ///////////////////////////////////////////////////////////////////// + + private bool is_valid(string trigger) { + // copy string + string check_string = trigger; + + // remove optional arguments + check_string = check_string.replace("[turbo]", ""); + check_string = check_string.replace("[delayed]", ""); + + if (this.get_mouse_button(check_string) > 0) { + // it seems to be a valid mouse-trigger so replace button part, + // with something accepted by gtk, and check it with gtk + int button_index = check_string.index_of("button"); + check_string = check_string.slice(0, button_index) + "a"; + } + + // now it shouls be a normal gtk accelerator + uint keysym = 0; + Gdk.ModifierType modifiers = 0; + Gtk.accelerator_parse(check_string, out keysym, out modifiers); + if (keysym == 0) + return false; + + return true; + } + + ///////////////////////////////////////////////////////////////////// + /// Returns the mouse button number of the given trigger string. + /// Returns -1 if it is not a mouse trigger. + ///////////////////////////////////////////////////////////////////// + + private int get_mouse_button(string trigger) { + if (trigger.contains("button")) { + // it seems to be a mouse-trigger so check the button part. + int button_index = trigger.index_of("button"); + int number = int.parse(trigger.slice(button_index + 6, trigger.length)); + if (number > 0) + return number; + } + + return -1; + } +} + +} |