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