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