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