finished merge of cairocanvas with windows and windows+cc branches
[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 #include <vector>
23
24 #include "ardour_ui.h"
25 #include "audio_clock.h"
26 #include "big_clock_window.h"
27 #include "public_editor.h"
28 #include "utils.h"
29
30 #include "i18n.h"
31
32 using std::min;
33 using std::string;
34
35 BigClockWindow::BigClockWindow (AudioClock& c) 
36         : ArdourWindow (_("Big Clock"))
37         , clock (c)
38         , resize_in_progress (false)
39         , original_height (0)
40         , original_width (0)
41         , original_font_size (0)
42 {
43         ARDOUR_UI::Clock.connect (sigc::mem_fun (clock, &AudioClock::set));
44
45         clock.set_corner_radius (0.0);
46         clock.mode_changed.connect (sigc::mem_fun (*this, &BigClockWindow::reset_aspect_ratio));
47
48         set_keep_above (true);
49         set_border_width (0);
50         add (clock);
51         clock.show_all ();
52 }
53
54 void
55 BigClockWindow::on_unmap ()
56 {
57         ArdourWindow::on_unmap ();
58
59         PublicEditor::instance().reset_focus ();
60 }
61
62 bool
63 BigClockWindow::on_key_press_event (GdkEventKey* ev)
64 {
65         return relay_key_press (ev, this);
66 }
67
68 void
69 BigClockWindow::on_realize ()
70 {
71         int x, y, w, d, h;
72
73         ArdourWindow::on_realize ();
74
75         get_window()->set_decorations (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH);
76         get_window()->get_geometry (x, y, w, h, d);
77
78         reset_aspect_ratio ();
79
80         original_height = h;
81         original_width = w;
82
83         Pango::FontDescription fd (clock.get_style()->get_font());
84         original_font_size = fd.get_size ();
85
86         if (!fd.get_size_is_absolute ()) {
87                 original_font_size /= PANGO_SCALE;
88         }
89 }
90
91 void
92 BigClockWindow::on_size_allocate (Gtk::Allocation& alloc)
93 {
94         ArdourWindow::on_size_allocate (alloc);
95
96         if (!resize_in_progress) {
97                 Glib::signal_idle().connect (sigc::bind (sigc::mem_fun (*this, &BigClockWindow::text_resizer), 0, 0));
98                 resize_in_progress = true;
99         }
100 }
101
102 void
103 BigClockWindow::reset_aspect_ratio ()
104 {
105         Gtk::Requisition req;
106
107         clock.size_request (req);
108
109         float aspect = req.width/(float)req.height;
110         Gdk::Geometry geom;
111
112         geom.min_aspect = aspect;
113         geom.max_aspect = aspect;
114         
115         set_geometry_hints (clock, geom, Gdk::HINT_ASPECT);
116 }
117
118 bool
119 BigClockWindow::text_resizer (int, int)
120 {
121         resize_in_progress = false;
122
123         Glib::RefPtr<Gdk::Window> win = get_window();
124         Pango::FontDescription fd (clock.get_style()->get_font());
125         int current_size = fd.get_size ();
126         int x, y, w, h, d;
127
128         if (!fd.get_size_is_absolute ()) {
129                 current_size /= PANGO_SCALE;
130         }
131
132         win->get_geometry (x, y, w, h, d);
133
134         double scale  = min (((double) w / (double) original_width),
135                              ((double) h / (double) original_height));
136
137         int size = (int) lrintf (original_font_size * scale);
138
139         if (size != current_size) {
140
141                 string family = fd.get_family();
142                 std::vector<char> buf(family.length()+16);
143                 snprintf (&buf[0], family.length()+16, "%s %d", family.c_str(), size);
144
145                 try {
146                         Pango::FontDescription fd (&buf[0]);
147                         Glib::RefPtr<Gtk::RcStyle> rcstyle = clock.get_modifier_style ();
148                         rcstyle->set_font (fd);
149                         clock.modify_style (rcstyle);
150                 }
151
152                 catch (...) {
153                         /* oh well, do nothing */
154                 }
155         }
156
157         return false;
158 }
159