The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

Tool.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
Circle boundaries heads/master
1 /* 2 Copyright (C) 2012 2014 2016 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 17 namespace BirdFont { 18 19 public class Tool : Widget { 20 21 public double x = 0; 22 public double y = 0; 23 public double w; 24 public double h; 25 26 double scale; 27 28 public bool active = false; 29 public bool selected = false; 30 31 public Text icon_font; 32 33 public signal void select_action (Tool selected); 34 public signal void deselect_action (Tool selected); 35 36 public signal void press_action (Tool selected, int button, int x, int y); 37 public signal void double_click_action (Tool selected, int button, int x, int y); 38 public signal void move_action (Tool selected, int x, int y); 39 public signal void move_out_action (Tool selected); 40 public signal void release_action (Tool selected, int button, int x, int y); 41 42 /** Returns true if tool is listening for scroll wheel actions. */ 43 public signal bool scroll_wheel_up_action (Tool selected); 44 public signal bool scroll_wheel_down_action (Tool selected); 45 46 public signal void key_press_action (Tool selected, uint32 keyval); 47 public signal void key_release_action (Tool selected, uint32 keyval); 48 49 public signal void panel_press_action (Tool selected, uint button, double x, double y); 50 public signal void panel_release_action (Tool selected, uint button, double x, double y); 51 public signal void panel_double_click_action (Tool selected, uint button, double x, double y); 52 53 /** @return true is event is consumed. */ 54 public signal bool panel_move_action (Tool selected, double x, double y); 55 56 public signal void draw_action (Tool selected, Context cr, Glyph glyph); 57 58 public string name = ""; 59 60 static int next_id = 1; 61 62 int id; 63 64 public bool new_selection = false; 65 66 bool show_bg = true; 67 68 public string tip = ""; 69 string extented_description = ""; 70 TextArea? help = null; 71 72 public bool persistent = false; 73 public bool editor_events = false; 74 75 bool waiting_for_tooltip = false; 76 bool showing_this_tooltip = false; 77 static Tool active_tooltip = new Tool (); 78 79 public bool visible = true; 80 public bool is_tool_modifier = false; 81 82 public string icon_color = ""; 83 84 public signal void redraw_tool (); 85 86 /** Create tool with a certain name and load icon "name".png */ 87 public Tool (string? name = null, string tip = "", string extended_description = "") { 88 this.tip = tip; 89 this.extented_description = extended_description; 90 91 icon_font = new Text (); 92 93 scale = MainWindow.units; 94 w = 33 * Toolbox.get_scale (); 95 h = (33 / 1.11) * Toolbox.get_scale (); 96 97 if (name != null) { 98 set_icon ((!) name); 99 this.name = (!) name; 100 } 101 102 id = next_id; 103 next_id++; 104 105 panel_press_action.connect ((self, button, x, y) => { 106 if (is_active ()) { 107 redraw (); 108 } 109 }); 110 111 select_action.connect ((self) => { 112 redraw (); 113 }); 114 115 deselect_action.connect ((self) => { 116 redraw (); 117 }); 118 119 move_out_action.connect ((self) => { 120 MainWindow.get_toolbox ().hide_tooltip (); 121 active_tooltip.showing_this_tooltip = false; 122 redraw (); 123 }); 124 125 panel_move_action.connect ((self, x, y) => { 126 if (is_active ()) { 127 wait_for_tooltip (); 128 redraw (); 129 } 130 return false; 131 }); 132 } 133 134 public virtual void clear_cache () { 135 help = null; 136 } 137 138 public virtual string get_tip () { 139 return tip; 140 } 141 142 public override void draw (Context cr) { 143 draw_tool (cr, 0, 0); 144 } 145 146 public void redraw () { 147 redraw_tool (); 148 Toolbox.redraw_tool_box (); 149 } 150 151 public override double get_height () { 152 return 33 * scale; 153 } 154 155 public override double get_width () { 156 return 33 * scale; 157 } 158 159 public void set_tool_visibility (bool v) { 160 visible = v; 161 } 162 163 public bool tool_is_visible () { 164 return visible; 165 } 166 167 void wait_for_tooltip () { 168 TimeoutSource timer_show; 169 int timeout_interval = 1500; 170 171 if (active_tooltip != this) { 172 if (active_tooltip.showing_this_tooltip) { 173 timeout_interval = 1; 174 } 175 176 active_tooltip.showing_this_tooltip = false; 177 showing_this_tooltip = false; 178 active_tooltip = this; 179 180 if (!waiting_for_tooltip) { 181 waiting_for_tooltip = true; 182 timer_show = new TimeoutSource (timeout_interval); 183 timer_show.set_callback (() => { 184 if (get_tip () != "" && active_tooltip.is_active () && !active_tooltip.showing_this_tooltip) { 185 show_tooltip (); 186 } 187 waiting_for_tooltip = false; 188 return waiting_for_tooltip; 189 }); 190 timer_show.attach (null); 191 } 192 } 193 } 194 195 public static void show_tooltip () { 196 TimeoutSource timer_hide; 197 Toolbox toolbox; 198 string tip; 199 string key_binding; 200 201 toolbox = MainWindow.get_toolbox (); 202 203 // hide tooltip label later 204 if (!active_tooltip.showing_this_tooltip) { 205 timer_hide = new TimeoutSource (1500); 206 timer_hide.set_callback (() => { 207 if (!active_tooltip.is_active ()) { 208 toolbox.hide_tooltip (); 209 active_tooltip.showing_this_tooltip = false; 210 active_tooltip = new Tool (); 211 } 212 return active_tooltip.showing_this_tooltip; 213 }); 214 timer_hide.attach (null); 215 } 216 217 active_tooltip.showing_this_tooltip = true; 218 219 tip = @"$(active_tooltip.get_tip ())"; 220 key_binding = active_tooltip.get_key_binding (); 221 222 if (key_binding != "") { 223 tip += " (" + key_binding + ")"; 224 } 225 226 toolbox.hide_tooltip (); 227 toolbox.show_tooltip (tip, (int) active_tooltip.x, (int) active_tooltip.y); 228 } 229 230 public string get_key_binding () { 231 if (is_null (MainWindow.get_menu ())) { 232 return ""; 233 } 234 235 ToolItem? ti = MainWindow.get_menu ().get_item_for_tool (this); 236 ToolItem t; 237 238 if (ti == null) { 239 warning ("No key binding for tool."); 240 return ""; 241 } 242 243 t = (!) ti; 244 return t.get_key_binding (); 245 } 246 247 public void set_icon (string name) { 248 bool found; 249 string icon_file; 250 251 icon_file = Theme.get_icon_file (); 252 icon_font = new Text ((!) name); 253 found = icon_font.load_font (icon_file); 254 icon_font.set_font_size (40 * Toolbox.get_scale ()); 255 256 if (!found) { 257 warning (@"Icon font for toolbox was not found. ($(icon_file))"); 258 } 259 } 260 261 public bool is_active () { 262 return active; 263 } 264 265 public void set_show_background (bool bg) { 266 show_bg = bg; 267 } 268 269 public int get_id () { 270 return id; 271 } 272 273 public string get_name () { 274 return name; 275 } 276 277 public bool is_selected () { 278 return selected; 279 } 280 281 public new bool is_over (double xp, double yp) { 282 bool r = (x <= xp <= x + w && y <= yp <= y + h); 283 return r; 284 } 285 286 public bool set_selected (bool a) { 287 new_selection = true; 288 selected = a; 289 set_active (a); 290 291 if (!a) { 292 deselect_action (this); 293 } 294 295 return true; 296 } 297 298 /** @return true if this tool changes state, */ 299 public bool set_active (bool ac) { 300 bool ret = (active != ac); 301 active = ac; 302 return ret; 303 } 304 305 public virtual void draw_tool (Context cr, double px, double py) { 306 double xt = x - px; 307 double yt = y - py; 308 309 double bgx, bgy; 310 double iconx, icony; 311 312 string border = "Button Border 3"; 313 string background = "Button Border 3"; 314 315 double scale = Toolbox.get_scale (); 316 317 cr.save (); 318 319 bgx = xt; 320 bgy = yt; 321 322 // Button in four states 323 if (selected) { 324 border = "Button Border 1"; 325 background = "Button Background 1"; 326 } 327 328 if (selected && active) { 329 border = "Button Border 2"; 330 background = "Button Background 2"; 331 } 332 333 if (!selected) { 334 border = "Button Border 3"; 335 background = "Button Background 3"; 336 } 337 338 if (!selected && active) { 339 border = "Button Border 4"; 340 background = "Button Background 4"; 341 } 342 343 Theme.color (cr, background); 344 draw_rounded_rectangle (cr, bgx, bgy, 34 * scale, 28 * scale, 4 * scale); 345 cr.fill (); 346 347 cr.set_line_width (1); 348 Theme.color (cr, border); 349 draw_rounded_rectangle (cr, bgx, bgy, 34 * scale, 28 * scale, 4 * scale); 350 cr.stroke (); 351 352 iconx = bgx + 1 + (w - 1) / 2 - icon_font.get_sidebearing_extent () / 2; 353 icony = bgy + 1 + (h - 1) / 2 - icon_font.get_height () / 2; 354 355 if (icon_color == "") { 356 if (!selected) { 357 Theme.text_color (icon_font, "Tool Foreground"); 358 } else { 359 Theme.text_color (icon_font, "Selected Tool Foreground"); 360 } 361 } else { 362 Theme.text_color (icon_font, icon_color); 363 } 364 365 icon_font.widget_x = iconx; 366 icon_font.widget_y = icony; 367 368 icon_font.draw (cr); 369 370 cr.restore (); 371 } 372 373 /** Run pending events in main loop before continuing. */ 374 public static void @yield () { 375 int t = 0; 376 TimeoutSource time = new TimeoutSource (500); 377 bool timeout; 378 unowned MainContext context; 379 bool acquired; 380 381 if (TestBirdFont.is_slow_test ()) { 382 timeout = false; 383 384 time.set_callback (() => { 385 timeout = true; 386 return false; 387 }); 388 389 time.attach (null); 390 } else { 391 timeout = true; 392 } 393 394 context = MainContext.default (); 395 acquired = context.acquire (); 396 397 if (unlikely (!acquired)) { 398 warning ("Failed to acquire main loop.\n"); 399 return; 400 } 401 402 while (context.pending () || TestBirdFont.is_slow_test ()) { 403 context.iteration (true); 404 t++; 405 406 if (!context.pending () && TestBirdFont.is_slow_test ()) { 407 if (timeout) break; 408 } 409 } 410 411 context.release (); 412 } 413 414 public void set_persistent (bool p) { 415 persistent = p; 416 } 417 418 public virtual void before_undo () { 419 } 420 421 public virtual void after_undo () { 422 } 423 424 } 425 426 } 427