]> git.lyx.org Git - lyx.git/blob - src/frontends/gtk/GBibtex.C
Extracted from r14281
[lyx.git] / src / frontends / gtk / GBibtex.C
1 /**
2  * \file GBibtex.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 "GBibtex.h"
22 #include "ControlBibtex.h"
23 #include "ghelpers.h"
24
25 #include "support/filetools.h"
26 #include "support/lstrings.h"
27
28 #include <libglademm.h>
29
30 using lyx::support::changeExtension;
31 using lyx::support::split;
32 using lyx::support::trim;
33
34 using std::vector;
35 using std::string;
36
37 namespace lyx {
38 namespace frontend {
39
40 GBibtex::GBibtex(Dialog & parent)
41         : GViewCB<ControlBibtex, GViewGladeB>(parent, _("BibTeX Bibliography"), false)
42 {}
43
44
45 void GBibtex::doBuild()
46 {
47         string const gladeName = findGladeFile("bibtex");
48         xml_ = Gnome::Glade::Xml::create(gladeName);
49
50         Gtk::Button * button;
51         xml_->get_widget("Cancel", button);
52         setCancel(button);
53         xml_->get_widget("OK", button);
54         setOK(button);
55
56         xml_->get_widget("Databases", databasesview_);
57         databasessel_ = databasesview_->get_selection();
58         // signal_changed gets connected at the end of this fn.
59
60         Gtk::TreeModel::ColumnRecord listcols;
61         listcols.add (stringcol_);
62         databasesstore_ = Gtk::ListStore::create(listcols);
63         databasesview_->set_model(databasesstore_);
64         databasesview_->append_column("Database", stringcol_);
65
66         xml_->get_widget("Add", button);
67         button->signal_clicked().connect(
68                 sigc::mem_fun(*this, &GBibtex::add));
69         bcview().addReadOnly(button);
70         xml_->get_widget("Remove", removebutton_);
71         removebutton_->signal_clicked().connect(
72                 sigc::mem_fun(*this, &GBibtex::remove));
73         bcview().addReadOnly(removebutton_);
74         xml_->get_widget("Browse", button);
75         button->signal_clicked().connect(
76                 sigc::mem_fun(*this, &GBibtex::browse));
77         bcview().addReadOnly(button);
78
79         Gtk::HBox *box;
80         xml_->get_widget("Style", box);
81         box->pack_start(stylecombo_);
82         stylecombo_.show();
83         bcview().addReadOnly(&stylecombo_);
84
85         xml_->get_widget("Content", contentcombo_);
86         bcview().addReadOnly(contentcombo_);
87         xml_->get_widget("TOC", toccheck_);
88         bcview().addReadOnly(toccheck_);
89
90         databasessel_->signal_changed().connect(
91                 sigc::mem_fun(*this, &GBibtex::validate));
92 }
93
94
95 void GBibtex::update()
96 {
97         string bibs(controller().params().getContents());
98         string bib;
99
100         databasesstore_->clear();
101         while (!bibs.empty()) {
102                 bibs = split(bibs, bib, ',');
103                 bib = trim(bib);
104                 if (!bib.empty()) {
105                         Gtk::TreeModel::iterator const row = databasesstore_->append();
106                         (*row)[stringcol_] = bib;
107                 }
108         }
109
110         string bibstyle(controller().getStylefile());
111
112         bool const bibtopic = controller().usingBibtopic();
113         if (controller().bibtotoc() && !bibtopic)
114                 toccheck_->set_active(true);
115         else
116                 toccheck_->set_active(false);
117
118         toccheck_->set_sensitive(!bibtopic);
119
120         string btprint(controller().params().getSecOptions());
121         int btp = 0;
122         if (btprint == "btPrintNotCited")
123                 btp = 1;
124         else if (btprint == "btPrintAll")
125                 btp = 2;
126
127         contentcombo_->set_active(btp);
128         contentcombo_->set_sensitive(bibtopic);
129
130         stylecombo_.clear();
131         vector<string> str;
132         controller().getBibStyles(str);
133
134         int item_nr(-1);
135         vector<string>::const_iterator it = str.begin();
136         vector<string>::const_iterator const end = str.end();
137         for (; it != end; ++it) {
138                 string item(changeExtension(*it, ""));
139                 if (item == bibstyle)
140                         item_nr = int(it - str.begin());
141                 stylecombo_.append_text (item);
142         }
143
144         if (item_nr == -1 && !bibstyle.empty()) {
145                 stylecombo_.append_text (bibstyle);
146                 item_nr = stylecombo_.get_model()->children().size() - 1;
147         }
148
149         if (item_nr != -1)
150                 stylecombo_.set_active(item_nr);
151         else
152                 stylecombo_.get_entry()->set_text("");
153
154         validate();
155 }
156
157
158 void GBibtex::apply()
159 {
160         Gtk::TreeModel::iterator it = databasesstore_->children().begin();
161         Gtk::TreeModel::iterator const end = databasesstore_->children().end();
162
163         string dblist;
164         for (; it != end; ++it) {
165                 Glib::ustring db = (*it)[stringcol_];
166                 dblist += string(db);
167                 if (it + 1 != end)
168                         dblist += ",";
169         }
170
171         controller().params().setContents(dblist);
172
173         string const bibstyle = stylecombo_.get_active_text();
174         bool const bibtotoc = toccheck_->get_active();
175         bool const bibtopic = controller().usingBibtopic();
176
177         if (!bibtopic && bibtotoc && (!bibstyle.empty())) {
178                 // both bibtotoc and style
179                 controller().params().setOptions("bibtotoc," + bibstyle);
180         } else if (!bibtopic && bibtotoc) {
181                 // bibtotoc and no style
182                 controller().params().setOptions("bibtotoc");
183         } else {
184                 // only style. An empty one is valid, because some
185                 // documentclasses have an own \bibliographystyle{}
186                 // command!
187                 controller().params().setOptions(bibstyle);
188         }
189
190         // bibtopic allows three kinds of sections:
191         // 1. sections that include all cited references of the database(s)
192         // 2. sections that include all uncited references of the database(s)
193         // 3. sections that include all references of the database(s), cited or not
194         int const btp = contentcombo_->get_active_row_number();
195
196         switch (btp) {
197         case 0:
198                 controller().params().setSecOptions("btPrintCited");
199                 break;
200         case 1:
201                 controller().params().setSecOptions("btPrintNotCited");
202                 break;
203         case 2:
204                 controller().params().setSecOptions("btPrintAll");
205                 break;
206         }
207
208         if (!bibtopic)
209                 controller().params().setSecOptions("");
210 }
211
212
213 void GBibtex::add()
214 {
215         string new_bib = controller().browseBib("");
216         if (new_bib.empty())
217                 return;
218
219         new_bib = changeExtension(new_bib, string());
220
221         Gtk::TreeModel::iterator const row = databasesstore_->append();
222         (*row)[stringcol_] = new_bib;
223
224         validate();
225 }
226
227
228 void GBibtex::remove()
229 {
230         Gtk::TreeModel::iterator const selected = databasessel_->get_selected();
231         if (!databasesstore_->iter_is_valid(selected))
232                 return;
233
234         databasesstore_->erase(selected);
235
236         validate();
237 }
238
239
240 void GBibtex::browse()
241 {
242         string const file = controller().browseBst("");
243
244         if (!file.empty()) {
245                 string const filen = changeExtension(file, "");
246                 bool present = false;
247
248                 for (unsigned int i = 0; i < stylecombo_.get_model()->children().size(); ++i) {
249                         stylecombo_.set_active(i);
250                         Glib::ustring const item = stylecombo_.get_active_text ();
251                         if (item == filen) {
252                                 present = true;
253                                 break;
254                         }
255                 }
256
257                 if (!present) {
258                         stylecombo_.append_text (filen);
259                         stylecombo_.set_active_text(filen);
260                 }
261         }
262 }
263
264
265 void GBibtex::validate()
266 {
267         if (databasesstore_->children().size())
268                 bc().valid(true);
269         else
270                 bc().valid(false);
271
272         Gtk::TreeModel::iterator selected = databasessel_->get_selected();
273         bool const enableremove = databasesstore_->iter_is_valid(selected) && !readOnly();
274         removebutton_->set_sensitive(enableremove);
275 }
276
277
278 } // namespace frontend
279 } // namespace lyx