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
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"
26 #include "support/lstrings.h"
27 #include "support/filetools.h"
28 #include "support/lyxalgo.h"
35 typedef FormCB<ControlBibtex, FormDB<FD_bibtex> > base_class;
37 FormBibtex::FormBibtex()
38 : base_class(_("BibTeX Database"))
42 void FormBibtex::build()
44 dialog_.reset(build_bibtex(this));
46 // Manage the ok, apply, restore and cancel/close buttons
47 bc().setOK(dialog_->button_ok);
48 bc().setApply(dialog_->button_apply);
49 bc().setCancel(dialog_->button_close);
50 bc().setRestore(dialog_->button_restore);
52 // disable for read-only documents
53 bc().addReadOnly(dialog_->input_database);
54 bc().addReadOnly(dialog_->button_database_browse);
55 bc().addReadOnly(dialog_->button_style_browse);
56 bc().addReadOnly(dialog_->button_rescan);
57 bc().addReadOnly(dialog_->input_style);
58 bc().addReadOnly(dialog_->check_bibtotoc);
60 // trigger an input event for cut&paste with middle mouse button.
61 setPrehandler(dialog_->input_database);
62 setPrehandler(dialog_->input_style);
64 fl_set_input_return(dialog_->input_database, FL_RETURN_CHANGED);
65 fl_set_input_return(dialog_->input_style, FL_RETURN_CHANGED);
67 // set up the tooltips
68 string str = _("The database you want to cite from. Insert it "
69 "without the default extension \".bib\". Use comma "
70 "to separate databases.");
71 tooltips().init(dialog_->button_database_browse, str);
73 str = _("Browse directory for BibTeX stylefiles");
74 tooltips().init(dialog_->button_style_browse, str);
76 str = _("The BibTeX style to use (only one allowed). Insert it without "
77 "the default extension \".bst\" and without path.");
78 tooltips().init(dialog_->input_style, str);
80 str = _("Select if the bibliography should appear in the Table "
82 tooltips().init(dialog_->check_bibtotoc, str);
84 str = _("Choose a BibTeX style from the list.");
85 tooltips().init(dialog_->browser_styles, str);
86 // Work-around xforms' bug; enable tooltips for browser widgets.
87 setPrehandler(dialog_->browser_styles);
89 str = _("Updates your TeX system for a new bibstyle list. Only "
90 "the styles which are in directories where TeX finds them "
92 tooltips().init(dialog_->button_rescan, str);
96 ButtonPolicy::SMInput FormBibtex::input(FL_OBJECT * ob, long)
98 if (ob == dialog_->button_database_browse) {
99 // When browsing, take the first file only
100 string const in_name = getString(dialog_->input_database);
102 controller().Browse("",
103 _("Select Database"),
104 _("*.bib| BibTeX Databases (*.bib)"));
105 if (!out_name.empty()) {
106 // add the database to any existing ones
107 if (!in_name.empty())
108 out_name = in_name + ',' + out_name;
110 fl_set_input(dialog_->input_database, out_name.c_str());
113 } else if (ob == dialog_->button_style_browse) {
114 string const in_name = getString(dialog_->input_style);
115 string const style = controller().Browse(in_name,
116 _("Select BibTeX-Style"),
117 _("*.bst| BibTeX Styles (*.bst)"));
118 if (!style.empty()) {
119 fl_set_input(dialog_->input_style, style.c_str());
122 } else if (ob == dialog_->browser_styles) {
123 string const style = getString(dialog_->browser_styles);
125 return ButtonPolicy::SMI_NOOP;
127 fl_set_input(dialog_->input_style,
128 ChangeExtension(style, "").c_str());
131 } else if (ob == dialog_->button_rescan) {
132 fl_clear_browser(dialog_->browser_styles);
133 controller().rescanBibStyles();
135 controller().getBibStyles();
136 fl_add_browser_line(dialog_->browser_styles, str.c_str());
139 // with an empty database nothing makes sense ...
140 if (!compare(fl_get_input(dialog_->input_database), "")) {
141 return ButtonPolicy::SMI_NOOP;
144 return ButtonPolicy::SMI_VALID;
148 void FormBibtex::update()
150 fl_set_input(dialog_->input_database,
151 controller().params().getContents().c_str());
153 string bibtotoc = "bibtotoc";
154 string bibstyle = controller().params().getOptions();
156 bool const bibtotoc_exists = prefixIs(bibstyle, bibtotoc);
157 fl_set_button(dialog_->check_bibtotoc, bibtotoc_exists);
158 if (bibtotoc_exists) {
159 if (contains(bibstyle, ',')) { // bibstyle exists?
160 bibstyle = split(bibstyle, bibtotoc, ',');
165 fl_set_input(dialog_->input_style, bibstyle.c_str());
167 fl_clear_browser(dialog_->browser_styles);
168 string const str = controller().getBibStyles();
169 fl_add_browser_line(dialog_->browser_styles, str.c_str());
174 string const unique_and_no_extensions(string const & str_in)
176 vector<string> dbase = getVectorFromString(str_in);
177 for (vector<string>::iterator it = dbase.begin();
178 it != dbase.end(); ++it) {
179 *it = ChangeExtension(*it, string());
181 lyx::eliminate_duplicates(dbase);
182 return getStringFromVector(dbase);
188 void FormBibtex::apply()
190 string const db = getString(dialog_->input_database);
192 // no database -> no bibtex-command and no options!
193 controller().params().setContents("");
194 controller().params().setOptions("");
198 controller().params().setContents(unique_and_no_extensions(db));
201 string bibstyle = getString(dialog_->input_style);
202 if (!bibstyle.empty()) {
203 // save the BibTeX style without any ".bst" extension
204 bibstyle = ChangeExtension(OnlyFilename(bibstyle), "");
207 bool const addtotoc = fl_get_button(dialog_->check_bibtotoc);
208 string const bibtotoc = addtotoc ? "bibtotoc" : "";
209 if (addtotoc && !bibstyle.empty()) {
210 // Both bibtotoc and style.
211 controller().params().setOptions(bibtotoc + ',' + bibstyle);
214 // At least one of addtotoc and bibstyle is empty. No harm to output both!
215 controller().params().setOptions(bibtotoc + bibstyle);