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