]> git.lyx.org Git - lyx.git/blob - src/frontends/gtk/GToc.C
Display accelerator (binding) labels in menus.
[lyx.git] / src / frontends / gtk / GToc.C
1 /**
2  * \file GToc.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author John Spray
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "GToc.h"
14 #include "ControlToc.h"
15
16 #include "ghelpers.h"
17
18 using std::vector;
19 using std::string;
20
21 namespace lyx {
22 namespace frontend {
23
24 GToc::GToc(Dialog & parent)
25         : GViewCB<ControlToc, GViewGladeB>(parent, _("Table of Contents"), false)
26 {}
27
28
29 void GToc::doBuild()
30 {
31         string const gladeName = findGladeFile("toc");
32         xml_ = Gnome::Glade::Xml::create(gladeName);
33
34         Gtk::Button * button;
35         xml_->get_widget("Close", button);
36         setCancel(button);
37         xml_->get_widget("Refresh", button);
38         button->signal_clicked().connect(
39                 sigc::mem_fun(*this, &GToc::updateContents));
40
41         changing_views_ = true;
42
43         // For both liststores
44         listCols_.add(listCol_);
45         listCols_.add(listColIndex_);
46
47         // TOC ListView
48         xml_->get_widget("Contents", tocview_);
49         tocstore_ = Gtk::ListStore::create(listCols_);
50         tocview_->set_model(tocstore_);
51         tocview_->append_column("Entry", listCol_);
52
53         listSel_ = tocview_->get_selection();
54         listSel_->signal_changed().connect(
55                 sigc::mem_fun(*this, &GToc::onTocViewSelected));
56
57         // Type Selection Combobox
58         xml_->get_widget("Type", typecombo_);
59         typestore_ = Gtk::ListStore::create(listCols_);
60         typecombo_->set_model(typestore_);
61         Gtk::CellRendererText * cell = Gtk::manage(new Gtk::CellRendererText);
62         typecombo_->pack_start(*cell, true);
63         typecombo_->add_attribute(*cell,"text",0);
64         typecombo_->set_size_request(130,-1);
65
66         typecombo_->signal_changed().connect(
67                 sigc::mem_fun(*this, &GToc::onTypeComboChanged));
68
69         changing_views_ = false;
70 }
71
72
73 void GToc::update()
74 {
75         updateType();
76         updateContents();
77 }
78
79
80 void GToc::updateType()
81 {
82         changing_views_ = true;
83         string const targettype =
84                 toc::getType(controller().params().getCmdName());
85
86         typestore_->clear();
87         vector<string> types = controller().getTypes();
88         
89         // Because tiny empty ComboBoxes just look silly
90         int const emptycombosize = 130;
91         typecombo_->set_size_request(types.empty() ? emptycombosize : -1, -1);
92         
93         vector<string>::iterator it = types.begin();
94         vector<string>::iterator end = types.end();
95         for(;it != end; ++it) {
96                 Gtk::TreeModel::iterator row = typestore_->append();
97                 (*row)[listCol_] = *it;
98                 if (*it == targettype)
99                         typecombo_->set_active(row);
100         }
101         changing_views_ = false;
102 }
103
104
105 void GToc::updateContents()
106 {
107         if (typestore_->children().empty()) {
108                 tocstore_->clear();
109                 (*tocstore_->append())[listCol_] = _("*** No Lists ***");
110                 tocview_->set_sensitive(false);
111                 return;
112         }
113
114         Gtk::TreeModel::iterator it = typecombo_->get_active();
115         Glib::ustring const type = (*it)[listCol_];
116         toc::Toc const contents = controller().getContents(type);
117
118         // Check if all elements are the same.
119         if (toc_ == contents) {
120                 return;
121         }
122
123         // List has changed, so let's update our view
124         toc_ = contents;
125
126         if (contents.empty()) {
127                 tocstore_->clear();
128                 (*tocstore_->append())[listCol_] = _("*** No Items ***");
129                 tocview_->set_sensitive(false);
130                 return;
131         }
132
133         // Okay, we're definitely going to put stuff in now
134         changing_views_ = true;
135         tocview_->set_sensitive(true);
136         tocstore_->clear();
137
138         toc::Toc::const_iterator cit = contents.begin();
139         toc::Toc::const_iterator end = contents.end();
140         
141         for (int rowindex = 0; cit != end; ++cit, ++rowindex) {
142                 Gtk::ListStore::iterator row = tocstore_->append();
143                 (*row)[listCol_] = cit->asString();
144                 (*row)[listColIndex_] = rowindex;
145         }
146         changing_views_ = false;
147 }
148
149
150 void GToc::onTocViewSelected()
151 {
152         // If we always do this, then an item is jumped to without
153         // the user clicking on one when he changes type from TOC->figures or so
154         if (!changing_views_) {
155                 unsigned int choice = (*listSel_->get_selected())[listColIndex_];
156                 if (choice < toc_.size()) {
157                         controller().goTo(toc_[choice]);
158                 }
159         }
160 }
161
162
163 void GToc::onTypeComboChanged()
164 {
165         updateContents();
166 }
167
168
169 } // namespace frontend
170 } // namespace lyx