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