Re-synced and fixed doi.h.
[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         print_func = default_printer;
36         print_arg = 0;
37
38         set_name (name);
39         add_events (Gdk::BUTTON_RELEASE_MASK|
40                     Gdk::BUTTON_PRESS_MASK|
41                     Gdk::ENTER_NOTIFY_MASK|
42                     Gdk::LEAVE_NOTIFY_MASK);
43         set_label ();
44
45         get_adjustment().signal_value_changed().connect (mem_fun (*this, &ClickBox::set_label));
46
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 }
50
51 ClickBox::~ClickBox ()
52 {
53 }
54
55 bool
56 ClickBox::button_press_handler (GdkEventButton* ev)
57 {
58         add_modal_grab();
59         AutoSpin::button_press (ev);
60         return true;
61 }
62
63 bool
64 ClickBox::button_release_handler (GdkEventButton* ev)
65 {
66         switch (ev->button) {
67         case 1:
68         case 2:
69         case 3:
70                 stop_spinning (0);
71         default:
72                 remove_modal_grab();
73                 break;
74         }
75         return true;
76 }
77
78 void
79 ClickBox::default_printer (char buf[32], Gtk::Adjustment &adj, 
80                                void *ignored)
81 {
82         sprintf (buf, "%.2f", adj.get_value());
83 }
84
85 void
86 ClickBox::set_label ()
87 {
88         queue_draw ();
89 }
90
91 bool
92 ClickBox::on_expose_event (GdkEventExpose *ev)
93 {
94         /* Why do we do things like this rather than use a Gtk::Label?
95            Because whenever Gtk::Label::set_label() is called, it
96            triggers a recomputation of its own size, along with that
97            of its container and on up the tree. That's intended
98            to be unnecessary here.
99         */
100
101         Gtk::DrawingArea::on_expose_event (ev);
102
103         if (print_func) {
104
105                 char buf[32];
106
107                 Glib::RefPtr<Gtk::Style> style (get_style());
108
109                 print_func (buf, get_adjustment(), print_arg);
110
111                 Glib::RefPtr <Gdk::Window> win (get_window());
112                 win->draw_rectangle (style->get_bg_gc(get_state()),
113                                      TRUE, 0, 0, -1, -1);
114
115                 {
116                         int width = 0;
117                         int height = 0;
118                         create_pango_layout(buf)->get_pixel_size(width, height);
119                         set_size_request(width, height);
120                 }
121         }
122
123         return true;
124 }