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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
|
/******************************************************************************
* Copyright (C) 2011-2013 Michael Hofmann <mh21@mh21.de> *
* *
* 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 3 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. *
******************************************************************************/
public class ItemPreferences : Object {
// always allocated
private Settings itemsettings;
// only when dialog is visible
private Gtk.Dialog items;
// helper
private unowned Gtk.ListStore itemstore;
private unowned Gtk.TreeView itemview;
private unowned Gtk.TreeSelection itemselection;
private unowned Gtk.Button itemadd;
private unowned Gtk.Button itemremove;
private unowned Gtk.Button itemedit;
private unowned Gtk.Button itemup;
private unowned Gtk.Button itemdown;
private bool itemsignoresignals;
public string settingskey { get; construct; }
public signal void itemhelp_show();
public ItemPreferences(string settingskey) {
Object(settingskey: settingskey);
}
construct {
this.itemsettings = new SettingsCache().generalsettings();
this.itemsettings.changed[this.settingskey].connect(reset_itemstore);
}
public void show() {
if (this.items != null) {
this.items.present();
return;
}
Gtk.Builder builder;
this.items = Utils.get_ui("itemdialog", this, {"itemstore"}, out builder) as Gtk.Dialog;
return_if_fail(this.items != null);
this.itemstore = builder.get_object("itemstore") as Gtk.ListStore;
this.itemview = builder.get_object("itemview") as Gtk.TreeView;
this.itemadd = builder.get_object("itemadd") as Gtk.Button;
this.itemremove = builder.get_object("itemremove") as Gtk.Button;
this.itemedit = builder.get_object("itemedit") as Gtk.Button;
this.itemup = builder.get_object("itemup") as Gtk.Button;
this.itemdown = builder.get_object("itemdown") as Gtk.Button;
this.itemselection = (builder.get_object("itemview") as Gtk.TreeView).get_selection();
this.itemsgsettingstostore();
// will invoke updatebuttons()
this.itemselection.select_path(new Gtk.TreePath.from_indices(0));
this.items.show_all();
// TODO: F2 does not work
}
public void reset_itemstore() {
if (this.items != null && !this.itemsignoresignals)
this.itemsgsettingstostore();
}
[CCode (instance_pos = -1)]
public void on_itemdialog_destroy(Gtk.Widget source) {
this.items = null;
}
[CCode (instance_pos = -1)]
public void on_itemdialog_response(Gtk.Dialog source, int response) {
switch (response) {
case 0: // close
source.destroy();
return;
case 1: // revert
this.itemsettings.reset(this.settingskey);
this.itemselection.select_path(new Gtk.TreePath.from_indices(0));
return;
case 2: // help
this.itemhelp_show();
return;
}
}
[CCode (instance_pos = -1)]
public void on_expressionrenderer_edited(Gtk.CellRendererText renderer,
string path, string new_text) {
Gtk.TreeIter iter;
this.itemstore.get_iter_from_string(out iter, path);
this.itemstore.set(iter, 0, new_text);
}
[CCode (instance_pos = -1)]
public void on_itemselection_changed(Gtk.TreeSelection selection) {
this.updatebuttons();
}
[CCode (instance_pos = -1)]
public void on_itemstore_row_inserted(Gtk.TreeModel model,
string path, Gtk.TreeIter iter) {
if (!this.itemsignoresignals)
this.itemsstoretogsettings();
}
[CCode (instance_pos = -1)]
public void on_itemstore_row_changed(Gtk.TreeModel model,
string path, Gtk.TreeIter iter) {
if (!this.itemsignoresignals)
itemsstoretogsettings();
}
[CCode (instance_pos = -1)]
public void on_itemstore_row_deleted(Gtk.TreeModel model,
string path) {
if (!this.itemsignoresignals)
this.itemsstoretogsettings();
}
[CCode (instance_pos = -1)]
public void on_itemadd_clicked(Gtk.Button button) {
uint pos = 0;
Gtk.TreeIter iter;
if (this.itemselection.get_selected(null, out iter)) {
var path = this.itemstore.get_path(iter);
var indices = path.get_indices();
pos = indices[0] + 1;
}
this.itemstore.insert(out iter, (int) pos);
this.itemview.grab_focus();
this.itemview.set_cursor(this.itemstore.get_path(iter),
this.itemview.get_column(0), true);
}
[CCode (instance_pos = -1)]
public void on_itemremove_clicked(Gtk.Button button) {
Gtk.TreeIter iter;
if (!this.itemselection.get_selected(null, out iter))
return;
var path = this.itemstore.get_path(iter);
this.itemstore.remove(iter);
if (!this.itemstore.get_iter(out iter, path))
path.prev();
this.itemselection.select_path(path);
}
[CCode (instance_pos = -1)]
public void on_itemedit_clicked(Gtk.Button button) {
Gtk.TreeIter iter;
if (!this.itemselection.get_selected(null, out iter))
return;
this.itemview.grab_focus();
this.itemview.set_cursor(this.itemstore.get_path(iter),
this.itemview.get_column(0), true);
}
[CCode (instance_pos = -1)]
public void on_itemup_clicked(Gtk.Button button) {
Gtk.TreeIter iter;
if (!this.itemselection.get_selected(null, out iter))
return;
Gtk.TreeIter previter;
var prevpath = this.itemstore.get_path(iter);
if (!prevpath.prev())
return;
if (!this.itemstore.get_iter(out previter, prevpath))
return;
Value value, prevvalue;
this.itemstore.get_value(iter, 0, out value);
this.itemstore.get_value(previter, 0, out prevvalue);
this.itemstore.set_value(iter, 0, prevvalue);
this.itemstore.set_value(previter, 0, value);
this.itemselection.select_path(prevpath);
}
[CCode (instance_pos = -1)]
public void on_itemdown_clicked(Gtk.Button button) {
Gtk.TreeIter iter;
if (!this.itemselection.get_selected(null, out iter))
return;
Gtk.TreeIter nextiter;
var nextpath = this.itemstore.get_path(iter);
nextpath.next();
if (!this.itemstore.get_iter(out nextiter, nextpath))
return;
Value value, nextvalue;
this.itemstore.get_value(iter, 0, out value);
this.itemstore.get_value(nextiter, 0, out nextvalue);
this.itemstore.set_value(iter, 0, nextvalue);
this.itemstore.set_value(nextiter, 0, value);
this.itemselection.select_path(nextpath);
}
private void updatebuttons() {
Gtk.TreeIter iter;
bool add = true, remove = false, edit = false, up = false, down = false;
if (this.itemselection.get_selected(null, out iter)) {
edit = true;
remove = true;
var path = this.itemstore.get_path(iter);
var indices = path.get_indices();
up = indices[0] > 0;
down = indices[0] + 1 < this.itemstore.iter_n_children(null);
}
this.itemadd.sensitive = add;
this.itemremove.sensitive = remove;
this.itemedit.sensitive = edit;
this.itemup.sensitive = up;
this.itemdown.sensitive = down;
}
private void itemsgsettingstostore() {
var expressions = this.itemsettings.get_strv(this.settingskey);
this.itemsignoresignals = true;
this.itemstore.clear();
for (uint i = 0, isize = expressions.length; i < isize; ++i)
this.itemstore.insert_with_values(null, (int) i, 0, expressions[i]);
this.itemsignoresignals = false;
}
private void itemsstoretogsettings() {
var result = new string[this.itemstore.iter_n_children(null)];
Gtk.TreeIter iter;
for (uint i = 0, isize = result.length; i < isize; ++i) {
this.itemstore.iter_nth_child(out iter, null, (int)i);
Value value;
this.itemstore.get_value(iter, 0, out value);
result[i] = value as string;
}
this.itemsignoresignals = true;
this.itemsettings.set_strv(this.settingskey, result);
this.itemsignoresignals = false;
}
}
|