]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormBibtex.C
Strip out another 180 #includes.
[lyx.git] / src / frontends / xforms / FormBibtex.C
1 /**
2  * \file FormBibtex.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Angus Leeming
7  * \author John Levon
8  * \author Herbert Voss
9  * \author Rob Lahaye
10  *
11  * Full author contact details are available in file CREDITS.
12  */
13
14 #include <config.h>
15
16 #include "FormBibtex.h"
17 #include "ControlBibtex.h"
18 #include "forms/form_bibtex.h"
19
20 #include "Tooltips.h"
21 #include "xforms_helpers.h"
22 #include "xformsBC.h"
23
24 #include "support/filetools.h"
25 #include "support/lstrings.h"
26 #include "support/lyxalgo.h"
27
28 #include "lyx_forms.h"
29
30 using namespace lyx::support;
31
32 using std::sort;
33 using std::vector;
34
35
36 typedef FormController<ControlBibtex, FormView<FD_bibtex> > base_class;
37
38 FormBibtex::FormBibtex(Dialog & parent)
39         : base_class(parent, _("BibTeX Database"))
40 {}
41
42
43 void FormBibtex::build()
44 {
45         dialog_.reset(build_bibtex(this));
46
47         // Manage the ok, apply, restore and cancel/close buttons
48         bcview().setOK(dialog_->button_ok);
49         bcview().setApply(dialog_->button_apply);
50         bcview().setCancel(dialog_->button_close);
51         bcview().setRestore(dialog_->button_restore);
52
53         // disable for read-only documents
54         bcview().addReadOnly(dialog_->input_database);
55         bcview().addReadOnly(dialog_->button_database_browse);
56         bcview().addReadOnly(dialog_->button_style_browse);
57         bcview().addReadOnly(dialog_->button_rescan);
58         bcview().addReadOnly(dialog_->input_style);
59         bcview().addReadOnly(dialog_->check_bibtotoc);
60
61         // trigger an input event for cut&paste with middle mouse button.
62         setPrehandler(dialog_->input_database);
63         setPrehandler(dialog_->input_style);
64
65         fl_set_input_return(dialog_->input_database, FL_RETURN_CHANGED);
66         fl_set_input_return(dialog_->input_style, FL_RETURN_CHANGED);
67
68         // callback for double click in browser
69         fl_set_browser_dblclick_callback(dialog_->browser_styles,
70                                          C_FormDialogView_InputCB, 2);
71
72         // set up the tooltips
73         string str = _("The database you want to cite from. Insert it "
74                        "without the default extension \".bib\". Use comma "
75                        "to separate databases.");
76         tooltips().init(dialog_->button_database_browse, str);
77
78         str = _("Browse directory for BibTeX stylefiles");
79         tooltips().init(dialog_->button_style_browse, str);
80
81         str = _("The BibTeX style to use (only one allowed). Insert it without "
82                 "the default extension \".bst\" and without path.");
83         tooltips().init(dialog_->input_style, str);
84
85         str = _("Select if the bibliography should appear in the Table "
86                 "of Contents");
87         tooltips().init(dialog_->check_bibtotoc, str);
88
89         str = _("Double click to choose a BibTeX style from the list.");
90         tooltips().init(dialog_->browser_styles, str);
91
92 #if FL_VERSION == 0 || (FL_REVISION == 0 && FL_FIXLEVEL == 0)
93         // Work-around xforms' bug; enable tooltips for browser widgets.
94         setPrehandler(dialog_->browser_styles);
95 #endif
96
97         str = _("Updates your TeX system for a new bibstyle list. Only "
98                 "the styles which are in directories where TeX finds them "
99                 "are listed!");
100         tooltips().init(dialog_->button_rescan, str);
101 }
102
103
104 ButtonPolicy::SMInput FormBibtex::input(FL_OBJECT * ob, long ob_value)
105 {
106         if (ob == dialog_->button_database_browse) {
107                 // When browsing, take the first file only
108                 string const in_name = getString(dialog_->input_database);
109                 string out_name =
110                         controller().Browse("",
111                                 _("Select Database"),
112                                 _("*.bib| BibTeX Databases (*.bib)"));
113                 if (!out_name.empty()) {
114                         // add the database to any existing ones
115                         if (!in_name.empty())
116                                 out_name = in_name + ',' + out_name;
117
118                         fl_set_input(dialog_->input_database, out_name.c_str());
119                 }
120
121         } else if (ob == dialog_->button_style_browse) {
122                 string const in_name = getString(dialog_->input_style);
123                 string const style = controller().Browse(in_name,
124                                         _("Select BibTeX-Style"),
125                                         _("*.bst| BibTeX Styles (*.bst)"));
126                 if (!style.empty()) {
127                         fl_set_input(dialog_->input_style, style.c_str());
128                 }
129
130         } else if (ob == dialog_->browser_styles && ob_value == 2) {
131                 // double clicked in styles browser
132                 string const style = getString(dialog_->browser_styles);
133                 if (style.empty()) {
134                         return ButtonPolicy::SMI_NOOP;
135                 } else {
136                         fl_set_input(dialog_->input_style,
137                                         ChangeExtension(style, "").c_str());
138                 }
139                 // reset the browser so that the following
140                 // single-click callback doesn't do anything
141                 fl_deselect_browser(dialog_->browser_styles);
142
143         } else if (ob == dialog_->button_rescan) {
144                 fl_clear_browser(dialog_->browser_styles);
145                 controller().rescanBibStyles();
146                 vector<string> styles;
147                 controller().getBibStyles(styles);
148                 fl_add_browser_line(dialog_->browser_styles,
149                                     getStringFromVector(styles, "\n").c_str());
150         }
151
152         // with an empty database nothing makes sense ...
153         if (!compare(fl_get_input(dialog_->input_database), "")) {
154                 return ButtonPolicy::SMI_NOOP;
155         }
156
157         return ButtonPolicy::SMI_VALID;
158 }
159
160
161 void FormBibtex::update()
162 {
163         fl_set_input(dialog_->input_database,
164                      controller().params().getContents().c_str());
165
166         string bibtotoc = "bibtotoc";
167         string bibstyle = controller().params().getOptions();
168
169         bool const bibtotoc_exists = prefixIs(bibstyle, bibtotoc);
170         fl_set_button(dialog_->check_bibtotoc, bibtotoc_exists);
171         if (bibtotoc_exists) {
172                 if (contains(bibstyle, ',')) { // bibstyle exists?
173                         bibstyle = split(bibstyle, bibtotoc, ',');
174                 } else {
175                         bibstyle.erase();
176                 }
177         }
178         fl_set_input(dialog_->input_style, bibstyle.c_str());
179
180         vector<string> styles;
181         controller().getBibStyles(styles);
182
183         fl_clear_browser(dialog_->browser_styles);
184         fl_add_browser_line(dialog_->browser_styles,
185                             getStringFromVector(styles, "\n").c_str());
186 }
187
188 namespace {
189
190 string const unique_and_no_extensions(string const & str_in)
191 {
192         vector<string> dbase = getVectorFromString(str_in);
193         for (vector<string>::iterator it = dbase.begin();
194              it != dbase.end(); ++it) {
195                 *it = ChangeExtension(*it, string());
196         }
197         lyx::eliminate_duplicates(dbase);
198         return getStringFromVector(dbase);
199 }
200
201 } // namespace anon
202
203
204 void FormBibtex::apply()
205 {
206         string const db = getString(dialog_->input_database);
207         if (db.empty()) {
208                 // no database -> no bibtex-command and no options!
209                 controller().params().setContents("");
210                 controller().params().setOptions("");
211                 return;
212         }
213
214         controller().params().setContents(unique_and_no_extensions(db));
215
216         // empty is valid!
217         string bibstyle = getString(dialog_->input_style);
218         if (!bibstyle.empty()) {
219                 // save the BibTeX style without any ".bst" extension
220                 bibstyle = ChangeExtension(OnlyFilename(bibstyle), "");
221         }
222
223         bool const addtotoc = fl_get_button(dialog_->check_bibtotoc);
224         string const bibtotoc = addtotoc ? "bibtotoc" : "";
225         if (addtotoc && !bibstyle.empty()) {
226                 // Both bibtotoc and style.
227                 controller().params().setOptions(bibtotoc + ',' + bibstyle);
228
229         } else {
230                 // At least one of addtotoc and bibstyle is empty. No harm to output both!
231                 controller().params().setOptions(bibtotoc + bibstyle);
232         }
233 }