]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormBibtex.C
use exclicit temp var
[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
7  * \author John Levon
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 "form_bibtex.h"
20 #include "gettext.h"
21 #include "debug.h"
22 #include "xforms_helpers.h"
23 #include "helper_funcs.h"
24 #include "support/LAssert.h"
25 #include "support/lstrings.h"
26 #include "support/filetools.h"
27
28
29 using std::vector;
30 using std::sort;
31
32
33 typedef FormCB<ControlBibtex, FormDB<FD_form_bibtex> > base_class;
34
35 FormBibtex::FormBibtex(ControlBibtex & c)
36         : base_class(c, _("BibTeX Database"))
37 {}
38
39
40 void FormBibtex::build()
41 {
42         dialog_.reset(build_bibtex());
43
44         // the help choice
45         fillTooltipChoice(dialog_->choice_help);
46         fl_set_input_return(dialog_->database, FL_RETURN_CHANGED);
47         fl_set_input_return(dialog_->style, FL_RETURN_CHANGED);
48
49         // Manage the ok, apply, restore and cancel/close buttons
50         bc().setOK(dialog_->button_ok);
51         bc().setCancel(dialog_->button_cancel);
52
53         bc().addReadOnly(dialog_->database_browse);
54         bc().addReadOnly(dialog_->database);
55         bc().addReadOnly(dialog_->style_browse);
56         bc().addReadOnly(dialog_->style);
57         bc().addReadOnly(dialog_->radio_bibtotoc);
58
59         // set up the help mechanism
60         setTooltipHandler(dialog_->database_browse);
61         setTooltipHandler(dialog_->database);
62         setTooltipHandler(dialog_->style_browse);
63         setTooltipHandler(dialog_->style);
64         setTooltipHandler(dialog_->radio_bibtotoc);
65 }
66
67
68 ButtonPolicy::SMInput FormBibtex::input(FL_OBJECT * ob, long)
69 {
70         if (ob == dialog_->database_browse) {
71                 // When browsing, take the first file only 
72                 string const in_name = fl_get_input(dialog_->database);
73                 string out_name = 
74                         controller().Browse("",
75                                             "Select Database",
76                                             "*.bib| BibTeX Databases (*.bib)");
77                 if (!out_name.empty()) {
78                         // add the database to any existing ones
79                         if (!in_name.empty())
80                                 out_name = in_name + ", " + out_name;
81
82                         fl_freeze_form(form()); 
83                         fl_set_input(dialog_->database, out_name.c_str());
84                         fl_unfreeze_form(form()); 
85                 }
86         }
87
88         if (ob == dialog_->style_browse) {
89                 string const in_name = fl_get_input(dialog_->style);
90                 string out_name = 
91                         controller().Browse(in_name,
92                                             "Select BibTeX-Style",
93                                             "*.bst| BibTeX Styles (*.bst)");
94                 if (!out_name.empty()) {
95                         fl_freeze_form(form()); 
96                         fl_set_input(dialog_->style, out_name.c_str());
97                         fl_unfreeze_form(form()); 
98                 }
99         }
100   
101         if (ob == dialog_->choice_help) {
102                 setTooltipLevel(dialog_->choice_help);
103                 return ButtonPolicy::SMI_NOOP;
104         }
105
106         if (!compare(fl_get_input(dialog_->database),"")) {
107                 return ButtonPolicy::SMI_NOOP;
108         }
109
110         return ButtonPolicy::SMI_VALID;
111 }
112
113
114 void FormBibtex::update()
115 {
116         fl_set_input(dialog_->database,
117                      controller().params().getContents().c_str());
118         string bibtotoc = "bibtotoc";
119         string bibstyle (controller().params().getOptions().c_str());
120         if (prefixIs(bibstyle,bibtotoc)) { // bibtotoc exists?
121                 fl_set_button(dialog_->radio_bibtotoc,1);
122                 if (contains(bibstyle,',')) { // bibstyle exists?
123                         bibstyle = split(bibstyle,bibtotoc,',');
124                 } else {
125                         bibstyle = "";
126                 }
127
128                 fl_set_input(dialog_->style,bibstyle.c_str());
129
130         } else {
131                 fl_set_button(dialog_->radio_bibtotoc,0);
132                 fl_set_input(dialog_->style,bibstyle.c_str());
133         }
134 }
135
136 namespace {
137
138 // Remove all duplicate entries in c.
139 // Taken stright out of Stroustrup
140 template<class C>
141 void eliminate_duplicates(C & c)
142 {
143         sort(c.begin(), c.end());
144         typename C::iterator p = std::unique(c.begin(), c.end());
145         c.erase(p, c.end());
146 }
147
148
149 string const unique_and_no_extensions(string const & str_in)
150 {
151         vector<string> dbase = getVectorFromString(str_in);
152         for (vector<string>::iterator it = dbase.begin();
153              it != dbase.end(); ++it) {
154                 *it = ChangeExtension(*it, "");
155         }
156         eliminate_duplicates(dbase);
157         return subst(getStringFromVector(dbase),",",", ");
158 }
159  
160 } // namespace anon
161
162
163 void FormBibtex::apply()
164 {
165         string const db = fl_get_input(dialog_->database);
166         if (db.empty()) {
167                 // no database -> no bibtex-command and no options!
168                 controller().params().setContents("");
169                 controller().params().setOptions("");
170                 return;
171         }
172         
173         controller().params().setContents(unique_and_no_extensions(db));
174
175         // empty is valid!
176         string bibstyle = fl_get_input(dialog_->style);
177         if (!bibstyle.empty()) {
178                 // save the BibTeX style without any ".bst" extension
179                 bibstyle = ChangeExtension(OnlyFilename(bibstyle), "");
180         }
181
182         bool const bibtotoc = fl_get_button(dialog_->radio_bibtotoc);
183         
184         if (bibtotoc && (!bibstyle.empty())) {
185                 // both bibtotoc and style
186                 controller().params().setOptions("bibtotoc,"+bibstyle);
187
188         } else if (bibtotoc) {
189                 // bibtotoc and no style
190                 controller().params().setOptions("bibtotoc");
191
192         } else if (!bibstyle.empty()) {
193                 // only style
194                 controller().params().setOptions(bibstyle);
195         }
196 }
197
198
199 string const FormBibtex::getMinimalTooltip(FL_OBJECT const * ob) const
200 {
201         string str;
202
203         if (ob == dialog_->database) {
204                 str = N_("The BibTeX Database");
205
206         } else if (ob == dialog_->database_browse) {
207                 str = _("Browse for BibTeX databases.");
208
209         } else if (ob == dialog_->style) {
210                 str = _("The BibTeX style to use");
211
212         } else if (ob == dialog_->style_browse) {
213                 str = _("Browse for BibTeX stylefiles.");
214
215         } else if (ob == dialog_->radio_bibtotoc) {
216                 str = _("Bibliography to Table of Contents");
217         }
218
219         return str;
220 }
221
222 string const FormBibtex::getVerboseTooltip(FL_OBJECT const * ob) const
223 {
224         string str;
225
226         if (ob == dialog_->database) {
227                 str = N_("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\".");
228
229         } else if (ob == dialog_->database_browse) {
230                 str = _("Browse your directory for BibTeX databases.");
231
232         } else if (ob == dialog_->style) {
233                 str = _("The BibTeX style to use (only one allowed). Insert it without the default extension \".bst\" and without path. Most of the bibstyles are stored in $TEXMF/bibtex/bst. $TEXMF is the root dir of the local TeX tree. In \"Help->TeX Info\" you can list all installed styles.");
234
235         } else if (ob == dialog_->style_browse) {
236                 str = _("Browse your directory for BibTeX stylefiles.");
237
238         } else if (ob == dialog_->radio_bibtotoc) {
239                 str = _("Activate this option if you want the bibliography to appear in the Table of Contents (which doesn't happen by default).");
240         }
241
242         return str;
243 }