3 * Copyright 2001 the LyX Team
4 * Read the file COPYING
6 * \author Angus Leeming, a.leeming@ic.ac.uk
7 * \author John Levon, moz@compsoc.man.ac.uk
8 * \author Herbert Voss, voss@lyx.org
12 #pragma implementation
17 #include "ControlBibtex.h"
18 #include "FormBibtex.h"
19 #include "forms/form_bibtex.h"
21 #include "xforms_helpers.h"
22 #include FORMS_H_LOCATION
24 #include "helper_funcs.h"
27 #include "support/LAssert.h"
28 #include "support/lstrings.h"
29 #include "support/filetools.h"
30 #include "support/lyxalgo.h"
37 typedef FormCB<ControlBibtex, FormDB<FD_bibtex> > base_class;
39 FormBibtex::FormBibtex(ControlBibtex & c)
40 : base_class(c, _("BibTeX Database"))
44 void FormBibtex::build()
46 dialog_.reset(build_bibtex(this));
48 fl_set_input_return(dialog_->input_database, FL_RETURN_CHANGED);
49 fl_set_input_return(dialog_->input_style, FL_RETURN_CHANGED);
51 setPrehandler(dialog_->input_database);
52 setPrehandler(dialog_->input_style);
54 // Manage the ok, apply, restore and cancel/close buttons
55 bc().setOK(dialog_->button_ok);
56 bc().setCancel(dialog_->button_close);
58 bc().addReadOnly(dialog_->button_database_browse);
59 bc().addReadOnly(dialog_->input_database);
60 bc().addReadOnly(dialog_->button_style_browse);
61 bc().addReadOnly(dialog_->button_style_choose);
62 bc().addReadOnly(dialog_->button_rescan);
63 bc().addReadOnly(dialog_->input_style);
64 bc().addReadOnly(dialog_->check_bibtotoc);
66 // set up the tooltips
67 string str = _("The database you want to cite from. Insert it without the default extension \".bib\". If you insert it with the browser, LyX strips the extension. Several databases must be separated by a comma: \"natbib, books\".");
68 tooltips().init(dialog_->button_database_browse, str);
70 str = _("Browse your directory for BibTeX stylefiles");
71 tooltips().init(dialog_->button_style_browse, str);
73 str = _("The BibTeX style to use (only one allowed). Insert it without the default extension \".bst\" and without path or choose one from the browsers list. Most of the bibstyles are stored in $TEXMF/bibtex/bst. $TEXMF is the root dir of the local TeX tree.");
74 tooltips().init(dialog_->input_style, str);
76 str = _("Activate this option if you want the bibliography to appear in the Table of Contents (which doesn't happen by default)");
77 tooltips().init(dialog_->check_bibtotoc, str);
79 str = _("Choose a BibTeX style from the browsers list");
80 tooltips().init(dialog_->button_style_choose, str);
82 str = _("Updates your TeX system for a new bibstyle list. Only the styles which are in directories where TeX finds them are listed!");
83 tooltips().init(dialog_->button_rescan, str);
88 ButtonPolicy::SMInput FormBibtex::input(FL_OBJECT * ob, long)
90 if (ob == dialog_->button_database_browse) {
91 // When browsing, take the first file only
92 string const in_name = fl_get_input(dialog_->input_database);
94 controller().Browse("",
96 _("*.bib| BibTeX Databases (*.bib)"));
97 if (!out_name.empty()) {
98 // add the database to any existing ones
100 out_name = in_name + ',' + out_name;
102 fl_freeze_form(form());
103 fl_set_input(dialog_->input_database, out_name.c_str());
104 fl_unfreeze_form(form());
106 } else if (ob == dialog_->button_style_browse) {
107 string const in_name = fl_get_input(dialog_->input_style);
109 controller().Browse(in_name,
110 _("Select BibTeX-Style"),
111 _("*.bst| BibTeX Styles (*.bst)"));
112 if (!out_name.empty()) {
113 fl_freeze_form(form());
114 fl_set_input(dialog_->input_style, out_name.c_str());
115 fl_unfreeze_form(form());
117 } else if (ob == dialog_->button_style_choose) {
118 unsigned int selection = fl_get_browser(dialog_->browser_styles);
119 string const out_name =
120 fl_get_browser_line(dialog_->browser_styles, selection);
121 fl_set_input(dialog_->input_style,
122 ChangeExtension(out_name, string()).c_str());
123 } else if (ob == dialog_->button_rescan)
124 controller().rescanBibStyles();
126 // with an empty database nothing makes sense ...
127 if (!compare(fl_get_input(dialog_->input_database), "")) {
128 return ButtonPolicy::SMI_NOOP;
131 return ButtonPolicy::SMI_VALID;
135 void FormBibtex::update()
137 fl_set_input(dialog_->input_database,
138 controller().params().getContents().c_str());
139 string bibtotoc = "bibtotoc";
140 string bibstyle (controller().params().getOptions().c_str());
141 if (prefixIs(bibstyle, bibtotoc)) { // bibtotoc exists?
142 fl_set_button(dialog_->check_bibtotoc,1);
143 if (contains(bibstyle, ',')) { // bibstyle exists?
144 bibstyle = split(bibstyle, bibtotoc, ',');
149 fl_set_input(dialog_->input_style, bibstyle.c_str());
152 fl_set_button(dialog_->check_bibtotoc,0);
153 fl_set_input(dialog_->input_style, bibstyle.c_str());
156 controller().getBibStyles();
157 fl_add_browser_line(dialog_->browser_styles, str.c_str());
162 string const unique_and_no_extensions(string const & str_in)
164 vector<string> dbase = getVectorFromString(str_in);
165 for (vector<string>::iterator it = dbase.begin();
166 it != dbase.end(); ++it) {
167 *it = ChangeExtension(*it, string());
169 lyx::eliminate_duplicates(dbase);
170 return getStringFromVector(dbase);
176 void FormBibtex::apply()
178 string const db = fl_get_input(dialog_->input_database);
180 // no database -> no bibtex-command and no options!
181 controller().params().setContents("");
182 controller().params().setOptions("");
186 controller().params().setContents(unique_and_no_extensions(db));
189 string bibstyle = fl_get_input(dialog_->input_style);
190 if (!bibstyle.empty()) {
191 // save the BibTeX style without any ".bst" extension
192 bibstyle = ChangeExtension(OnlyFilename(bibstyle), "");
195 bool const bibtotoc = fl_get_button(dialog_->check_bibtotoc);
197 if (bibtotoc && (!bibstyle.empty())) {
198 // both bibtotoc and style
199 controller().params().setOptions("bibtotoc," + bibstyle);
201 } else if (bibtotoc) {
202 // bibtotoc and no style
203 controller().params().setOptions("bibtotoc");
205 } else // all other cases
206 controller().params().setOptions(bibstyle);