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