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 old scroll wheel 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 28 static const int TEXT_INPUT_HEIGHT = 51; 29 30 static double last_press_time = 0; 31 32 public static void zoom_in () { 33 if (MenuTab.suppress_event) { 34 } 35 36 GlyphCanvas.current_display.zoom_in (); 37 } 38 39 public static void zoom_out () { 40 if (MenuTab.suppress_event) { 41 return; 42 } 43 44 GlyphCanvas.current_display.zoom_out (); 45 } 46 47 public static void move_view (double x, double y) { 48 if (MenuTab.suppress_event) { 49 return; 50 } 51 52 GlyphCanvas.current_display.move_view (x, y); 53 } 54 55 public static bool has_scrollbar () { 56 if (MenuTab.suppress_event) { 57 return false; 58 } 59 60 return GlyphCanvas.current_display.has_scrollbar (); 61 } 62 63 public static void scroll_to (double percent) { 64 if (MenuTab.suppress_event) { 65 return; 66 } 67 68 GlyphCanvas.current_display.scroll_to (percent); 69 } 70 71 public static void draw (WidgetAllocation allocation, Context cr) { 72 AbstractMenu menu; 73 Dialog dialog; 74 75 if (unlikely (MenuTab.suppress_event)) { 76 cr.save (); 77 Theme.color (cr, "Background 1"); 78 cr.rectangle (0, 0, allocation.width, allocation.height); 79 cr.fill (); 80 cr.restore (); 81 } else { 82 menu = MainWindow.get_menu (); 83 dialog = MainWindow.get_dialog (); 84 85 GlyphCanvas.set_allocation (allocation); 86 MainWindow.get_current_glyph ().resized (allocation); 87 GlyphCanvas.current_display.draw (allocation, cr); 88 89 if (dialog.visible) { 90 dialog.allocation = allocation; 91 dialog.draw (cr); 92 } 93 94 if (menu.show_menu) { 95 menu.draw (allocation, cr); 96 } 97 98 if (FontDisplay.dirty_scrollbar) { 99 GlyphCanvas.current_display.update_scrollbar (); 100 FontDisplay.dirty_scrollbar = false; 101 } 102 103 if (text_input_visible) { 104 draw_text_input (allocation, cr); 105 } 106 } 107 } 108 109 public static void key_press (uint keyval) { 110 if (MenuTab.suppress_event) { 111 return; 112 } 113 114 KeyBindings.add_modifier_from_keyval (keyval); 115 116 if (!text_input_visible) { 117 MainWindow.get_menu ().process_key_binding_events (keyval); 118 GlyphCanvas.current_display.key_press (keyval); 119 } else { 120 text_input.key_press (keyval); 121 } 122 } 123 124 public static void key_release (uint keyval) { 125 if (MenuTab.suppress_event) { 126 return; 127 } 128 129 KeyBindings.remove_modifier_from_keyval (keyval); 130 131 if (!text_input_visible) { 132 GlyphCanvas.current_display.key_release (keyval); 133 } 134 } 135 136 public static void motion_notify (double x, double y) { 137 Toolbox toolbox; 138 139 if (MenuTab.suppress_event) { 140 return; 141 } 142 143 if (!text_input_visible) { 144 GlyphCanvas.current_display.motion_notify (x, y); 145 } else { 146 text_input.motion (x, y); 147 GlyphCanvas.redraw (); 148 } 149 150 toolbox = MainWindow.get_toolbox (); 151 toolbox.hide_tooltip (); 152 } 153 154 public static void button_release (int button, double x, double y) { 155 if (MenuTab.suppress_event) { 156 return; 157 } 158 159 if (MainWindow.get_dialog ().visible) { 160 MainWindow.get_dialog ().button_release (button, x, y); 161 } else if (MainWindow.get_menu ().show_menu) { 162 MainWindow.get_menu ().button_release (button, x, y); 163 } else if (text_input_visible) { 164 text_input_button.button_release (button, x, y); 165 text_input.button_release (button, x, y); 166 GlyphCanvas.redraw (); 167 } else { 168 GlyphCanvas.current_display.button_release (button, x, y); 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_pixel_delta (double x, double y, 206 double pixeldelta_x, double pixeldelta_y) { 207 208 if (MenuTab.suppress_event) { 209 return; 210 } 211 212 if (!MainWindow.get_menu ().show_menu) { 213 GlyphCanvas.current_display.scroll_wheel (x, y, pixeldelta_x, pixeldelta_y); 214 } 215 } 216 217 public static void scroll_wheel_up (double x, double y) { 218 if (MenuTab.suppress_event) { 219 return; 220 } 221 222 if (!MainWindow.get_menu ().show_menu) { 223 GlyphCanvas.current_display.scroll_wheel (x, y, 15, 0); 224 } 225 } 226 227 public static void scroll_wheel_down (double x, double y) { 228 if (MenuTab.suppress_event) { 229 return; 230 } 231 232 if (!MainWindow.get_menu ().show_menu) { 233 GlyphCanvas.current_display.scroll_wheel (x, y, -15, 0); 234 } 235 } 236 237 public static void tap_down (int finger, int x, int y) { 238 if (MenuTab.suppress_event) { 239 return; 240 } 241 242 if (!MainWindow.get_menu ().show_menu) { 243 GlyphCanvas.current_display.tap_down (finger, x, y); 244 } 245 } 246 247 public static void tap_up (int finger, int x, int y) { 248 if (MenuTab.suppress_event) { 249 return; 250 } 251 252 if (!MainWindow.get_menu ().show_menu) { 253 GlyphCanvas.current_display.tap_up (finger, x, y); 254 } 255 } 256 257 public static void tap_move (int finger, int x, int y) { 258 if (MenuTab.suppress_event) { 259 return; 260 } 261 262 if (!MainWindow.get_menu ().show_menu) { 263 GlyphCanvas.current_display.tap_move (finger, x, y); 264 } 265 } 266 267 public static void undo () { 268 if (MenuTab.suppress_event) { 269 return; 270 } 271 272 GlyphCanvas.current_display.undo (); 273 } 274 275 public static void redo () { 276 if (MenuTab.suppress_event) { 277 return; 278 } 279 280 GlyphCanvas.current_display.redo (); 281 } 282 283 public static string path_to_uri (string path) { 284 string uri = path; 285 string wp; 286 287 // wine uri hack 288 if (BirdFont.win32) { 289 wp = wine_to_unix_path (uri); 290 291 if (SearchPaths.find_file (wp, "").query_exists ()) { 292 uri = wp; 293 } 294 295 if (uri.index_of ("\\") > -1) { 296 uri = uri.replace ("\\", "/"); 297 } 298 } 299 300 if (uri.index_of ("/") == 0) { 301 uri = @"file://$uri"; 302 } else { 303 uri = @"file:///$uri"; 304 } 305 306 return uri; 307 } 308 309 public static void draw_text_input (WidgetAllocation allocation, Context cr) { 310 cr.save (); 311 312 Theme.color (cr, "Default Background"); 313 cr.rectangle (0, 0, allocation.width, TEXT_INPUT_HEIGHT); 314 cr.fill (); 315 cr.restore (); 316 317 Theme.text_color (text_input_label, "Button Foreground"); 318 319 text_input_label.widget_x = 10; 320 text_input_label.widget_y = 17; 321 322 text_input.allocation = allocation; 323 text_input.layout (); 324 text_input.widget_x = text_input_label.get_extent () + 20; 325 text_input.widget_y = 10; 326 text_input.width = allocation.width 327 - text_input_button.get_width () 328 - text_input_label.get_extent () 329 - 40; 330 331 text_input_button.allocation = allocation; 332 text_input_button.widget_x = text_input.widget_x + text_input.width + 10; 333 text_input_button.widget_y = 10; 334 335 text_input_label.draw (cr); 336 text_input.draw (cr); 337 text_input_button.draw (cr); 338 } 339 340 public static void show_text_input (TextListener tl) { 341 text_callback = tl; 342 343 text_input_label = new Text (tl.label); 344 text_input = new LineTextArea (20 * MainWindow.units); 345 text_input_button = new Button (tl.button_label); 346 347 text_input.carret_is_visible = true; 348 349 text_input.set_text (tl.default_text); 350 text_input.text_changed.connect ((text) => { 351 tl.signal_text_input (text); 352 }); 353 354 text_input.enter.connect ((text) => { 355 tl.signal_submit (text); 356 text_input_visible = false; 357 GlyphCanvas.redraw (); 358 }); 359 360 text_input_button.action.connect (() => { 361 tl.signal_submit (text_input.get_text ()); 362 }); 363 364 text_input_visible = true; 365 GlyphCanvas.redraw (); 366 } 367 368 public static void hide_text_input () { 369 text_input_visible = false; 370 text_callback = new TextListener ("", "", ""); 371 } 372 373 public static void reset_modifier () { 374 TabContent.key_release (Key.CTRL_RIGHT); 375 TabContent.key_release (Key.CTRL_LEFT); 376 TabContent.key_release (Key.SHIFT_LEFT); 377 TabContent.key_release (Key.SHIFT_RIGHT); 378 TabContent.key_release (Key.ALT_LEFT); 379 TabContent.key_release (Key.ALT_RIGHT); 380 TabContent.key_release (Key.LOGO_LEFT); 381 TabContent.key_release (Key.LOGO_RIGHT); 382 383 if (MainWindow.get_current_display () is Glyph) { 384 TabContent.key_release ((uint) ' '); 385 } 386 } 387 } 388 389 } 390