The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

TabContent.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/TabContent.vala.
Bounding box for SVG rectangles
1 /* 2 Copyright (C) 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 /** Interface for events from native window to the current tab. */ 20 public class TabContent : GLib.Object { 21 22 static Text text_input_label; 23 static LineTextArea text_input; 24 static Button text_input_button; 25 static bool text_input_visible = false; 26 static TextListener text_callback; 27 static ImageSurface? pause_surface = null; 28 29 static const int TEXT_INPUT_HEIGHT = 51; 30 31 static double last_press_time = 0; 32 33 public static void zoom_in () { 34 if (MenuTab.has_suppress_event ()) { 35 } 36 37 GlyphCanvas.current_display.zoom_in (); 38 } 39 40 public static void zoom_out () { 41 if (MenuTab.has_suppress_event ()) { 42 return; 43 } 44 45 GlyphCanvas.current_display.zoom_out (); 46 } 47 48 public static void move_view (double x, double y) { 49 if (MenuTab.has_suppress_event ()) { 50 return; 51 } 52 53 GlyphCanvas.current_display.move_view (x, y); 54 } 55 56 public static bool has_scrollbar () { 57 if (MenuTab.has_suppress_event ()) { 58 return false; 59 } 60 61 return GlyphCanvas.current_display.has_scrollbar (); 62 } 63 64 public static void scroll_to (double percent) { 65 if (MenuTab.has_suppress_event ()) { 66 return; 67 } 68 69 GlyphCanvas.current_display.scroll_to (percent); 70 } 71 72 public static void draw (WidgetAllocation allocation, Context cr) { 73 AbstractMenu menu; 74 Dialog dialog; 75 76 if (unlikely (MenuTab.has_suppress_event ())) { 77 cr.save (); 78 Theme.color (cr, "Background 1"); 79 cr.rectangle (0, 0, allocation.width, allocation.height); 80 cr.fill (); 81 82 if (pause_surface != null) { 83 cr.scale (1.0 / Screen.get_scale (), 1.0 / Screen.get_scale ()); 84 cr.set_source_surface ((!) pause_surface, 0, 0); 85 cr.paint (); 86 } 87 88 cr.restore (); 89 } else { 90 menu = MainWindow.get_menu (); 91 dialog = MainWindow.get_dialog (); 92 93 GlyphCanvas.set_allocation (allocation); 94 MainWindow.get_current_glyph ().resized (allocation); 95 GlyphCanvas.current_display.draw (allocation, cr); 96 97 if (dialog.visible) { 98 dialog.allocation = allocation; 99 dialog.layout (); 100 dialog.draw (cr); 101 } else if (menu.show_menu) { 102 menu.draw (allocation, cr); 103 } 104 105 if (FontDisplay.dirty_scrollbar) { 106 GlyphCanvas.current_display.update_scrollbar (); 107 FontDisplay.dirty_scrollbar = false; 108 } 109 110 if (text_input_visible) { 111 draw_text_input (allocation, cr); 112 } 113 } 114 115 Help help = MainWindow.get_help (); 116 117 if (help.is_visible ()) { 118 help.draw (cr, allocation); 119 } 120 } 121 122 public static void create_pause_surface () { 123 Context cr; 124 WidgetAllocation alloc; 125 126 if (MenuTab.has_suppress_event ()) { 127 warning ("Background surface already created."); 128 return; 129 } 130 131 alloc = GlyphCanvas.get_allocation (); 132 pause_surface = Screen.create_background_surface (alloc.width, alloc.height); 133 cr = new Context ((!) pause_surface); 134 cr.scale (Screen.get_scale (), Screen.get_scale ()); 135 draw (alloc, cr); 136 } 137 138 public static void key_press (uint keyval) { 139 if (MenuTab.has_suppress_event () || MainWindow.get_dialog ().visible) { 140 return; 141 } 142 143 unichar c = (unichar) keyval; 144 145 if (unlikely (!c.validate ())) { 146 warning ("Invalid unichar: $(keyval)"); 147 return; 148 } 149 150 KeyBindings.add_modifier_from_keyval (keyval); 151 152 if (!text_input_visible) { 153 AbstractMenu menu = MainWindow.get_menu (); 154 bool consumed = menu.process_key_binding_events (keyval); 155 156 if (!consumed) { 157 GlyphCanvas.current_display.key_press (keyval); 158 } 159 } else { 160 text_input.key_press (keyval); 161 } 162 } 163 164 public static void key_release (uint keyval) { 165 if (MenuTab.has_suppress_event () || MainWindow.get_dialog ().visible) { 166 return; 167 } 168 169 unichar c = (unichar) keyval; 170 171 if (unlikely (!c.validate ())) { 172 warning ("Invalid unichar: $(keyval)"); 173 return; 174 } 175 176 KeyBindings.remove_modifier_from_keyval (keyval); 177 178 if (!text_input_visible) { 179 GlyphCanvas.current_display.key_release (keyval); 180 } 181 } 182 183 public static void motion_notify (double x, double y) { 184 Toolbox toolbox; 185 186 if (MenuTab.has_suppress_event ()) { 187 return; 188 } 189 190 if (!text_input_visible) { 191 GlyphCanvas.current_display.motion_notify (x, y); 192 } else { 193 text_input.motion (x, y); 194 GlyphCanvas.redraw (); 195 } 196 197 toolbox = MainWindow.get_toolbox (); 198 toolbox.hide_tooltip (); 199 } 200 201 public static void button_release (int button, double x, double y) { 202 if (MenuTab.has_suppress_event ()) { 203 return; 204 } 205 206 if (MainWindow.get_dialog ().visible) { 207 MainWindow.get_dialog ().button_release (button, x, y); 208 } else if (MainWindow.get_menu ().show_menu) { 209 MainWindow.get_menu ().button_release (button, x, y); 210 } else if (text_input_visible) { 211 text_input_button.button_release (button, x, y); 212 text_input.button_release (button, x, y); 213 GlyphCanvas.redraw (); 214 } else { 215 GlyphCanvas.current_display.button_release (button, x, y); 216 } 217 } 218 219 public static void button_press (uint button, double x, double y) { 220 if (MenuTab.has_suppress_event ()) { 221 return; 222 } 223 224 last_press_time = GLib.get_real_time (); 225 226 Help help = MainWindow.get_help (); 227 228 if (help.button_press (button, x, y)) { 229 return; // event consumed by help text 230 } 231 232 if (MainWindow.get_dialog ().visible) { 233 MainWindow.get_dialog ().button_press (button, x, y); 234 } else if (!MainWindow.get_menu ().show_menu) { 235 if (text_input_visible) { 236 text_input.button_press (button, x, y); 237 text_input_button.button_press (button, x, y); 238 239 if (y > TEXT_INPUT_HEIGHT) { 240 hide_text_input (); 241 } 242 } else { 243 GlyphCanvas.current_display.button_press (button, x, y); 244 } 245 } 246 } 247 248 public static void double_click (uint button, double ex, double ey) { 249 if (MenuTab.has_suppress_event ()) { 250 return; 251 } 252 253 if (!MainWindow.get_menu ().show_menu) { 254 GlyphCanvas.current_display.double_click (button, ex, ey); 255 } 256 } 257 258 public static void scroll_wheel_pixel_delta (double x, double y, 259 double pixeldelta_x, double pixeldelta_y) { 260 261 if (MenuTab.has_suppress_event ()) { 262 return; 263 } 264 265 if (!MainWindow.get_menu ().show_menu) { 266 GlyphCanvas.current_display.scroll_wheel (x, y, pixeldelta_x, pixeldelta_y); 267 } 268 } 269 270 public static void scroll_wheel_up (double x, double y) { 271 if (MenuTab.has_suppress_event ()) { 272 return; 273 } 274 275 if (!MainWindow.get_menu ().show_menu) { 276 GlyphCanvas.current_display.scroll_wheel (x, y, 0, 15); 277 } 278 } 279 280 public static void scroll_wheel_down (double x, double y) { 281 if (MenuTab.has_suppress_event ()) { 282 return; 283 } 284 285 if (!MainWindow.get_menu ().show_menu) { 286 GlyphCanvas.current_display.scroll_wheel (x, y, 0, -15); 287 } 288 } 289 290 public static void magnify (double magnification) { 291 if (MenuTab.has_suppress_event ()) { 292 return; 293 } 294 295 if (!MainWindow.get_menu ().show_menu) { 296 GlyphCanvas.current_display.magnify (magnification); 297 } 298 } 299 300 public static void tap_down (int finger, int x, int y) { 301 if (MenuTab.has_suppress_event ()) { 302 return; 303 } 304 305 if (!MainWindow.get_menu ().show_menu) { 306 GlyphCanvas.current_display.tap_down (finger, x, y); 307 } 308 } 309 310 public static void tap_up (int finger, int x, int y) { 311 if (MenuTab.has_suppress_event ()) { 312 return; 313 } 314 315 if (!MainWindow.get_menu ().show_menu) { 316 GlyphCanvas.current_display.tap_up (finger, x, y); 317 } 318 } 319 320 public static void tap_move (int finger, int x, int y) { 321 if (MenuTab.has_suppress_event ()) { 322 return; 323 } 324 325 if (!MainWindow.get_menu ().show_menu) { 326 GlyphCanvas.current_display.tap_move (finger, x, y); 327 } 328 } 329 330 public static void undo () { 331 if (MenuTab.has_suppress_event ()) { 332 return; 333 } 334 335 GlyphCanvas.current_display.undo (); 336 } 337 338 public static void redo () { 339 if (MenuTab.has_suppress_event ()) { 340 return; 341 } 342 343 GlyphCanvas.current_display.redo (); 344 } 345 346 public static string path_to_uri (string path) { 347 string uri = path; 348 string wp; 349 350 // wine uri hack 351 if (BirdFont.win32) { 352 wp = wine_to_unix_path (uri); 353 354 if (SearchPaths.find_file (wp, "").query_exists ()) { 355 uri = wp; 356 } 357 358 if (uri.index_of ("\\") > -1) { 359 uri = uri.replace ("\\", "/"); 360 } 361 } 362 363 if (uri.index_of ("/") == 0) { 364 uri = @"file://$uri"; 365 } else { 366 uri = @"file:///$uri"; 367 } 368 369 return uri; 370 } 371 372 public static void draw_text_input (WidgetAllocation allocation, Context cr) { 373 cr.save (); 374 375 Theme.color (cr, "Default Background"); 376 cr.rectangle (0, 0, allocation.width, TEXT_INPUT_HEIGHT); 377 cr.fill (); 378 cr.restore (); 379 380 Theme.text_color (text_input_label, "Button Foreground"); 381 382 text_input_label.widget_x = 10; 383 text_input_label.widget_y = 17; 384 385 text_input.allocation = allocation; 386 text_input.layout (); 387 text_input.widget_x = text_input_label.get_extent () + 20; 388 text_input.widget_y = 10; 389 text_input.width = allocation.width 390 - text_input_button.get_width () 391 - text_input_label.get_extent () 392 - 40; 393 394 text_input_button.allocation = allocation; 395 text_input_button.widget_x = text_input.widget_x + text_input.width + 10; 396 text_input_button.widget_y = 10; 397 398 text_input_label.draw (cr); 399 text_input.draw (cr); 400 text_input_button.draw (cr); 401 } 402 403 public static void show_text_input (TextListener tl) { 404 text_callback = tl; 405 406 text_input_label = new Text (tl.label); 407 text_input = new LineTextArea (20 * MainWindow.units); 408 text_input_button = new Button (tl.button_label); 409 410 text_input.carret_is_visible = true; 411 412 text_input.set_text (tl.default_text); 413 text_input.text_changed.connect ((text) => { 414 tl.signal_text_input (text); 415 }); 416 417 text_input.enter.connect ((text) => { 418 tl.signal_submit (text); 419 text_input_visible = false; 420 GlyphCanvas.redraw (); 421 }); 422 423 text_input_button.action.connect (() => { 424 tl.signal_submit (text_input.get_text ()); 425 }); 426 427 text_input_visible = true; 428 GlyphCanvas.redraw (); 429 } 430 431 public static void hide_text_input () { 432 text_input_visible = false; 433 text_callback = new TextListener ("", "", ""); 434 } 435 436 public static void reset_modifier () { 437 TabContent.key_release (Key.CTRL_RIGHT); 438 TabContent.key_release (Key.CTRL_LEFT); 439 TabContent.key_release (Key.SHIFT_LEFT); 440 TabContent.key_release (Key.SHIFT_RIGHT); 441 TabContent.key_release (Key.ALT_LEFT); 442 TabContent.key_release (Key.ALT_RIGHT); 443 TabContent.key_release (Key.LOGO_LEFT); 444 TabContent.key_release (Key.LOGO_RIGHT); 445 446 if (MainWindow.get_current_display () is GlyphTab) { 447 TabContent.key_release ((uint) ' '); 448 } 449 } 450 } 451 452 } 453