use different source file for canvas tester
[ardour.git] / gtk2_ardour / toolbar_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/actions.h"
18 #include "gtkmm2ext/application.h"
19 #include "gtkmm2ext/window_title.h"
20 #include "gtkmm2ext/gtk_ui.h"
21 #include "gtkmm2ext/utils.h"
22
23 #include "canvas/types.h"
24 #include "canvas/canvas.h"
25 #include "canvas/container.h"
26 #include "canvas/colors.h"
27 #include "canvas/debug.h"
28 #include "canvas/grid.h"
29 #include "canvas/scroll_group.h"
30 #include "canvas/text.h"
31 #include "canvas/widget.h"
32
33 #include "ardour_button.h"
34 #include "ui_config.h"
35
36 #include "pbd/i18n.h"
37
38 using namespace std;
39 using namespace ARDOUR;
40 using namespace PBD;
41 using namespace Gtkmm2ext;
42 using namespace Gtk;
43
44 #include "ardour/vst_types.h"
45
46 static const char* localedir = LOCALEDIR;
47 int vstfx_init (void*) { return 0; }
48 void vstfx_exit () {}
49 void vstfx_destroy_editor (VSTState*) {}
50
51 class LogReceiver : public Receiver
52 {
53   protected:
54     void receive (Transmitter::Channel chn, const char * str);
55 };
56
57 static LogReceiver log_receiver;
58
59 void
60 LogReceiver::receive (Transmitter::Channel chn, const char * str)
61 {
62         const char *prefix = "";
63
64         switch (chn) {
65         case Transmitter::Error:
66                 prefix = "[ERROR]: ";
67                 break;
68         case Transmitter::Info:
69                 prefix = "[INFO]: ";
70                 break;
71         case Transmitter::Warning:
72                 prefix = "[WARNING]: ";
73                 break;
74         case Transmitter::Fatal:
75                 prefix = "[FATAL]: ";
76                 break;
77         case Transmitter::Throw:
78                 /* this isn't supposed to happen */
79                 cerr << "Game Over\n";
80                 abort ();
81         }
82
83         /* note: iostreams are already thread-safe: no external
84            lock required.
85         */
86
87         std::cout << prefix << str << std::endl;
88
89         if (chn == Transmitter::Fatal) {
90                 ::exit (9);
91         }
92 }
93
94 /* ***************************************************************************/
95 /* ***************************************************************************/
96 /* ***************************************************************************/
97
98 ArdourButton*
99 make_transport_button (std::string const & action, Gtkmm2ext::ArdourIcon::Icon icon)
100 {
101         ArdourButton* button = new ArdourButton;
102         button->set_name ("transport button");
103         Glib::RefPtr<Gtk::Action> act;
104         act = ActionManager::get_action (action.c_str());
105         button->set_related_action (act);
106         button->set_icon (icon);
107         return button;
108 }
109
110 class CANVAS_UI : public Gtkmm2ext::UI, public ARDOUR::SessionHandlePtr
111 {
112 public:
113         CANVAS_UI (int *argcp, char **argvp[], const char* localedir);
114         ~CANVAS_UI();
115 private:
116         int starting ();
117         bool main_window_delete_event (GdkEventAny* ev) { finish (); return true; }
118         void finish () { quit (); }
119         Gtk::Window _main_window;
120
121         void initialize_canvas (ArdourCanvas::Canvas& canvas);
122
123         void canvas_size_request (Gtk::Requisition* req);
124         void canvas_size_allocated (Gtk::Allocation& alloc);
125
126         ArdourCanvas::GtkCanvas* canvas;
127         ArdourCanvas::Container* group;
128         ArdourCanvas::Grid* grid;
129
130         ArdourButton test_button;
131 };
132
133 /* ***************************************************************************/
134
135 CANVAS_UI::CANVAS_UI (int *argcp, char **argvp[], const char* localedir)
136         : Gtkmm2ext::UI (PROGRAM_NAME, X_("gui"), argcp, argvp)
137 {
138         Gtkmm2ext::init (localedir);
139         UIConfiguration::instance().post_gui_init ();
140
141         Gtkmm2ext::WindowTitle title ("Canvas Toolbar Test");
142         _main_window.set_title (title.get_string());
143         _main_window.set_flags (CAN_FOCUS);
144         _main_window.signal_delete_event().connect (sigc::mem_fun (*this, &CANVAS_UI::main_window_delete_event));
145
146         canvas = new ArdourCanvas::GtkCanvas ();
147
148         initialize_canvas (*canvas);
149
150         canvas->signal_size_request().connect (sigc::mem_fun (*this, &CANVAS_UI::canvas_size_request));
151         canvas->signal_size_allocate().connect (sigc::mem_fun (*this, &CANVAS_UI::canvas_size_allocated));
152
153         _main_window.add (*canvas);
154         _main_window.show_all ();
155 }
156
157 CANVAS_UI::~CANVAS_UI ()
158 {
159 }
160
161 int
162 CANVAS_UI::starting ()
163 {
164         Application* app = Application::instance ();
165         app->ready ();
166         return 0;
167 }
168
169 void
170 CANVAS_UI::initialize_canvas (ArdourCanvas::Canvas& canvas)
171 {
172         using namespace ArdourCanvas;
173         canvas.set_background_color (rgba_to_color (0.0, 0.0, 0.4, 1.0));
174
175         ScrollGroup* scroll_group = new ScrollGroup (canvas.root(),
176                         ScrollGroup::ScrollSensitivity (ScrollGroup::ScrollsVertically|ScrollGroup::ScrollsHorizontally));
177
178         grid = new ArdourCanvas::Grid (scroll_group);
179
180         grid->set_padding (3.0);
181
182         grid->set_row_spacing (3.0);
183         grid->set_col_spacing (3.0);
184         grid->set_homogenous (true);
185
186         ArdourCanvas::Widget* w1 = new ArdourCanvas::Widget (&canvas, *make_transport_button ("Transport/Roll", ArdourIcon::TransportPlay));
187         CANVAS_DEBUG_NAME (w1, "w1");
188         grid->place (w1, 0, 0);
189         ArdourCanvas::Widget* w2 = new ArdourCanvas::Widget (&canvas, *make_transport_button ("Transport/Stop", ArdourIcon::TransportStop));
190         CANVAS_DEBUG_NAME (w2, "w2");
191         grid->place (w2, 1, 0);
192         ArdourCanvas::Widget* w3 = new ArdourCanvas::Widget (&canvas, *make_transport_button ("Transport/Record", ArdourIcon::RecButton));
193         CANVAS_DEBUG_NAME (w3, "w3");
194         grid->place (w3, 2, 0);
195         ArdourCanvas::Widget* w4 = new ArdourCanvas::Widget (&canvas, *make_transport_button ("Transport/Loop", ArdourIcon::TransportLoop));
196         CANVAS_DEBUG_NAME (w4, "w4");
197         grid->place (w4, 3, 0);
198 }
199
200 void
201 CANVAS_UI::canvas_size_request (Gtk::Requisition* req)
202 {
203         ArdourCanvas::Rect bbox = canvas->root()->bounding_box();
204         req->width = bbox.width();
205         req->height = bbox.height();
206 }
207
208 void
209 CANVAS_UI::canvas_size_allocated (Gtk::Allocation& alloc)
210 {
211 }
212
213 /* ***************************************************************************/
214 /* ***************************************************************************/
215 /* ***************************************************************************/
216
217 static CANVAS_UI  *ui = 0;
218
219 int main (int argc, char **argv)
220 {
221 #if 0
222         fixup_bundle_environment (argc, argv, localedir);
223         load_custom_fonts();
224         // TODO setlocale..
225 #endif
226
227         if (!ARDOUR::init (false, true, localedir)) {
228                 cerr << "Ardour failed to initialize\n" << endl;
229                 ::exit (EXIT_FAILURE);
230         }
231
232         if (!Glib::thread_supported()) {
233                 Glib::thread_init();
234         }
235
236         pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, 0);
237
238         log_receiver.listen_to (error);
239         log_receiver.listen_to (info);
240         log_receiver.listen_to (fatal);
241         log_receiver.listen_to (warning);
242
243         if (UIConfiguration::instance().pre_gui_init ()) {
244                 error << _("Could not complete pre-GUI initialization") << endmsg;
245                 exit (1);
246         }
247
248         // we could load a session here, if needed
249         // see ../session_utils/common.cc
250
251         ui = new CANVAS_UI (&argc, &argv, localedir);
252         ui->run (log_receiver);
253
254         info << "Farewell" << endmsg;
255
256         Gtkmm2ext::Application::instance()->cleanup();
257         delete ui;
258         ui = 0;
259
260         ARDOUR::cleanup ();
261         pthread_cancel_all ();
262         return 0;
263 }