.
1 /*
2 Copyright (C) 2014 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 using Math;
17
18 namespace BirdFont {
19
20 public class ZoomBar : Tool {
21
22 double zoom_level = 1 / 3.0;
23 bool update_zoom = false;
24
25 public signal void new_zoom (double zoom_level);
26
27 public ZoomBar () {
28 base ();
29
30 panel_press_action.connect ((selected, button, tx, ty) => {
31 if (y <= ty <= y + h) {
32 set_zoom_from_mouse (tx);
33 update_zoom = true;
34 }
35 });
36
37 panel_move_action.connect ((selected, button, tx, ty) => {
38 if (update_zoom) {
39 set_zoom_from_mouse (tx);
40 }
41
42 return true;
43 });
44
45 panel_release_action.connect ((selected, button, tx, ty) => {
46 update_zoom = false;
47 });
48 }
49
50 /** Zoom level from 0 to 1. */
51 public void set_zoom (double z) {
52 zoom_level = z;
53
54 if (!MenuTab.background_thread) {
55 Toolbox.redraw_tool_box ();
56 }
57 }
58
59 void set_zoom_from_mouse (double tx) {
60 double margin = w * 0.1;
61 double bar_width = w - margin - x;
62
63 tx -= x;
64 zoom_level = tx / bar_width;
65
66 if (zoom_level > 1) {
67 zoom_level = 1;
68 }
69
70 if (zoom_level < 0) {
71 zoom_level = 0;
72 }
73
74 set_zoom (zoom_level);
75
76 if (!MenuTab.suppress_event) {
77 new_zoom (zoom_level);
78 }
79
80 FontDisplay.dirty_scrollbar = true;
81 }
82
83 public override void draw (Context cr) {
84 double margin = w * 0.1;
85 double bar_width = w - margin - x;
86
87 // filled
88 cr.save ();
89 Theme.color (cr, "Foreground 3");
90 draw_bar (cr);
91 cr.fill ();
92 cr.restore ();
93
94 // remove non filled parts
95 cr.save ();
96 Theme.color (cr, "Background 4");
97 cr.rectangle (x + bar_width * zoom_level, y, w, h);
98 cr.fill ();
99 cr.restore ();
100
101 // border
102 cr.save ();
103 Theme.color (cr, "Foreground 1");
104 cr.set_line_width (0.8);
105 draw_bar (cr);
106 cr.stroke ();
107 cr.restore ();
108 }
109
110 void draw_bar (Context cr) {
111 double height = h;
112 double radius = height / 2;
113 double margin = w * 0.1;
114
115 cr.move_to (x + radius, y + height);
116 cr.arc (x + radius, y + radius, radius, PI / 2, 3 * (PI / 2));
117 cr.line_to (w - margin - radius, y);
118 cr.arc (w - margin - radius, y + radius, radius, 3 * (PI / 2), 5 * (PI / 2));
119 cr.line_to (x + radius, y + height);
120 cr.close_path ();
121 }
122 }
123
124 }
125