1 /*
2  * Copyright (C) 2016-2017 Robin Gareus <robin@gareus.org>
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 along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18 #ifndef __ardour_luawindow_h__
19 #define __ardour_luawindow_h__
20 
21 #include <glibmm/thread.h>
22 
23 #include <gtkmm/box.h>
24 #include <gtkmm/scrolledwindow.h>
25 #include <gtkmm/label.h>
26 #include <gtkmm/textview.h>
27 #include <gtkmm/window.h>
28 
29 #include "pbd/signals.h"
30 #include "pbd/stateful.h"
31 
32 #include "ardour/ardour.h"
33 #include "ardour/luascripting.h"
34 #include "ardour/session_handle.h"
35 #include "ardour/types.h"
36 
37 #include "gtkmm2ext/visibility_tracker.h"
38 
39 #include "lua/luastate.h"
40 
41 #include "widgets/ardour_button.h"
42 #include "widgets/ardour_dropdown.h"
43 
44 class LuaWindow :
45 	public Gtk::Window,
46 	public PBD::ScopedConnectionList,
47 	public ARDOUR::SessionHandlePtr,
48 	public Gtkmm2ext::VisibilityTracker
49 {
50 public:
51 	static LuaWindow* instance();
52 	~LuaWindow();
53 
54 	void show_window ();
55 	bool hide_window (GdkEventAny *ev);
56 	void edit_script (const std::string&, const std::string&);
57 
58 	void set_session (ARDOUR::Session* s);
59 
60 	typedef enum {
61 		Buffer_NOFLAG     = 0x00,
62 		Buffer_Valid      = 0x01, ///< script is loaded
63 		Buffer_HasFile    = 0x02,
64 		Buffer_ReadOnly   = 0x04,
65 		Buffer_Dirty      = 0x08,
66 		Buffer_Scratch    = 0x10,
67 	} BufferFlags;
68 
69 	class ScriptBuffer
70 	{
71 	public:
72 		ScriptBuffer (const std::string&);
73 		ScriptBuffer (ARDOUR::LuaScriptInfoPtr);
74 		//ScriptBuffer (const ScriptBuffer& other);
75 		~ScriptBuffer ();
76 
77 		bool load ();
78 
79 		std::string script;
80 		std::string name;
81 		std::string path;
82 		BufferFlags flags;
83 		ARDOUR::LuaScriptInfo::ScriptType type;
84 	};
85 
86 private:
87 	LuaWindow ();
88 	static LuaWindow* _instance;
89 
90 	LuaState *lua;
91 	bool _visible;
92 
93 	Gtk::Menu* _menu_scratch;
94 	Gtk::Menu* _menu_snippet;
95 	Gtk::Menu* _menu_actions;
96 
97 	sigc::connection _script_changed_connection;
98 
99 	Gtk::TextView entry;
100 	Gtk::TextView outtext;
101 	Gtk::ScrolledWindow scrollout;
102 
103 	ArdourWidgets::ArdourButton _btn_run;
104 	ArdourWidgets::ArdourButton _btn_clear;
105 	ArdourWidgets::ArdourButton _btn_open;
106 	ArdourWidgets::ArdourButton _btn_save;
107 	ArdourWidgets::ArdourButton _btn_delete;
108 	ArdourWidgets::ArdourButton _btn_revert;
109 
110 	ArdourWidgets::ArdourDropdown script_select;
111 
112 	typedef boost::shared_ptr<ScriptBuffer> ScriptBufferPtr;
113 	typedef std::vector<ScriptBufferPtr> ScriptBufferList;
114 
115 	ScriptBufferList script_buffers;
116 	ScriptBufferPtr _current_buffer;
117 
118 	void session_going_away ();
119 	void update_title ();
120 	void reinit_lua ();
121 
122 	void setup_buffers ();
123 	void refresh_scriptlist ();
124 	void rebuild_menu ();
125 	uint32_t count_scratch_buffers () const;
126 
127 	void script_changed ();
128 	void script_selection_changed (ScriptBufferPtr n, bool force = false);
129 	void update_gui_state ();
130 
131 	void append_text (std::string s);
132 	void scroll_to_bottom ();
133 	void clear_output ();
134 
135 	void run_script ();
136 
137 	void new_script ();
138 	void delete_script ();
139 	void revert_script ();
140 	void import_script ();
141 	void save_script ();
142 };
143 
144 
145 #endif
146