diff options
Diffstat (limited to 'src/images')
-rw-r--r-- | src/images/icon.vala | 18 | ||||
-rw-r--r-- | src/images/renderedText.vala | 81 | ||||
-rw-r--r-- | src/images/themedIcon.vala | 129 |
3 files changed, 116 insertions, 112 deletions
diff --git a/src/images/icon.vala b/src/images/icon.vala index 81eb2d9..e942e7c 100644 --- a/src/images/icon.vala +++ b/src/images/icon.vala @@ -75,6 +75,24 @@ public class Icon : Image { } ///////////////////////////////////////////////////////////////////// + /// Returns the icon name for a given GLib.Icon. + ///////////////////////////////////////////////////////////////////// + + public static string get_icon_name(GLib.Icon? icon) { + if (icon != null) { + var icon_names = icon.to_string().split(" "); + + foreach (var icon_name in icon_names) { + if (Gtk.IconTheme.get_default().has_icon(icon_name)) { + return icon_name; + } + } + } + + return ""; + } + + ///////////////////////////////////////////////////////////////////// /// Returns the filename for a given system icon. ///////////////////////////////////////////////////////////////////// diff --git a/src/images/renderedText.vala b/src/images/renderedText.vala index 41146d6..e99d26a 100644 --- a/src/images/renderedText.vala +++ b/src/images/renderedText.vala @@ -50,39 +50,60 @@ public class RenderedText : Image { public void render_text(string text, int width, int height, string font, Color color, double scale) { - + this.surface = new Cairo.ImageSurface(Cairo.Format.ARGB32, width, height); - var ctx = this.context(); - - // set the 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() * 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"); + if (text != "") { + + var ctx = this.context(); + + // set the 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() * 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 = ""; + var lines = layout.get_lines().copy(); + + foreach (var line in lines) { + + string next_line = text.substring(line.start_index, line.length); + + if (broken_string == "") { + broken_string = next_line; + } else if (next_line != "") { + // test whether the addition of a line would cause the height to become too large + string broken_string_tmp = broken_string + "\n" + next_line; + + layout.set_text(broken_string_tmp, -1); + Pango.Rectangle extents; + layout.get_pixel_extents(null, out extents); + + if (extents.height > height) broken_string = broken_string + next_line; + else broken_string = broken_string_tmp; + } + } + + layout.set_text(broken_string, -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); } - 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/images/themedIcon.vala b/src/images/themedIcon.vala index 6c904a6..f816e0f 100644 --- a/src/images/themedIcon.vala +++ b/src/images/themedIcon.vala @@ -23,54 +23,12 @@ namespace GnomePie { ///////////////////////////////////////////////////////////////////////// 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; - } + public ThemedIcon(string caption, string icon_name, bool active) { // get layers for the desired slice type var layers = active ? Config.global.theme.active_slice_layers : Config.global.theme.inactive_slice_layers; @@ -78,7 +36,8 @@ public class ThemedIcon : Image { // get max size int size = 1; foreach (var layer in layers) { - if (layer.image.width() > size) size = layer.image.width(); + if (layer.image != null && layer.image.width() > size) + size = layer.image.width(); } this.surface = new Cairo.ImageSurface(Cairo.Format.ARGB32, size, size); @@ -86,7 +45,8 @@ public class ThemedIcon : Image { // get size of icon layer int icon_size = size; foreach (var layer in layers) { - if (layer.is_icon) icon_size = layer.image.width(); + if (layer.image != null && layer.layer_type == SliceLayer.Type.ICON) + icon_size = layer.image.width(); } Image icon; @@ -104,49 +64,54 @@ public class ThemedIcon : Image { // now render all layers on top of each other foreach (var layer in layers) { - if (layer.colorize) { - ctx.push_group(); - } - - if (layer.is_icon) { + if (layer.visibility == SliceLayer.Visibility.ANY || + (Config.global.show_captions == (layer.visibility == SliceLayer.Visibility.WITH_CAPTION))) { - 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()); + if (layer.colorize) { + ctx.push_group(); } - - icon.paint_on(ctx); + + if (layer.layer_type == SliceLayer.Type.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.pop_group_to_source(); + ctx.paint(); + ctx.set_operator(Cairo.Operator.OVER); + + } else if (layer.layer_type == SliceLayer.Type.CAPTION) { + Image text = new RenderedText(caption, layer.width, layer.height, layer.font, layer.color, Config.global.global_scale); + ctx.translate(0, layer.position); + text.paint_on(ctx); + ctx.translate(0, -layer.position); + } else if (layer.layer_type == SliceLayer.Type.FILE) { + layer.image.paint_on(ctx); + } - ctx.set_operator(Cairo.Operator.OVER); - ctx.pop_group_to_source(); - ctx.paint(); + // 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); } ///////////////////////////////////////////////////////////////////// |