Add initial right mouse menu to DataView.
[gmpc.git] / src / Widgets / gmpc-data-view.vala
bloba3f4d594fd9e11bb39140a6402bdeafb0cf02f4a
1 using Gmpc;
3 using Gtk;
5 const string log_domain = "Gmpc.DataView";
7 /** The Default column width. */
8 const int default_column_width = 200;
10 /**
11 * List of columns.
12 * * List of column ids to show.
13 * * Name of each column
14 * * Default set of enabled columns.
16 const int NUM_COLS = 20;
17 const int[] gmpc_data_view_col_ids = {
18 Gmpc.MpdData.ColumnTypes.MARKUP,
19 Gmpc.MpdData.ColumnTypes.SONG_ARTIST, /* album name */
20 Gmpc.MpdData.ColumnTypes.SONG_ALBUM, /* album name */
21 Gmpc.MpdData.ColumnTypes.SONG_TITLE, /* song title */
22 Gmpc.MpdData.ColumnTypes.SONG_TITLEFILE, /* song title */
23 Gmpc.MpdData.ColumnTypes.SONG_GENRE, /* song genre */
24 Gmpc.MpdData.ColumnTypes.SONG_TRACK, /* song track */
25 Gmpc.MpdData.ColumnTypes.SONG_NAME, /* stream name */
26 Gmpc.MpdData.ColumnTypes.SONG_COMPOSER, /* composer name */
27 Gmpc.MpdData.ColumnTypes.SONG_PERFORMER, /* performer */
28 Gmpc.MpdData.ColumnTypes.SONG_DATE, /* date */
29 Gmpc.MpdData.ColumnTypes.SONG_LENGTH_FORMAT, /* length formatted */
30 Gmpc.MpdData.ColumnTypes.SONG_DISC, /* disc */
31 Gmpc.MpdData.ColumnTypes.SONG_COMMENT, /* comment */
32 Gmpc.MpdData.ColumnTypes.ICON_ID, /* icon id */
33 Gmpc.MpdData.ColumnTypes.SONG_POS,
34 Gmpc.MpdData.ColumnTypes.SONG_ALBUMARTIST,
35 Gmpc.MpdData.ColumnTypes.PATH_EXTENSION, /* Extension */
36 Gmpc.MpdData.ColumnTypes.PATH_DIRECTORY, /* Directory */
37 Gmpc.MpdData.ColumnTypes.SONG_PRIORITY
39 const string[] gmpc_data_view_col_names = {
40 N_("Markup"),
41 N_("Artist"),
42 N_("Album"),
43 N_("Title"),
44 N_("File"),
45 N_("Genre"),
46 N_("Track"),
47 N_("Name"),
48 N_("Composer"),
49 N_("Performer"),
50 N_("Date"),
51 N_("Duration"),
52 N_("Disc"),
53 N_("Comment"),
54 N_("Icon Id"),
55 N_("Position"),
56 N_("AlbumArtist"),
57 N_("Extension"),
58 N_("Directory"),
59 N_("Priority")
62 const bool[] gmpc_data_view_col_enabled = {
63 false,//"Markup",
64 true, //"Artist",
65 true,//"Album",
66 true,//"Title",
67 false,//"File",
68 false,//"Genre",
69 false,//"Track",
70 false,//"Name",
71 false,//"Composer",
72 false,//"Performer",
73 false,//"Date",
74 false,//"Duration",
75 false,//"Disc",
76 false,//"Comment",
77 true,//"Icon Id"
78 false,//"Position"
79 false,//"AlbumArtist"
80 false,//Extension
81 false,//Directory
82 false//Priority
86 const int[] gmpc_data_view_col_position = {
87 14,//"Markup",
88 3, //"Artist",
89 2,//"Album",
90 1,//"Title",
91 4,//"File",
92 5,//"Genre",
93 6,//"Track",
94 7,//"Name",
95 8,//"Composer",
96 9,//"Performer",
97 10,//"Date",
98 11,//"Duration",
99 12,//"Disc",
100 13,//"Comment",
101 0,//"Icon Id"
102 15, // "Position"
103 18, // "AlbumArtist"
104 16,// Extension
105 17, // Directory
109 public class Gmpc.DataView : Gtk.TreeView
111 private Gtk.TreeViewColumn[] tree_columns = new Gtk.TreeViewColumn[NUM_COLS];
113 * If we are a play-queue we should treat the content.
114 * slightly different.
115 * e.g. add-replace will be play-crop
117 public bool is_play_queue {get; set; default=false;}
120 * The name of the treeview.
121 * This is used to store the column layout.
123 public string uid {get; set; default="default";}
127 * Construction function.
129 public DataView(string name)
131 log(log_domain, LogLevelFlags.LEVEL_INFO, "Constructing dataview: "+name);
133 this.uid = name;
134 // Connect row activated signal.
135 this.row_activated.connect(__row_activated);
136 this.key_release_event.connect(__key_release_event_callback);
137 this.button_press_event.connect(__button_press_event_callback);
138 this.button_release_event.connect(__button_release_event_callback);
139 // When it getst he destroy signal.
140 this.destroy.connect(column_store_state);
142 this.set_rules_hint(true);
144 this.get_selection().set_mode(Gtk.SelectionMode.MULTIPLE);
145 this.set_fixed_height_mode(true);
146 // Create the view.
147 column_populate();
152 * Deconstructor.
154 ~DataView()
159 public void right_mouse_menu(Gtk.Menu menu)
161 int selected_rows = this.get_selection().count_selected_rows();
162 if(selected_rows == 1) {
163 var item = new Gtk.ImageMenuItem.from_stock(Gtk.STOCK_MEDIA_PLAY,null);
164 item.activate.connect((source)=>{
165 selected_songs_play();
167 menu.append(item);
170 if(is_play_queue)
172 // Add play if there is one selected row.
173 if(selected_rows > 0) {
174 var item = new Gtk.ImageMenuItem.from_stock(Gtk.STOCK_REMOVE,null);
175 item.activate.connect((source)=>{
176 selected_songs_remove();
178 menu.append(item);
186 * Internal functions.
190 * Store the position, visibility and width of the columns
192 private void column_store_state()
194 // Save the position of the columns
195 var columns = get_columns();
196 int index = 0;
197 foreach(var column in columns)
199 int col_index = column.get_data("index");
200 int width = column.get_width();
201 config.set_int(uid+"-colpos", gmpc_data_view_col_names[col_index], index);
202 config.set_bool(uid+"-colshow", gmpc_data_view_col_names[col_index], column.visible);
203 // Only store width if bigger then 0.
204 if(width > 0 ) {
205 config.set_int(uid+"-colsize",gmpc_data_view_col_names[col_index], width);
207 index++;
210 // Hack to make vala not destroy the menu directly.
211 private Gtk.Menu column_selection_menu = null;
212 private void column_show_selection_menu()
214 column_selection_menu = new Gtk.Menu();
215 foreach(var col in tree_columns)
217 int index = col.get_data("index");
218 // Do not show the icon id in the selection list.
219 if(gmpc_data_view_col_ids[index] == MpdData.ColumnTypes.ICON_ID) continue;
220 var item = new Gtk.CheckMenuItem.with_label(gmpc_data_view_col_names[index]);
221 if(col.visible) {
222 item.set_active(true);
224 // On activation toggle the state.
225 item.activate.connect((source) => {
226 col.visible = (source as Gtk.CheckMenuItem).get_active();
228 column_selection_menu.append(item);
230 column_selection_menu.show_all();
231 column_selection_menu.popup(null, null, null, 0, Gtk.get_current_event_time());
234 * Populate the treeview with the right columns.
235 * The treeview should have a name now.
237 private void column_populate()
239 for(int i = 0; i < NUM_COLS; i++)
241 Gtk.TreeViewColumn col = new Gtk.TreeViewColumn();
242 col.set_data("index", i);
243 if(gmpc_data_view_col_ids[i] == Gmpc.MpdData.ColumnTypes.ICON_ID)
246 * Picture.
248 var renderer = new Gtk.CellRendererPixbuf();
249 renderer.xalign = 0.0f;
250 // Set up column
251 col.pack_start(renderer, true);
252 col.set_attributes(renderer, "icon-name", Gmpc.MpdData.ColumnTypes.ICON_ID);
253 col.set_resizable(false);
254 col.set_fixed_width(20);
255 col.clickable = true;
256 // If the user clicks on the column, show dropdown allowing to enable/disable columns.
257 col.clicked.connect((source) => {
258 column_show_selection_menu();
260 } else {
262 * Text column
264 col.set_title(gmpc_data_view_col_names[i]);
265 var renderer = new Gtk.CellRendererText();
266 renderer.ellipsize = Pango.EllipsizeMode.END;
267 renderer.weight_set = true;
268 // Set up column
269 col.pack_start(renderer, true);
270 col.set_attributes(renderer, "text", gmpc_data_view_col_ids[i]);
271 col.set_resizable(true);
272 if(is_play_queue) {
273 // TODO fix this.
274 // col.set_cell_data_func(renderer, (Gtk.CellLayoutDataFunc)highlight_row_current_song_playing);
277 int width = config.get_int_with_default(uid+"-colsize",gmpc_data_view_col_names[i], default_column_width);
278 // Do not set to size 0, then revert back to 200.
279 col.set_fixed_width(width>0?width:default_column_width);
281 col.set_sizing(Gtk.TreeViewColumnSizing.FIXED);
282 col.set_reorderable(true);
284 // Fixed width.
285 int pos = config.get_int_with_default(uid+"-colpos", gmpc_data_view_col_names[i], gmpc_data_view_col_position[i]);
286 this.tree_columns[pos] = col;
288 // Add the columns (in right order)
289 for(int i = 0; i < NUM_COLS; i++) {
290 int index = this.tree_columns[i].get_data("index");
291 this.insert_column(this.tree_columns[i], i);
292 this.tree_columns[i].set_visible(config.get_bool_with_default(uid+"-colshow", gmpc_data_view_col_names[index], gmpc_data_view_col_enabled[index]));
301 * Function handles the row-activate signal.
303 private void __row_activated (Gtk.TreePath path, Gtk.TreeViewColumn col)
305 Gtk.TreeModel? model = this.get_model();
306 if(model != null)
308 Gtk.TreeIter iter;
309 if(!model.get_iter(out iter, path)) return;
310 if(is_play_queue) {
311 /* If we are play-queue, play the selected song. */
312 int song_id;
313 model.get(iter, Gmpc.MpdData.ColumnTypes.SONG_ID, out song_id);
314 MPD.Player.play_id(Gmpc.server, song_id);
315 } else {
316 /* If we are a song browser, add the path and play it. */
317 string song_path;
318 model.get(iter, Gmpc.MpdData.ColumnTypes.PATH, out song_path);
319 MpdInteraction.play_path(song_path);
324 * Check if current row is playing.
325 * TODO
327 private void highlight_row_current_song_playing(Gtk.TreeViewColumn col, Gtk.CellRenderer renderer, Gtk.TreeModel model, Gtk.TreeIter iter)
329 (renderer as Gtk.CellRendererText).weight = Pango.Weight.BOLD;
334 * Handle keyboard input.
336 private bool __key_release_event_callback_play_queue(Gdk.EventKey event)
338 if (event.keyval == Gdk.Key_Q)
340 // remove priority.
341 return selected_songs_remove_priority();
343 else if (event.keyval == Gdk.Key_q)
345 // Raise priority.
346 return selected_songs_raise_priority();
348 else if (event.keyval == Gdk.Key_d)
350 if(!selected_songs_remove())
352 // Detach model (for some reason keeping it attached
353 // Makes thing break, work-around for now)
354 // TODO: fixme
355 var model = get_model();
356 this.model = null;
357 // Clear
358 MPD.PlayQueue.clear(server);
359 // Re-add model
360 this.model = model;
361 return true;
364 return false;
366 private bool __key_release_event_callback(Gdk.EventKey event)
368 if(event.keyval == Gdk.Key_y)
370 // Copy data to clipboard
373 else if (event.keyval == Gdk.Key_c)
375 // Cut (if available) into clipboard
377 else if (event.keyval == Gdk.Key_P)
379 // Paste before
381 else if (event.keyval == Gdk.Key_p)
383 // Paste after
385 else if (event.keyval == Gdk.Key_Escape)
389 else if (event.keyval == Gdk.Key_m)
391 // Configure columns
392 column_show_selection_menu();
393 return true;
395 else if (event.keyval == Gdk.Key_Menu)
397 __button_press_menu = new Gtk.Menu();
398 right_mouse_menu(__button_press_menu);
399 __button_press_menu.show_all();
400 __button_press_menu.popup(null, null,null,0, Gtk.get_current_event_time());
401 return true;
404 // Commands specific to play_queue
405 if(is_play_queue)
407 if(__key_release_event_callback_play_queue(event)) return true;
409 else
411 if(event.keyval == Gdk.Key_i)
413 // Insert
416 return false;
420 * Right mouse popup.
422 // Hack to stop vala from destroying my menu.
423 private Gtk.Menu __button_press_menu = null;
424 private bool __button_press_event_callback(Gdk.EventButton event)
426 if(event.button == 3) return true;
427 return false;
429 private bool __button_release_event_callback(Gdk.EventButton event)
431 if(event.button == 3)
433 __button_press_menu = new Gtk.Menu();
434 right_mouse_menu(__button_press_menu);
435 __button_press_menu.show_all();
436 __button_press_menu.popup(null, null,null, event.button, event.time);
437 return true;
439 return false;
444 * Interaction on selected songs.
446 // Set priority on the selected songs.
447 private bool selected_songs_raise_priority()
449 if(server.check_command_allowed("prioid") != MPD.Server.Command.ALLOWED) return false;
450 var selection = this.get_selection();
452 if(selection.count_selected_rows() > 254) {
453 Gmpc.Messages.show(_("You can only queue 254 songs at the time."), Gmpc.Messages.Level.WARNING);
454 return false;
457 int priority = 255;
458 Gtk.TreeModel model;
459 foreach(var path in selection.get_selected_rows(out model))
461 Gtk.TreeIter iter;
462 if(model.get_iter(out iter, path))
464 int song_id;
465 model.get(iter,Gmpc.MpdData.ColumnTypes.SONG_ID, out song_id);
466 MPD.PlayQueue.set_priority(server, song_id, priority--);
469 return true;
471 // Remove the set priority from the selected songs.
472 private bool selected_songs_remove_priority()
474 if(server.check_command_allowed("prioid") != MPD.Server.Command.ALLOWED) return false;
475 var selection = this.get_selection();
477 Gtk.TreeModel model;
478 foreach(var path in selection.get_selected_rows(out model))
480 Gtk.TreeIter iter;
481 if(model.get_iter(out iter, path))
483 int song_id;
484 model.get(iter,Gmpc.MpdData.ColumnTypes.SONG_ID, out song_id);
485 MPD.PlayQueue.set_priority(server, song_id, 0);
488 return true;
490 // Play the selected song
491 private bool selected_songs_play()
493 var selection = this.get_selection();
494 Gtk.TreeModel model;
495 foreach(var path in selection.get_selected_rows(out model))
497 Gtk.TreeIter iter;
498 if(model.get_iter(out iter, path))
500 if(is_play_queue) {
501 int song_id;
502 model.get(iter, Gmpc.MpdData.ColumnTypes.SONG_ID, out song_id);
503 if(song_id >= 0){
504 MPD.Player.play_id(server, song_id);
505 return true;
507 }else{
508 string song_path;
509 model.get(iter, Gmpc.MpdData.ColumnTypes.PATH, out song_path);
510 MpdInteraction.play_path(song_path);
511 return true;
515 return selection.count_selected_rows() > 0;
517 // Remove the selected songs from the play queue.
518 private bool selected_songs_remove()
520 int deleted_rows = 0;
521 var selection = this.get_selection();
523 Gtk.TreeModel model;
524 foreach(var path in selection.get_selected_rows(out model))
526 Gtk.TreeIter iter;
527 if(model.get_iter(out iter, path))
529 int song_id;
530 model.get(iter,Gmpc.MpdData.ColumnTypes.SONG_ID, out song_id);
531 MPD.PlayQueue.queue_delete_id(server, song_id);
532 deleted_rows++;
535 MPD.PlayQueue.queue_commit(server);
536 return (deleted_rows > 0);