use Gtk::Label::set_text() rather than Gtk::Label::set_markup() where no markup is...
[ardour.git] / gtk2_ardour / search_path_option.cc
1 /*
2     Copyright (C) 2010 Paul Davis
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
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19 #include "pbd/strsplit.h"
20 #include "pbd/compose.h"
21 #include "search_path_option.h"
22 #include "i18n.h"
23
24 using namespace std;
25 using namespace Gtk;
26
27 SearchPathOption::SearchPathOption (const string& pathname, const string& label,
28                                     sigc::slot<std::string> get, sigc::slot<bool, std::string> set)
29         : Option (pathname, label)
30         , _get (get)
31         , _set (set)
32         , add_chooser (_("Select folder to search for media"), FILE_CHOOSER_ACTION_SELECT_FOLDER)
33 {
34         add_chooser.signal_file_set().connect (sigc::mem_fun (*this, &SearchPathOption::path_chosen));
35
36         HBox* hbox = manage (new HBox);
37
38         hbox->set_border_width (12);
39         hbox->set_spacing (6);
40         hbox->pack_end (add_chooser, true, true);
41         hbox->pack_end (*manage (new Label (_("Click to add a new location"))), false, false);
42         hbox->show_all ();
43
44         vbox.pack_start (path_box);
45         vbox.pack_end (*hbox);
46
47         session_label.set_use_markup (true);
48         session_label.set_markup (string_compose ("<i>%1</i>", _("the session folder")));
49         session_label.set_alignment (0.0, 0.5);
50         session_label.show ();
51
52         path_box.pack_start (session_label);
53 }
54
55 SearchPathOption::~SearchPathOption()
56 {
57
58
59 }
60
61 void
62 SearchPathOption::path_chosen ()
63 {
64         string path = add_chooser.get_filename ();
65         add_path (path);
66         changed ();
67 }
68
69 void
70 SearchPathOption::add_to_page (OptionEditorPage* p)
71 {
72         int const n = p->table.property_n_rows();
73         p->table.resize (n + 1, 3);
74
75         Label* label = manage (new Label);
76         label->set_alignment (0.0, 0.0);
77         label->set_text (string_compose ("%1", _name));
78
79         p->table.attach (*label, 1, 2, n, n + 1, FILL | EXPAND);
80         p->table.attach (vbox, 2, 3, n, n + 1, FILL | EXPAND);
81 }
82
83 void
84 SearchPathOption::clear ()
85 {
86         path_box.remove (session_label);
87         for (list<PathEntry*>::iterator p = paths.begin(); p != paths.end(); ++p) {
88                 path_box.remove ((*p)->box);
89                 delete *p;
90         }
91         paths.clear ();
92 }
93
94 void
95 SearchPathOption::set_state_from_config ()
96 {
97         string str = _get ();
98         vector<string> dirs;
99
100         clear ();
101         path_box.pack_start (session_label);
102
103         split (str, dirs, ':');
104
105         for (vector<string>::iterator d = dirs.begin(); d != dirs.end(); ++d) {
106                 add_path (*d);
107         }
108 }
109
110 void
111 SearchPathOption::changed ()
112 {
113         string str;
114
115         for (list<PathEntry*>::iterator p = paths.begin(); p != paths.end(); ++p) {
116
117                 if (!str.empty()) {
118                         str += ':';
119                 }
120                 str += (*p)->entry.get_text ();
121         }
122
123         _set (str);
124 }
125
126 void
127 SearchPathOption::add_path (const string& path, bool removable)
128 {
129         PathEntry* pe = new PathEntry (path, removable);
130         paths.push_back (pe);
131         path_box.pack_start (pe->box, false, false);
132         pe->remove_button.signal_clicked().connect (sigc::bind (sigc::mem_fun (*this, &SearchPathOption::remove_path), pe));
133 }
134
135 void
136 SearchPathOption::remove_path (PathEntry* pe)
137 {
138         path_box.remove (pe->box);
139         paths.remove (pe);
140         delete pe;
141         changed ();
142 }
143
144 SearchPathOption::PathEntry::PathEntry (const std::string& path, bool removable)
145         : remove_button (Stock::REMOVE)
146 {
147         entry.set_text (path);
148         entry.show ();
149
150         box.set_spacing (6);
151         box.set_homogeneous (false);
152         box.pack_start (entry, true, true);
153
154         if (removable) {
155                 box.pack_start (remove_button, false, false);
156                 remove_button.show ();
157         }
158
159         box.show ();
160 }