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.
Send events only to text input area when it has focus
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 27 static const int TEXT_INPUT_HEIGHT = 51; 28 29 public static void zoom_in () { 30 if (MenuTab.suppress_event) { 31 } 32 33 GlyphCanvas.current_display.zoom_in (); 34 } 35 36 public static void zoom_out () { 37 if (MenuTab.suppress_event) { 38 return; 39 } 40 41 GlyphCanvas.current_display.zoom_out (); 42 } 43 44 public static void move_view (double x, double y) { 45 if (MenuTab.suppress_event) { 46 return; 47 } 48 49 GlyphCanvas.current_display.move_view (x, y); 50 } 51 52 public static bool has_scrollbar () { 53 if (MenuTab.suppress_event) { 54 return false; 55 } 56 57 return GlyphCanvas.current_display.has_scrollbar (); 58 } 59 60 public static void scroll_to (double percent) { 61 if (MenuTab.suppress_event) { 62 return; 63 } 64 65 GlyphCanvas.current_display.scroll_to (percent); 66 } 67 68 public static void draw (WidgetAllocation allocation, Context cr) { 69 Menu menu; 70 Dialog dialog; 71 72 if (unlikely (MenuTab.suppress_event)) { 73 cr.save (); 74 Theme.color (cr, "Background 1"); 75 cr.rectangle (0, 0, allocation.width, allocation.height); 76 cr.fill (); 77 cr.restore (); 78 } else { 79 menu = MainWindow.get_menu (); 80 dialog = MainWindow.get_dialog (); 81 82 GlyphCanvas.set_allocation (allocation); 83 MainWindow.get_current_glyph ().resized (allocation); 84 GlyphCanvas.current_display.draw (allocation, cr); 85 86 if (dialog.visible) { 87 dialog.allocation = allocation; 88 dialog.draw (cr); 89 } 90 91 if (menu.show_menu) { 92 menu.draw (allocation, cr); 93 } 94 95 if (FontDisplay.dirty_scrollbar) { 96 GlyphCanvas.current_display.update_scrollbar (); 97 FontDisplay.dirty_scrollbar = false; 98 } 99 100 if (text_input_visible) { 101 draw_text_input (allocation, cr); 102 } 103 } 104 } 105 106 public static void key_press (uint keyval) { 107 if (MenuTab.suppress_event) { 108 return; 109 } 110 111 KeyBindings.add_modifier_from_keyval (keyval); 112 113 if (!text_input_visible) { 114 MainWindow.get_menu ().process_key_binding_events (keyval); 115 GlyphCanvas.current_display.key_press (keyval); 116 } else { 117 text_input.key_press (keyval); 118 } 119 } 120 121 public static void key_release (uint keyval) { 122 if (MenuTab.suppress_event) { 123 return; 124 } 125 126 KeyBindings.remove_modifier_from_keyval (keyval); 127 128 if (!text_input_visible) { 129 GlyphCanvas.current_display.key_release (keyval); 130 } 131 } 132 133 public static void motion_notify (double x, double y) { 134 if (MenuTab.suppress_event) { 135 return; 136 } 137 138 if (!text_input_visible) { 139 GlyphCanvas.current_display.motion_notify (x, y); 140 } else { 141 text_input.motion (x, y); 142 GlyphCanvas.redraw (); 143 } 144 } 145 146 public static void button_release (int button, double x, double y) { 147 if (MenuTab.suppress_event) { 148 return; 149 } 150 151 if (MainWindow.get_menu ().show_menu) { 152 MainWindow.get_menu ().button_release (button, x, y); 153 } else { 154 if (text_input_visible) { 155 text_input.button_release (button, x, y); 156 157 if (y > TEXT_INPUT_HEIGHT) { 158 hide_text_input (); 159 } 160 161 GlyphCanvas.redraw (); 162 } else { 163 GlyphCanvas.current_display.button_release (button, x, y); 164 } 165 } 166 } 167 168 public static void button_press (uint button, double x, double y) { 169 if (MenuTab.suppress_event) { 170 return; 171 } 172 173 if (MainWindow.get_dialog ().visible) { 174 MainWindow.get_dialog ().button_press (button, x, y); 175 } else if (!MainWindow.get_menu ().show_menu) { 176 if (text_input_visible) { 177 text_input.button_press (button, x, y); 178 text_input_button.button_press (button, x, y); 179 } else { 180 GlyphCanvas.current_display.button_press (button, x, y); 181 } 182 } 183 } 184 185 public static void double_click (uint button, double ex, double ey) { 186 if (MenuTab.suppress_event) { 187 return; 188 } 189 190 if (!MainWindow.get_menu ().show_menu) { 191 GlyphCanvas.current_display.double_click (button, ex, ey); 192 } 193 } 194 195 public static void scroll_wheel_up (double x, double y) { 196 if (MenuTab.suppress_event) { 197 return; 198 } 199 200 if (!MainWindow.get_menu ().show_menu) { 201 GlyphCanvas.current_display.scroll_wheel_up (x, y); 202 } 203 } 204 205 public static void scroll_wheel_down (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_down (x, y); 212 } 213 } 214 215 public static void tap_down (int finger, int x, int y) { 216 if (MenuTab.suppress_event) { 217 return; 218 } 219 220 if (!MainWindow.get_menu ().show_menu) { 221 GlyphCanvas.current_display.tap_down (finger, x, y); 222 } 223 } 224 225 public static void tap_up (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_up (finger, x, y); 232 } 233 } 234 235 public static void tap_move (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_move (finger, x, y); 242 } 243 } 244 245 public static void undo () { 246 if (MenuTab.suppress_event) { 247 return; 248 } 249 250 GlyphCanvas.current_display.undo (); 251 } 252 253 public static void redo () { 254 if (MenuTab.suppress_event) { 255 return; 256 } 257 258 GlyphCanvas.current_display.redo (); 259 } 260 261 public static string path_to_uri (string path) { 262 string uri = path; 263 string wp; 264 265 // wine uri hack 266 if (BirdFont.win32) { 267 wp = wine_to_unix_path (uri); 268 269 if (SearchPaths.find_file (wp, "").query_exists ()) { 270 uri = wp; 271 } 272 273 if (uri.index_of ("\\") > -1) { 274 uri = uri.replace ("\\", "/"); 275 } 276 } 277 278 if (uri.index_of ("/") == 0) { 279 uri = @"file://$uri"; 280 } else { 281 uri = @"file:///$uri"; 282 } 283 284 return uri; 285 } 286 287 public static void draw_text_input (WidgetAllocation allocation, Context cr) { 288 cr.save (); 289 Theme.color (cr, "Background 4"); 290 cr.rectangle (0, 0, allocation.width, TEXT_INPUT_HEIGHT); 291 cr.fill (); 292 cr.restore (); 293 294 Theme.text_color (text_input_label, "Background 1"); 295 296 text_input_label.widget_x = 10; 297 text_input_label.widget_y = 17; 298 299 text_input.allocation = allocation; 300 text_input.layout (); 301 text_input.widget_x = text_input_label.get_extent () + 20; 302 text_input.widget_y = 10; 303 text_input.width = allocation.width 304 - text_input_button.get_width () 305 - text_input_label.get_extent () 306 - 40; 307 308 text_input_button.allocation = allocation; 309 text_input_button.widget_x = text_input.widget_x + text_input.width + 10; 310 text_input_button.widget_y = 10; 311 312 text_input_label.draw (cr); 313 text_input.draw (cr); 314 text_input_button.draw (cr); 315 } 316 317 public static void show_text_input (TextListener tl) { 318 text_input_label = new Text (tl.label); 319 text_input = new LineTextArea (20 * MainWindow.units); 320 text_input_button = new Button (tl.button_label); 321 322 text_input.carret_is_visible = true; 323 324 text_input.set_text (tl.default_text); 325 text_input.text_changed.connect ((text) => { 326 tl.signal_text_input (text); 327 }); 328 329 text_input.enter.connect ((text) => { 330 tl.signal_submit (text); 331 text_input_visible = false; 332 GlyphCanvas.redraw (); 333 }); 334 335 text_input_button.action.connect (() => { 336 tl.signal_submit (text_input.get_text ()); 337 }); 338 339 text_input_visible = true; 340 GlyphCanvas.redraw (); 341 } 342 343 public static void hide_text_input () { 344 text_input_visible = false; 345 } 346 } 347 348 } 349