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