3 * Copyright 2001 the LyX Team
4 * Read the file COPYING
6 * \author Angus Leeming, a.leeming@ic.ac.uk
7 * \author John Levon, moz@compsoc.man.ac.uk
8 * \author Herbert Voss, voss@lyx.org
12 #pragma implementation
17 #include "ControlBibtex.h"
18 #include "FormBibtex.h"
19 #include "form_bibtex.h"
21 #include "xforms_helpers.h"
22 #include "helper_funcs.h"
25 #include "support/LAssert.h"
26 #include "support/lstrings.h"
27 #include "support/filetools.h"
34 typedef FormCB<ControlBibtex, FormDB<FD_form_bibtex> > base_class;
36 FormBibtex::FormBibtex(ControlBibtex & c)
37 : base_class(c, _("BibTeX Database"))
41 void FormBibtex::build()
43 dialog_.reset(build_bibtex());
45 fl_set_input_return(dialog_->input_database, FL_RETURN_CHANGED);
46 fl_set_input_return(dialog_->input_style, FL_RETURN_CHANGED);
48 setPrehandler(dialog_->input_database);
49 setPrehandler(dialog_->input_style);
51 // Manage the ok, apply, restore and cancel/close buttons
52 bc().setOK(dialog_->button_ok);
53 bc().setCancel(dialog_->button_close);
55 bc().addReadOnly(dialog_->button_database_browse);
56 bc().addReadOnly(dialog_->input_database);
57 bc().addReadOnly(dialog_->button_style_browse);
58 bc().addReadOnly(dialog_->input_style);
59 bc().addReadOnly(dialog_->check_bibtotoc);
61 // set up the tooltips
62 string str = _("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\".");
63 tooltips().init(dialog_->button_database_browse, str);
65 str = _("Browse your directory for BibTeX stylefiles.");
66 tooltips().init(dialog_->button_style_browse, str);
68 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 \"View->TeX Information\" you can list all installed styles.");
69 tooltips().init(dialog_->input_style, str);
71 str = _("Activate this option if you want the bibliography to appear in the Table of Contents (which doesn't happen by default).");
72 tooltips().init(dialog_->check_bibtotoc, str);
76 ButtonPolicy::SMInput FormBibtex::input(FL_OBJECT * ob, long)
78 if (ob == dialog_->button_database_browse) {
79 // When browsing, take the first file only
80 string const in_name = fl_get_input(dialog_->input_database);
82 controller().Browse("",
84 _("*.bib| BibTeX Databases (*.bib)"));
85 if (!out_name.empty()) {
86 // add the database to any existing ones
88 out_name = in_name + ',' + out_name;
90 fl_freeze_form(form());
91 fl_set_input(dialog_->input_database, out_name.c_str());
92 fl_unfreeze_form(form());
96 if (ob == dialog_->button_style_browse) {
97 string const in_name = fl_get_input(dialog_->input_style);
99 controller().Browse(in_name,
100 _("Select BibTeX-Style"),
101 _("*.bst| BibTeX Styles (*.bst)"));
102 if (!out_name.empty()) {
103 fl_freeze_form(form());
104 fl_set_input(dialog_->input_style, out_name.c_str());
105 fl_unfreeze_form(form());
109 if (!compare(fl_get_input(dialog_->input_database),"")) {
110 return ButtonPolicy::SMI_NOOP;
113 return ButtonPolicy::SMI_VALID;
117 void FormBibtex::update()
119 fl_set_input(dialog_->input_database,
120 controller().params().getContents().c_str());
121 string bibtotoc = "bibtotoc";
122 string bibstyle (controller().params().getOptions().c_str());
123 if (prefixIs(bibstyle,bibtotoc)) { // bibtotoc exists?
124 fl_set_button(dialog_->check_bibtotoc,1);
125 if (contains(bibstyle,',')) { // bibstyle exists?
126 bibstyle = split(bibstyle,bibtotoc,',');
131 fl_set_input(dialog_->input_style,bibstyle.c_str());
134 fl_set_button(dialog_->check_bibtotoc,0);
135 fl_set_input(dialog_->input_style,bibstyle.c_str());
141 // Remove all duplicate entries in c.
142 // Taken stright out of Stroustrup
144 void eliminate_duplicates(C & c)
146 sort(c.begin(), c.end());
147 typename C::iterator p = std::unique(c.begin(), c.end());
152 string const unique_and_no_extensions(string const & str_in)
154 vector<string> dbase = getVectorFromString(str_in);
155 for (vector<string>::iterator it = dbase.begin();
156 it != dbase.end(); ++it) {
157 *it = ChangeExtension(*it, "");
159 eliminate_duplicates(dbase);
160 return getStringFromVector(dbase);
166 void FormBibtex::apply()
168 string const db = fl_get_input(dialog_->input_database);
170 // no database -> no bibtex-command and no options!
171 controller().params().setContents("");
172 controller().params().setOptions("");
176 controller().params().setContents(unique_and_no_extensions(db));
179 string bibstyle = fl_get_input(dialog_->input_style);
180 if (!bibstyle.empty()) {
181 // save the BibTeX style without any ".bst" extension
182 bibstyle = ChangeExtension(OnlyFilename(bibstyle), "");
185 bool const bibtotoc = fl_get_button(dialog_->check_bibtotoc);
187 if (bibtotoc && (!bibstyle.empty())) {
188 // both bibtotoc and style
189 controller().params().setOptions("bibtotoc,"+bibstyle);
191 } else if (bibtotoc) {
192 // bibtotoc and no style
193 controller().params().setOptions("bibtotoc");
195 } else // all other cases
196 controller().params().setOptions(bibstyle);