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 "helper_funcs.h"
23 #include "support/lstrings.h"
24 #include "support/filetools.h"
27 typedef FormCB<ControlBibtex, FormDB<FD_form_bibtex> > base_class;
29 FormBibtex::FormBibtex(ControlBibtex & c)
30 : base_class(c, _("BibTeX Database"))
34 void FormBibtex::build()
36 dialog_.reset(build_bibtex());
38 fl_set_input_return(dialog_->database, FL_RETURN_CHANGED);
39 fl_set_input_return(dialog_->style, FL_RETURN_CHANGED);
41 // Manage the ok, apply, restore and cancel/close buttons
42 bc().setOK(dialog_->button_ok);
43 bc().setCancel(dialog_->button_cancel);
45 bc().addReadOnly(dialog_->database_browse);
46 bc().addReadOnly(dialog_->database);
47 bc().addReadOnly(dialog_->style_browse);
48 bc().addReadOnly(dialog_->style);
49 bc().addReadOnly(dialog_->radio_bibtotoc);
53 ButtonPolicy::SMInput FormBibtex::input(FL_OBJECT * ob, long)
55 if (ob == dialog_->database_browse) {
56 // When browsing, take the first file only
57 string const in_name = fl_get_input(dialog_->database);
59 controller().Browse("",
61 "*.bib| BibTeX Databases (*.bib)");
62 if (!out_name.empty()) {
63 // add the database to any existing ones
65 out_name = in_name + ", " + out_name;
67 fl_freeze_form(form());
68 fl_set_input(dialog_->database, out_name.c_str());
69 fl_unfreeze_form(form());
73 if (ob == dialog_->style_browse) {
74 string const in_name = fl_get_input(dialog_->style);
76 controller().Browse(in_name,
77 "Select BibTeX-Style",
78 "*.bst| BibTeX Styles (*.bst)");
79 if (!out_name.empty()) {
80 fl_freeze_form(form());
81 fl_set_input(dialog_->style, out_name.c_str());
82 fl_unfreeze_form(form());
86 if (!compare(fl_get_input(dialog_->database),"")) {
87 return ButtonPolicy::SMI_NOOP;
90 return ButtonPolicy::SMI_VALID;
94 void FormBibtex::update()
96 fl_set_input(dialog_->database,
97 controller().params().getContents().c_str());
98 string bibtotoc = "bibtotoc";
99 string bibstyle (controller().params().getOptions().c_str());
100 if (prefixIs(bibstyle,bibtotoc)) { // bibtotoc exists?
101 fl_set_button(dialog_->radio_bibtotoc,1);
102 if (contains(bibstyle,',')) { // bibstyle exists?
103 bibstyle = split(bibstyle,bibtotoc,',');
108 fl_set_input(dialog_->style,bibstyle.c_str());
111 fl_set_button(dialog_->radio_bibtotoc,0);
112 fl_set_input(dialog_->style,bibstyle.c_str());
118 // Remove all duplicate entries in c.
119 // Taken stright out of Stroustrup
120 template<class C> void eliminate_duplicates(C & c)
122 std::sort(c.begin(), c.end()); // sort
123 typename C::iterator p = std::unique(c.begin(), c.end()); // compact
124 c.erase(p, c.end()); // shrink
127 string const unique_and_no_extensions(string const & str_in)
129 std::vector<string> dbase = getVectorFromString(str_in);
130 for (std::vector<string>::iterator it = dbase.begin();
131 it != dbase.end(); ++it) {
132 *it = ChangeExtension(*it, "");
134 eliminate_duplicates(dbase);
135 return subst(getStringFromVector(dbase),",",", ");
141 void FormBibtex::apply()
143 string const db = fl_get_input(dialog_->database);
145 // no database -> no bibtex-command and no options!
146 controller().params().setContents("");
147 controller().params().setOptions("");
151 controller().params().setContents(unique_and_no_extensions(db));
154 string bibstyle = fl_get_input(dialog_->style);
155 if (!bibstyle.empty()) {
156 // save the BibTeX style without any ".bst" extension
157 bibstyle = ChangeExtension(OnlyFilename(bibstyle), "");
160 bool const bibtotoc = fl_get_button(dialog_->radio_bibtotoc);
162 if (bibtotoc && (!bibstyle.empty())) {
163 // both bibtotoc and style
164 controller().params().setOptions("bibtotoc,"+bibstyle);
166 } else if (bibtotoc) {
167 // bibtotoc and no style
168 controller().params().setOptions("bibtotoc");
170 } else if (!bibstyle.empty()){
172 controller().params().setOptions(bibstyle);