1### Copyright (C) 2002-2009 Stephen Kennedy <stevek@gnome.org>
2### Copyright (C) 2010-2011 Kai Willadsen <kai.willadsen@gmail.com>
3
4### This program is free software; you can redistribute it and/or modify
5### it under the terms of the GNU General Public License as published by
6### the Free Software Foundation; either version 2 of the License, or
7### (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
12### GNU General Public License for more details.
13
14### You should have received a copy of the GNU General Public License
15### along with this program; if not, write to the Free Software
16### Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
17### USA.
18
19
20from meld import paths
21from . import gnomeglade
22
23
24class ListWidget(gnomeglade.Component):
25
26    def __init__(self, ui_file, widget, store, treeview, new_row_data=None):
27        gnomeglade.Component.__init__(self, paths.ui_dir(ui_file),
28                                      widget, store)
29        self.new_row_data = new_row_data
30        self.list = getattr(self, treeview)
31        self.model = self.list.get_model()
32        selection = self.list.get_selection()
33        selection.connect("changed", self._update_sensitivity)
34
35    def _update_sensitivity(self, *args):
36        (model, it, path) = self._get_selected()
37        if not it:
38            self.remove.set_sensitive(False)
39            self.move_up.set_sensitive(False)
40            self.move_down.set_sensitive(False)
41        else:
42            self.remove.set_sensitive(True)
43            self.move_up.set_sensitive(path > 0)
44            self.move_down.set_sensitive(path < len(model) - 1)
45
46    def _get_selected(self):
47        (model, it) = self.list.get_selection().get_selected()
48        if it:
49            path = model.get_path(it)[0]
50        else:
51            path = None
52        return (model, it, path)
53
54    def on_add_clicked(self, button):
55        self.model.append(self.new_row_data)
56
57    def on_remove_clicked(self, button):
58        (model, it, path) = self._get_selected()
59        self.model.remove(it)
60
61    def on_move_up_clicked(self, button):
62        (model, it, path) = self._get_selected()
63        model.swap(it, model.get_iter(path - 1))
64
65    def on_move_down_clicked(self, button):
66        (model, it, path) = self._get_selected()
67        model.swap(it, model.get_iter(path + 1))
68