merge with master
[ardour.git] / gtk2_ardour / big_clock_window.cc
1 /*
2     Copyright (C) 20002-2013 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <algorithm>
21 #include <string>
22
23 #include "ardour_ui.h"
24 #include "audio_clock.h"
25 #include "big_clock_window.h"
26 #include "public_editor.h"
27 #include "utils.h"
28
29 #include "i18n.h"
30
31 using std::min;
32 using std::string;
33
34 BigClockWindow::BigClockWindow (AudioClock& c) 
35         : ArdourWindow (_("Big Clock"))
36         , clock (c)
37         , resize_in_progress (false)
38         , original_height (0)
39         , original_width (0)
40         , original_font_size (0)
41 {
42         ARDOUR_UI::Clock.connect (sigc::mem_fun (clock, &AudioClock::set));
43
44         clock.set_corner_radius (0.0);
45         clock.mode_changed.connect (sigc::mem_fun (*this, &BigClockWindow::reset_aspect_ratio));
46
47         set_keep_above (true);
48         set_border_width (0);
49         add (clock);
50         clock.show_all ();
51 }
52
53 void
54 BigClockWindow::on_unmap ()
55 {
56         ArdourWindow::on_unmap ();
57
58         PublicEditor::instance().reset_focus ();
59 }
60
61 bool
62 BigClockWindow::on_key_press_event (GdkEventKey* ev)
63 {
64         return relay_key_press (ev, this);
65 }
66
67 void
68 BigClockWindow::on_realize ()
69 {
70         int x, y, w, d, h;
71
72         ArdourWindow::on_realize ();
73
74         get_window()->set_decorations (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH);
75         get_window()->get_geometry (x, y, w, h, d);
76
77         reset_aspect_ratio ();
78
79         original_height = h;
80         original_width = w;
81
82         Pango::FontDescription fd (clock.get_style()->get_font());
83         original_font_size = fd.get_size ();
84
85         if (!fd.get_size_is_absolute ()) {
86                 original_font_size /= PANGO_SCALE;
87         }
88 }
89
90 void
91 BigClockWindow::on_size_allocate (Gtk::Allocation& alloc)
92 {
93         ArdourWindow::on_size_allocate (alloc);
94
95         if (!resize_in_progress) {
96                 Glib::signal_idle().connect (sigc::bind (sigc::mem_fun (*this, &BigClockWindow::text_resizer), 0, 0));
97                 resize_in_progress = true;
98         }
99 }
100
101 void
102 BigClockWindow::reset_aspect_ratio ()
103 {
104         Gtk::Requisition req;
105
106         clock.size_request (req);
107
108         float aspect = req.width/(float)req.height;
109         Gdk::Geometry geom;
110
111         geom.min_aspect = aspect;
112         geom.max_aspect = aspect;
113         
114         set_geometry_hints (clock, geom, Gdk::HINT_ASPECT);
115 }
116
117 bool
118 BigClockWindow::text_resizer (int, int)
119 {
120         resize_in_progress = false;
121
122         Glib::RefPtr<Gdk::Window> win = get_window();
123         Pango::FontDescription fd (clock.get_style()->get_font());
124         int current_size = fd.get_size ();
125         int x, y, w, h, d;
126
127         if (!fd.get_size_is_absolute ()) {
128                 current_size /= PANGO_SCALE;
129         }
130
131         win->get_geometry (x, y, w, h, d);
132
133         double scale  = min (((double) w / (double) original_width),
134                              ((double) h / (double) original_height));
135
136         int size = (int) lrintf (original_font_size * scale);
137
138         if (size != current_size) {
139
140                 string family = fd.get_family();
141                 char buf[family.length()+16];
142                 snprintf (buf, family.length()+16, "%s %d", family.c_str(), size);
143
144                 try {
145                         Pango::FontDescription fd (buf);
146                         Glib::RefPtr<Gtk::RcStyle> rcstyle = clock.get_modifier_style ();
147                         rcstyle->set_font (fd);
148                         clock.modify_style (rcstyle);
149                 }
150
151                 catch (...) {
152                         /* oh well, do nothing */
153                 }
154         }
155
156         return false;
157 }
158