]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormBibtex.C
No longer pass Controller & or Dialogs & to the View c-tors.
[lyx.git] / src / frontends / xforms / FormBibtex.C
1 /**
2  * \file FormBibtex.C
3  * Copyright 2001 the LyX Team
4  * Read the file COPYING
5  *
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
9  */
10
11 #ifdef __GNUG__
12 #pragma implementation
13 #endif
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 FORMS_H_LOCATION
23
24 #include "helper_funcs.h"
25 #include "gettext.h"
26 #include "debug.h"
27 #include "support/LAssert.h"
28 #include "support/lstrings.h"
29 #include "support/filetools.h"
30 #include "support/lyxalgo.h"
31
32
33 using std::vector;
34 using std::sort;
35
36
37 typedef FormCB<ControlBibtex, FormDB<FD_bibtex> > base_class;
38
39 FormBibtex::FormBibtex()
40         : base_class(_("BibTeX Database"))
41 {}
42
43
44 void FormBibtex::build()
45 {
46         dialog_.reset(build_bibtex(this));
47
48         fl_set_input_return(dialog_->input_database, FL_RETURN_CHANGED);
49         fl_set_input_return(dialog_->input_style, FL_RETURN_CHANGED);
50
51         setPrehandler(dialog_->input_database);
52         setPrehandler(dialog_->input_style);
53
54         // Manage the ok, apply, restore and cancel/close buttons
55         bc().setOK(dialog_->button_ok);
56         bc().setCancel(dialog_->button_close);
57
58         bc().addReadOnly(dialog_->button_database_browse);
59         bc().addReadOnly(dialog_->input_database);
60         bc().addReadOnly(dialog_->button_style_browse);
61         bc().addReadOnly(dialog_->button_style_choose);
62         bc().addReadOnly(dialog_->button_rescan);
63         bc().addReadOnly(dialog_->input_style);
64         bc().addReadOnly(dialog_->check_bibtotoc);
65
66         // set up the tooltips
67         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\".");
68         tooltips().init(dialog_->button_database_browse, str);
69
70         str = _("Browse your directory for BibTeX stylefiles");
71         tooltips().init(dialog_->button_style_browse, str);
72
73         str = _("The BibTeX style to use (only one allowed). Insert it without the default extension \".bst\" and without path or choose one from the browsers list. Most of the bibstyles are stored in $TEXMF/bibtex/bst. $TEXMF is the root dir of the local TeX tree.");
74         tooltips().init(dialog_->input_style, str);
75
76         str = _("Activate this option if you want the bibliography to appear in the Table of Contents (which doesn't happen by default)");
77         tooltips().init(dialog_->check_bibtotoc, str);
78
79         str = _("Choose a BibTeX style from the browsers list");
80         tooltips().init(dialog_->button_style_choose, str);
81
82         str = _("Updates your TeX system for a new bibstyle list. Only the styles which are in directories where TeX finds them are listed!");
83         tooltips().init(dialog_->button_rescan, str);
84
85 }
86
87
88 ButtonPolicy::SMInput FormBibtex::input(FL_OBJECT * ob, long)
89 {
90         if (ob == dialog_->button_database_browse) {
91                 // When browsing, take the first file only
92                 string const in_name = fl_get_input(dialog_->input_database);
93                 string out_name =
94                         controller().Browse("",
95                                 _("Select Database"),
96                                 _("*.bib| BibTeX Databases (*.bib)"));
97                 if (!out_name.empty()) {
98                         // add the database to any existing ones
99                         if (!in_name.empty())
100                                 out_name = in_name + ',' + out_name;
101
102                         fl_freeze_form(form());
103                         fl_set_input(dialog_->input_database, out_name.c_str());
104                         fl_unfreeze_form(form());
105                 }
106         } else if (ob == dialog_->button_style_browse) {
107                 string const in_name = fl_get_input(dialog_->input_style);
108                 string out_name =
109                         controller().Browse(in_name,
110                                 _("Select BibTeX-Style"),
111                                 _("*.bst| BibTeX Styles (*.bst)"));
112                 if (!out_name.empty()) {
113                         fl_freeze_form(form());
114                         fl_set_input(dialog_->input_style, out_name.c_str());
115                         fl_unfreeze_form(form());
116                 }
117         } else if (ob == dialog_->button_style_choose) {
118                 unsigned int selection = fl_get_browser(dialog_->browser_styles);
119                 string const out_name =
120                         fl_get_browser_line(dialog_->browser_styles, selection);
121                 fl_set_input(dialog_->input_style,
122                         ChangeExtension(out_name, string()).c_str());
123         } else if (ob == dialog_->button_rescan) {
124                 fl_clear_browser(dialog_->browser_styles);
125                 controller().rescanBibStyles();
126                 string const str =
127                         controller().getBibStyles();
128                 fl_add_browser_line(dialog_->browser_styles, str.c_str());
129         }
130
131         // with an empty database nothing makes sense ...
132         if (!compare(fl_get_input(dialog_->input_database), "")) {
133                 return ButtonPolicy::SMI_NOOP;
134         }
135
136         return ButtonPolicy::SMI_VALID;
137 }
138
139
140 void FormBibtex::update()
141 {
142         fl_set_input(dialog_->input_database,
143                      controller().params().getContents().c_str());
144         string bibtotoc = "bibtotoc";
145         string bibstyle (controller().params().getOptions().c_str());
146         if (prefixIs(bibstyle, bibtotoc)) { // bibtotoc exists?
147                 fl_set_button(dialog_->check_bibtotoc,1);
148                 if (contains(bibstyle, ',')) { // bibstyle exists?
149                         bibstyle = split(bibstyle, bibtotoc, ',');
150                 } else {
151                         bibstyle = string();
152                 }
153
154                 fl_set_input(dialog_->input_style, bibstyle.c_str());
155
156         } else {
157                 fl_set_button(dialog_->check_bibtotoc,0);
158                 fl_set_input(dialog_->input_style, bibstyle.c_str());
159         }
160         fl_clear_browser(dialog_->browser_styles);
161         string const str =
162                 controller().getBibStyles();
163         fl_add_browser_line(dialog_->browser_styles, str.c_str());
164 }
165
166 namespace {
167
168 string const unique_and_no_extensions(string const & str_in)
169 {
170         vector<string> dbase = getVectorFromString(str_in);
171         for (vector<string>::iterator it = dbase.begin();
172              it != dbase.end(); ++it) {
173                 *it = ChangeExtension(*it, string());
174         }
175         lyx::eliminate_duplicates(dbase);
176         return getStringFromVector(dbase);
177 }
178
179 } // namespace anon
180
181
182 void FormBibtex::apply()
183 {
184         string const db = fl_get_input(dialog_->input_database);
185         if (db.empty()) {
186                 // no database -> no bibtex-command and no options!
187                 controller().params().setContents("");
188                 controller().params().setOptions("");
189                 return;
190         }
191
192         controller().params().setContents(unique_and_no_extensions(db));
193
194         // empty is valid!
195         string bibstyle = fl_get_input(dialog_->input_style);
196         if (!bibstyle.empty()) {
197                 // save the BibTeX style without any ".bst" extension
198                 bibstyle = ChangeExtension(OnlyFilename(bibstyle), "");
199         }
200
201         bool const bibtotoc = fl_get_button(dialog_->check_bibtotoc);
202
203         if (bibtotoc && (!bibstyle.empty())) {
204                 // both bibtotoc and style
205                 controller().params().setOptions("bibtotoc," + bibstyle);
206
207         } else if (bibtotoc) {
208                 // bibtotoc and no style
209                 controller().params().setOptions("bibtotoc");
210
211         } else  // all other cases
212                 controller().params().setOptions(bibstyle);
213 }