1/*
2* Copyright (c) 2018 (https://github.com/phase1geo/Minder)
3*
4* This program is free software; you can redistribute it and/or
5* modify it under the terms of the GNU General Public
6* License as published by the Free Software Foundation; either
7* version 2 of the License, or (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 GNU
12* General Public License for more details.
13*
14* You should have received a copy of the GNU General Public
15* License along with this program; if not, write to the
16* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17* Boston, MA 02110-1301 USA
18*
19* Authored by: Trevor Williams <phase1geo@gmail.com>
20*/
21
22using Gtk;
23
24public class GroupsMenu : Gtk.Menu {
25
26  DrawArea     _da;
27  Gtk.MenuItem _delete;
28  Gtk.MenuItem _merge;
29  Gtk.MenuItem _color;
30  Gtk.MenuItem _selnodes;
31  Gtk.MenuItem _selmain;
32
33  /* Default constructor */
34  public GroupsMenu( DrawArea da, AccelGroup accel_group ) {
35
36    _da = da;
37
38    _delete = new Gtk.MenuItem();
39    _delete.add( new Granite.AccelLabel( _( "Delete" ), "Delete" ) );
40    _delete.activate.connect( delete_groups );
41
42    _merge = new Gtk.MenuItem.with_label( _( "Merge" ) );
43    _merge.activate.connect( merge_groups );
44
45    _color = new Gtk.MenuItem.with_label( _( "Change color…" ) );
46    _color.activate.connect( change_color );
47
48    var selmenu = new Gtk.Menu();
49    var select = new Gtk.MenuItem.with_label( _( "Select" ) );
50    select.set_submenu( selmenu );
51
52    _selmain = new Gtk.MenuItem.with_label( _( "Top Nodes" ) );
53    _selmain.activate.connect( select_main );
54
55    _selnodes = new Gtk.MenuItem.with_label( _( "All Grouped Nodes" ) );
56    _selnodes.activate.connect( select_all );
57
58    selmenu.add( _selmain );
59    selmenu.add( _selnodes );
60
61    /* Add the menu items to the menu */
62    add( _delete );
63    add( new SeparatorMenuItem() );
64    add( _color );
65    add( _merge );
66    add( new SeparatorMenuItem() );
67    add( select );
68
69    /* Make the menu visible */
70    show_all();
71
72    /* Make sure that we handle menu state when we are popped up */
73    show.connect( on_popup );
74
75  }
76
77  /* Called when the menu is popped up */
78  private void on_popup() {
79
80    var groups = _da.get_selected_groups();
81    var num    = groups.length;
82
83    /* Set the menu sensitivity */
84    _merge.set_sensitive( num > 1 );
85
86  }
87
88  /* Deletes the current group */
89  private void delete_groups() {
90    _da.remove_groups();
91  }
92
93  /* Merges two or more groups into a single group */
94  private void merge_groups() {
95    _da.add_group();
96  }
97
98  /* Allows the user to change the color of the selected groups */
99  private void change_color() {
100    var color_picker = new ColorChooserDialog( _( "Select a link color" ), _da.win );
101    if( color_picker.run() == ResponseType.OK ) {
102      _da.change_group_color( color_picker.get_rgba() );
103    }
104    color_picker.close();
105  }
106
107  /* Selects the top-most nodes in each selected node group */
108  private void select_main() {
109    var groups   = _da.get_selected_groups();
110    var selected = _da.get_selections();
111    for( int i=0; i<groups.length; i++ ) {
112      var nodes = groups.index( i ).nodes;
113      for( int j=0; j<nodes.length; j++ ) {
114        selected.add_node( nodes.index( j ), false );
115      }
116    }
117    selected.clear_groups();
118  }
119
120  /* Selects all of the nodes within the group */
121  private void select_all() {
122    var groups   = _da.get_selected_groups();
123    var selected = _da.get_selections();
124    for( int i=0; i<groups.length; i++ ) {
125      var nodes = groups.index( i ).nodes;
126      for( int j=0; j<nodes.length; j++ ) {
127        selected.add_node_tree( nodes.index( j ), false );
128      }
129    }
130    selected.clear_groups();
131  }
132
133}
134