The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

DescriptionDisplay.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/DescriptionDisplay.vala.
Switch keyboard focus with tab key in name and description tab
1 /* 2 Copyright (C) 2014 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 Cairo; 16 using Math; 17 18 namespace BirdFont { 19 20 public class DescriptionDisplay : FontDisplay { 21 22 double scroll = 0; 23 double content_height = 1; 24 WidgetAllocation allocation; 25 26 TextArea? keyboard_focus = null; 27 28 TextArea postscript_name; 29 TextArea name; 30 TextArea style; 31 CheckBox bold; 32 CheckBox italic; 33 TextArea weight; 34 TextArea full_name; 35 TextArea unique_id; 36 TextArea version; 37 TextArea description; 38 TextArea copyright; 39 40 Gee.ArrayList<Widget> widgets; 41 42 private static bool disable_copyright = false; 43 44 public DescriptionDisplay () { 45 double margin = 12 * MainWindow.units; 46 double label_size = 20 * MainWindow.units; 47 double label_margin = 4 * MainWindow.units; 48 Headline headline; 49 Font font = BirdFont.get_current_font (); 50 51 allocation = new WidgetAllocation (); 52 53 widgets = new Gee.ArrayList<Widget> (); 54 55 postscript_name = new LineTextArea (label_size); 56 name = new LineTextArea (label_size); 57 style = new LineTextArea (label_size); 58 weight = new LineTextArea (label_size); 59 full_name = new LineTextArea (label_size); 60 unique_id = new LineTextArea (label_size); 61 version = new LineTextArea (label_size); 62 description = new TextArea (label_size); 63 copyright = new TextArea (label_size); 64 65 headline = new Headline (t_("Name and Description")); 66 headline.margin_bottom = 20 * MainWindow.units; 67 widgets.add (headline); 68 69 widgets.add (new Text (t_("PostScript Name"), label_size, label_margin)); 70 postscript_name.margin_bottom = margin; 71 postscript_name.set_text (font.postscript_name); 72 postscript_name.text_changed.connect ((t) => { 73 font.postscript_name = t; 74 font.touch (); 75 }); 76 widgets.add (postscript_name); 77 78 widgets.add (new Text (t_("Name"), label_size, label_margin)); 79 name.margin_bottom = margin; 80 name.set_text (font.name); 81 name.text_changed.connect ((t) => { 82 font.name = t; 83 font.touch (); 84 }); 85 widgets.add (name); 86 87 widgets.add (new Text (t_("Style"), label_size, label_margin)); 88 style.margin_bottom = 1.5 * margin; 89 style.set_text (font.subfamily); 90 style.text_changed.connect ((t) => { 91 font.subfamily = t; 92 font.touch (); 93 }); 94 widgets.add (style); 95 96 bold = new CheckBox (t_("Bold"), label_size); 97 bold.updated.connect ((c) => { 98 font.bold = c; 99 font.touch (); 100 }); 101 bold.checked = font.bold; 102 widgets.add (bold); 103 104 italic = new CheckBox (t_("Italic"), label_size); 105 italic.updated.connect ((c) => { 106 font.italic = c; 107 font.touch (); 108 }); 109 italic.checked = font.italic; 110 italic.margin_bottom = margin; 111 widgets.add (italic); 112 113 widgets.add (new Text (t_("Weight"), label_size, label_margin)); 114 weight.margin_bottom = margin; 115 weight.set_text (font.get_weight ()); 116 weight.text_changed.connect ((t) => { 117 font.set_weight (t); 118 font.touch (); 119 }); 120 widgets.add (weight); 121 122 widgets.add (new Text (t_("Full Name (Name and Style)"), label_size, label_margin)); 123 full_name.margin_bottom = margin; 124 full_name.set_text (font.full_name); 125 full_name.text_changed.connect ((t) => { 126 font.full_name = t; 127 font.touch (); 128 }); 129 widgets.add (full_name); 130 131 widgets.add (new Text (t_("Unique Identifier"), label_size, label_margin)); 132 unique_id.margin_bottom = margin; 133 unique_id.set_text (font.unique_identifier); 134 unique_id.text_changed.connect ((t) => { 135 font.unique_identifier = t; 136 font.touch (); 137 }); 138 widgets.add (unique_id); 139 140 widgets.add (new Text (t_("Version"), label_size, label_margin)); 141 version.margin_bottom = margin; 142 version.set_text (font.version); 143 version.text_changed.connect ((t) => { 144 font.version = t; 145 font.touch (); 146 }); 147 widgets.add (version); 148 149 widgets.add (new Text (t_("Description"), label_size, label_margin)); 150 description.margin_bottom = margin; 151 description.set_text (font.description); 152 description.scroll.connect (scroll_event); 153 description.text_changed.connect ((t) => { 154 font.description = t; 155 font.touch (); 156 }); 157 widgets.add (description); 158 159 widgets.add (new Text (t_("Copyright"), label_size, label_margin)); 160 copyright.margin_bottom = margin; 161 copyright.set_text (font.copyright); 162 copyright.scroll.connect (scroll_event); 163 copyright.text_changed.connect ((t) => { 164 font.copyright = t; 165 font.touch (); 166 }); 167 copyright.set_editable (!disable_copyright); 168 widgets.add (copyright); 169 } 170 171 public static void set_copyright_editable (bool t) { 172 disable_copyright = !t; 173 } 174 175 public override void draw (WidgetAllocation allocation, Context cr) { 176 this.allocation = allocation; 177 178 layout (); 179 180 // background 181 cr.save (); 182 cr.rectangle (0, 0, allocation.width, allocation.height); 183 cr.set_line_width (0); 184 185 Theme.color (cr, "Background 4"); 186 187 cr.fill (); 188 cr.stroke (); 189 cr.restore (); 190 191 foreach (Widget w in widgets) { 192 if (w.is_on_screen ()) { 193 if (w is Text) { 194 cr.save (); 195 Theme.color (cr, "Foreground 1"); 196 w.draw (cr); 197 cr.restore (); 198 } else { 199 w.draw (cr); 200 } 201 } 202 } 203 } 204 205 void layout () { 206 double y = -scroll; 207 208 foreach (Widget w in widgets) { 209 w.widget_x = 17 * MainWindow.units; 210 w.widget_y = y; 211 w.allocation = allocation; 212 213 if (w is TextArea) { 214 ((TextArea) w).layout (); 215 } 216 217 y += w.get_height () + w.margin_bottom; 218 } 219 220 content_height = y + scroll; 221 update_scrollbar (); 222 } 223 224 public void scroll_event (double p) { 225 scroll += p; 226 layout (); 227 GlyphCanvas.redraw (); 228 } 229 230 public override void key_press (uint keyval) { 231 TextArea focus; 232 233 if (keyboard_focus == null) { 234 return; 235 } 236 237 focus = (!) keyboard_focus; 238 focus.key_press (keyval); 239 } 240 241 public override void button_press (uint button, double x, double y) { 242 TextArea t; 243 TextArea old; 244 CheckBox c; 245 246 foreach (Widget w in widgets) { 247 if (w.is_over (x, y)) { 248 if (w is TextArea) { 249 t = (TextArea) w; 250 if (keyboard_focus != null && (!) keyboard_focus != t) { 251 old = (!) keyboard_focus; 252 old.draw_carret = false; 253 } 254 255 t.draw_carret = true; 256 t.button_press (button, x, y); 257 keyboard_focus = t; 258 } else if (w is CheckBox) { 259 c = (CheckBox) w; 260 c.set_checked (!c.checked); 261 } 262 } 263 } 264 265 GlyphCanvas.redraw (); 266 } 267 268 public override void button_release (int button, double x, double y) { 269 TextArea t; 270 271 if (keyboard_focus != null) { 272 t = (!) keyboard_focus; 273 t.button_release (button, x, y); 274 } 275 276 GlyphCanvas.redraw (); 277 } 278 279 public override void motion_notify (double x, double y) { 280 TextArea t; 281 282 if (keyboard_focus != null) { 283 t = (!) keyboard_focus; 284 if (t.motion (x, y)) { 285 GlyphCanvas.redraw (); 286 } 287 } 288 } 289 290 public override string get_label () { 291 return t_("Name and Description"); 292 } 293 294 public override string get_name () { 295 return "Description"; 296 } 297 298 public override bool has_scrollbar () { 299 return true; 300 } 301 302 public override void scroll_wheel_down (double x, double y) { 303 scroll += 25 * MainWindow.units; 304 305 if (scroll + allocation.height >= content_height) { 306 scroll = content_height - allocation.height; 307 } 308 309 update_scrollbar (); 310 GlyphCanvas.redraw (); 311 } 312 313 public override void scroll_wheel_up (double x, double y) { 314 scroll -= 25 * MainWindow.units; 315 316 if (scroll < 0) { 317 scroll = 0; 318 } 319 320 update_scrollbar (); 321 GlyphCanvas.redraw (); 322 } 323 324 public override void selected_canvas () { 325 copyright.set_editable (!disable_copyright); 326 update_scrollbar (); 327 GlyphCanvas.redraw (); 328 } 329 330 public override void update_scrollbar () { 331 double h = content_height - allocation.height; 332 MainWindow.set_scrollbar_size (allocation.height / content_height); 333 MainWindow.set_scrollbar_position (scroll / h); 334 } 335 336 public override void scroll_to (double percent) { 337 double h = content_height - allocation.height; 338 scroll = percent * h; 339 GlyphCanvas.redraw (); 340 } 341 } 342 343 } 344