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