compiles and runs, but crashes ... duh
[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         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 (delete_on_hide) {
78                 std::cerr << "deleting prompter\n";
79                 if (popdown_time != 0 && timeout != -1) {
80                         gtk_timeout_remove (timeout);
81                 }
82                 gtk_idle_add (idle_delete, this);
83         }
84 }
85
86 void
87 PopUp::touch ()
88 {
89         if (is_visible ()) {
90                 remove ();
91         } else {
92                 set_size_request_to_display_given_text (label, my_text.c_str(), 25, 10);
93                 label.set_text (my_text);
94                 show_all ();
95                 
96                 if (popdown_time != 0) {
97                         timeout = gtk_timeout_add (popdown_time, 
98                                                    remove_prompt_timeout, 
99                                                    this);
100                 }
101         }
102 }
103
104 gint
105 PopUp::button_click (GdkEventButton *ev)
106 {
107         remove ();
108         return TRUE;
109 }
110
111 void
112 PopUp::set_text (string txt)
113 {
114         my_text = txt;
115 }
116
117 void
118 PopUp::set_name (string name)
119 {
120         Window::set_name (name);
121         label.set_name (name);
122 }
123
124 bool
125 PopUp::on_delete_event (GdkEventAny* ev)
126 {
127         hide();
128
129         if (delete_on_hide) {
130                 std::cerr << "deleting prompter\n" << endl;
131                 if (popdown_time != 0 && timeout != -1) {
132                         gtk_timeout_remove (timeout);
133                 }
134                 gtk_idle_add (idle_delete, this);
135         }
136
137         return true;
138 }