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