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

Revisions

View the latest version of libbirdfont/Tool.vala.
Platform independent tool tip
1 /* 2 Copyright (C) 2012, 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 17 namespace BirdFont { 18 19 public class Tool : Widget { 20 21 public double x = 0; 22 public double y = 0; 23 public double w = 33 * Toolbox.get_scale (); 24 public double h = (33 / 1.11) * Toolbox.get_scale (); 25 26 double scale; 27 28 public bool active = false; 29 public bool selected = false; 30 31 Text icon_font; 32 ImageSurface? icon = null; 33 34 public signal void select_action (Tool selected); 35 public signal void deselect_action (Tool selected); 36 37 public signal void press_action (Tool selected, int button, int x, int y); 38 public signal void double_click_action (Tool selected, int button, int x, int y); 39 public signal void move_action (Tool selected, int x, int y); 40 public signal void move_out_action (Tool selected); 41 public signal void release_action (Tool selected, int button, int x, int y); 42 43 /** Returns true if tool is listening for scroll wheel actions. */ 44 public signal bool scroll_wheel_up_action (Tool selected); 45 public signal bool scroll_wheel_down_action (Tool selected); 46 47 public signal void key_press_action (Tool selected, uint32 keyval); 48 public signal void key_release_action (Tool selected, uint32 keyval); 49 50 public signal void panel_press_action (Tool selected, uint button, double x, double y); 51 public signal void panel_release_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 70 // keyboard bindings 71 public uint modifier_flag; 72 public unichar key; 73 74 public bool persistent = false; 75 public bool editor_events = false; 76 77 bool waiting_for_tooltip = false; 78 bool showing_this_tooltip = false; 79 static Tool active_tooltip = new Tool (); 80 81 bool visible = true; 82 83 /** Create tool with a certain name and load icon "name".png */ 84 public Tool (string? name = null, string tip = "") { 85 this.tip = tip; 86 87 icon_font = new Text (); 88 89 scale = w / 111.0; // scale to 320 dpi 90 91 if (name != null) { 92 set_icon ((!) name); 93 this.name = (!) name; 94 } 95 96 id = next_id; 97 next_id++; 98 99 panel_press_action.connect ((self, button, x, y) => { 100 }); 101 102 move_out_action.connect ((self) => { 103 MainWindow.native_window.hide_tooltip (); 104 active_tooltip.showing_this_tooltip = false; 105 }); 106 107 panel_move_action.connect ((self, x, y) => { 108 if (is_active ()) { 109 wait_for_tooltip (); 110 } 111 return false; 112 }); 113 } 114 115 public override double get_height () { 116 return 33 * scale; 117 } 118 119 public override double get_width () { 120 return 33 * scale; 121 } 122 123 public void set_tool_visibility (bool v) { 124 visible = v; 125 } 126 127 public bool tool_is_visible () { 128 return visible; 129 } 130 131 void wait_for_tooltip () { 132 TimeoutSource timer_show; 133 int timeout_interval = 1500; 134 135 if (active_tooltip != this) { 136 if (active_tooltip.showing_this_tooltip) { 137 timeout_interval = 1; 138 } 139 140 active_tooltip.showing_this_tooltip = false; 141 showing_this_tooltip = false; 142 active_tooltip = this; 143 144 if (!waiting_for_tooltip) { 145 waiting_for_tooltip = true; 146 timer_show = new TimeoutSource (timeout_interval); 147 timer_show.set_callback (() => { 148 if (tip != "" && active_tooltip.is_active () && !active_tooltip.showing_this_tooltip) { 149 show_tooltip (); 150 } 151 waiting_for_tooltip = false; 152 return waiting_for_tooltip; 153 }); 154 timer_show.attach (null); 155 } 156 } 157 } 158 159 public static void show_tooltip () { 160 TimeoutSource timer_hide; 161 162 // hide tooltip label later 163 if (!active_tooltip.showing_this_tooltip) { 164 timer_hide = new TimeoutSource (1500); 165 timer_hide.set_callback (() => { 166 if (!active_tooltip.is_active ()) { 167 MainWindow.native_window.hide_tooltip (); 168 active_tooltip.showing_this_tooltip = false; 169 active_tooltip = new Tool (); 170 } 171 return active_tooltip.showing_this_tooltip; 172 }); 173 timer_hide.attach (null); 174 } 175 176 active_tooltip.showing_this_tooltip = true; 177 MainWindow.native_window.hide_tooltip (); 178 MainWindow.native_window.show_tooltip (active_tooltip.tip, (int)active_tooltip.x, (int)active_tooltip.y); 179 } 180 181 public void set_icon (string name) { 182 bool found; 183 184 icon_font = new Text ((!) name); 185 found = icon_font.load_font ("icons.bf"); 186 icon_font.use_cache (false); 187 icon_font.set_font_size (35); 188 189 if (!found) { 190 warning ("Icon font for toolbox was not found."); 191 } 192 } 193 194 public bool is_active () { 195 return active; 196 } 197 198 public void set_show_background (bool bg) { 199 show_bg = bg; 200 } 201 202 public int get_id () { 203 return id; 204 } 205 206 public string get_name () { 207 return name; 208 } 209 210 public bool is_selected () { 211 return selected; 212 } 213 214 public string get_tip () { 215 return tip; 216 } 217 218 public new bool is_over (double xp, double yp) { 219 return (x <= xp <= x + w && y <= yp <= y + h); 220 } 221 222 public bool set_selected (bool a) { 223 new_selection = true; 224 selected = a; 225 set_active (a); 226 227 if (!a) { 228 deselect_action (this); 229 } 230 231 return true; 232 } 233 234 /** @return true if this tool changes state, */ 235 public bool set_active (bool ac) { 236 bool ret = (active != ac); 237 active = ac; 238 return ret; 239 } 240 241 public override void draw (Context cr) { 242 double xt = x; 243 double yt = y; 244 245 double bgx, bgy; 246 double iconx, icony; 247 248 string border = "Tool Border 3"; 249 string background = "Tool Border 3"; 250 251 cr.save (); 252 253 bgx = xt; 254 bgy = yt; 255 256 // Button in four states 257 if (selected) { 258 border = "Tool Border 1"; 259 background = "Tool Background 1"; 260 } 261 262 if (selected && active) { 263 border = "Tool Border 2"; 264 background = "Tool Background 2"; 265 } 266 267 if (!selected) { 268 border = "Tool Border 3"; 269 background = "Tool Background 3"; 270 } 271 272 if (!selected && active) { 273 border = "Tool Border 4"; 274 background = "Tool Background 4"; 275 } 276 277 Theme.color (cr, background); 278 draw_rounded_rectangle (cr, bgx, bgy, 34, 28, 4); 279 cr.fill (); 280 281 cr.set_line_width (1); 282 Theme.color (cr, border); 283 draw_rounded_rectangle (cr, bgx, bgy, 34, 28, 4); 284 cr.stroke (); 285 286 iconx = bgx + w / 2 - icon_font.get_sidebearing_extent () / 2; 287 icony = bgy + h / 2 - icon_font.get_height () / 2; 288 289 Theme.text_color (icon_font, "Button Foreground"); 290 291 icon_font.widget_x = iconx; 292 icon_font.widget_y = icony; 293 294 icon_font.draw (cr); 295 296 cr.restore (); 297 } 298 299 /** Run pending events in main loop before continuing. */ 300 public static void @yield () { 301 int t = 0; 302 TimeoutSource time = new TimeoutSource (500); 303 bool timeout; 304 unowned MainContext context; 305 bool acquired; 306 307 if (TestBirdFont.is_slow_test ()) { 308 timeout = false; 309 310 time.set_callback (() => { 311 timeout = true; 312 return false; 313 }); 314 315 time.attach (null); 316 } else { 317 timeout = true; 318 } 319 320 context = MainContext.default (); 321 acquired = context.acquire (); 322 323 if (unlikely (!acquired)) { 324 warning ("Failed to acquire main loop.\n"); 325 return; 326 } 327 328 while (context.pending () || TestBirdFont.is_slow_test ()) { 329 context.iteration (true); 330 t++; 331 332 if (!context.pending () && TestBirdFont.is_slow_test ()) { 333 if (timeout) break; 334 } 335 } 336 337 context.release (); 338 } 339 340 public void set_persistent (bool p) { 341 persistent = p; 342 } 343 } 344 345 } 346