add another conditional to decide if we should merge disk MIDI data into input MIDI...
[ardour.git] / libs / widgets / popup.cc
1 /*
2  * Copyright (C) 1998 Paul Davis <paul@linuxaudiosystems.com>
3  * Copyright (C) 2017 Robin Gareus <robin@gareus.org>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19
20 #include <iostream>
21
22 #include <gtkmm2ext/gtk_ui.h>
23 #include <gtkmm2ext/utils.h>
24
25 #include <widgets/popup.h>
26
27 using namespace std;
28 using namespace Gtk;
29 using namespace ArdourWidgets;
30
31 PopUp::PopUp (Gtk::WindowPosition pos, unsigned int showfor_msecs, bool doh)
32         : Window (WINDOW_POPUP)
33 {
34         add_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK);
35         signal_button_press_event().connect(mem_fun(*this,&PopUp::button_click));
36         set_border_width (12);
37         add (label);
38         set_position (pos);
39
40         delete_on_hide = doh;
41         popdown_time = showfor_msecs;
42         timeout = -1;
43 }
44
45
46 PopUp::~PopUp ()
47 {
48 }
49
50 void
51 PopUp::on_realize ()
52 {
53         Gtk::Window::on_realize();
54         get_window()->set_decorations (Gdk::WMDecoration (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH));
55 }
56
57 gint
58 PopUp::remove_prompt_timeout (void *arg)
59 {
60         PopUp *pup = (PopUp *) arg;
61
62         pup->remove ();
63         return FALSE;
64 }
65
66 static gint idle_delete (void *arg)
67 {
68         delete static_cast<PopUp*> (arg);
69         return FALSE;
70 }
71
72 void
73 PopUp::remove ()
74 {
75         hide ();
76
77         if (popdown_time != 0 && timeout != -1) {
78                 g_source_remove (timeout);
79         }
80
81         if (delete_on_hide) {
82                 std::cerr << "deleting prompter\n";
83                 g_idle_add (idle_delete, this);
84         }
85 }
86 #define ENSURE_GUI_THREAD(slot) \
87      if (!Gtkmm2ext::UI::instance()->caller_is_ui_thread()) {\
88              Gtkmm2ext::UI::instance()->call_slot (MISSING_INVALIDATOR, (slot)); \
89         return;\
90      }
91
92
93 void
94 PopUp::touch ()
95 {
96         ENSURE_GUI_THREAD (mem_fun (*this, &PopUp::touch));
97
98         if (is_visible ()) {
99                 remove ();
100         } else {
101                 Gtkmm2ext::set_size_request_to_display_given_text (label, my_text.c_str(), 25, 10);
102                 label.set_text (my_text);
103                 show_all ();
104
105                 if (popdown_time != 0) {
106                         timeout = g_timeout_add (popdown_time,
107                                                    remove_prompt_timeout,
108                                                    this);
109                 }
110         }
111 }
112
113 gint
114 PopUp::button_click (GdkEventButton* /*ev*/)
115 {
116         remove ();
117         return TRUE;
118 }
119
120 void
121 PopUp::set_text (string txt)
122 {
123         my_text = txt;
124 }
125
126 void
127 PopUp::set_name (string name)
128 {
129         Window::set_name (name);
130         label.set_name (name);
131 }
132
133 bool
134 PopUp::on_delete_event (GdkEventAny* /*ev*/)
135 {
136         hide();
137
138         if (popdown_time != 0 && timeout != -1) {
139                 g_source_remove (timeout);
140         }
141
142         if (delete_on_hide) {
143                 std::cerr << "deleting prompter\n" << endl;
144                 g_idle_add (idle_delete, this);
145         }
146
147         return true;
148 }