| 1 | /* i4ucauthorslist.vala |
|---|
| 2 | * |
|---|
| 3 | * Copyright (C) 2010 Matias De la Puente |
|---|
| 4 | * |
|---|
| 5 | * This program is free software: you can redistribute it and/or modify |
|---|
| 6 | * it under the terms of the GNU General Public License as published by |
|---|
| 7 | * the Free Software Foundation, either version 3 of the License, or |
|---|
| 8 | * (at your option) any later version. |
|---|
| 9 | * |
|---|
| 10 | * This program is distributed in the hope that it will be useful, |
|---|
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 13 | * GNU General Public License for more details. |
|---|
| 14 | * |
|---|
| 15 | * You should have received a copy of the GNU General Public License |
|---|
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
|---|
| 17 | * |
|---|
| 18 | * Author: |
|---|
| 19 | * Matias De la Puente <mfpuente.ar@gmail.com> |
|---|
| 20 | */ |
|---|
| 21 | using Gtk; |
|---|
| 22 | using Gee; |
|---|
| 23 | |
|---|
| 24 | public class I4uc.AuthorsList : Gtk.VBox |
|---|
| 25 | { |
|---|
| 26 | private ListStore _authors_store; |
|---|
| 27 | private TreeView _authors_view; |
|---|
| 28 | |
|---|
| 29 | private ArrayList<string> _authors = new ArrayList<string> (); |
|---|
| 30 | |
|---|
| 31 | public Gee.List<string> authors { owned get { return _authors.read_only_view; } } |
|---|
| 32 | |
|---|
| 33 | private enum AuthorsColumns |
|---|
| 34 | { |
|---|
| 35 | AUTHOR, |
|---|
| 36 | N_COLUMNS |
|---|
| 37 | } |
|---|
| 38 | |
|---|
| 39 | public signal void author_added (string author); |
|---|
| 40 | public signal void author_removed (string author); |
|---|
| 41 | |
|---|
| 42 | public AuthorsList () |
|---|
| 43 | { |
|---|
| 44 | _authors_store = new ListStore (AuthorsColumns.N_COLUMNS, typeof (string)); |
|---|
| 45 | _authors_view = new TreeView.with_model (_authors_store); |
|---|
| 46 | |
|---|
| 47 | var column = new TreeViewColumn (); |
|---|
| 48 | var renderer = new CellRendererText (); |
|---|
| 49 | column.pack_start (renderer, false); |
|---|
| 50 | column.add_attribute (renderer, "text", AuthorsColumns.AUTHOR); |
|---|
| 51 | _authors_view.append_column (column); |
|---|
| 52 | _authors_view.set_headers_visible (false); |
|---|
| 53 | |
|---|
| 54 | var add_author_button = new Button.from_stock (STOCK_ADD); |
|---|
| 55 | var remove_author_button = new Button.from_stock (STOCK_REMOVE); |
|---|
| 56 | |
|---|
| 57 | var buttons_vbox = new VBox (false, 0); |
|---|
| 58 | buttons_vbox.pack_start (add_author_button, false, false, 2); |
|---|
| 59 | buttons_vbox.pack_start (remove_author_button, false, false, 2); |
|---|
| 60 | |
|---|
| 61 | var authors_hbox = new HBox (false, 0); |
|---|
| 62 | authors_hbox.pack_start (_authors_view, true, true, 2); |
|---|
| 63 | authors_hbox.pack_start (buttons_vbox, false, false, 0); |
|---|
| 64 | |
|---|
| 65 | pack_start (authors_hbox, true, true, 2); |
|---|
| 66 | |
|---|
| 67 | add_author_button.clicked.connect (on_add_author); |
|---|
| 68 | remove_author_button.clicked.connect (on_remove_author); |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | private void on_add_author () |
|---|
| 72 | { |
|---|
| 73 | var dialog = new Dialog (); |
|---|
| 74 | dialog.title = _("Add new author"); |
|---|
| 75 | var label = new Label (_("Insert new author name:")); |
|---|
| 76 | dialog.vbox.pack_start (label, false, false, 5); |
|---|
| 77 | var entry = new Entry (); |
|---|
| 78 | dialog.vbox.pack_start (entry, true, true, 5); |
|---|
| 79 | dialog.vbox.show_all (); |
|---|
| 80 | dialog.add_button (STOCK_CANCEL, ResponseType.CANCEL); |
|---|
| 81 | dialog.add_button (STOCK_OK, ResponseType.OK); |
|---|
| 82 | dialog.set_default_response (ResponseType.OK); |
|---|
| 83 | |
|---|
| 84 | if (dialog.run () == ResponseType.OK) |
|---|
| 85 | { |
|---|
| 86 | TreeIter iter; |
|---|
| 87 | _authors_store.append (out iter); |
|---|
| 88 | _authors_store.set (iter, AuthorsColumns.AUTHOR, entry.text); |
|---|
| 89 | _authors.add (entry.text); |
|---|
| 90 | this.author_added (entry.text); |
|---|
| 91 | } |
|---|
| 92 | dialog.destroy (); |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | private void on_remove_author () |
|---|
| 96 | { |
|---|
| 97 | TreeIter iter; |
|---|
| 98 | if (_authors_view.get_selection ().get_selected (null, out iter)) |
|---|
| 99 | { |
|---|
| 100 | string author; |
|---|
| 101 | _authors_store.get (iter, AuthorsColumns.AUTHOR, out author); |
|---|
| 102 | _authors_store.remove (iter); |
|---|
| 103 | _authors.remove (author); |
|---|
| 104 | this.author_removed (author); |
|---|
| 105 | } |
|---|
| 106 | } |
|---|
| 107 | } |
|---|