61eb4884f5b236a4c54fa59c580e8237b205c398
[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, int margin_y)
34         : _target (target)
35         , _window (0)
36         , _label (0)
37         , _maybe_dragging (false)
38         , _margin_y (margin_y)
39 {
40         target->signal_enter_notify_event().connect (sigc::mem_fun (*this, &PersistentTooltip::enter), false);
41         target->signal_leave_notify_event().connect (sigc::mem_fun (*this, &PersistentTooltip::leave), false);
42         target->signal_button_press_event().connect (sigc::mem_fun (*this, &PersistentTooltip::press), false);
43         target->signal_button_release_event().connect (sigc::mem_fun (*this, &PersistentTooltip::release), false);
44 }
45
46 PersistentTooltip::~PersistentTooltip ()
47 {
48         delete _window;
49 }
50
51 bool
52 PersistentTooltip::enter (GdkEventCrossing *)
53 {
54         if (_timeout.connected()) {
55                 leave(NULL);
56         }
57         _timeout = Glib::signal_timeout().connect (sigc::mem_fun (*this, &PersistentTooltip::timeout), 500);
58         return false;
59 }
60
61 bool
62 PersistentTooltip::timeout ()
63 {
64         show ();
65         return true;
66 }
67
68 bool
69 PersistentTooltip::leave (GdkEventCrossing *)
70 {
71         _timeout.disconnect ();
72         if (!dragging ()) {
73                 hide ();
74         }
75
76         return false;
77 }
78
79 bool
80 PersistentTooltip::press (GdkEventButton* ev)
81 {
82         if (ev->type == GDK_BUTTON_PRESS && ev->button == 1) {
83                 _maybe_dragging = true;
84         }
85
86         return false;
87 }
88
89 bool
90 PersistentTooltip::release (GdkEventButton* ev)
91 {
92         if (ev->type == GDK_BUTTON_RELEASE && ev->button == 1) {
93                 _maybe_dragging = false;
94         }
95
96         return false;
97 }
98
99 bool
100 PersistentTooltip::dragging () const
101 {
102         return _maybe_dragging;
103 }
104
105 void
106 PersistentTooltip::hide ()
107 {
108         if (_window) {
109                 _window->hide ();
110         }
111 }
112
113 void
114 PersistentTooltip::show ()
115 {
116         if (_tip.empty()) {
117                 return;
118         }
119
120         if (!_window) {
121                 _window = new Window (WINDOW_POPUP);
122                 _window->set_name (X_("ContrastingPopup"));
123                 _window->set_position (WIN_POS_MOUSE);
124                 _window->set_decorated (false);
125
126                 _label = manage (new Label);
127                 _label->set_use_markup (true);
128
129                 _window->set_border_width (6);
130                 _window->add (*_label);
131                 _label->show ();
132
133                 Gtk::Window* tlw = dynamic_cast<Gtk::Window*> (_target->get_toplevel ());
134                 if (tlw) {
135                         _window->set_transient_for (*tlw);
136                 }
137         }
138         
139         set_tip (_tip);
140
141         if (!_window->is_visible ()) {
142                 int rx, ry;
143                 int sw = gdk_screen_width();
144
145                 _target->get_window()->get_origin (rx, ry);
146                 
147                 /* the window needs to be realized first
148                  * for _window->get_width() to be correct.
149                  */
150
151                 _window->present ();
152
153                 if (sw < rx + _window->get_width()) {
154                         rx = sw - _window->get_width();
155                         _window->move (rx, ry + _target->get_height());
156                 } else {
157                         _window->move (rx + (_target->get_width () - _window->get_width ()) / 2, ry + _target->get_height());
158                 }
159         }
160 }
161
162 void
163 PersistentTooltip::set_tip (string t)
164 {
165         _tip = t;
166
167         if (_label) {
168                 _label->set_markup (t);
169         }
170 }