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