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