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