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