Factor out 'persistent' tooltip code from the panner
[ardour.git] / libs / gtkmm2ext / persistent_tooltip.cc
1 /*
2     Copyright (C) 2012 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 <gtkmm/window.h>
21 #include <gtkmm/label.h>
22 #include "gtkmm2ext/persistent_tooltip.h"
23
24 #include "i18n.h"
25
26 using namespace std;
27 using namespace Gtk;
28 using namespace Gtkmm2ext;
29
30 /** @param target The widget to provide the tooltip for */
31 PersistentTooltip::PersistentTooltip (Gtk::Widget* target)
32         : _target (target)
33         , _window (0)
34         , _label (0)
35         , _maybe_dragging (false)
36 {
37         target->signal_enter_notify_event().connect (sigc::mem_fun (*this, &PersistentTooltip::enter), false);
38         target->signal_leave_notify_event().connect (sigc::mem_fun (*this, &PersistentTooltip::leave), false);
39         target->signal_button_press_event().connect (sigc::mem_fun (*this, &PersistentTooltip::press), false);
40         target->signal_button_release_event().connect (sigc::mem_fun (*this, &PersistentTooltip::release), false);
41 }
42
43 PersistentTooltip::~PersistentTooltip ()
44 {
45         delete _window;
46 }
47
48 bool
49 PersistentTooltip::enter (GdkEventCrossing *)
50 {
51         _timeout = Glib::signal_timeout().connect (sigc::mem_fun (*this, &PersistentTooltip::timeout), 500);
52         return false;
53 }
54
55 bool
56 PersistentTooltip::timeout ()
57 {
58         show ();
59         return false;
60 }
61
62 bool
63 PersistentTooltip::leave (GdkEventCrossing *)
64 {
65         _timeout.disconnect ();
66         if (!dragging ()) {
67                 hide ();
68         }
69
70         return false;
71 }
72
73 bool
74 PersistentTooltip::press (GdkEventButton* ev)
75 {
76         if (ev->type == GDK_BUTTON_PRESS && ev->button == 1) {
77                 _maybe_dragging = true;
78         }
79
80         return false;
81 }
82
83 bool
84 PersistentTooltip::release (GdkEventButton* ev)
85 {
86         if (ev->type == GDK_BUTTON_RELEASE && ev->button == 1) {
87                 _maybe_dragging = false;
88         }
89
90         return false;
91 }
92
93 bool
94 PersistentTooltip::dragging () const
95 {
96         return _maybe_dragging;
97 }
98
99 void
100 PersistentTooltip::hide ()
101 {
102         if (_window) {
103                 _window->hide ();
104         }
105 }
106
107 void
108 PersistentTooltip::show ()
109 {
110         if (!_window) {
111                 _window = new Window (WINDOW_POPUP);
112                 _window->set_name (X_("ContrastingPopup"));
113                 _window->set_position (WIN_POS_MOUSE);
114                 _window->set_decorated (false);
115
116                 _label = manage (new Label);
117                 _label->set_use_markup (true);
118
119                 _window->set_border_width (6);
120                 _window->add (*_label);
121                 _label->show ();
122
123                 Gtk::Window* tlw = dynamic_cast<Gtk::Window*> (_target->get_toplevel ());
124                 if (tlw) {
125                         _window->set_transient_for (*tlw);
126                 }
127         }
128         
129         set_tip (_tip);
130
131         if (!_window->is_visible ()) {
132                 /* move the window a little away from the mouse */
133                 int rx, ry;
134                 _target->get_window()->get_origin (rx, ry);
135                 _window->move (rx, ry + _target->get_height());
136                 _window->present ();
137         }
138 }
139
140 void
141 PersistentTooltip::set_tip (string t)
142 {
143         _tip = t;
144
145         if (_label) {
146                 _label->set_markup (t);
147         }
148 }