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