add 0.5 second sleep after closing JACK connection so that next startup/connect is...
[ardour.git] / libs / gtkmm2ext / gtkmm2ext / gtk_ui.h
1 /*
2     Copyright (C) 1999 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 */
19
20 #ifndef __pbd_gtk_ui_h__
21 #define __pbd_gtk_ui_h__
22
23 #include <string>
24 #include <map>
25
26 #include <stdint.h>
27 #include <setjmp.h>
28 #include <pthread.h>
29
30 #include <glibmm/thread.h>
31
32 #include <gtkmm/widget.h>
33 #include <gtkmm/style.h>
34 #ifndef GTK_NEW_TOOLTIP_API
35 #include <gtkmm/tooltips.h>
36 #endif
37 #include <gtkmm/textbuffer.h>
38 #include <gtkmm/main.h>
39 #include <gdkmm/color.h>
40 #include <pbd/abstract_ui.h>
41 #include <pbd/ringbufferNPT.h>
42  
43 #include <pbd/pool.h>
44 #include <pbd/error.h>
45 #include <pbd/receiver.h>
46
47 class Touchable;
48
49 namespace Gtkmm2ext {
50
51 class TextViewer;
52
53 extern BaseUI::RequestType NullMessage;
54 extern BaseUI::RequestType ErrorMessage;
55 extern BaseUI::RequestType CallSlot;
56 extern BaseUI::RequestType TouchDisplay;
57 extern BaseUI::RequestType StateChange;
58 extern BaseUI::RequestType SetTip;
59 extern BaseUI::RequestType AddIdle;
60 extern BaseUI::RequestType AddTimeout;
61
62 struct UIRequest : public BaseUI::BaseRequestObject {
63      
64      /* this once used anonymous unions to merge elements
65         that are never part of the same request. that makes
66         the creation of a legal copy constructor difficult
67         because of the semantics of the slot member.
68      */
69      
70     Touchable *display;
71     const char *msg;
72     Gtk::StateType new_state;
73     int (*function)(void *);
74     Gtk::Widget *widget;
75     Transmitter::Channel chn;
76     void *arg;
77     const char *msg2;
78
79     UIRequest () {
80             type = NullMessage;
81     }
82     
83     ~UIRequest () { 
84             if (type == ErrorMessage && msg) {
85                     /* msg was strdup()'ed */
86                     free (const_cast<char *>(msg));
87             }
88     }
89 };
90
91 class UI : public AbstractUI<UIRequest>
92 {
93   private:
94         class MyReceiver : public Receiver {
95           public:
96                 MyReceiver (UI& ui) : _ui (ui) {}
97                 void receive (Transmitter::Channel chn, const char *msg) {
98                         _ui.receive (chn, msg);
99                 }
100           private:
101                 UI& _ui;
102         };
103
104         MyReceiver _receiver;
105
106   public:
107         UI (std::string name, int *argc, char **argv[]);
108         virtual ~UI ();
109
110         static UI *instance() { return theGtkUI; }
111
112         /* receiver interface */
113
114         void receive (Transmitter::Channel, const char *);
115
116         /* Abstract UI interfaces */
117
118         bool caller_is_ui_thread ();
119
120         /* Gtk-UI specific interfaces */
121
122         bool running ();
123         void quit    ();
124         int  load_rcfile (std::string, bool themechange = false);
125         void run (Receiver &old_receiver);
126
127         void set_state (Gtk::Widget *w, Gtk::StateType state);
128         void popup_error (const std::string& text);
129         void flush_pending ();
130         void toggle_errors ();
131         void show_errors ();
132         void touch_display (Touchable *);
133         void set_tip (Gtk::Widget &w, const gchar *tip);
134         void set_tip (Gtk::Widget &w, const std::string &tip);
135         void set_tip (Gtk::Widget *w, const gchar *tip, const gchar *hlp="");
136         void idle_add (int (*func)(void *), void *arg);
137
138         Gtk::Main& main() const { return *theMain; }
139
140         template<class T> static bool idle_delete (T *obj) { delete obj; return false; }
141         template<class T> static void delete_when_idle (T *obj) {
142                 Glib::signal_idle().connect (bind (slot (&UI::idle_delete<T>), obj));
143         }
144
145         template<class T> void delete_in_self (T *obj) {
146                 call_slot (boost::bind (&UI::delete_in_self, this, obj));
147         }
148
149         Gdk::Color get_color (const std::string& prompt, bool& picked, const Gdk::Color *initial = 0);
150
151         /* starting is sent just before we enter the main loop,
152            stopping just after we return from it (at the top level)
153         */
154
155         virtual int starting() = 0;
156
157         sigc::signal<void> theme_changed;
158
159         static bool just_hide_it (GdkEventAny *, Gtk::Window *);
160
161   protected:
162         virtual void handle_fatal (const char *);
163         virtual void display_message (const char *prefix, gint prefix_len,
164                         Glib::RefPtr<Gtk::TextBuffer::Tag> ptag, Glib::RefPtr<Gtk::TextBuffer::Tag> mtag,
165                         const char *msg);
166
167   private:
168         static UI *theGtkUI;
169
170         bool _active;
171         Gtk::Main *theMain;
172 #ifndef GTK_NEW_TOOLTIP_API
173         Gtk::Tooltips *tips;
174 #endif
175         TextViewer *errors;
176         Glib::RefPtr<Gtk::TextBuffer::Tag> error_ptag;
177         Glib::RefPtr<Gtk::TextBuffer::Tag> error_mtag;
178         Glib::RefPtr<Gtk::TextBuffer::Tag> fatal_ptag;
179         Glib::RefPtr<Gtk::TextBuffer::Tag> fatal_mtag;
180         Glib::RefPtr<Gtk::TextBuffer::Tag> info_ptag;
181         Glib::RefPtr<Gtk::TextBuffer::Tag> info_mtag;
182         Glib::RefPtr<Gtk::TextBuffer::Tag> warning_ptag;
183         Glib::RefPtr<Gtk::TextBuffer::Tag> warning_mtag;
184
185         static void signal_pipe_callback (void *, gint, GdkInputCondition);
186         void process_error_message (Transmitter::Channel, const char *);
187         void do_quit ();
188
189         void color_selection_done (bool status);
190         bool color_selection_deleted (GdkEventAny *);
191         bool color_picked;
192
193         void do_request (UIRequest*);
194
195 };
196
197 } /* namespace */
198
199 #endif /* __pbd_gtk_ui_h__ */