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