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"
36 using support::ChangeExtension;
37 using support::compare;
38 using support::contains;
39 using support::FileFilterList;
40 using support::getStringFromVector;
41 using support::getVectorFromString;
42 using support::OnlyFilename;
43 using support::prefixIs;
49 typedef FormController<ControlBibtex, FormView<FD_bibtex> > base_class;
51 FormBibtex::FormBibtex(Dialog & parent)
52 : base_class(parent, _("BibTeX Database"))
56 void FormBibtex::build()
58 dialog_.reset(build_bibtex(this));
60 // Manage the ok, apply, restore and cancel/close buttons
61 bcview().setOK(dialog_->button_ok);
62 bcview().setApply(dialog_->button_apply);
63 bcview().setCancel(dialog_->button_close);
64 bcview().setRestore(dialog_->button_restore);
66 // disable for read-only documents
67 bcview().addReadOnly(dialog_->input_database);
68 bcview().addReadOnly(dialog_->button_database_browse);
69 bcview().addReadOnly(dialog_->button_style_browse);
70 bcview().addReadOnly(dialog_->button_rescan);
71 bcview().addReadOnly(dialog_->input_style);
72 bcview().addReadOnly(dialog_->check_bibtotoc);
73 bcview().addReadOnly(dialog_->choice_btprint);
75 // trigger an input event for cut&paste with middle mouse button.
76 setPrehandler(dialog_->input_database);
77 setPrehandler(dialog_->input_style);
79 fl_set_input_return(dialog_->input_database, FL_RETURN_CHANGED);
80 fl_set_input_return(dialog_->input_style, FL_RETURN_CHANGED);
82 // callback for double click in browser
83 fl_set_browser_dblclick_callback(dialog_->browser_styles,
84 C_FormDialogView_InputCB, 2);
86 fl_addto_choice(dialog_->choice_btprint,
87 _(" all cited references "
88 "| all uncited references "
89 "| all references ").c_str());
91 // set up the tooltips
92 string str = _("The database you want to cite from. Insert it "
93 "without the default extension \".bib\". Use comma "
94 "to separate databases.");
95 tooltips().init(dialog_->button_database_browse, str);
97 str = _("Browse directory for BibTeX stylefiles");
98 tooltips().init(dialog_->button_style_browse, str);
100 str = _("The BibTeX style to use (only one allowed). Insert it without "
101 "the default extension \".bst\" and without path.");
102 tooltips().init(dialog_->input_style, str);
104 str = _("Select if the bibliography should appear in the Table "
106 tooltips().init(dialog_->check_bibtotoc, str);
108 str = _("Double click to choose a BibTeX style from the list.");
109 tooltips().init(dialog_->browser_styles, str);
111 #if FL_VERSION == 0 || (FL_REVISION == 0 && FL_FIXLEVEL == 0)
112 // Work-around xforms' bug; enable tooltips for browser widgets.
113 setPrehandler(dialog_->browser_styles);
116 str = _("Updates your TeX system for a new bibstyle list. Only "
117 "the styles which are in directories where TeX finds them "
119 tooltips().init(dialog_->button_rescan, str);
121 str = _("The bibliography section contains...");
122 tooltips().init(dialog_->choice_btprint, str);
126 ButtonPolicy::SMInput FormBibtex::input(FL_OBJECT * ob, long ob_value)
128 if (ob == dialog_->button_database_browse) {
129 // When browsing, take the first file only
130 string const in_name = getString(dialog_->input_database);
131 string out_name = controller().browseBib("");
132 if (!out_name.empty()) {
133 // add the database to any existing ones
134 if (!in_name.empty())
135 out_name = in_name + ',' + out_name;
137 fl_set_input(dialog_->input_database, out_name.c_str());
140 } else if (ob == dialog_->button_style_browse) {
141 string const in_name = getString(dialog_->input_style);
142 string const style = controller().browseBst(in_name);
143 if (!style.empty()) {
144 fl_set_input(dialog_->input_style, style.c_str());
147 } else if (ob == dialog_->browser_styles && ob_value == 2) {
148 // double clicked in styles browser
149 string const style = getString(dialog_->browser_styles);
151 return ButtonPolicy::SMI_NOOP;
153 fl_set_input(dialog_->input_style,
154 ChangeExtension(style, "").c_str());
156 // reset the browser so that the following
157 // single-click callback doesn't do anything
158 fl_deselect_browser(dialog_->browser_styles);
160 } else if (ob == dialog_->button_rescan) {
161 fl_clear_browser(dialog_->browser_styles);
162 controller().rescanBibStyles();
163 vector<string> styles;
164 controller().getBibStyles(styles);
165 fl_add_browser_line(dialog_->browser_styles,
166 getStringFromVector(styles, "\n").c_str());
169 // with an empty database nothing makes sense ...
170 if (!compare(fl_get_input(dialog_->input_database), "")) {
171 return ButtonPolicy::SMI_NOOP;
174 return ButtonPolicy::SMI_VALID;
178 void FormBibtex::update()
180 fl_set_input(dialog_->input_database,
181 controller().params().getContents().c_str());
183 string bibtotoc = "bibtotoc";
184 string bibstyle = controller().params().getOptions();
186 bool const bibtopic = controller().usingBibtopic();
187 bool const bibtotoc_exists = prefixIs(bibstyle, bibtotoc);
188 fl_set_button(dialog_->check_bibtotoc, bibtotoc_exists && !bibtopic);
189 setEnabled(dialog_->check_bibtotoc, !bibtopic);
190 if (bibtotoc_exists) {
191 if (contains(bibstyle, ',')) { // bibstyle exists?
192 bibstyle = split(bibstyle, bibtotoc, ',');
197 fl_set_input(dialog_->input_style, bibstyle.c_str());
199 string btprint = controller().params().getSecOptions();
201 if (btprint == "btPrintNotCited")
203 else if (btprint == "btPrintAll")
206 fl_set_choice(dialog_->choice_btprint, btp);
207 setEnabled(dialog_->choice_btprint, bibtopic);
209 vector<string> styles;
210 controller().getBibStyles(styles);
212 fl_clear_browser(dialog_->browser_styles);
213 fl_add_browser_line(dialog_->browser_styles,
214 getStringFromVector(styles, "\n").c_str());
219 string const unique_and_no_extensions(string const & str_in)
221 vector<string> dbase = getVectorFromString(str_in);
222 for (vector<string>::iterator it = dbase.begin();
223 it != dbase.end(); ++it) {
224 *it = ChangeExtension(*it, string());
226 eliminate_duplicates(dbase);
227 return getStringFromVector(dbase);
233 void FormBibtex::apply()
235 string const db = getString(dialog_->input_database);
237 // no database -> no bibtex-command and no options!
238 controller().params().setContents("");
239 controller().params().setOptions("");
240 controller().params().setSecOptions("");
244 controller().params().setContents(unique_and_no_extensions(db));
247 string bibstyle = getString(dialog_->input_style);
248 if (!bibstyle.empty()) {
249 // save the BibTeX style without any ".bst" extension
250 bibstyle = ChangeExtension(OnlyFilename(bibstyle), "");
253 bool const addtotoc = fl_get_button(dialog_->check_bibtotoc);
254 string const bibtotoc = addtotoc ? "bibtotoc" : "";
255 if (addtotoc && !bibstyle.empty()) {
256 // Both bibtotoc and style.
257 controller().params().setOptions(bibtotoc + ',' + bibstyle);
260 // At least one of addtotoc and bibstyle is empty.
261 // No harm to output both!
262 controller().params().setOptions(bibtotoc + bibstyle);
265 // bibtopic allows three kinds of sections:
266 // 1. sections that include all cited references of the database(s)
267 // 2. sec. that include all uncited references of the database(s)
268 // 3. sec. that include all references of the database(s), cited or not
269 if (controller().usingBibtopic()){
270 int btp = fl_get_choice(dialog_->choice_btprint);
273 controller().params().setSecOptions("btPrintCited");
276 controller().params().setSecOptions("btPrintNotCited");
279 controller().params().setSecOptions("btPrintAll");
285 controller().params().setSecOptions("");
289 } // namespace frontend