fixed return types for ::on_....; changed set_usize_... utility functions to set_size...
[ardour.git] / libs / gtkmm2ext / 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/popup.h>
24 #include <gtkmm2ext/utils.h>
25
26 using namespace std;
27 using namespace Gtk;
28 using namespace Gtkmm2ext;
29
30 PopUp::PopUp (Gtk::WindowPosition pos, unsigned int showfor_msecs, bool doh)
31         : Window (WINDOW_POPUP)
32 {
33
34         realize ();
35         Glib::RefPtr<Gdk::Window> win (get_window());
36         win->set_decorations (Gdk::WMDecoration (Gdk::DECOR_BORDER|Gdk::DECOR_RESIZEH));
37         add_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK);
38         signal_button_press_event().connect(mem_fun(*this,&PopUp::button_click));
39         set_border_width (12);
40         add (label);
41         set_position (pos);
42
43         delete_on_hide = doh;
44         popdown_time = showfor_msecs;
45         timeout = -1;
46 }
47
48
49 PopUp::~PopUp ()
50 {
51 }
52
53 gint
54 PopUp::remove_prompt_timeout (void *arg)
55 {
56         PopUp *pup = (PopUp *) arg;
57
58         pup->remove ();
59         return FALSE;
60 }
61
62 static gint idle_delete (void *arg)
63 {
64         delete static_cast<PopUp*> (arg);
65         return FALSE;
66 }
67
68 void
69 PopUp::remove ()
70 {
71         hide ();
72
73         if (delete_on_hide) {
74                 std::cerr << "deleting prompter\n";
75                 if (popdown_time != 0 && timeout != -1) {
76                         gtk_timeout_remove (timeout);
77                 }
78                 gtk_idle_add (idle_delete, this);
79         }
80 }
81
82 void
83 PopUp::touch ()
84 {
85         if (is_visible ()) {
86                 remove ();
87         } else {
88                 set_size_request_to_display_given_text (label, my_text.c_str(), 25, 10);
89                 label.set_text (my_text);
90                 show_all ();
91                 
92                 if (popdown_time != 0) {
93                         timeout = gtk_timeout_add (popdown_time, 
94                                                    remove_prompt_timeout, 
95                                                    this);
96                 }
97         }
98 }
99
100 gint
101 PopUp::button_click (GdkEventButton *ev)
102 {
103         remove ();
104         return TRUE;
105 }
106
107 void
108 PopUp::set_text (string txt)
109 {
110         my_text = txt;
111 }
112
113 void
114 PopUp::set_name (string name)
115 {
116         Window::set_name (name);
117         label.set_name (name);
118 }
119
120 bool
121 PopUp::on_delete_event (GdkEventAny* ev)
122 {
123         hide();
124
125         if (delete_on_hide) {
126                 std::cerr << "deleting prompter\n" << endl;
127                 if (popdown_time != 0 && timeout != -1) {
128                         gtk_timeout_remove (timeout);
129                 }
130                 gtk_idle_add (idle_delete, this);
131         }
132
133         return true;
134 }