.
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 Toolbox toolbox;
135
136 if (MenuTab.suppress_event) {
137 return;
138 }
139
140 if (!text_input_visible) {
141 GlyphCanvas.current_display.motion_notify (x, y);
142 } else {
143 text_input.motion (x, y);
144 GlyphCanvas.redraw ();
145 }
146
147 toolbox = MainWindow.get_toolbox ();
148 toolbox.hide_tooltip ();
149 }
150
151 public static void button_release (int button, double x, double y) {
152 if (MenuTab.suppress_event) {
153 return;
154 }
155
156 if (MainWindow.get_menu ().show_menu) {
157 MainWindow.get_menu ().button_release (button, x, y);
158 } else {
159 if (text_input_visible) {
160 text_input.button_release (button, x, y);
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
180 if (y > TEXT_INPUT_HEIGHT) {
181 hide_text_input ();
182 }
183 } else {
184 GlyphCanvas.current_display.button_press (button, x, y);
185 }
186 }
187 }
188
189 public static void double_click (uint button, double ex, double ey) {
190 if (MenuTab.suppress_event) {
191 return;
192 }
193
194 if (!MainWindow.get_menu ().show_menu) {
195 GlyphCanvas.current_display.double_click (button, ex, ey);
196 }
197 }
198
199 public static void scroll_wheel_up (double x, double y) {
200 if (MenuTab.suppress_event) {
201 return;
202 }
203
204 if (!MainWindow.get_menu ().show_menu) {
205 GlyphCanvas.current_display.scroll_wheel_up (x, y);
206 }
207 }
208
209 public static void scroll_wheel_down (double x, double y) {
210 if (MenuTab.suppress_event) {
211 return;
212 }
213
214 if (!MainWindow.get_menu ().show_menu) {
215 GlyphCanvas.current_display.scroll_wheel_down (x, y);
216 }
217 }
218
219 public static void tap_down (int finger, int x, int y) {
220 if (MenuTab.suppress_event) {
221 return;
222 }
223
224 if (!MainWindow.get_menu ().show_menu) {
225 GlyphCanvas.current_display.tap_down (finger, x, y);
226 }
227 }
228
229 public static void tap_up (int finger, int x, int y) {
230 if (MenuTab.suppress_event) {
231 return;
232 }
233
234 if (!MainWindow.get_menu ().show_menu) {
235 GlyphCanvas.current_display.tap_up (finger, x, y);
236 }
237 }
238
239 public static void tap_move (int finger, int x, int y) {
240 if (MenuTab.suppress_event) {
241 return;
242 }
243
244 if (!MainWindow.get_menu ().show_menu) {
245 GlyphCanvas.current_display.tap_move (finger, x, y);
246 }
247 }
248
249 public static void undo () {
250 if (MenuTab.suppress_event) {
251 return;
252 }
253
254 GlyphCanvas.current_display.undo ();
255 }
256
257 public static void redo () {
258 if (MenuTab.suppress_event) {
259 return;
260 }
261
262 GlyphCanvas.current_display.redo ();
263 }
264
265 public static string path_to_uri (string path) {
266 string uri = path;
267 string wp;
268
269 // wine uri hack
270 if (BirdFont.win32) {
271 wp = wine_to_unix_path (uri);
272
273 if (SearchPaths.find_file (wp, "").query_exists ()) {
274 uri = wp;
275 }
276
277 if (uri.index_of ("\\") > -1) {
278 uri = uri.replace ("\\", "/");
279 }
280 }
281
282 if (uri.index_of ("/") == 0) {
283 uri = @"file://$uri";
284 } else {
285 uri = @"file:///$uri";
286 }
287
288 return uri;
289 }
290
291 public static void draw_text_input (WidgetAllocation allocation, Context cr) {
292 cr.save ();
293 Theme.color (cr, "Background 4");
294 cr.rectangle (0, 0, allocation.width, TEXT_INPUT_HEIGHT);
295 cr.fill ();
296 cr.restore ();
297
298 Theme.text_color (text_input_label, "Background 1");
299
300 text_input_label.widget_x = 10;
301 text_input_label.widget_y = 17;
302
303 text_input.allocation = allocation;
304 text_input.layout ();
305 text_input.widget_x = text_input_label.get_extent () + 20;
306 text_input.widget_y = 10;
307 text_input.width = allocation.width
308 - text_input_button.get_width ()
309 - text_input_label.get_extent ()
310 - 40;
311
312 text_input_button.allocation = allocation;
313 text_input_button.widget_x = text_input.widget_x + text_input.width + 10;
314 text_input_button.widget_y = 10;
315
316 text_input_label.draw (cr);
317 text_input.draw (cr);
318 text_input_button.draw (cr);
319 }
320
321 public static void show_text_input (TextListener tl) {
322 text_input_label = new Text (tl.label);
323 text_input = new LineTextArea (20 * MainWindow.units);
324 text_input_button = new Button (tl.button_label);
325
326 text_input.carret_is_visible = true;
327
328 text_input.set_text (tl.default_text);
329 text_input.text_changed.connect ((text) => {
330 tl.signal_text_input (text);
331 });
332
333 text_input.enter.connect ((text) => {
334 tl.signal_submit (text);
335 text_input_visible = false;
336 GlyphCanvas.redraw ();
337 });
338
339 text_input_button.action.connect (() => {
340 tl.signal_submit (text_input.get_text ());
341 });
342
343 text_input_visible = true;
344 GlyphCanvas.redraw ();
345 }
346
347 public static void hide_text_input () {
348 text_input_visible = false;
349 }
350 }
351
352 }
353