3 * Copyright 2001 the LyX Team
4 * Read the file COPYING
6 * \author Angus Leeming
8 * \author Herbert Voss <voss@lyx.org>
12 #pragma implementation
17 #include "ControlBibtex.h"
18 #include "FormBibtex.h"
19 #include "form_bibtex.h"
22 #include "xforms_helpers.h"
23 #include "helper_funcs.h"
24 #include "support/LAssert.h"
25 #include "support/lstrings.h"
26 #include "support/filetools.h"
33 typedef FormCB<ControlBibtex, FormDB<FD_form_bibtex> > base_class;
35 FormBibtex::FormBibtex(ControlBibtex & c)
36 : base_class(c, _("BibTeX Database"))
40 void FormBibtex::build()
42 dialog_.reset(build_bibtex());
45 fillTooltipChoice(dialog_->choice_help);
46 fl_set_input_return(dialog_->database, FL_RETURN_CHANGED);
47 fl_set_input_return(dialog_->style, FL_RETURN_CHANGED);
49 // Manage the ok, apply, restore and cancel/close buttons
50 bc().setOK(dialog_->button_ok);
51 bc().setCancel(dialog_->button_cancel);
53 bc().addReadOnly(dialog_->database_browse);
54 bc().addReadOnly(dialog_->database);
55 bc().addReadOnly(dialog_->style_browse);
56 bc().addReadOnly(dialog_->style);
57 bc().addReadOnly(dialog_->radio_bibtotoc);
59 // set up the help mechanism
60 setTooltipHandler(dialog_->database_browse);
61 setTooltipHandler(dialog_->database);
62 setTooltipHandler(dialog_->style_browse);
63 setTooltipHandler(dialog_->style);
64 setTooltipHandler(dialog_->radio_bibtotoc);
68 ButtonPolicy::SMInput FormBibtex::input(FL_OBJECT * ob, long)
70 if (ob == dialog_->database_browse) {
71 // When browsing, take the first file only
72 string const in_name = fl_get_input(dialog_->database);
74 controller().Browse("",
76 "*.bib| BibTeX Databases (*.bib)");
77 if (!out_name.empty()) {
78 // add the database to any existing ones
80 out_name = in_name + ", " + out_name;
82 fl_freeze_form(form());
83 fl_set_input(dialog_->database, out_name.c_str());
84 fl_unfreeze_form(form());
88 if (ob == dialog_->style_browse) {
89 string const in_name = fl_get_input(dialog_->style);
91 controller().Browse(in_name,
92 "Select BibTeX-Style",
93 "*.bst| BibTeX Styles (*.bst)");
94 if (!out_name.empty()) {
95 fl_freeze_form(form());
96 fl_set_input(dialog_->style, out_name.c_str());
97 fl_unfreeze_form(form());
101 if (ob == dialog_->choice_help) {
102 setTooltipLevel(dialog_->choice_help);
103 return ButtonPolicy::SMI_NOOP;
106 if (!compare(fl_get_input(dialog_->database),"")) {
107 return ButtonPolicy::SMI_NOOP;
110 return ButtonPolicy::SMI_VALID;
114 void FormBibtex::update()
116 fl_set_input(dialog_->database,
117 controller().params().getContents().c_str());
118 string bibtotoc = "bibtotoc";
119 string bibstyle (controller().params().getOptions().c_str());
120 if (prefixIs(bibstyle,bibtotoc)) { // bibtotoc exists?
121 fl_set_button(dialog_->radio_bibtotoc,1);
122 if (contains(bibstyle,',')) { // bibstyle exists?
123 bibstyle = split(bibstyle,bibtotoc,',');
128 fl_set_input(dialog_->style,bibstyle.c_str());
131 fl_set_button(dialog_->radio_bibtotoc,0);
132 fl_set_input(dialog_->style,bibstyle.c_str());
138 // Remove all duplicate entries in c.
139 // Taken stright out of Stroustrup
141 void eliminate_duplicates(C & c)
143 sort(c.begin(), c.end());
144 typename C::iterator p = std::unique(c.begin(), c.end());
149 string const unique_and_no_extensions(string const & str_in)
151 vector<string> dbase = getVectorFromString(str_in);
152 for (vector<string>::iterator it = dbase.begin();
153 it != dbase.end(); ++it) {
154 *it = ChangeExtension(*it, "");
156 eliminate_duplicates(dbase);
157 return subst(getStringFromVector(dbase),",",", ");
163 void FormBibtex::apply()
165 string const db = fl_get_input(dialog_->database);
167 // no database -> no bibtex-command and no options!
168 controller().params().setContents("");
169 controller().params().setOptions("");
173 controller().params().setContents(unique_and_no_extensions(db));
176 string bibstyle = fl_get_input(dialog_->style);
177 if (!bibstyle.empty()) {
178 // save the BibTeX style without any ".bst" extension
179 bibstyle = ChangeExtension(OnlyFilename(bibstyle), "");
182 bool const bibtotoc = fl_get_button(dialog_->radio_bibtotoc);
184 if (bibtotoc && (!bibstyle.empty())) {
185 // both bibtotoc and style
186 controller().params().setOptions("bibtotoc,"+bibstyle);
188 } else if (bibtotoc) {
189 // bibtotoc and no style
190 controller().params().setOptions("bibtotoc");
192 } else if (!bibstyle.empty()) {
194 controller().params().setOptions(bibstyle);
199 string const FormBibtex::getMinimalTooltip(FL_OBJECT const * ob) const
203 if (ob == dialog_->database) {
204 str = N_("The BibTeX Database");
206 } else if (ob == dialog_->database_browse) {
207 str = _("Browse for BibTeX databases.");
209 } else if (ob == dialog_->style) {
210 str = _("The BibTeX style to use");
212 } else if (ob == dialog_->style_browse) {
213 str = _("Browse for BibTeX stylefiles.");
215 } else if (ob == dialog_->radio_bibtotoc) {
216 str = _("Bibliography to Table of Contents");
222 string const FormBibtex::getVerboseTooltip(FL_OBJECT const * ob) const
226 if (ob == dialog_->database) {
227 str = N_("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\".");
229 } else if (ob == dialog_->database_browse) {
230 str = _("Browse your directory for BibTeX databases.");
232 } else if (ob == dialog_->style) {
233 str = _("The BibTeX style to use (only one allowed). Insert it without the default extension \".bst\" and without path. Most of the bibstyles are stored in $TEXMF/bibtex/bst. $TEXMF is the root dir of the local TeX tree. In \"Help->TeX Info\" you can list all installed styles.");
235 } else if (ob == dialog_->style_browse) {
236 str = _("Browse your directory for BibTeX stylefiles.");
238 } else if (ob == dialog_->radio_bibtotoc) {
239 str = _("Activate this option if you want the bibliography to appear in the Table of Contents (which doesn't happen by default).");