2 Copyright (C) 1999 Paul Barton-Davis
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.
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.
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.
21 #ifndef __pbd_gtk_ui_h__
22 #define __pbd_gtk_ui_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>
40 #include <pbd/error.h>
41 #include <pbd/lockmonitor.h>
52 class UI : public AbstractUI
56 UI (string name, int *argc, char **argv[], string rcfile);
59 static UI *instance() { return theGtkUI; }
61 /* Abstract UI interfaces */
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);
75 bool caller_is_gui_thread () {
76 return pthread_equal (gui_thread, pthread_self());
79 /* Gtk-UI specific interfaces */
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 ();
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));
95 Gdk::Color get_color (const string& prompt, bool& picked, Gdk::Color *initial = 0);
97 /* starting is sent just before we enter the main loop,
98 stopping just after we return from it (at the top level)
101 sigc::signal<void> starting;
102 sigc::signal<void> stopping;
104 static bool just_hide_it (GdkEventAny *, Gtk::Window *);
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,
113 /* stuff to invoke member functions in another
114 thread so that we can keep the GUI running.
117 template<class UI_CLASS> struct thread_arg {
119 void (UI_CLASS::*func)(void *);
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);
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.
138 Gtk::StateType new_state;
139 int (*function)(void *);
141 Transmitter::Channel chn;
144 unsigned int timeout;
145 sigc::slot<void> slot;
147 /* this is for CallSlotLocked requests */
149 pthread_mutex_t slot_lock;
150 pthread_cond_t slot_cond;
154 if (type == ErrorMessage && msg) {
155 /* msg was strdup()'ed */
162 static pthread_t gui_thread;
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;
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;
184 int setup_signal_pipe ();
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 *);
193 void color_selection_done (bool status);
194 bool color_selection_deleted (GdkEventAny *);
197 jmp_buf quit_context;
200 template<class UI_CLASS> void *
201 UI::start_other_thread (void *arg)
204 thread_arg<UI_CLASS> *ta = (thread_arg<UI_CLASS> *) arg;
205 (ta->ui->*ta->func)(ta->arg);
210 template<class UI_CLASS> void
211 UI::other_thread (void (UI_CLASS::*func)(void *), void *arg)
215 thread_arg<UI_CLASS> *ta = new thread_arg<UI_CLASS>;
217 ta->ui = dynamic_cast<UI_CLASS *> (this);
219 error << "UI::other thread called illegally"
225 pthread_create (&thread_id, 0, start_other_thread, ta);
231 #endif /* __pbd_gtk_ui_h__ */