more changes to broken-out tempo code
[ardour.git] / libs / widgets / click_box.cc
1 /*
2     Copyright (C) 1999 Paul Barton-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     $Id$
19 */
20
21 #include <iostream>
22 #include <cstdio> /* for sprintf, sigh ... */
23
24 #include "pbd/controllable.h"
25 #include "gtkmm2ext/utils.h"
26 #include "widgets/click_box.h"
27
28 using namespace std;
29 using namespace Gtk;
30 using namespace ArdourWidgets;
31 using namespace sigc;
32
33 ClickBox::ClickBox (Gtk::Adjustment *adjp, const string &name, bool round_to_steps)
34         : AutoSpin (*adjp,0,round_to_steps)
35 {
36         layout = create_pango_layout ("");
37         twidth = 0;
38         theight = 0;
39
40
41         add_events (Gdk::BUTTON_RELEASE_MASK|
42                     Gdk::BUTTON_PRESS_MASK|
43                     Gdk::ENTER_NOTIFY_MASK|
44                     Gdk::LEAVE_NOTIFY_MASK);
45
46         get_adjustment().signal_value_changed().connect (mem_fun (*this, &ClickBox::set_label));
47         signal_style_changed().connect (mem_fun (*this, &ClickBox::style_changed));
48         signal_button_press_event().connect (mem_fun (*this, &ClickBox::button_press_handler));
49         signal_button_release_event().connect (mem_fun (*this, &ClickBox::button_release_handler));
50         set_name (name);
51         set_label ();
52 }
53
54 ClickBox::~ClickBox ()
55 {
56 }
57
58 bool
59 ClickBox::button_press_handler (GdkEventButton* ev)
60 {
61         if (_binding_proxy.button_press_handler (ev)) {
62                 return true;
63         }
64         add_modal_grab();
65         AutoSpin::button_press (ev);
66         return true;
67 }
68
69 bool
70 ClickBox::on_scroll_event (GdkEventScroll* ev)
71 {
72         AutoSpin::scroll_event (ev);
73         return true;
74 }
75
76 bool
77 ClickBox::button_release_handler (GdkEventButton* ev)
78 {
79         switch (ev->button) {
80         case 1:
81         case 2:
82         case 3:
83                 stop_spinning (0);
84         default:
85                 remove_modal_grab();
86                 break;
87         }
88         return true;
89 }
90
91 void
92 ClickBox::set_label ()
93 {
94         char buf[32];
95         int width, height;
96
97         bool const h = _printer (buf, get_adjustment());
98         if (!h) {
99                 /* the printer didn't handle it, so use a default */
100                 sprintf (buf, "%.2f", get_adjustment().get_value ());
101         }
102
103         layout->set_text (buf);
104         layout->get_pixel_size (width, height);
105
106         if (twidth < width && (width > 50))  {
107                 /* override GenericPluginUI::build_control_ui()
108                  * Gtkmm2ext::set_size_request_to_display_given_text ("g9999999")
109                  * see http://tracker.ardour.org/view.php?id=6499
110                  */
111                 set_size_request (std::min (300, width + 6), height + 4);
112         }
113
114         twidth = width; theight = height;
115
116         queue_draw ();
117 }
118
119 void
120 ClickBox::style_changed (const Glib::RefPtr<Gtk::Style>&)
121 {
122         layout->context_changed ();
123         layout->get_pixel_size (twidth, theight);
124 }
125
126 bool
127 ClickBox::on_expose_event (GdkEventExpose *ev)
128 {
129         /* Why do we do things like this rather than use a Gtk::Label?
130            Because whenever Gtk::Label::set_label() is called, it
131            triggers a recomputation of its own size, along with that
132            of its container and on up the tree. That's intended
133            to be unnecessary here.
134         */
135
136         Gtk::DrawingArea::on_expose_event (ev);
137
138         Glib::RefPtr<Gtk::Style> style (get_style());
139         Glib::RefPtr<Gdk::GC> fg_gc (style->get_fg_gc (Gtk::STATE_NORMAL));
140         Glib::RefPtr<Gdk::GC> bg_gc (style->get_bg_gc (Gtk::STATE_NORMAL));
141         Glib::RefPtr<Gdk::Window> win (get_window());
142
143         GdkRectangle base_rect;
144         GdkRectangle draw_rect;
145         gint x, y, width, height, depth;
146
147         win->get_geometry (x, y, width, height, depth);
148
149         base_rect.width = width;
150         base_rect.height = height;
151         base_rect.x = 0;
152         base_rect.y = 0;
153
154         gdk_rectangle_intersect (&ev->area, &base_rect, &draw_rect);
155         win->draw_rectangle (bg_gc, true, draw_rect.x, draw_rect.y, draw_rect.width, draw_rect.height);
156
157         if (twidth && theight) {
158                 win->draw_layout (fg_gc, (width - twidth) / 2, (height - theight) / 2, layout);
159         }
160
161         return true;
162 }
163
164 void
165 ClickBox::set_printer (sigc::slot<bool, char *, Gtk::Adjustment &> p)
166 {
167         _printer = p;
168         set_label ();
169 }
170
171 bool
172 ClickBox::on_enter_notify_event (GdkEventCrossing* ev)
173 {
174         boost::shared_ptr<PBD::Controllable> c (_binding_proxy.get_controllable ());
175         if (c) {
176                 PBD::Controllable::GUIFocusChanged (boost::weak_ptr<PBD::Controllable> (c));
177         }
178         return false;
179 }
180
181 bool
182 ClickBox::on_leave_notify_event (GdkEventCrossing* ev)
183 {
184         if (_binding_proxy.get_controllable()) {
185                 PBD::Controllable::GUIFocusChanged (boost::weak_ptr<PBD::Controllable> ());
186         }
187         return false;
188 }