more changes to broken-out tempo code
[ardour.git] / libs / widgets / popup.cc
1 /*
2     Copyright (C) 1998-99 Paul Barton-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     $Id$
19 */
20
21 #include <iostream>
22
23 #include <gtkmm2ext/gtk_ui.h>
24 #include <gtkmm2ext/utils.h>
25
26 #include <widgets/popup.h>
27
28 using namespace std;
29 using namespace Gtk;
30 using namespace ArdourWidgets;
31
32 PopUp::PopUp (Gtk::WindowPosition pos, unsigned int showfor_msecs, bool doh)
33         : Window (WINDOW_POPUP)
34 {
35         add_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK);
36         signal_button_press_event().connect(mem_fun(*this,&PopUp::button_click));
37         set_border_width (12);
38         add (label);
39         set_position (pos);
40
41         delete_on_hide = doh;
42         popdown_time = showfor_msecs;
43         timeout = -1;
44 }
45
46
47 PopUp::~PopUp ()
48 {
49 }
50
51 void
52 PopUp::on_realize ()
53 {
54         Gtk::Window::on_realize();
55         get_window()->set_decorations (Gdk::WMDecoration (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH));
56 }
57
58 gint
59 PopUp::remove_prompt_timeout (void *arg)
60 {
61         PopUp *pup = (PopUp *) arg;
62
63         pup->remove ();
64         return FALSE;
65 }
66
67 static gint idle_delete (void *arg)
68 {
69         delete static_cast<PopUp*> (arg);
70         return FALSE;
71 }
72
73 void
74 PopUp::remove ()
75 {
76         hide ();
77
78         if (popdown_time != 0 && timeout != -1) {
79                 g_source_remove (timeout);
80         }
81
82         if (delete_on_hide) {
83                 std::cerr << "deleting prompter\n";
84                 g_idle_add (idle_delete, this);
85         }
86 }
87 #define ENSURE_GUI_THREAD(slot) \
88      if (!Gtkmm2ext::UI::instance()->caller_is_ui_thread()) {\
89              Gtkmm2ext::UI::instance()->call_slot (MISSING_INVALIDATOR, (slot)); \
90         return;\
91      }
92
93
94 void
95 PopUp::touch ()
96 {
97         ENSURE_GUI_THREAD (mem_fun (*this, &PopUp::touch));
98
99         if (is_visible ()) {
100                 remove ();
101         } else {
102                 Gtkmm2ext::set_size_request_to_display_given_text (label, my_text.c_str(), 25, 10);
103                 label.set_text (my_text);
104                 show_all ();
105
106                 if (popdown_time != 0) {
107                         timeout = g_timeout_add (popdown_time,
108                                                    remove_prompt_timeout,
109                                                    this);
110                 }
111         }
112 }
113
114 gint
115 PopUp::button_click (GdkEventButton* /*ev*/)
116 {
117         remove ();
118         return TRUE;
119 }
120
121 void
122 PopUp::set_text (string txt)
123 {
124         my_text = txt;
125 }
126
127 void
128 PopUp::set_name (string name)
129 {
130         Window::set_name (name);
131         label.set_name (name);
132 }
133
134 bool
135 PopUp::on_delete_event (GdkEventAny* /*ev*/)
136 {
137         hide();
138
139         if (popdown_time != 0 && timeout != -1) {
140                 g_source_remove (timeout);
141         }
142
143         if (delete_on_hide) {
144                 std::cerr << "deleting prompter\n" << endl;
145                 g_idle_add (idle_delete, this);
146         }
147
148         return true;
149 }