the usual blob of fixes. note the requirement for ComboBoxText::set_active_text()
[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     $Id$
19 */
20
21 #ifndef __pbd_gtk_ui_h__
22 #define __pbd_gtk_ui_h__
23
24 #include <string>
25 #include <queue>
26 #include <map>
27
28 #include <setjmp.h>
29 #include <pthread.h>
30 #include <gtkmm/widget.h>
31 #include <gtkmm/style.h>
32 #include <gtkmm/textbuffer.h>
33 #include <gtkmm/main.h>
34 #include <gtkmm/tooltips.h>
35 #include <gdkmm/color.h>
36 #include <pbd/abstract_ui.h>
37 #include <pbd/ringbufferNPT.h>
38 #include <pbd/atomic.h>
39 #include <pbd/pool.h>
40 #include <pbd/error.h>
41 #include <pbd/lockmonitor.h>
42
43 using std::string;
44 using std::queue;
45
46 class Touchable;
47
48 namespace Gtkmm2ext {
49
50 class TextViewer;
51
52 class UI : public AbstractUI
53
54 {
55   public:
56         UI (string name, int *argc, char **argv[], string rcfile);
57         virtual ~UI ();
58
59         static UI *instance() { return theGtkUI; }
60
61         /* Abstract UI interfaces */
62
63         bool running ();
64         void quit    ();
65         void kill    ();
66         int  load_rcfile (string);
67         void request (RequestType); 
68         void run (Receiver &old_receiver);
69         void call_slot (sigc::slot<void>);
70         void call_slot_locked (sigc::slot<void>);
71         void touch_display (Touchable *);
72         void receive (Transmitter::Channel, const char *);
73         void register_thread (pthread_t, string);
74
75         bool caller_is_gui_thread () { 
76                 return pthread_equal (gui_thread, pthread_self());
77         }
78
79         /* Gtk-UI specific interfaces */
80
81         int  set_quit_context ();
82         void set_tip (Gtk::Widget *, const gchar *txt, const gchar *hlp = 0);
83         void set_state (Gtk::Widget *w, Gtk::StateType state);
84         void idle_add (int (*)(void *), void *);
85         void timeout_add (unsigned int, int (*)(void *), void *);
86         void popup_error (const char *text);
87         void flush_pending ();
88         void toggle_errors ();
89
90         template<class T> static bool idle_delete (T *obj) { delete obj; return false; }
91         template<class T> static void delete_when_idle (T *obj) {
92                 Glib::signal_idle().connect (bind (slot (&UI::idle_delete<T>), obj));
93         }
94
95         Gdk::Color get_color (const string& prompt, bool& picked, Gdk::Color *initial = 0);
96
97         /* starting is sent just before we enter the main loop,
98            stopping just after we return from it (at the top level)
99         */
100
101         sigc::signal<void> starting;
102         sigc::signal<void> stopping;
103
104         static bool just_hide_it (GdkEventAny *, Gtk::Window *);
105
106   protected:
107         virtual void handle_fatal (const char *);
108         virtual void display_message (const char *prefix, gint prefix_len, 
109                                       Glib::RefPtr<Gtk::TextBuffer::Tag> ptag, 
110                                       Glib::RefPtr<Gtk::TextBuffer::Tag> mtag, 
111                                       const char *msg);
112
113         /* stuff to invoke member functions in another
114            thread so that we can keep the GUI running.
115         */
116
117         template<class UI_CLASS> struct thread_arg {
118             UI_CLASS *ui;
119             void (UI_CLASS::*func)(void *);
120             void *arg;
121         };
122
123         template<class UI_CLASS> static void *start_other_thread (void *arg);
124         template<class UI_CLASS> void other_thread (void (UI_CLASS::*func)(void *), void *arg = 0);
125
126   private:
127         struct Request {
128
129             /* this once used anonymous unions to merge elements
130                that are never part of the same request. that makes
131                the creation of a legal copy constructor difficult
132                because of the semantics of the slot member.
133             */
134
135             RequestType type;
136             Touchable *display;
137             const char *msg;
138             Gtk::StateType new_state;
139             int (*function)(void *);
140             Gtk::Widget *widget;
141             Transmitter::Channel chn;
142             void *arg;
143             const char *msg2;
144             unsigned int timeout;
145             sigc::slot<void> slot;
146
147             /* this is for CallSlotLocked requests */
148
149             pthread_mutex_t slot_lock;
150             pthread_cond_t  slot_cond;
151
152             Request ();
153             ~Request () { 
154                     if (type == ErrorMessage && msg) {
155                             /* msg was strdup()'ed */
156                             free ((char *)msg);
157                     }
158             }
159         };
160
161         static UI *theGtkUI;
162         static pthread_t gui_thread;
163         bool _active;
164         string _ui_name;
165         Gtk::Main *theMain;
166         Gtk::Tooltips *tips;
167         TextViewer *errors;
168         Glib::RefPtr<Gtk::TextBuffer::Tag> error_ptag;
169         Glib::RefPtr<Gtk::TextBuffer::Tag> error_mtag;
170         Glib::RefPtr<Gtk::TextBuffer::Tag> fatal_ptag;
171         Glib::RefPtr<Gtk::TextBuffer::Tag> fatal_mtag;
172         Glib::RefPtr<Gtk::TextBuffer::Tag> info_ptag;
173         Glib::RefPtr<Gtk::TextBuffer::Tag> info_mtag;
174         Glib::RefPtr<Gtk::TextBuffer::Tag> warning_ptag;
175         Glib::RefPtr<Gtk::TextBuffer::Tag> warning_mtag;
176
177         int signal_pipe[2];
178         PBD::Lock request_buffer_map_lock;
179         typedef std::map<pthread_t,RingBufferNPT<Request>* > RequestBufferMap;
180         RequestBufferMap request_buffers;
181         Request* get_request(RequestType);
182         pthread_key_t thread_request_buffer_key;
183
184         int setup_signal_pipe ();
185
186         void handle_ui_requests ();
187         void do_request (Request *);
188         void send_request (Request *);
189         static void signal_pipe_callback (void *, gint, GdkInputCondition);
190         void process_error_message (Transmitter::Channel, const char *);
191         void do_quit ();
192
193         void color_selection_done (bool status);
194         bool color_selection_deleted (GdkEventAny *);
195         bool color_picked;
196
197         jmp_buf quit_context;
198 };
199
200 template<class UI_CLASS> void *
201 UI::start_other_thread (void *arg)
202
203 {
204         thread_arg<UI_CLASS> *ta = (thread_arg<UI_CLASS> *) arg;
205         (ta->ui->*ta->func)(ta->arg);
206         delete ta;
207         return 0;
208 }
209
210 template<class UI_CLASS> void
211 UI::other_thread (void (UI_CLASS::*func)(void *), void *arg)
212
213 {
214         pthread_t thread_id;
215         thread_arg<UI_CLASS> *ta = new thread_arg<UI_CLASS>;
216
217         ta->ui = dynamic_cast<UI_CLASS *> (this);
218         if (ta->ui == 0) {
219                 error << "UI::other thread called illegally"
220                       << endmsg;
221                 return;
222         }
223         ta->func = func;
224         ta->arg = arg;
225         pthread_create (&thread_id, 0, start_other_thread, ta);
226 }
227
228
229 } /* namespace */
230
231 #endif /* __pbd_gtk_ui_h__ */