fix merge conflicts from master
[ardour.git] / gtk2_ardour / session_metadata_dialog.h
1 /*
2     Copyright (C) 2008 Paul Davis
3     Author: Sakari Bergen
4
5     This program is free software; you can redistribute it and/or modify it
6     under the terms of the GNU General Public License as published by the Free
7     Software Foundation; either version 2 of the License, or (at your option)
8     any later version.
9
10     This program is distributed in the hope that it will be useful, but WITHOUT
11     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13     for more details.
14
15     You should have received a copy of the GNU General Public License along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #ifndef __session_metadata_dialog_h__
21 #define __session_metadata_dialog_h__
22
23 #include "ardour_dialog.h"
24
25 #ifdef interface
26 #undef interface
27 #endif
28
29 #include <gtkmm.h>
30 #include <boost/shared_ptr.hpp>
31
32 #include <string>
33 #include <list>
34
35 #include "ardour/session_metadata.h"
36
37 class MetadataField;
38 typedef boost::shared_ptr<MetadataField> MetadataPtr;
39
40 /// Wraps a metadata field to be used in a GUI
41 class MetadataField {
42   public:
43         MetadataField (std::string const & field_name);
44         virtual ~MetadataField();
45         virtual MetadataPtr copy () = 0;
46
47         virtual void save_data (ARDOUR::SessionMetadata & data) const = 0;
48         virtual void load_data (ARDOUR::SessionMetadata const & data) = 0;
49
50         virtual std::string name() { return _name; }
51         virtual std::string value() { return _value; }
52
53         /// Get widget containing name of field
54         virtual Gtk::Widget & name_widget () = 0;
55         /// Get label containing value of field
56         virtual Gtk::Widget & value_widget () = 0;
57         /// Get widget for editing value
58         virtual Gtk::Widget & edit_widget () = 0;
59   protected:
60         std::string _name;
61         std::string _value;
62 };
63
64 /// MetadataField that contains text
65 class TextMetadataField : public MetadataField {
66   private:
67         typedef std::string (ARDOUR::SessionMetadata::*Getter) () const;
68         typedef void (ARDOUR::SessionMetadata::*Setter) (std::string const &);
69   public:
70         TextMetadataField (Getter getter, Setter setter, std::string const & field_name, guint width = 50);
71         MetadataPtr copy ();
72
73         void save_data (ARDOUR::SessionMetadata & data) const;
74         void load_data (ARDOUR::SessionMetadata const & data);
75
76         Gtk::Widget & name_widget ();
77         Gtk::Widget & value_widget ();
78         Gtk::Widget & edit_widget ();
79   private:
80         void update_value ();
81
82         Getter getter;
83         Setter setter;
84
85         Gtk::Label* label;
86         Gtk::Label* value_label;
87         Gtk::Entry* entry;
88
89         guint width;
90 };
91
92 /// MetadataField that accepts only numbers
93 class NumberMetadataField : public MetadataField {
94   private:
95         typedef uint32_t (ARDOUR::SessionMetadata::*Getter) () const;
96         typedef void (ARDOUR::SessionMetadata::*Setter) (uint32_t);
97   public:
98         NumberMetadataField (Getter getter, Setter setter, std::string const & field_name, guint numbers, guint width = 50);
99         MetadataPtr copy ();
100
101         void save_data (ARDOUR::SessionMetadata & data) const;
102         void load_data (ARDOUR::SessionMetadata const & data);
103
104         Gtk::Widget & name_widget ();
105         Gtk::Widget & value_widget ();
106         Gtk::Widget & edit_widget ();
107   private:
108         void update_value ();
109         std::string uint_to_str (uint32_t i) const;
110         uint32_t str_to_uint (std::string const & str) const;
111
112         Getter getter;
113         Setter setter;
114
115         Gtk::Label* label;
116         Gtk::Label* value_label;
117         Gtk::Entry* entry;
118
119         guint numbers;
120         guint width;
121 };
122
123 /// Interface for MetadataFields
124 class SessionMetadataSet : public ARDOUR::SessionHandlePtr {
125   public:
126         SessionMetadataSet (std::string const & name);
127         virtual ~SessionMetadataSet () {};
128
129         void add_data_field (MetadataPtr field);
130
131         /// allows loading extra data into data sets (for importing etc.)
132         virtual void load_extra_data (ARDOUR::SessionMetadata const & /*data*/) { }
133         /// Saves data to session
134         virtual void save_data () = 0;
135
136         virtual Gtk::Widget & get_widget () = 0;
137         virtual Gtk::Widget & get_tab_widget () = 0;
138
139   protected:
140         typedef std::list<MetadataPtr> DataList;
141         DataList list;
142         std::string name;
143 };
144
145 /// Contains MetadataFields for editing
146 class SessionMetadataSetEditable : public SessionMetadataSet {
147   public:
148         SessionMetadataSetEditable (std::string const & name);
149
150         Gtk::Widget & get_widget () { return vbox; }
151         Gtk::Widget & get_tab_widget ();
152
153         /// Sets session and loads data
154         void set_session (ARDOUR::Session * s);
155         /// Saves from MetadataFields into data
156         void save_data ();
157
158   private:
159         Gtk::VBox vbox;
160         Gtk::Table table;
161         Gtk::Label tab_widget;
162 };
163
164 /// Contains MetadataFields for importing
165 class SessionMetadataSetImportable : public SessionMetadataSet {
166   public:
167         SessionMetadataSetImportable (std::string const & name);
168
169         Gtk::Widget & get_widget () { return tree_view; }
170         Gtk::Widget & get_tab_widget ();
171         Gtk::Widget & get_select_all_widget ();
172
173         /// Loads importable data from data
174         void load_extra_data (ARDOUR::SessionMetadata const & data);
175         /// Saves from importable data (see load_data) to session_data
176         void save_data ();
177
178   private:
179         DataList & session_list; // References MetadataSet::list
180         DataList import_list;
181
182         struct Columns : public Gtk::TreeModel::ColumnRecord
183         {
184           public:
185                 Gtk::TreeModelColumn<std::string>     field;
186                 Gtk::TreeModelColumn<std::string>     values;
187                 Gtk::TreeModelColumn<bool>        import;
188                 Gtk::TreeModelColumn<MetadataPtr> data;
189
190                 Columns() { add (field); add (values); add (import); add (data); }
191         };
192
193         Glib::RefPtr<Gtk::ListStore>  tree;
194         Columns                       tree_cols;
195         Gtk::TreeView                 tree_view;
196
197         Gtk::Label                    tab_widget;
198         Gtk::CheckButton              select_all_check;
199
200         void select_all ();
201         void selection_changed (std::string const & path);
202 };
203
204 /// Metadata dialog interface
205 /**
206  * The DataSets are initalized in this class so that all
207  * Dialogs have the same sets of data in the same order.
208  */
209 template <typename DataSet>
210 class SessionMetadataDialog : public ArdourDialog
211 {
212   public:
213         SessionMetadataDialog (std::string const & name);
214
215   protected:
216         void init_data ( bool skip_user = false );
217         void load_extra_data (ARDOUR::SessionMetadata const & data);
218         void save_data ();
219
220         virtual void init_gui () = 0;
221         virtual void save_and_close ();
222         virtual void end_dialog ();
223
224         void warn_user (std::string const & string);
225
226         typedef std::list<Gtk::Widget *> WidgetList;
227         typedef boost::shared_ptr<WidgetList> WidgetListPtr;
228         typedef Gtk::Widget & (DataSet::*WidgetFunc) ();
229
230         /// Returns list of widgets gathered by calling f for each data set
231         WidgetListPtr get_custom_widgets (WidgetFunc f);
232
233         /// Adds a widget to the table (vertical stacking) with automatic spacing
234         void add_widget (Gtk::Widget & widget);
235
236         Gtk::Notebook     notebook;
237
238   private:
239         void init_user_data ();
240         void init_track_data ();
241         void init_album_data ();
242         void init_people_data ();
243         void init_school_data ();
244
245         typedef boost::shared_ptr<SessionMetadataSet> DataSetPtr;
246         typedef std::list<DataSetPtr> DataSetList;
247         DataSetList data_list;
248
249         Gtk::Button *     save_button;
250         Gtk::Button *     cancel_button;
251 };
252
253 class SessionMetadataEditor : public SessionMetadataDialog<SessionMetadataSetEditable> {
254   public:
255         SessionMetadataEditor ();
256         ~SessionMetadataEditor ();
257         void run ();
258   private:
259         void init_gui ();
260 };
261
262 class SessionMetadataImporter : public SessionMetadataDialog<SessionMetadataSetImportable> {
263   public:
264         SessionMetadataImporter ();
265         ~SessionMetadataImporter ();
266         void run ();
267
268   private:
269         void init_gui ();
270
271         // Select all from -widget
272         Gtk::HBox    selection_hbox;
273         Gtk::Label   selection_label;
274
275 };
276
277 #endif