summaryrefslogtreecommitdiff
path: root/gtk2_ardour/search_path_option.cc
blob: 74d74de86311105a589ee14af368ce32f5dfc683 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
 * Copyright (C) 2010-2011 Carl Hetherington <carl@carlh.net>
 * Copyright (C) 2010-2016 Paul Davis <paul@linuxaudiosystems.com>
 * Copyright (C) 2014 John Emmas <john@creativepost.co.uk>
 * Copyright (C) 2017-2019 Robin Gareus <robin@gareus.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <gtkmm/stock.h>

#include "pbd/strsplit.h"
#include "pbd/compose.h"
#include "pbd/shortpath.h"

#include "gtkmm2ext/utils.h"

#include "search_path_option.h"
#include "pbd/i18n.h"

using namespace std;
using namespace Gtk;

SearchPathOption::SearchPathOption (const string& pathname, const string& label,
				    const string& default_path,
                                    sigc::slot<std::string> get, sigc::slot<bool, std::string> set)
	: Option (pathname, label)
	, _get (get)
	, _set (set)
	, add_chooser (_("Select folder to search for media"), FILE_CHOOSER_ACTION_SELECT_FOLDER)
{
	Gtkmm2ext::add_volume_shortcuts (add_chooser);
	add_chooser.signal_file_set().connect (sigc::mem_fun (*this, &SearchPathOption::path_chosen));

	HBox* hbox = manage (new HBox);

	hbox->set_border_width (12);
	hbox->set_spacing (6);
	hbox->pack_end (add_chooser, true, true);
	hbox->pack_end (*manage (new Label (_("Click to add a new location"))), false, false);
	hbox->show_all ();

	vbox.pack_start (path_box);
	vbox.pack_end (*hbox);

	session_label.set_use_markup (true);
	session_label.set_markup (string_compose ("<i>%1 (%2)</i>", _("the session folder"), short_path (default_path, 32)));
	session_label.set_alignment (0.0, 0.5);
	session_label.show ();

	path_box.pack_start (session_label);
}

SearchPathOption::~SearchPathOption()
{


}

void
SearchPathOption::path_chosen ()
{
	string path = add_chooser.get_filename ();
	add_path (path);
	changed ();
}

void
SearchPathOption::add_to_page (OptionEditorPage* p)
{
	int const n = p->table.property_n_rows();
	p->table.resize (n + 1, 3);

	Label* label = manage (new Label);
	label->set_alignment (0.0, 0.0);
	label->set_text (string_compose ("%1", _name));

	p->table.attach (*label, 1, 2, n, n + 1, FILL | EXPAND);
	p->table.attach (vbox, 2, 3, n, n + 1, FILL | EXPAND);
}

void
SearchPathOption::clear ()
{
	path_box.remove (session_label);
	for (list<PathEntry*>::iterator p = paths.begin(); p != paths.end(); ++p) {
		path_box.remove ((*p)->box);
		delete *p;
	}
	paths.clear ();
}

void
SearchPathOption::set_state_from_config ()
{
	string str = _get ();
	vector<string> dirs;

	clear ();
	path_box.pack_start (session_label);

	split (str, dirs, G_SEARCHPATH_SEPARATOR);

	for (vector<string>::iterator d = dirs.begin(); d != dirs.end(); ++d) {
		add_path (*d);
	}
}

void
SearchPathOption::changed ()
{
	string str;

	for (list<PathEntry*>::iterator p = paths.begin(); p != paths.end(); ++p) {

		if (!str.empty()) {
			str += G_SEARCHPATH_SEPARATOR;
		}
		str += (*p)->entry.get_text ();
	}

	_set (str);
}

void
SearchPathOption::add_path (const string& path, bool removable)
{
	PathEntry* pe = new PathEntry (path, removable);
	paths.push_back (pe);
	path_box.pack_start (pe->box, false, false);
	pe->remove_button.signal_clicked().connect (sigc::bind (sigc::mem_fun (*this, &SearchPathOption::remove_path), pe));
}

void
SearchPathOption::remove_path (PathEntry* pe)
{
	path_box.remove (pe->box);
	paths.remove (pe);
	delete pe;
	changed ();
}

SearchPathOption::PathEntry::PathEntry (const std::string& path, bool removable)
	: remove_button (Stock::REMOVE)
{
	entry.set_text (path);
	entry.show ();

	box.set_spacing (6);
	box.set_homogeneous (false);
	box.pack_start (entry, true, true);

	if (removable) {
		box.pack_start (remove_button, false, false);
		remove_button.show ();
	}

	box.show ();
}