2012-06-11 17:21:10 -04:00
|
|
|
/*
|
|
|
|
Copyright (C) 2012 Paul Davis
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <gtkmm/window.h>
|
|
|
|
#include <gtkmm/label.h>
|
|
|
|
#include "gtkmm2ext/persistent_tooltip.h"
|
|
|
|
|
2015-03-25 04:55:55 -04:00
|
|
|
#include "pbd/stacktrace.h"
|
|
|
|
|
2012-06-11 17:21:10 -04:00
|
|
|
#include "i18n.h"
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace Gtk;
|
|
|
|
using namespace Gtkmm2ext;
|
|
|
|
|
|
|
|
/** @param target The widget to provide the tooltip for */
|
2015-04-24 19:16:39 -04:00
|
|
|
PersistentTooltip::PersistentTooltip (Gtk::Widget* target, bool draggable, int margin_y)
|
2012-06-11 17:21:10 -04:00
|
|
|
: _target (target)
|
|
|
|
, _window (0)
|
|
|
|
, _label (0)
|
2015-04-27 11:32:00 -04:00
|
|
|
, _draggable (draggable)
|
2012-06-11 17:21:10 -04:00
|
|
|
, _maybe_dragging (false)
|
2015-04-27 11:32:00 -04:00
|
|
|
, _align_to_center (true)
|
2015-04-24 19:16:39 -04:00
|
|
|
, _margin_y (margin_y)
|
2012-06-11 17:21:10 -04:00
|
|
|
{
|
|
|
|
target->signal_enter_notify_event().connect (sigc::mem_fun (*this, &PersistentTooltip::enter), false);
|
|
|
|
target->signal_leave_notify_event().connect (sigc::mem_fun (*this, &PersistentTooltip::leave), false);
|
|
|
|
target->signal_button_press_event().connect (sigc::mem_fun (*this, &PersistentTooltip::press), false);
|
|
|
|
target->signal_button_release_event().connect (sigc::mem_fun (*this, &PersistentTooltip::release), false);
|
|
|
|
}
|
|
|
|
|
|
|
|
PersistentTooltip::~PersistentTooltip ()
|
|
|
|
{
|
|
|
|
delete _window;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
PersistentTooltip::enter (GdkEventCrossing *)
|
|
|
|
{
|
2013-07-16 16:44:54 -04:00
|
|
|
if (_timeout.connected()) {
|
|
|
|
leave(NULL);
|
|
|
|
}
|
2012-06-11 17:21:10 -04:00
|
|
|
_timeout = Glib::signal_timeout().connect (sigc::mem_fun (*this, &PersistentTooltip::timeout), 500);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
PersistentTooltip::timeout ()
|
|
|
|
{
|
|
|
|
show ();
|
2015-03-25 04:55:55 -04:00
|
|
|
return true;
|
2012-06-11 17:21:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
PersistentTooltip::leave (GdkEventCrossing *)
|
|
|
|
{
|
2015-04-24 21:00:57 -04:00
|
|
|
_timeout.disconnect ();
|
2012-06-11 17:21:10 -04:00
|
|
|
if (!dragging ()) {
|
|
|
|
hide ();
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
PersistentTooltip::press (GdkEventButton* ev)
|
|
|
|
{
|
|
|
|
if (ev->type == GDK_BUTTON_PRESS && ev->button == 1) {
|
|
|
|
_maybe_dragging = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
PersistentTooltip::release (GdkEventButton* ev)
|
|
|
|
{
|
|
|
|
if (ev->type == GDK_BUTTON_RELEASE && ev->button == 1) {
|
|
|
|
_maybe_dragging = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
PersistentTooltip::dragging () const
|
|
|
|
{
|
2015-04-27 11:32:00 -04:00
|
|
|
return _maybe_dragging && _draggable;
|
2012-06-11 17:21:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PersistentTooltip::hide ()
|
|
|
|
{
|
|
|
|
if (_window) {
|
|
|
|
_window->hide ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PersistentTooltip::show ()
|
|
|
|
{
|
2015-04-24 19:02:53 -04:00
|
|
|
if (_tip.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
2015-03-25 04:55:55 -04:00
|
|
|
|
2012-06-11 17:21:10 -04:00
|
|
|
if (!_window) {
|
|
|
|
_window = new Window (WINDOW_POPUP);
|
|
|
|
_window->set_name (X_("ContrastingPopup"));
|
|
|
|
_window->set_position (WIN_POS_MOUSE);
|
|
|
|
_window->set_decorated (false);
|
|
|
|
|
|
|
|
_label = manage (new Label);
|
2015-04-27 11:32:00 -04:00
|
|
|
_label->modify_font (_font);
|
2012-06-11 17:21:10 -04:00
|
|
|
_label->set_use_markup (true);
|
|
|
|
|
|
|
|
_window->set_border_width (6);
|
|
|
|
_window->add (*_label);
|
|
|
|
_label->show ();
|
|
|
|
|
|
|
|
Gtk::Window* tlw = dynamic_cast<Gtk::Window*> (_target->get_toplevel ());
|
|
|
|
if (tlw) {
|
|
|
|
_window->set_transient_for (*tlw);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
set_tip (_tip);
|
|
|
|
|
2012-12-25 17:23:13 -05:00
|
|
|
if (!_window->is_visible ()) {
|
2015-03-25 04:55:55 -04:00
|
|
|
int rx, ry;
|
2015-04-27 11:32:00 -04:00
|
|
|
int sw = gdk_screen_width ();
|
2012-12-25 17:23:13 -05:00
|
|
|
|
2015-03-25 04:55:55 -04:00
|
|
|
_target->get_window()->get_origin (rx, ry);
|
|
|
|
|
2012-12-25 17:23:13 -05:00
|
|
|
/* the window needs to be realized first
|
|
|
|
* for _window->get_width() to be correct.
|
|
|
|
*/
|
2015-03-25 04:55:55 -04:00
|
|
|
|
|
|
|
_window->present ();
|
|
|
|
|
2012-12-25 17:23:13 -05:00
|
|
|
if (sw < rx + _window->get_width()) {
|
2015-04-27 11:32:00 -04:00
|
|
|
/* right edge of window would be off the right edge of
|
|
|
|
the screen, so don't show it in the usual place.
|
|
|
|
*/
|
2012-12-25 17:23:13 -05:00
|
|
|
rx = sw - _window->get_width();
|
2015-04-24 19:16:39 -04:00
|
|
|
_window->move (rx, ry + _target->get_height() + _margin_y);
|
2015-03-25 04:55:55 -04:00
|
|
|
} else {
|
2015-04-27 11:32:00 -04:00
|
|
|
if (_align_to_center) {
|
|
|
|
_window->move (rx + (_target->get_width () - _window->get_width ()) / 2, ry + _target->get_height());
|
|
|
|
} else {
|
|
|
|
_window->move (rx, ry + _target->get_height());
|
|
|
|
}
|
2012-12-25 17:23:13 -05:00
|
|
|
}
|
|
|
|
}
|
2012-06-11 17:21:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PersistentTooltip::set_tip (string t)
|
|
|
|
{
|
|
|
|
_tip = t;
|
|
|
|
|
|
|
|
if (_label) {
|
|
|
|
_label->set_markup (t);
|
|
|
|
}
|
|
|
|
}
|
2015-04-27 11:32:00 -04:00
|
|
|
|
|
|
|
void
|
|
|
|
PersistentTooltip::set_font (Pango::FontDescription font)
|
|
|
|
{
|
|
|
|
_font = font;
|
|
|
|
|
|
|
|
if (_label) {
|
|
|
|
_label->modify_font (_font);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PersistentTooltip::set_center_alignment (bool align_to_center)
|
|
|
|
{
|
|
|
|
_align_to_center = align_to_center;
|
|
|
|
}
|