The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

Theme.vala in libbirdfont

This file is a part of the Birdfont project.

Contributing

Send patches or pull requests to johan.mattsson.m@gmail.com.
Clone this repository: git clone https://github.com/johanmattssonm/birdfont.git

Revisions

View the latest version of libbirdfont/Theme.vala.
Store order for theme colors in setting tab
1 /* 2 Copyright (C) 2015 Johan Mattsson 3 4 This library is free software; you can redistribute it and/or modify 5 it under the terms of the GNU Lesser General Public License as 6 published by the Free Software Foundation; either version 3 of the 7 License, or (at your option) any later version. 8 9 This library is distributed in the hope that it will be useful, but 10 WITHOUT ANY WARRANTY; without even the implied warranty of 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 Lesser General Public License for more details. 13 */ 14 15 using Bird; 16 using Cairo; 17 18 namespace BirdFont { 19 20 public class Theme : GLib.Object { 21 22 static Gee.HashMap<string, Color> colors; 23 public static Gee.ArrayList<string> color_list; 24 25 public static void text_color (Text text, string name) { 26 Color c; 27 28 if (unlikely (!colors.has_key (name))) { 29 warning (@"Theme does not have a color for $name"); 30 return; 31 } 32 33 c = colors.get (name); 34 text.set_source_rgba (c.r, c.g, c.b, c.a); 35 } 36 37 public static void color (Context cr, string name) { 38 Color c; 39 40 if (unlikely (!colors.has_key (name))) { 41 warning (@"Theme does not have a color for $name"); 42 return; 43 } 44 45 c = colors.get (name); 46 cr.set_source_rgba (c.r, c.g, c.b, c.a); 47 } 48 49 public static void color_opacity (Context cr, string name, double opacity) { 50 Color c; 51 52 if (unlikely (!colors.has_key (name))) { 53 warning (@"Theme does not have a color for $name"); 54 return; 55 } 56 57 c = colors.get (name); 58 cr.set_source_rgba (c.r, c.g, c.b, opacity); 59 } 60 61 public static void text_color_opacity (Text text, string name, double opacity) { 62 Color c; 63 64 if (unlikely (!colors.has_key (name))) { 65 warning (@"Theme does not have a color for $name"); 66 return; 67 } 68 69 c = colors.get (name); 70 text.set_source_rgba (c.r, c.g, c.b, opacity); 71 } 72 public static Color get_color (string name) { 73 Color c; 74 75 if (unlikely (!colors.has_key (name))) { 76 warning (@"Theme does not have a color for $name"); 77 return new Color (0, 0, 0, 1); 78 } 79 80 return colors.get (name); 81 } 82 83 public static void set_default_colors () { 84 color_list = new Gee.ArrayList<string> (); 85 colors = new Gee.HashMap<string, Color> (); 86 87 Theme.set_default_color ("Background 1", 1, 1, 1, 1); 88 Theme.set_default_color ("Background 2", 101 / 255.0, 108 / 255.0, 116 / 255.0, 1); 89 Theme.set_default_color ("Background 3", 38 / 255.0, 39 / 255.0, 43 / 255.0, 1); 90 Theme.set_default_color ("Background 4", 51 / 255.0, 54 / 255.0, 59 / 255.0, 1); 91 Theme.set_default_color ("Background 5", 0.3, 0.3, 0.3, 1); 92 Theme.set_default_color ("Background 6", 224/255.0, 224/255.0, 224/255.0, 1); 93 Theme.set_default_color ("Background 7", 56 / 255.0, 59 / 255.0, 65 / 255.0, 1); 94 Theme.set_default_color ("Background 8", 55/255.0, 55/255.0, 55/255.0, 1); 95 Theme.set_default_color ("Background 9", 72/255.0, 72/255.0, 72/255.0, 1); 96 97 Theme.set_default_color ("Foreground 1", 0, 0, 0, 1); 98 Theme.set_default_color ("Foreground 2", 101 / 255.0, 108 / 255.0, 116 / 255.0, 1); 99 Theme.set_default_color ("Foreground 3", 26 / 255.0, 30 / 255.0, 32 / 255.0, 1); 100 Theme.set_default_color ("Foreground 4", 40 / 255.0, 57 / 255.0, 65 / 255.0, 1); 101 Theme.set_default_color ("Foreground 5", 70 / 255.0, 77 / 255.0, 83 / 255.0, 1); 102 103 Theme.set_default_color ("Highlighted 1", 234 / 255.0, 77 / 255.0, 26 / 255.0, 1); 104 105 Theme.set_default_color ("Highlighted Guide", 0, 0, 0.3, 1); 106 Theme.set_default_color ("Guide 1", 0.7, 0.7, 0.8, 1); 107 Theme.set_default_color ("Guide 2", 0.7, 0, 0, 0.5); 108 Theme.set_default_color ("Guide 3", 120 / 255.0, 68 / 255.0, 120 / 255.0, 120 / 255.0); 109 110 Theme.set_default_color ("Grid",0.2, 0.6, 0.2, 0.2); 111 112 Theme.set_default_color ("Background Glyph", 0.2, 0.2, 0.2, 0.5); 113 114 Theme.set_default_color ("Tool Border 1", 38 / 255.0, 39 / 255.0, 43 / 255.0, 1); 115 Theme.set_default_color ("Tool Background 1", 14 / 255.0, 16 / 255.0, 17 / 255.0, 1); 116 117 Theme.set_default_color ("Tool Border 2", 38 / 255.0, 39 / 255.0, 43 / 255.0, 1); 118 Theme.set_default_color ("Tool Background 2", 26 / 255.0, 30 / 255.0, 32 / 255.0, 1); 119 120 Theme.set_default_color ("Tool Border 3", 38 / 255.0, 39 / 255.0, 43 / 255.0, 1); 121 Theme.set_default_color ("Tool Background 3", 44 / 255.0, 47 / 255.0, 51 / 255.0, 1); 122 123 Theme.set_default_color ("Tool Border 4", 38 / 255.0, 39 / 255.0, 43 / 255.0, 1); 124 Theme.set_default_color ("Tool Background 4", 33 / 255.0, 36 / 255.0, 39 / 255.0, 1); 125 126 Theme.set_default_color ("Button Foreground", 101 / 255.0, 108 / 255.0, 116 / 255.0, 1); 127 128 N_("Background 1"); 129 N_("Background 2"); 130 N_("Background 3"); 131 N_("Background 4"); 132 N_("Background 5"); 133 N_("Background 6"); 134 N_("Background 7"); 135 N_("Background 8"); 136 N_("Background 9"); 137 138 N_("Foreground 1"); 139 N_("Foreground 2"); 140 N_("Foreground 3"); 141 N_("Foreground 4"); 142 N_("Foreground 5"); 143 144 N_("Highlighted 1"); 145 N_("Highlighted Guide"); 146 147 N_("Grid"); 148 149 N_("Guide 1"); 150 N_("Guide 2"); 151 N_("Guide 3"); 152 153 N_("Tool Border 1"); 154 N_("Tool Background 1"); 155 N_("Tool Border 2"); 156 N_("Tool Background 2"); 157 N_("Tool Border 3"); 158 N_("Tool Background 3"); 159 N_("Tool Border 4"); 160 N_("Tool Background 4"); 161 162 N_("Button Foreground"); 163 } 164 165 public static void set_default_color (string name, double r, double g, double b, double a) { 166 color_list.add (name); 167 colors.set (name, new Color (r, g, b, a)); 168 write_theme (); // FIXME: don't overwrite color 169 } 170 171 public static void save_color (string name, double r, double g, double b, double a) { 172 colors.set (name, new Color (r, g, b, a)); 173 write_theme (); 174 } 175 176 public static void load_theme () { 177 File default_theme = SearchPaths.find_file (null, "theme.xml"); 178 File user_theme = get_child (BirdFont.get_settings_directory (), "theme.xml"); 179 180 if (default_theme.query_exists ()) { 181 parse_theme (default_theme); 182 } 183 184 if (user_theme.query_exists ()) { 185 parse_theme (user_theme); 186 } 187 } 188 189 public static void write_theme () { 190 DataOutputStream os; 191 File file; 192 193 file = get_child (BirdFont.get_settings_directory (), "theme.xml"); 194 195 try { 196 if (file.query_exists ()) { 197 file.delete (); 198 } 199 } catch (GLib.Error e) { 200 warning (e.message); 201 } 202 203 try { 204 os = new DataOutputStream (file.create (FileCreateFlags.REPLACE_DESTINATION)); 205 os.put_string ("""<?xml version="1.0" encoding="utf-8" standalone="yes"?>"""); 206 os.put_string ("\n"); 207 208 os.put_string ("<theme>\n"); 209 foreach (string name in colors.keys) { 210 Color color = colors.get (name); 211 212 os.put_string ("\t<color "); 213 214 os.put_string (@"name=\"$(Markup.escape_text (name))\" "); 215 os.put_string (@"red=\"$(color.r)\" "); 216 os.put_string (@"green=\"$(color.g)\" "); 217 os.put_string (@"blue=\"$(color.b)\" "); 218 os.put_string (@"alpha=\"$(color.a)\""); 219 220 os.put_string ("/>\n"); 221 } 222 os.put_string ("</theme>\n"); 223 224 os.close (); 225 } catch (GLib.Error e) { 226 warning (e.message); 227 } 228 } 229 230 static void parse_theme (File f) { 231 string xml_data; 232 XmlParser parser; 233 234 try { 235 FileUtils.get_contents((!) f.get_path (), out xml_data); 236 parser = new XmlParser (xml_data); 237 parse_colors (parser.get_root_tag ()); 238 } catch (GLib.Error e) { 239 warning (e.message); 240 } 241 } 242 243 static void parse_colors (Tag tag) { 244 foreach (Tag t in tag) { 245 if (t.get_name () == "color") { 246 parse_color (t.get_attributes ()); 247 } 248 } 249 } 250 251 static void parse_color (Attributes attributes) { 252 string name = ""; 253 double r = 0; 254 double g = 0; 255 double b = 0; 256 double a = 1; 257 258 foreach (Attribute attr in attributes) { 259 if (attr.get_name () == "name") { 260 name = attr.get_content (); 261 } 262 263 if (attr.get_name () == "red") { 264 r = double.parse (attr.get_content ()); 265 } 266 267 if (attr.get_name () == "green") { 268 g = double.parse (attr.get_content ()); 269 } 270 271 if (attr.get_name () == "blue") { 272 b = double.parse (attr.get_content ()); 273 } 274 275 if (attr.get_name () == "alpha") { 276 a = double.parse (attr.get_content ()); 277 } 278 } 279 280 colors.set (name, new Color (r, g, b, a)); 281 } 282 } 283 284 } 285