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