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