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