3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Angus Leeming
11 * Full author contact details are available in file CREDITS.
16 #include "FormBibtex.h"
17 #include "ControlBibtex.h"
18 #include "forms/form_bibtex.h"
21 #include "xforms_helpers.h"
24 #include "support/filetools.h"
25 #include "support/globbing.h"
26 #include "support/lstrings.h"
27 #include "support/lyxalgo.h"
29 #include "lyx_forms.h"
31 using lyx::support::ChangeExtension;
32 using lyx::support::compare;
33 using lyx::support::contains;
34 using lyx::support::FileFilterList;
35 using lyx::support::getStringFromVector;
36 using lyx::support::getVectorFromString;
37 using lyx::support::OnlyFilename;
38 using lyx::support::prefixIs;
39 using lyx::support::split;
45 typedef FormController<ControlBibtex, FormView<FD_bibtex> > base_class;
47 FormBibtex::FormBibtex(Dialog & parent)
48 : base_class(parent, _("BibTeX Database"))
52 void FormBibtex::build()
54 dialog_.reset(build_bibtex(this));
56 // Manage the ok, apply, restore and cancel/close buttons
57 bcview().setOK(dialog_->button_ok);
58 bcview().setApply(dialog_->button_apply);
59 bcview().setCancel(dialog_->button_close);
60 bcview().setRestore(dialog_->button_restore);
62 // disable for read-only documents
63 bcview().addReadOnly(dialog_->input_database);
64 bcview().addReadOnly(dialog_->button_database_browse);
65 bcview().addReadOnly(dialog_->button_style_browse);
66 bcview().addReadOnly(dialog_->button_rescan);
67 bcview().addReadOnly(dialog_->input_style);
68 bcview().addReadOnly(dialog_->check_bibtotoc);
70 // trigger an input event for cut&paste with middle mouse button.
71 setPrehandler(dialog_->input_database);
72 setPrehandler(dialog_->input_style);
74 fl_set_input_return(dialog_->input_database, FL_RETURN_CHANGED);
75 fl_set_input_return(dialog_->input_style, FL_RETURN_CHANGED);
77 // callback for double click in browser
78 fl_set_browser_dblclick_callback(dialog_->browser_styles,
79 C_FormDialogView_InputCB, 2);
81 // set up the tooltips
82 string str = _("The database you want to cite from. Insert it "
83 "without the default extension \".bib\". Use comma "
84 "to separate databases.");
85 tooltips().init(dialog_->button_database_browse, str);
87 str = _("Browse directory for BibTeX stylefiles");
88 tooltips().init(dialog_->button_style_browse, str);
90 str = _("The BibTeX style to use (only one allowed). Insert it without "
91 "the default extension \".bst\" and without path.");
92 tooltips().init(dialog_->input_style, str);
94 str = _("Select if the bibliography should appear in the Table "
96 tooltips().init(dialog_->check_bibtotoc, str);
98 str = _("Double click to choose a BibTeX style from the list.");
99 tooltips().init(dialog_->browser_styles, str);
101 #if FL_VERSION == 0 || (FL_REVISION == 0 && FL_FIXLEVEL == 0)
102 // Work-around xforms' bug; enable tooltips for browser widgets.
103 setPrehandler(dialog_->browser_styles);
106 str = _("Updates your TeX system for a new bibstyle list. Only "
107 "the styles which are in directories where TeX finds them "
109 tooltips().init(dialog_->button_rescan, str);
113 ButtonPolicy::SMInput FormBibtex::input(FL_OBJECT * ob, long ob_value)
115 if (ob == dialog_->button_database_browse) {
116 // When browsing, take the first file only
117 string const in_name = getString(dialog_->input_database);
119 filter(_("*.bib| BibTeX Databases (*.bib)"));
121 controller().browse("", _("Select Database"),
123 if (!out_name.empty()) {
124 // add the database to any existing ones
125 if (!in_name.empty())
126 out_name = in_name + ',' + out_name;
128 fl_set_input(dialog_->input_database, out_name.c_str());
131 } else if (ob == dialog_->button_style_browse) {
132 string const in_name = getString(dialog_->input_style);
134 filter(_("*.bst| BibTeX Styles (*.bst)"));
135 string const style = controller()
136 .browse(in_name, _("Select BibTeX-Style"), filter);
137 if (!style.empty()) {
138 fl_set_input(dialog_->input_style, style.c_str());
141 } else if (ob == dialog_->browser_styles && ob_value == 2) {
142 // double clicked in styles browser
143 string const style = getString(dialog_->browser_styles);
145 return ButtonPolicy::SMI_NOOP;
147 fl_set_input(dialog_->input_style,
148 ChangeExtension(style, "").c_str());
150 // reset the browser so that the following
151 // single-click callback doesn't do anything
152 fl_deselect_browser(dialog_->browser_styles);
154 } else if (ob == dialog_->button_rescan) {
155 fl_clear_browser(dialog_->browser_styles);
156 controller().rescanBibStyles();
157 vector<string> styles;
158 controller().getBibStyles(styles);
159 fl_add_browser_line(dialog_->browser_styles,
160 getStringFromVector(styles, "\n").c_str());
163 // with an empty database nothing makes sense ...
164 if (!compare(fl_get_input(dialog_->input_database), "")) {
165 return ButtonPolicy::SMI_NOOP;
168 return ButtonPolicy::SMI_VALID;
172 void FormBibtex::update()
174 fl_set_input(dialog_->input_database,
175 controller().params().getContents().c_str());
177 string bibtotoc = "bibtotoc";
178 string bibstyle = controller().params().getOptions();
180 bool const bibtotoc_exists = prefixIs(bibstyle, bibtotoc);
181 fl_set_button(dialog_->check_bibtotoc, bibtotoc_exists);
182 if (bibtotoc_exists) {
183 if (contains(bibstyle, ',')) { // bibstyle exists?
184 bibstyle = split(bibstyle, bibtotoc, ',');
189 fl_set_input(dialog_->input_style, bibstyle.c_str());
191 vector<string> styles;
192 controller().getBibStyles(styles);
194 fl_clear_browser(dialog_->browser_styles);
195 fl_add_browser_line(dialog_->browser_styles,
196 getStringFromVector(styles, "\n").c_str());
201 string const unique_and_no_extensions(string const & str_in)
203 vector<string> dbase = getVectorFromString(str_in);
204 for (vector<string>::iterator it = dbase.begin();
205 it != dbase.end(); ++it) {
206 *it = ChangeExtension(*it, string());
208 lyx::eliminate_duplicates(dbase);
209 return getStringFromVector(dbase);
215 void FormBibtex::apply()
217 string const db = getString(dialog_->input_database);
219 // no database -> no bibtex-command and no options!
220 controller().params().setContents("");
221 controller().params().setOptions("");
225 controller().params().setContents(unique_and_no_extensions(db));
228 string bibstyle = getString(dialog_->input_style);
229 if (!bibstyle.empty()) {
230 // save the BibTeX style without any ".bst" extension
231 bibstyle = ChangeExtension(OnlyFilename(bibstyle), "");
234 bool const addtotoc = fl_get_button(dialog_->check_bibtotoc);
235 string const bibtotoc = addtotoc ? "bibtotoc" : "";
236 if (addtotoc && !bibstyle.empty()) {
237 // Both bibtotoc and style.
238 controller().params().setOptions(bibtotoc + ',' + bibstyle);
241 // At least one of addtotoc and bibstyle is empty. No harm to output both!
242 controller().params().setOptions(bibtotoc + bibstyle);