2 * Copyright (C) 2012-2016 Robin Gareus <robin@gareus.org>
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.
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.
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.
19 #include <gtkmm/window.h>
20 #include <gtkmm/label.h>
21 #include <gtkmm/settings.h>
22 #include "gtkmm2ext/persistent_tooltip.h"
24 #include "pbd/stacktrace.h"
30 using namespace Gtkmm2ext;
32 bool PersistentTooltip::_tooltips_enabled = true;
33 unsigned int PersistentTooltip::_tooltip_timeout = 500;
35 /** @param target The widget to provide the tooltip for */
36 PersistentTooltip::PersistentTooltip (Gtk::Widget* target, bool draggable, int margin_y)
40 , _draggable (draggable)
41 , _maybe_dragging (false)
42 , _align_to_center (true)
43 , _margin_y (margin_y)
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 ();
52 PersistentTooltip::~PersistentTooltip ()
58 PersistentTooltip::enter (GdkEventCrossing *)
60 if (_timeout.connected()) {
63 _timeout = Glib::signal_timeout().connect (sigc::mem_fun (*this, &PersistentTooltip::timeout), _tooltip_timeout);
68 PersistentTooltip::timeout ()
75 PersistentTooltip::leave (GdkEventCrossing *)
77 _timeout.disconnect ();
86 PersistentTooltip::press (GdkEventButton* ev)
88 if (ev->type == GDK_BUTTON_PRESS && ev->button == 1) {
89 _maybe_dragging = true;
96 PersistentTooltip::release (GdkEventButton* ev)
98 if (ev->type == GDK_BUTTON_RELEASE && ev->button == 1) {
99 _maybe_dragging = false;
106 PersistentTooltip::dragging () const
108 return _maybe_dragging && _draggable;
112 PersistentTooltip::hide ()
120 PersistentTooltip::show ()
122 if (_tip.empty() || !_tooltips_enabled) {
127 _window = new Window (WINDOW_POPUP);
128 _window->set_name (X_("ContrastingPopup"));
129 _window->set_position (WIN_POS_MOUSE);
130 _window->set_decorated (false);
132 _label = manage (new Label);
133 _label->modify_font (_font);
134 _label->set_use_markup (true);
136 _window->set_border_width (6);
137 _window->add (*_label);
140 Gtk::Window* tlw = dynamic_cast<Gtk::Window*> (_target->get_toplevel ());
142 _window->set_transient_for (*tlw);
148 if (!_window->is_visible ()) {
150 int sw = gdk_screen_width ();
152 _target->get_window()->get_origin (rx, ry);
154 /* the window needs to be realized first
155 * for _window->get_width() to be correct.
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.
163 rx = sw - _window->get_width();
164 _window->move (rx, ry + _target->get_height() + _margin_y);
166 if (_align_to_center) {
167 _window->move (rx + (_target->get_width () - _window->get_width ()) / 2, ry + _target->get_height());
169 _window->move (rx, ry + _target->get_height());
179 PersistentTooltip::set_tip (string t)
184 _label->set_markup (t);
189 PersistentTooltip::set_font (Pango::FontDescription font)
194 _label->modify_font (_font);
199 PersistentTooltip::set_center_alignment (bool align_to_center)
201 _align_to_center = align_to_center;