implement scroll-wheel support for ClickBoxes
[ardour.git] / libs / gtkmm2ext / 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 <gtkmm2ext/utils.h>
25 #include <gtkmm2ext/click_box.h>
26
27 using namespace std;
28 using namespace Gtk;
29 using namespace Gtkmm2ext;
30 using namespace sigc;
31
32 ClickBox::ClickBox (Gtk::Adjustment *adjp, const string &name, bool round_to_steps)
33         : AutoSpin (*adjp,0,round_to_steps)
34 {
35         layout = create_pango_layout ("");
36         twidth = 0;
37         theight = 0;
38
39
40         add_events (Gdk::BUTTON_RELEASE_MASK|
41                     Gdk::BUTTON_PRESS_MASK|
42                     Gdk::ENTER_NOTIFY_MASK|
43                     Gdk::LEAVE_NOTIFY_MASK);
44
45         get_adjustment().signal_value_changed().connect (mem_fun (*this, &ClickBox::set_label));
46         signal_style_changed().connect (mem_fun (*this, &ClickBox::style_changed));
47         signal_button_press_event().connect (mem_fun (*this, &ClickBox::button_press_handler));
48         signal_button_release_event().connect (mem_fun (*this, &ClickBox::button_release_handler));
49         set_name (name);
50         set_label ();
51 }
52
53 ClickBox::~ClickBox ()
54 {
55 }
56
57 bool
58 ClickBox::button_press_handler (GdkEventButton* ev)
59 {
60         add_modal_grab();
61         AutoSpin::button_press (ev);
62         return true;
63 }
64
65 bool
66 ClickBox::on_scroll_event (GdkEventScroll* ev)
67 {
68         AutoSpin::scroll_event (ev);
69         return true;
70 }
71
72 bool
73 ClickBox::button_release_handler (GdkEventButton* ev)
74 {
75         switch (ev->button) {
76         case 1:
77         case 2:
78         case 3:
79                 stop_spinning (0);
80         default:
81                 remove_modal_grab();
82                 break;
83         }
84         return true;
85 }
86
87 void
88 ClickBox::set_label ()
89 {
90         char buf[32];
91
92         bool const h = _printer (buf, get_adjustment());
93         if (!h) {
94                 /* the printer didn't handle it, so use a default */
95                 sprintf (buf, "%.2f", get_adjustment().get_value ());
96         }
97
98         layout->set_text (buf);
99         layout->get_pixel_size (twidth, theight);
100
101         queue_draw ();
102 }
103
104 void
105 ClickBox::style_changed (const Glib::RefPtr<Gtk::Style>&)
106 {
107         layout->context_changed (); 
108         layout->get_pixel_size (twidth, theight);
109 }
110
111 bool
112 ClickBox::on_expose_event (GdkEventExpose *ev)
113 {
114         /* Why do we do things like this rather than use a Gtk::Label?
115            Because whenever Gtk::Label::set_label() is called, it
116            triggers a recomputation of its own size, along with that
117            of its container and on up the tree. That's intended
118            to be unnecessary here.
119         */
120
121         Gtk::DrawingArea::on_expose_event (ev);
122
123         Glib::RefPtr<Gtk::Style> style (get_style());
124         Glib::RefPtr<Gdk::GC> fg_gc (style->get_fg_gc (Gtk::STATE_NORMAL));
125         Glib::RefPtr<Gdk::GC> bg_gc (style->get_bg_gc (Gtk::STATE_NORMAL));
126         Glib::RefPtr<Gdk::Window> win (get_window());
127         
128         GdkRectangle base_rect;
129         GdkRectangle draw_rect;
130         gint x, y, width, height, depth;
131         
132         win->get_geometry (x, y, width, height, depth);
133         
134         base_rect.width = width;
135         base_rect.height = height;
136         base_rect.x = 0;
137         base_rect.y = 0;
138         
139         gdk_rectangle_intersect (&ev->area, &base_rect, &draw_rect);
140         win->draw_rectangle (bg_gc, true, draw_rect.x, draw_rect.y, draw_rect.width, draw_rect.height);
141         
142         if (twidth && theight) {
143                 win->draw_layout (fg_gc, (width - twidth) / 2, (height - theight) / 2, layout);
144         }
145
146         return true;
147 }
148
149 void
150 ClickBox::set_printer (sigc::slot<bool, char *, Gtk::Adjustment &> p)
151 {
152         _printer = p;
153         set_label ();
154 }
155