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