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
267
|
/*
Copyright (c) 2011 by Simon Schneegans
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, see <http://www.gnu.org/licenses/>.
*/
namespace GnomePie {
/////////////////////////////////////////////////////////////////////////
/// A list, containing one entry for each existing Pie.
/////////////////////////////////////////////////////////////////////////
class PieList : Gtk.TreeView {
/////////////////////////////////////////////////////////////////////
/// This signal gets emitted when the user selects a new Pie.
/////////////////////////////////////////////////////////////////////
public signal void on_select(string id);
/////////////////////////////////////////////////////////////////////
/// Stores the data internally.
/////////////////////////////////////////////////////////////////////
private Gtk.ListStore data;
private enum DataPos {ICON, ICON_NAME, NAME, ID}
/////////////////////////////////////////////////////////////////////
/// Stores where a drag startet.
/////////////////////////////////////////////////////////////////////
private Gtk.TreeIter? drag_start = null;
/////////////////////////////////////////////////////////////////////
/// Rembers the time when a last drag move event was reported. Used
/// to avoid frequent changes of selected Pie when a Pie is dragged
/// over this widget.
/////////////////////////////////////////////////////////////////////
private uint last_hover = 0;
/////////////////////////////////////////////////////////////////////
/// C'tor, constructs the Widget.
/////////////////////////////////////////////////////////////////////
public PieList() {
GLib.Object();
this.data = new Gtk.ListStore(4, typeof(Gdk.Pixbuf),
typeof(string),
typeof(string),
typeof(string));
this.data.set_sort_column_id(DataPos.NAME, Gtk.SortType.ASCENDING);
this.set_model(this.data);
this.set_headers_visible(false);
this.set_grid_lines(Gtk.TreeViewGridLines.NONE);
this.width_request = 170;
this.set_enable_search(false);
this.set_events(Gdk.EventMask.POINTER_MOTION_MASK);
var main_column = new Gtk.TreeViewColumn();
var icon_render = new Gtk.CellRendererPixbuf();
icon_render.xpad = 4;
icon_render.ypad = 4;
main_column.pack_start(icon_render, false);
var name_render = new Gtk.CellRendererText();
name_render.ellipsize = Pango.EllipsizeMode.END;
name_render.ellipsize_set = true;
main_column.pack_start(name_render, true);
base.append_column(main_column);
main_column.add_attribute(icon_render, "pixbuf", DataPos.ICON);
main_column.add_attribute(name_render, "text", DataPos.NAME);
// setup drag'n'drop
Gtk.TargetEntry uri_source = {"text/uri-list", 0, 0};
Gtk.TargetEntry[] entries = { uri_source };
this.enable_model_drag_source(Gdk.ModifierType.BUTTON1_MASK, entries, Gdk.DragAction.LINK);
this.enable_model_drag_dest(entries, Gdk.DragAction.COPY | Gdk.DragAction.MOVE | Gdk.DragAction.LINK);
this.drag_data_get.connect(this.on_dnd_source);
this.drag_data_received.connect(this.on_dnd_received);
this.drag_begin.connect_after(this.on_start_drag);
this.drag_motion.connect(this.on_drag_move);
this.drag_leave.connect(() => {
this.last_hover = 0;
});
this.get_selection().changed.connect(() => {
Gtk.TreeIter active;
if (this.get_selection().get_selected(null, out active)) {
string id = "";
this.data.get(active, DataPos.ID, out id);
this.on_select(id);
}
});
reload_all();
}
/////////////////////////////////////////////////////////////////////
/// Loads all existing Pies to the list.
/////////////////////////////////////////////////////////////////////
public void reload_all() {
Gtk.TreeIter active;
string id = "";
if (this.get_selection().get_selected(null, out active))
this.data.get(active, DataPos.ID, out id);
data.clear();
foreach (var pie in PieManager.all_pies.entries) {
this.load_pie(pie.value);
}
select(id);
}
/////////////////////////////////////////////////////////////////////
/// Selects the first Pie.
/////////////////////////////////////////////////////////////////////
public void select_first() {
Gtk.TreeIter active;
if(this.data.get_iter_first(out active) ) {
this.get_selection().select_iter(active);
string id = "";
this.data.get(active, DataPos.ID, out id);
this.on_select(id);
} else {
this.on_select("");
}
}
/////////////////////////////////////////////////////////////////////
/// Selects the Pie with the given ID.
/////////////////////////////////////////////////////////////////////
public void select(string id) {
this.data.foreach((model, path, iter) => {
string pie_id;
this.data.get(iter, DataPos.ID, out pie_id);
if (id == pie_id) {
this.get_selection().select_iter(iter);
return true;
}
return false;
});
}
/////////////////////////////////////////////////////////////////////
/// Loads one given pie to the list.
/////////////////////////////////////////////////////////////////////
private void load_pie(Pie pie) {
if (pie.id.length == 3) {
Gtk.TreeIter last;
this.data.append(out last);
var icon = new Icon(pie.icon, 24);
this.data.set(last, DataPos.ICON, icon.to_pixbuf(),
DataPos.ICON_NAME, pie.icon,
DataPos.NAME, pie.name,
DataPos.ID, pie.id);
}
}
/////////////////////////////////////////////////////////////////////
/// Called when a drag which started on this Widget was successfull.
/////////////////////////////////////////////////////////////////////
private void on_dnd_source(Gdk.DragContext context, Gtk.SelectionData selection_data, uint info, uint time_) {
if (this.drag_start != null) {
string id = "";
this.data.get(this.drag_start, DataPos.ID, out id);
selection_data.set_uris({"file://" + Paths.launchers + "/" + id + ".desktop"});
}
}
/////////////////////////////////////////////////////////////////////
/// Called when a drag operation is started on this Widget.
/////////////////////////////////////////////////////////////////////
private void on_start_drag(Gdk.DragContext ctx) {
if (this.get_selection().get_selected(null, out this.drag_start)) {
string icon_name = "";
this.data.get(this.drag_start, DataPos.ICON_NAME, out icon_name);
var icon = new Icon(icon_name, 48);
var pixbuf = icon.to_pixbuf();
Gtk.drag_set_icon_pixbuf(ctx, pixbuf, icon.size()/2, icon.size()/2);
}
}
/////////////////////////////////////////////////////////////////////
/// Called when something is dragged over this Widget.
/////////////////////////////////////////////////////////////////////
private bool on_drag_move(Gdk.DragContext context, int x, int y, uint time) {
Gtk.TreeViewDropPosition position;
Gtk.TreePath path;
if (!this.get_dest_row_at_pos(x, y, out path, out position))
return false;
if (position == Gtk.TreeViewDropPosition.BEFORE)
this.set_drag_dest_row(path, Gtk.TreeViewDropPosition.INTO_OR_BEFORE);
else if (position == Gtk.TreeViewDropPosition.AFTER)
this.set_drag_dest_row(path, Gtk.TreeViewDropPosition.INTO_OR_AFTER);
Gdk.drag_status(context, context.get_suggested_action(), time);
// avoid too frequent selection...
this.last_hover = time;
GLib.Timeout.add(150, () => {
if (this.last_hover == time)
this.get_selection().select_path(path);
return false;
});
return true;
}
/////////////////////////////////////////////////////////////////////
/// Called when the user finishes a drag operation on this widget.
/// Only used for external drags.
/////////////////////////////////////////////////////////////////////
private void on_dnd_received(Gdk.DragContext context, int x, int y,
Gtk.SelectionData selection_data, uint info, uint time_) {
Gtk.TreeIter active;
if (this.get_selection().get_selected(null, out active)) {
string id = "";
this.data.get(active, DataPos.ID, out id);
var pie = PieManager.all_pies[id];
foreach (var uri in selection_data.get_uris()) {
pie.add_action(ActionRegistry.new_for_uri(uri), 0);
}
this.on_select(id);
}
}
}
}
|