prevent stuck & vanishing tooltips
[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 "pbd/stacktrace.h"
25
26 #include "i18n.h"
27
28 using namespace std;
29 using namespace Gtk;
30 using namespace Gtkmm2ext;
31
32 /** @param target The widget to provide the tooltip for */
33 PersistentTooltip::PersistentTooltip (Gtk::Widget* target, bool  draggable, int margin_y)
34         : _target (target)
35         , _window (0)
36         , _label (0)
37         , _draggable (draggable)
38         , _maybe_dragging (false)
39         , _align_to_center (true)
40         , _margin_y (margin_y)
41 {
42         target->signal_enter_notify_event().connect (sigc::mem_fun (*this, &PersistentTooltip::enter), false);
43         target->signal_leave_notify_event().connect (sigc::mem_fun (*this, &PersistentTooltip::leave), false);
44         target->signal_button_press_event().connect (sigc::mem_fun (*this, &PersistentTooltip::press), false);
45         target->signal_button_release_event().connect (sigc::mem_fun (*this, &PersistentTooltip::release), false);
46 }
47
48 PersistentTooltip::~PersistentTooltip ()
49 {
50         delete _window;
51 }
52
53 bool
54 PersistentTooltip::enter (GdkEventCrossing *)
55 {
56         if (_timeout.connected()) {
57                 leave(NULL);
58         }
59         _timeout = Glib::signal_timeout().connect (sigc::mem_fun (*this, &PersistentTooltip::timeout), 500);
60         return false;
61 }
62
63 bool
64 PersistentTooltip::timeout ()
65 {
66         show ();
67         return false;
68 }
69
70 bool
71 PersistentTooltip::leave (GdkEventCrossing *)
72 {
73         _timeout.disconnect ();
74         if (!dragging ()) {
75                 hide ();
76         }
77
78         return false;
79 }
80
81 bool
82 PersistentTooltip::press (GdkEventButton* ev)
83 {
84         if (ev->type == GDK_BUTTON_PRESS && ev->button == 1) {
85                 _maybe_dragging = true;
86         }
87
88         return false;
89 }
90
91 bool
92 PersistentTooltip::release (GdkEventButton* ev)
93 {
94         if (ev->type == GDK_BUTTON_RELEASE && ev->button == 1) {
95                 _maybe_dragging = false;
96         }
97
98         return false;
99 }
100
101 bool
102 PersistentTooltip::dragging () const
103 {
104         return _maybe_dragging && _draggable;
105 }
106
107 void
108 PersistentTooltip::hide ()
109 {
110         if (_window) {
111                 _window->hide ();
112         }
113 }
114
115 void
116 PersistentTooltip::show ()
117 {
118         if (_tip.empty()) {
119                 return;
120         }
121
122         if (!_window) {
123                 _window = new Window (WINDOW_POPUP);
124                 _window->set_name (X_("ContrastingPopup"));
125                 _window->set_position (WIN_POS_MOUSE);
126                 _window->set_decorated (false);
127
128                 _label = manage (new Label);
129                 _label->modify_font (_font);
130                 _label->set_use_markup (true);
131
132                 _window->set_border_width (6);
133                 _window->add (*_label);
134                 _label->show ();
135
136                 Gtk::Window* tlw = dynamic_cast<Gtk::Window*> (_target->get_toplevel ());
137                 if (tlw) {
138                         _window->set_transient_for (*tlw);
139                 }
140         }
141         
142         set_tip (_tip);
143
144         if (!_window->is_visible ()) {
145                 int rx, ry;
146                 int sw = gdk_screen_width ();
147
148                 _target->get_window()->get_origin (rx, ry);
149                 
150                 /* the window needs to be realized first
151                  * for _window->get_width() to be correct.
152                  */
153
154
155                 if (sw < rx + _window->get_width()) {
156                         /* right edge of window would be off the right edge of
157                            the screen, so don't show it in the usual place.
158                         */
159                         rx = sw - _window->get_width();
160                         _window->move (rx, ry + _target->get_height() + _margin_y);
161                 } else {
162                         if (_align_to_center) {
163                                 _window->move (rx + (_target->get_width () - _window->get_width ()) / 2, ry + _target->get_height());
164                         } else {
165                                 _window->move (rx, ry + _target->get_height());
166                         }
167                 }
168
169                 _window->present ();
170
171         }
172 }
173
174 void
175 PersistentTooltip::set_tip (string t)
176 {
177         _tip = t;
178
179         if (_label) {
180                 _label->set_markup (t);
181         }
182 }
183
184 void
185 PersistentTooltip::set_font (Pango::FontDescription font)
186 {
187         _font = font;
188
189         if (_label) {
190                 _label->modify_font (_font);
191         }
192 }
193
194 void
195 PersistentTooltip::set_center_alignment (bool align_to_center)
196 {
197         _align_to_center = align_to_center;
198 }