Consolidate two more engine-checks
[ardour.git] / gtk2_ardour / canvas_test.cc
1 #include <glibmm.h>
2 #include <gtkmm/main.h>
3 #include <gtkmm/box.h>
4 #include <gtkmm/window.h>
5
6 #include "pbd/debug.h"
7 #include "pbd/error.h"
8 #include "pbd/failed_constructor.h"
9 #include "pbd/pthread_utils.h"
10 #include "pbd/receiver.h"
11 #include "pbd/transmitter.h"
12
13 #include "ardour/audioengine.h"
14 #include "ardour/filename_extensions.h"
15 #include "ardour/types.h"
16
17 #include "gtkmm2ext/application.h"
18 #include "gtkmm2ext/window_title.h"
19 #include "gtkmm2ext/gtk_ui.h"
20 #include "gtkmm2ext/utils.h"
21
22 #include "canvas/types.h"
23 #include "canvas/canvas.h"
24 #include "canvas/container.h"
25 #include "gtkmm2ext/colors.h"
26 #include "canvas/debug.h"
27 #include "canvas/grid.h"
28 #include "canvas/scroll_group.h"
29 #include "canvas/text.h"
30 #include "canvas/widget.h"
31
32 #include "ardour_button.h"
33 #include "ui_config.h"
34
35 #include "pbd/i18n.h"
36
37 using namespace std;
38 using namespace ARDOUR;
39 using namespace PBD;
40 using namespace Gtkmm2ext;
41 using namespace Gtk;
42
43 #include "ardour/vst_types.h"
44
45 static const char* localedir = LOCALEDIR;
46 int vstfx_init (void*) { return 0; }
47 void vstfx_exit () {}
48 void vstfx_destroy_editor (VSTState*) {}
49
50 class LogReceiver : public Receiver
51 {
52   protected:
53     void receive (Transmitter::Channel chn, const char * str);
54 };
55
56 static LogReceiver log_receiver;
57
58 void
59 LogReceiver::receive (Transmitter::Channel chn, const char * str)
60 {
61         const char *prefix = "";
62
63         switch (chn) {
64         case Transmitter::Error:
65                 prefix = "[ERROR]: ";
66                 break;
67         case Transmitter::Info:
68                 prefix = "[INFO]: ";
69                 break;
70         case Transmitter::Warning:
71                 prefix = "[WARNING]: ";
72                 break;
73         case Transmitter::Fatal:
74                 prefix = "[FATAL]: ";
75                 break;
76         case Transmitter::Throw:
77                 /* this isn't supposed to happen */
78                 cerr << "Game Over\n";
79                 abort ();
80         }
81
82         /* note: iostreams are already thread-safe: no external
83            lock required.
84         */
85
86         std::cout << prefix << str << std::endl;
87
88         if (chn == Transmitter::Fatal) {
89                 ::exit (9);
90         }
91 }
92
93 /* ***************************************************************************/
94 /* ***************************************************************************/
95 /* ***************************************************************************/
96
97 class CANVAS_UI : public Gtkmm2ext::UI, public ARDOUR::SessionHandlePtr
98 {
99 public:
100         CANVAS_UI (int *argcp, char **argvp[], const char* localedir);
101         ~CANVAS_UI();
102 private:
103         int starting ();
104         bool main_window_delete_event (GdkEventAny* ev) { finish (); return true; }
105         void finish () { quit (); }
106         Gtk::Window _main_window;
107
108         ArdourCanvas::Container* initialize_canvas (ArdourCanvas::Canvas& canvas);
109
110         void canvas_size_request (Gtk::Requisition* req);
111         void canvas_size_allocated (Gtk::Allocation& alloc);
112
113         ArdourCanvas::GtkCanvas* canvas;
114         ArdourCanvas::Container* group;
115         ArdourCanvas::Grid* grid;
116
117         ArdourButton test_button;
118 };
119
120 /* ***************************************************************************/
121
122 CANVAS_UI::CANVAS_UI (int *argcp, char **argvp[], const char* localedir)
123         : Gtkmm2ext::UI (PROGRAM_NAME, X_("gui"), argcp, argvp)
124 {
125         Gtkmm2ext::init (localedir);
126         UIConfiguration::instance().post_gui_init ();
127
128         Gtkmm2ext::WindowTitle title ("Canvas Test");
129         _main_window.set_title (title.get_string());
130         _main_window.set_flags (CAN_FOCUS);
131         _main_window.signal_delete_event().connect (sigc::mem_fun (*this, &CANVAS_UI::main_window_delete_event));
132
133
134         VBox* b = manage (new VBox);
135         Label* l = manage (new Label ("Hello there"));
136
137         canvas = new ArdourCanvas::GtkCanvas ();
138         group = initialize_canvas (*canvas);
139
140         canvas->signal_size_request().connect (sigc::mem_fun (*this, &CANVAS_UI::canvas_size_request));
141         canvas->signal_size_allocate().connect (sigc::mem_fun (*this, &CANVAS_UI::canvas_size_allocated));
142         test_button.signal_clicked.connect (sigc::mem_fun (*this, &CANVAS_UI::finish));
143
144         test_button.set_text ("Don't click me");
145
146         b->pack_start (*l, false, 0);
147         b->pack_start (*canvas, true, 0);
148
149         _main_window.add (*b);
150         _main_window.show_all ();
151 }
152
153 CANVAS_UI::~CANVAS_UI ()
154 {
155 }
156
157 int
158 CANVAS_UI::starting ()
159 {
160         Application* app = Application::instance ();
161         app->ready ();
162         return 0;
163 }
164
165 ArdourCanvas::Container*
166 CANVAS_UI::initialize_canvas (ArdourCanvas::Canvas& canvas)
167 {
168         using namespace ArdourCanvas;
169         canvas.set_background_color (rgba_to_color (0.0, 0.0, 0.4, 1.0));
170
171         ScrollGroup* scroll_group = new ScrollGroup (canvas.root(),
172                         ScrollGroup::ScrollSensitivity (ScrollGroup::ScrollsVertically|ScrollGroup::ScrollsHorizontally));
173
174         grid = new ArdourCanvas::Grid (scroll_group);
175
176         grid->set_padding (40.0);
177         grid->set_margin (0.0);
178
179         grid->set_outline_width (3.0);
180         grid->set_outline_color (Color (0x3daec1ff));
181         grid->set_outline (false);
182         grid->set_row_spacing (60.0);
183         grid->set_col_spacing (3.0);
184         grid->set_homogenous (false);
185
186         ArdourCanvas::Text* text1 = new ArdourCanvas::Text (&canvas);
187         text1->set ("hello, world");
188         text1->set_color (Color (0xff0000ff));
189
190         ArdourCanvas::Text* text2 = new ArdourCanvas::Text (&canvas);
191         text2->set ("goodbye, cruel world");
192         text2->set_color (Color (0x00ff00ff));
193
194         ArdourCanvas::Text* text3 = new ArdourCanvas::Text (&canvas);
195         text3->set ("I am the third");
196         text3->set_color (Color (0xff00ffff));
197
198         ArdourCanvas::Text* text4 = new ArdourCanvas::Text (&canvas);
199         text4->set ("I am fourth");
200         text4->set_color (Color (0xffff00ff));
201
202         grid->place (text1, 0, 0, 2, 1);
203         grid->place (text2, 2, 0);
204         grid->place (text3, 0, 2, 1, 2);
205         grid->place (text4, 1, 3);
206
207         ArdourButton* button1 = new ArdourButton ("auto-return");
208         ArdourButton* button2 = new ArdourButton ("auto-play");
209         ArdourButton* button3 = new ArdourButton ("follow range");
210         ArdourButton* button4 = new ArdourButton ("auto-input");
211
212         ArdourCanvas::Widget* w1 = new ArdourCanvas::Widget (&canvas, *button1);
213         CANVAS_DEBUG_NAME (w1, "w1");
214         grid->place (w1, 3, 0, 2, 0);
215         ArdourCanvas::Widget* w2 = new ArdourCanvas::Widget (&canvas, *button2);
216         CANVAS_DEBUG_NAME (w2, "w2");
217         grid->place (w2, 5, 0, 2, 0);
218         ArdourCanvas::Widget* w3 = new ArdourCanvas::Widget (&canvas, *button3);
219         CANVAS_DEBUG_NAME (w3, "w3");
220         grid->place (w3, 3, 1);
221         ArdourCanvas::Widget* w4 = new ArdourCanvas::Widget (&canvas, *button4);
222         CANVAS_DEBUG_NAME (w4, "w4");
223         grid->place (w4, 4, 1);
224
225         //ArdourCanvas::Widget* w = new ArdourCanvas::Widget (scroll_group, test_button);
226         //CANVAS_DEBUG_NAME (w, "TheW");
227
228         return new ArdourCanvas::Container (scroll_group);
229 }
230
231 void
232 CANVAS_UI::canvas_size_request (Gtk::Requisition* req)
233 {
234         req->width = 100;
235         req->height = 100;
236 }
237
238 void
239 CANVAS_UI::canvas_size_allocated (Gtk::Allocation& alloc)
240 {
241 }
242
243 /* ***************************************************************************/
244 /* ***************************************************************************/
245 /* ***************************************************************************/
246
247 static CANVAS_UI  *ui = 0;
248
249 int main (int argc, char **argv)
250 {
251 #if 0
252         fixup_bundle_environment (argc, argv, localedir);
253         load_custom_fonts();
254         // TODO setlocale..
255 #endif
256
257         if (!ARDOUR::init (false, true, localedir)) {
258                 cerr << "Ardour failed to initialize\n" << endl;
259                 ::exit (EXIT_FAILURE);
260         }
261
262         if (!Glib::thread_supported()) {
263                 Glib::thread_init();
264         }
265
266         pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, 0);
267
268         log_receiver.listen_to (error);
269         log_receiver.listen_to (info);
270         log_receiver.listen_to (fatal);
271         log_receiver.listen_to (warning);
272
273         if (UIConfiguration::instance().pre_gui_init ()) {
274                 error << _("Could not complete pre-GUI initialization") << endmsg;
275                 exit (1);
276         }
277
278         // we could load a session here, if needed
279         // see ../session_utils/common.cc
280
281         ui = new CANVAS_UI (&argc, &argv, localedir);
282         ui->run (log_receiver);
283
284         info << "Farewell" << endmsg;
285
286         Gtkmm2ext::Application::instance()->cleanup();
287         delete ui;
288         ui = 0;
289
290         ARDOUR::cleanup ();
291         pthread_cancel_all ();
292         return 0;
293 }