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