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