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
15using Cairo;
16using Math;
17
18namespace BirdFont {
19
20public 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
61	void set_zoom_from_mouse (double tx) {
62		double margin = w * margin_percent;
63		double bar_width = w - margin - x;
64
65		tx -= x;
66		zoom_level = tx / bar_width;
67
68		if (zoom_level > 1) {
69			zoom_level = 1;
70		}
71
72		if (zoom_level < 0) {
73			zoom_level = 0;
74		}
75
76		set_zoom (zoom_level);
77
78		if (!MenuTab.has_suppress_event ()) {
79			new_zoom (zoom_level);
80		}
81
82		FontDisplay.dirty_scrollbar = true;
83		redraw ();
84	}
85
86	public override void draw_tool (Context cr, double px, double py) {
87		double margin = w * margin_percent;
88		double bar_width = w - margin - x;
89
90		// filled
91		cr.save ();
92		Theme.color (cr, "Button Border 1");
93		draw_bar (cr, px, py);
94		cr.fill ();
95		cr.restore ();
96
97		// remove non filled parts
98		cr.save ();
99		Theme.color (cr, "Default Background");
100		cr.rectangle (x + bar_width * zoom_level - px, y - py, w, h);
101		cr.fill ();
102		cr.restore ();
103
104		// border
105		cr.save ();
106		Theme.color (cr, "Zoom Bar Border");
107		cr.set_line_width (0.8);
108		draw_bar (cr, px, py);
109		cr.stroke ();
110		cr.restore ();
111	}
112
113	void draw_bar (Context cr, double px, double py) {
114		double x = this.x - px;
115		double y = this.y - py;
116		double w = this.w - px;
117		double height = h;
118		double radius = height / 2;
119		double margin = w * margin_percent;
120
121		cr.move_to (x + radius, y + height);
122		cr.arc (x + radius, y + radius, radius, PI / 2, 3 * (PI / 2));
123		cr.line_to (w - margin - radius, y);
124		cr.arc (w - margin - radius, y + radius, radius, 3 * (PI / 2), 5 * (PI / 2));
125		cr.line_to (x + radius, y + height);
126		cr.close_path ();
127	}
128}
129
130}
131