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"
29 typedef FormCB<ControlBibtex, FormDB<FD_form_bibtex> > base_class;
31 FormBibtex::FormBibtex(ControlBibtex & c)
32 : base_class(c, _("BibTeX Database"))
36 void FormBibtex::build()
38 dialog_.reset(build_bibtex());
41 fillTooltipChoice(dialog_->choice_help);
42 fl_set_input_return(dialog_->database, FL_RETURN_CHANGED);
43 fl_set_input_return(dialog_->style, FL_RETURN_CHANGED);
45 // Manage the ok, apply, restore and cancel/close buttons
46 bc().setOK(dialog_->button_ok);
47 bc().setCancel(dialog_->button_cancel);
49 bc().addReadOnly(dialog_->database_browse);
50 bc().addReadOnly(dialog_->database);
51 bc().addReadOnly(dialog_->style_browse);
52 bc().addReadOnly(dialog_->style);
53 bc().addReadOnly(dialog_->radio_bibtotoc);
55 // set up the help mechanism
56 setTooltipHandler(dialog_->database_browse);
57 setTooltipHandler(dialog_->database);
58 setTooltipHandler(dialog_->style_browse);
59 setTooltipHandler(dialog_->style);
60 setTooltipHandler(dialog_->radio_bibtotoc);
64 ButtonPolicy::SMInput FormBibtex::input(FL_OBJECT * ob, long)
66 if (ob == dialog_->database_browse) {
67 // When browsing, take the first file only
68 string const in_name = fl_get_input(dialog_->database);
70 controller().Browse("",
72 "*.bib| BibTeX Databases (*.bib)");
73 if (!out_name.empty()) {
74 // add the database to any existing ones
76 out_name = in_name + ", " + out_name;
78 fl_freeze_form(form());
79 fl_set_input(dialog_->database, out_name.c_str());
80 fl_unfreeze_form(form());
84 if (ob == dialog_->style_browse) {
85 string const in_name = fl_get_input(dialog_->style);
87 controller().Browse(in_name,
88 "Select BibTeX-Style",
89 "*.bst| BibTeX Styles (*.bst)");
90 if (!out_name.empty()) {
91 fl_freeze_form(form());
92 fl_set_input(dialog_->style, out_name.c_str());
93 fl_unfreeze_form(form());
97 if (ob == dialog_->choice_help) {
98 setTooltipLevel(dialog_->choice_help);
99 return ButtonPolicy::SMI_NOOP;
102 if (!compare(fl_get_input(dialog_->database),"")) {
103 return ButtonPolicy::SMI_NOOP;
106 return ButtonPolicy::SMI_VALID;
110 void FormBibtex::update()
112 fl_set_input(dialog_->database,
113 controller().params().getContents().c_str());
114 string bibtotoc = "bibtotoc";
115 string bibstyle (controller().params().getOptions().c_str());
116 if (prefixIs(bibstyle,bibtotoc)) { // bibtotoc exists?
117 fl_set_button(dialog_->radio_bibtotoc,1);
118 if (contains(bibstyle,',')) { // bibstyle exists?
119 bibstyle = split(bibstyle,bibtotoc,',');
124 fl_set_input(dialog_->style,bibstyle.c_str());
127 fl_set_button(dialog_->radio_bibtotoc,0);
128 fl_set_input(dialog_->style,bibstyle.c_str());
134 // Remove all duplicate entries in c.
135 // Taken stright out of Stroustrup
136 template<class C> void eliminate_duplicates(C & c)
138 std::sort(c.begin(), c.end()); // sort
139 typename C::iterator p = std::unique(c.begin(), c.end()); // compact
140 c.erase(p, c.end()); // shrink
143 string const unique_and_no_extensions(string const & str_in)
145 std::vector<string> dbase = getVectorFromString(str_in);
146 for (std::vector<string>::iterator it = dbase.begin();
147 it != dbase.end(); ++it) {
148 *it = ChangeExtension(*it, "");
150 eliminate_duplicates(dbase);
151 return subst(getStringFromVector(dbase),",",", ");
157 void FormBibtex::apply()
159 string const db = fl_get_input(dialog_->database);
161 // no database -> no bibtex-command and no options!
162 controller().params().setContents("");
163 controller().params().setOptions("");
167 controller().params().setContents(unique_and_no_extensions(db));
170 string bibstyle = fl_get_input(dialog_->style);
171 if (!bibstyle.empty()) {
172 // save the BibTeX style without any ".bst" extension
173 bibstyle = ChangeExtension(OnlyFilename(bibstyle), "");
176 bool const bibtotoc = fl_get_button(dialog_->radio_bibtotoc);
178 if (bibtotoc && (!bibstyle.empty())) {
179 // both bibtotoc and style
180 controller().params().setOptions("bibtotoc,"+bibstyle);
182 } else if (bibtotoc) {
183 // bibtotoc and no style
184 controller().params().setOptions("bibtotoc");
186 } else if (!bibstyle.empty()){
188 controller().params().setOptions(bibstyle);
193 string const FormBibtex::getMinimalTooltip(FL_OBJECT const * ob) const
197 if (ob == dialog_->database) {
198 str = N_("The BibTeX Database");
200 } else if (ob == dialog_->database_browse) {
201 str = _("Browse for BibTeX databases.");
203 } else if (ob == dialog_->style) {
204 str = _("The BibTeX style to use");
206 } else if (ob == dialog_->style_browse) {
207 str = _("Browse for BibTeX stylefiles.");
209 } else if (ob == dialog_->radio_bibtotoc) {
210 str = _("Bibliography to Table of Contents");
216 string const FormBibtex::getVerboseTooltip(FL_OBJECT const * ob) const
220 if (ob == dialog_->database) {
221 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\".");
223 } else if (ob == dialog_->database_browse) {
224 str = _("Browse your directory for BibTeX databases.");
226 } else if (ob == dialog_->style) {
227 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.");
229 } else if (ob == dialog_->style_browse) {
230 str = _("Browse your directory for BibTeX stylefiles.");
232 } else if (ob == dialog_->radio_bibtotoc) {
233 str = _("Activate this option if you want the bibliography to appear in the Table of Contents (which doesn't happen by default).");