again, remove valgrind debugging hack
[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         layout = create_pango_layout ("");
38         twidth = 0;
39         theight = 0;
40
41         set_name (name);
42         add_events (Gdk::BUTTON_RELEASE_MASK|
43                     Gdk::BUTTON_PRESS_MASK|
44                     Gdk::ENTER_NOTIFY_MASK|
45                     Gdk::LEAVE_NOTIFY_MASK);
46         set_label ();
47
48         get_adjustment().signal_value_changed().connect (mem_fun (*this, &ClickBox::set_label));
49
50         signal_button_press_event().connect (mem_fun (*this, &ClickBox::button_press_handler));
51         signal_button_release_event().connect (mem_fun (*this, &ClickBox::button_release_handler));
52 }
53
54 ClickBox::~ClickBox ()
55 {
56 }
57
58 bool
59 ClickBox::button_press_handler (GdkEventButton* ev)
60 {
61         add_modal_grab();
62         AutoSpin::button_press (ev);
63         return true;
64 }
65
66 bool
67 ClickBox::button_release_handler (GdkEventButton* ev)
68 {
69         switch (ev->button) {
70         case 1:
71         case 2:
72         case 3:
73                 stop_spinning (0);
74         default:
75                 remove_modal_grab();
76                 break;
77         }
78         return true;
79 }
80
81 void
82 ClickBox::default_printer (char buf[32], Gtk::Adjustment &adj, 
83                                void *ignored)
84 {
85         sprintf (buf, "%.2f", adj.get_value());
86 }
87
88 void
89 ClickBox::set_label ()
90 {
91         if (!print_func) {
92                 return;
93         }
94
95         char buf[32];
96
97         print_func (buf, get_adjustment(), print_arg);
98
99         layout->set_text (buf);
100         layout->get_pixel_size (twidth, theight);
101
102         queue_draw ();
103 }
104
105 bool
106 ClickBox::on_expose_event (GdkEventExpose *ev)
107 {
108         /* Why do we do things like this rather than use a Gtk::Label?
109            Because whenever Gtk::Label::set_label() is called, it
110            triggers a recomputation of its own size, along with that
111            of its container and on up the tree. That's intended
112            to be unnecessary here.
113         */
114
115         Gtk::DrawingArea::on_expose_event (ev);
116
117         if (print_func) {
118
119                 Glib::RefPtr<Gtk::Style> style (get_style());
120                 Glib::RefPtr<Gdk::GC> fg_gc (style->get_fg_gc (Gtk::STATE_NORMAL));
121                 Glib::RefPtr<Gdk::GC> bg_gc (style->get_bg_gc (Gtk::STATE_NORMAL));
122                 Glib::RefPtr<Gdk::Window> win (get_window());
123                 
124                 GdkRectangle base_rect;
125                 GdkRectangle draw_rect;
126                 gint x, y, width, height, depth;
127                 
128                 win->get_geometry (x, y, width, height, depth);
129                 
130                 base_rect.width = width;
131                 base_rect.height = height;
132                 base_rect.x = 0;
133                 base_rect.y = 0;
134                 
135                 gdk_rectangle_intersect (&ev->area, &base_rect, &draw_rect);
136                 win->draw_rectangle (bg_gc, true, draw_rect.x, draw_rect.y, draw_rect.width, draw_rect.height);
137
138                 if (twidth && theight) {
139                         win->draw_layout (fg_gc, width - (twidth + 2), (height - theight) + 2, layout);
140                 }
141         }
142
143         return true;
144 }