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