1 /*
2  * Copyright (C) 2017-2018 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 
19 #ifndef _gtk2ardour_luadialog_h_
20 #define _gtk2ardour_luadialog_h_
21 
22 #include <cassert>
23 #include <gtkmm/table.h>
24 #include <gtkmm/progressbar.h>
25 
26 #include "LuaBridge/LuaBridge.h"
27 
28 #include "ardour_message.h"
29 
30 namespace LuaDialog {
31 
32 class Message {
33 public:
34 
35 	enum MessageType {
36 		Info, Warning, Question, Error
37 	};
38 
39 	enum ButtonType {
40 		OK, Close, Cancel, Yes_No, OK_Cancel
41 	};
42 
43 	Message (std::string const&, std::string const&, Message::MessageType, Message::ButtonType);
44 
45 	int run ();
46 
47 private:
48 	Message (Message const&); // prevent copy construction
49 
50 	static Gtk::ButtonsType to_gtk_bt (ButtonType bt);
51 	static Gtk::MessageType to_gtk_mt (MessageType mt);
52 
53 	ArdourMessageDialog _message_dialog;
54 };
55 
56 class LuaDialogWidget {
57 public:
58 	LuaDialogWidget (std::string const& key, std::string const& label, int col = 0, int colspan = -1)
_key(key)59 		: _key (key), _label (label), _col (col), _colspan (colspan)
60 	{
61 		if (_colspan < 0) {
62 			_colspan = label.empty () ? 1 : 2;
63 		}
64 	}
65 
~LuaDialogWidget()66 	virtual ~LuaDialogWidget () {}
67 
68 	virtual Gtk::Widget* widget () = 0;
69 	virtual void assign (luabridge::LuaRef* rv) const = 0;
label()70 	std::string const&  label () const { return _label; }
key()71 	std::string const&  key   () const { return _key; }
col()72 	int                 col   () const { return _col; }
span()73 	int                 span  () const { return _colspan; }
74 
set_col(int col)75 	void set_col  (int col)  { _col = col; }
set_span(int span)76 	void set_span (int span) { _colspan = span; }
77 
78 protected:
79 	std::string _key;
80 	std::string _label;
81 	int _col;
82 	int _colspan;
83 };
84 
85 
86 class Dialog {
87 public:
88 	Dialog (std::string const&, luabridge::LuaRef);
89 	~Dialog ();
90 	int run (lua_State *L);
91 
92 private:
93 	Dialog (Dialog const&); // prevent copy construction
94 	void table_size_alloc (Gtk::Allocation&);
95 
96 	ArdourDialog _ad;
97 	Gtk::ScrolledWindow _scroller;
98 	typedef std::vector<LuaDialogWidget*> DialogWidgets;
99 	DialogWidgets _widgets;
100 	std::string _title;
101 };
102 
103 /** Synchronous GUI-thread Progress dialog
104  *
105  * This shows a modal progress dialog with an optional
106  * "Cancel" button. Since it runs in the UI thread
107  * the script needs to regularly call progress(),
108  * as well as close the dialog, as needed.
109  */
110 class ProgressWindow : public ArdourDialog
111 {
112 public:
113 	/** Create a new progress window.
114 	 * @param title Window title
115 	 * @param allow_cancel include a "Cancel" option
116 	 */
117 	ProgressWindow (std::string const& title, bool allow_cancel);
118 
119 	/** Report progress and update GUI.
120 	 * @param prog progress in range 0..1 show a bar, values outside this range show a pulsing dialog.
121 	 * @param text optional text to show on the progress-bar
122 	 * @return true if cancel was clicked, false otherwise
123 	 */
124 	bool progress (float prog, std::string const& text = "");
125 
canceled()126 	bool canceled () const {
127 		return _canceled;
128 	}
129 
130 	/** Close and hide the dialog.
131 	 *
132 	 * This is required to be at the end, since the dialog
133 	 * is modal and prevents other UI operations while visible.
134 	 */
135 	void done ();
136 
137 private:
cancel_clicked()138 	void cancel_clicked () {
139 		_canceled = true;
140 	}
141 
142 	Gtk::ProgressBar _bar;
143 	bool             _canceled;
144 };
145 
146 }; // namespace
147 
148 #endif
149