update canvas test tool
[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 "canvas/colors.h"
26 #include "canvas/scroll_group.h"
27 #include "canvas/text.h"
28 #include "canvas/widget.h"
29
30 #include "ardour_button.h"
31 #include "ui_config.h"
32
33 #include "pbd/i18n.h"
34
35 using namespace std;
36 using namespace ARDOUR;
37 using namespace PBD;
38 using namespace Gtkmm2ext;
39 using namespace Gtk;
40
41 #include "ardour/vst_types.h"
42
43 static const char* localedir = LOCALEDIR;
44 int vstfx_init (void*) { return 0; }
45 void vstfx_exit () {}
46 void vstfx_destroy_editor (VSTState*) {}
47
48 class LogReceiver : public Receiver
49 {
50   protected:
51     void receive (Transmitter::Channel chn, const char * str);
52 };
53
54 static LogReceiver log_receiver;
55
56 void
57 LogReceiver::receive (Transmitter::Channel chn, const char * str)
58 {
59         const char *prefix = "";
60
61         switch (chn) {
62         case Transmitter::Error:
63                 prefix = "[ERROR]: ";
64                 break;
65         case Transmitter::Info:
66                 prefix = "[INFO]: ";
67                 break;
68         case Transmitter::Warning:
69                 prefix = "[WARNING]: ";
70                 break;
71         case Transmitter::Fatal:
72                 prefix = "[FATAL]: ";
73                 break;
74         case Transmitter::Throw:
75                 /* this isn't supposed to happen */
76                 cerr << "Game Over\n";
77                 abort ();
78         }
79
80         /* note: iostreams are already thread-safe: no external
81            lock required.
82         */
83
84         std::cout << prefix << str << std::endl;
85
86         if (chn == Transmitter::Fatal) {
87                 ::exit (9);
88         }
89 }
90
91 /* ***************************************************************************/
92 /* ***************************************************************************/
93 /* ***************************************************************************/
94
95 class CANVAS_UI : public Gtkmm2ext::UI, public ARDOUR::SessionHandlePtr
96 {
97 public:
98         CANVAS_UI (int *argcp, char **argvp[], const char* localedir);
99         ~CANVAS_UI();
100 private:
101         int starting ();
102         bool main_window_delete_event (GdkEventAny* ev) { finish (); return true; }
103         void finish () { quit (); }
104         Gtk::Window _main_window;
105
106         ArdourCanvas::Container* initialize_canvas (ArdourCanvas::Canvas& canvas);
107
108         void canvas_size_request (Gtk::Requisition* req);
109         void canvas_size_allocated (Gtk::Allocation& alloc);
110
111         ArdourCanvas::GtkCanvas* canvas;
112         ArdourCanvas::Container* group;
113
114         ArdourButton test_button;
115 };
116
117 /* ***************************************************************************/
118
119 CANVAS_UI::CANVAS_UI (int *argcp, char **argvp[], const char* localedir)
120         : Gtkmm2ext::UI (PROGRAM_NAME, X_("gui"), argcp, argvp)
121 {
122         Gtkmm2ext::init (localedir);
123         UIConfiguration::instance().post_gui_init ();
124
125         Gtkmm2ext::WindowTitle title ("Canvas Test");
126         _main_window.set_title (title.get_string());
127         _main_window.set_flags (CAN_FOCUS);
128         _main_window.signal_delete_event().connect (sigc::mem_fun (*this, &CANVAS_UI::main_window_delete_event));
129
130
131         VBox* b = manage (new VBox);
132         Label* l = manage (new Label ("Hello there"));
133
134         canvas = new ArdourCanvas::GtkCanvas ();
135         group = initialize_canvas (*canvas);
136
137         canvas->signal_size_request().connect (sigc::mem_fun (*this, &CANVAS_UI::canvas_size_request));
138         canvas->signal_size_allocate().connect (sigc::mem_fun (*this, &CANVAS_UI::canvas_size_allocated));
139         test_button.signal_clicked.connect (sigc::mem_fun (*this, &CANVAS_UI::finish));
140
141         test_button.set_text ("Don't click me");
142
143         b->pack_start (*l, false, 0);
144         b->pack_start (*canvas, true, 0);
145
146         _main_window.add (*b);
147         _main_window.show_all ();
148 }
149
150 CANVAS_UI::~CANVAS_UI ()
151 {
152 }
153
154 int
155 CANVAS_UI::starting ()
156 {
157         Application* app = Application::instance ();
158         app->ready ();
159         return 0;
160 }
161
162 ArdourCanvas::Container*
163 CANVAS_UI::initialize_canvas (ArdourCanvas::Canvas& canvas)
164 {
165         using namespace ArdourCanvas;
166         canvas.set_background_color (rgba_to_color (0.0, 0.0, 1.0, 1.0));
167
168         ScrollGroup* scroll_group = new ScrollGroup (canvas.root(),
169                         ScrollGroup::ScrollSensitivity (ScrollGroup::ScrollsVertically|ScrollGroup::ScrollsHorizontally));
170
171         ArdourCanvas::Widget* w = new ArdourCanvas::Widget (scroll_group, test_button);
172
173         return new ArdourCanvas::Container (scroll_group);
174 }
175
176 void
177 CANVAS_UI::canvas_size_request (Gtk::Requisition* req)
178 {
179         req->width = 100;
180         req->height = 100;
181 }
182
183 void
184 CANVAS_UI::canvas_size_allocated (Gtk::Allocation& alloc)
185 {
186 }
187
188 /* ***************************************************************************/
189 /* ***************************************************************************/
190 /* ***************************************************************************/
191
192 static CANVAS_UI  *ui = 0;
193
194 int main (int argc, char **argv)
195 {
196 #if 0
197         fixup_bundle_environment (argc, argv, localedir);
198         load_custom_fonts();
199         // TODO setlocale..
200 #endif
201
202         if (!ARDOUR::init (false, true, localedir)) {
203                 cerr << "Ardour failed to initialize\n" << endl;
204                 ::exit (EXIT_FAILURE);
205         }
206
207         if (!Glib::thread_supported()) {
208                 Glib::thread_init();
209         }
210
211         pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, 0);
212
213         log_receiver.listen_to (error);
214         log_receiver.listen_to (info);
215         log_receiver.listen_to (fatal);
216         log_receiver.listen_to (warning);
217
218         if (UIConfiguration::instance().pre_gui_init ()) {
219                 error << _("Could not complete pre-GUI initialization") << endmsg;
220                 exit (1);
221         }
222
223         // we could load a session here, if needed
224         // see ../session_utils/common.cc
225
226         ui = new CANVAS_UI (&argc, &argv, localedir);
227         ui->run (log_receiver);
228
229         info << "Farewell" << endmsg;
230
231         Gtkmm2ext::Application::instance()->cleanup();
232         delete ui;
233         ui = 0;
234
235         ARDOUR::cleanup ();
236         pthread_cancel_all ();
237         return 0;
238 }