]> git.lyx.org Git - lyx.git/blob - src/frontends/gtk/GToc.C
some tabular fixes for the problems reported by Helge
[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, _("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         for(;it != end; ++it) {
104                 Gtk::TreeModel::iterator row = typestore_->append();
105                 (*row)[listCol_] = *it;
106                 if (*it == targettype)
107                         typecombo_->set_active(row);
108         }
109         changing_views_ = false;
110 }
111
112
113 void GToc::updateContents()
114 {
115         if (typestore_->children().empty()) {
116                 tocstore_->clear();
117                 (*tocstore_->append())[listCol_] = _("*** No Lists ***");
118                 tocview_->set_sensitive(false);
119                 return;
120         }
121
122         Gtk::TreeModel::iterator it = typecombo_->get_active();
123         Glib::ustring const type = (*it)[listCol_];
124         toc::Toc const contents = controller().getContents(type);
125
126         // Check if all elements are the same.
127         if (toc_ == contents) {
128                 return;
129         }
130
131         // List has changed, so let's update our view
132         toc_ = contents;
133
134         if (contents.empty()) {
135                 tocstore_->clear();
136                 (*tocstore_->append())[listCol_] = _("*** No Items ***");
137                 tocview_->set_sensitive(false);
138                 return;
139         }
140
141         // Okay, we're definitely going to put stuff in now
142         changing_views_ = true;
143         tocview_->set_sensitive(true);
144         tocstore_->clear();
145
146         toc::Toc::const_iterator cit = contents.begin();
147         toc::Toc::const_iterator end = contents.end();
148
149         for (int rowindex = 0; cit != end; ++cit, ++rowindex) {
150                 Gtk::ListStore::iterator row = tocstore_->append();
151                 (*row)[listCol_] = cit->asString();
152                 (*row)[listColIndex_] = rowindex;
153         }
154         changing_views_ = false;
155 }
156
157
158 void GToc::onTocViewSelected()
159 {
160         // If we always do this, then an item is jumped to without
161         // the user clicking on one when he changes type from TOC->figures or so
162         if (!changing_views_) {
163                 unsigned int choice = (*listSel_->get_selected())[listColIndex_];
164                 if (choice < toc_.size()) {
165                         controller().goTo(toc_[choice]);
166                 }
167         }
168 }
169
170
171 void GToc::onTypeComboChanged()
172 {
173         if(!changing_views_)
174                 updateContents();
175 }
176
177
178 } // namespace frontend
179 } // namespace lyx