1/*
2 * This file is part of GNOME LaTeX.
3 *
4 * Copyright © 2012 Sébastien Wilmet
5 *
6 * GNOME LaTeX is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * GNOME LaTeX is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with GNOME LaTeX.  If not, see <http://www.gnu.org/licenses/>.
18 *
19 * Author: Sébastien Wilmet
20 */
21
22using Gtk;
23
24// Lists of LaTeX symbols, displayed in the side panel.
25// There are different categories, listed in a combo box.
26// There is a special category that contains the most used symbols, with a clear button.
27public class SymbolsView : Grid
28{
29    private unowned MainWindow _main_window;
30    private ComboBox _combo_box;
31    private IconView _symbol_view;
32    private Button _clear_button;
33
34    public SymbolsView (MainWindow main_window)
35    {
36        _main_window = main_window;
37
38        create_combo_box ();
39        create_icon_view ();
40        create_clear_button ();
41
42        // Packing widgets
43        orientation = Orientation.VERTICAL;
44        set_row_spacing (3);
45
46        add (_combo_box);
47
48        ScrolledWindow sw = Utils.add_scrollbar (_symbol_view);
49        sw.set_shadow_type (ShadowType.IN);
50        add (sw);
51
52        add (_clear_button);
53
54        show_all ();
55
56        _combo_box.set_active (0);
57    }
58
59    private void create_combo_box ()
60    {
61        TreeModel categories_model = Symbols.get_default ().get_categories_model ();
62
63        _combo_box = new ComboBox.with_model (categories_model);
64        _combo_box.hexpand = true;
65        _combo_box.margin_end = 3;
66
67        CellRendererPixbuf pixbuf_renderer = new CellRendererPixbuf ();
68        _combo_box.pack_start (pixbuf_renderer, false);
69        _combo_box.set_attributes (pixbuf_renderer,
70            "icon-name", SymbolsCategoryColumn.ICON, null);
71
72        CellRendererText text_renderer = new CellRendererText ();
73        text_renderer.ellipsize_set = true;
74        text_renderer.ellipsize = Pango.EllipsizeMode.END;
75        _combo_box.pack_start (text_renderer, true);
76        _combo_box.set_attributes (text_renderer,
77            "text", SymbolsCategoryColumn.NAME, null);
78
79        _combo_box.changed.connect (() =>
80        {
81            TreeIter iter = {};
82
83            if (!_combo_box.get_active_iter (out iter))
84                return;
85
86            Gtk.ListStore store;
87            SymbolsCategoryType type;
88
89            categories_model.get (iter,
90                SymbolsCategoryColumn.SYMBOLS_STORE, out store,
91                SymbolsCategoryColumn.TYPE, out type
92            );
93
94            if (_symbol_view != null)
95                _symbol_view.set_model (store);
96
97            if (type == SymbolsCategoryType.MOST_USED)
98                _clear_button.show ();
99            else
100                _clear_button.hide ();
101        });
102    }
103
104    private void create_icon_view ()
105    {
106        /* show the symbols */
107        _symbol_view = new IconView ();
108        _symbol_view.set_pixbuf_column (SymbolColumn.PIXBUF);
109        _symbol_view.set_tooltip_column (SymbolColumn.TOOLTIP);
110        _symbol_view.set_selection_mode (SelectionMode.SINGLE);
111        _symbol_view.spacing = 0;
112        _symbol_view.row_spacing = 0;
113        _symbol_view.column_spacing = 0;
114        _symbol_view.expand = true;
115
116        _symbol_view.selection_changed.connect (() =>
117        {
118            if (_main_window.active_tab == null)
119            {
120                _symbol_view.unselect_all ();
121                return;
122            }
123
124            var selected_items = _symbol_view.get_selected_items ();
125
126            // Unselect the symbol, so the user can insert several times the same symbol.
127            _symbol_view.unselect_all ();
128
129            TreePath path = selected_items.nth_data (0);
130            TreeModel model = _symbol_view.get_model ();
131            TreeIter iter = {};
132
133            if (path != null && model.get_iter (out iter, path))
134            {
135                string latex_command;
136                string id;
137
138                model.get (iter,
139                    SymbolColumn.COMMAND, out latex_command,
140                    SymbolColumn.ID, out id
141                );
142
143                // insert the symbol in the current document
144                _main_window.active_document.begin_user_action ();
145                _main_window.active_document.insert_at_cursor (@"$latex_command ", -1);
146                _main_window.active_document.end_user_action ();
147                _main_window.active_view.grab_focus ();
148
149                // insert to most used symbol
150                MostUsedSymbols.get_default ().increment_symbol (id);
151            }
152        });
153    }
154
155    private void create_clear_button ()
156    {
157        _clear_button = new Button.with_mnemonic (_("_Clear"));
158        _clear_button.margin_end = 3;
159        _clear_button.tooltip_text = _("Clear most used symbols");
160
161        _clear_button.clicked.connect (() =>
162        {
163            MostUsedSymbols.get_default ().clear ();
164        });
165    }
166}
167