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