]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormBibtex.C
change some __GNUG_ to __GNUG__
[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 "form_bibtex.h"
20 #include "Tooltips.h"
21 #include "xforms_helpers.h"
22 #include "helper_funcs.h"
23 #include "gettext.h"
24 #include "debug.h"
25 #include "support/LAssert.h"
26 #include "support/lstrings.h"
27 #include "support/filetools.h"
28
29
30 using std::vector;
31 using std::sort;
32
33
34 typedef FormCB<ControlBibtex, FormDB<FD_form_bibtex> > base_class;
35
36 FormBibtex::FormBibtex(ControlBibtex & c)
37         : base_class(c, _("BibTeX Database"))
38 {}
39
40
41 void FormBibtex::build()
42 {
43         dialog_.reset(build_bibtex());
44
45         fl_set_input_return(dialog_->input_database, FL_RETURN_CHANGED);
46         fl_set_input_return(dialog_->input_style, FL_RETURN_CHANGED);
47
48         setPrehandler(dialog_->input_database);
49         setPrehandler(dialog_->input_style);
50
51         // Manage the ok, apply, restore and cancel/close buttons
52         bc().setOK(dialog_->button_ok);
53         bc().setCancel(dialog_->button_close);
54
55         bc().addReadOnly(dialog_->button_database_browse);
56         bc().addReadOnly(dialog_->input_database);
57         bc().addReadOnly(dialog_->button_style_browse);
58         bc().addReadOnly(dialog_->input_style);
59         bc().addReadOnly(dialog_->check_bibtotoc);
60
61         // set up the tooltips
62         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\".");
63         tooltips().init(dialog_->button_database_browse, str);
64
65         str = _("Browse your directory for BibTeX stylefiles.");
66         tooltips().init(dialog_->button_style_browse, str);
67
68         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.");
69         tooltips().init(dialog_->input_style, str);
70
71         str = _("Activate this option if you want the bibliography to appear in the Table of Contents (which doesn't happen by default).");
72         tooltips().init(dialog_->check_bibtotoc, str);
73 }
74
75
76 ButtonPolicy::SMInput FormBibtex::input(FL_OBJECT * ob, long)
77 {
78         if (ob == dialog_->button_database_browse) {
79                 // When browsing, take the first file only
80                 string const in_name = fl_get_input(dialog_->input_database);
81                 string out_name =
82                         controller().Browse("",
83                                             "Select Database",
84                                             "*.bib| BibTeX Databases (*.bib)");
85                 if (!out_name.empty()) {
86                         // add the database to any existing ones
87                         if (!in_name.empty())
88                                 out_name = in_name + ", " + out_name;
89
90                         fl_freeze_form(form());
91                         fl_set_input(dialog_->input_database, out_name.c_str());
92                         fl_unfreeze_form(form());
93                 }
94         }
95
96         if (ob == dialog_->button_style_browse) {
97                 string const in_name = fl_get_input(dialog_->input_style);
98                 string out_name =
99                         controller().Browse(in_name,
100                                             "Select BibTeX-Style",
101                                             "*.bst| BibTeX Styles (*.bst)");
102                 if (!out_name.empty()) {
103                         fl_freeze_form(form());
104                         fl_set_input(dialog_->input_style, out_name.c_str());
105                         fl_unfreeze_form(form());
106                 }
107         }
108
109         if (!compare(fl_get_input(dialog_->input_database),"")) {
110                 return ButtonPolicy::SMI_NOOP;
111         }
112
113         return ButtonPolicy::SMI_VALID;
114 }
115
116
117 void FormBibtex::update()
118 {
119         fl_set_input(dialog_->input_database,
120                      controller().params().getContents().c_str());
121         string bibtotoc = "bibtotoc";
122         string bibstyle (controller().params().getOptions().c_str());
123         if (prefixIs(bibstyle,bibtotoc)) { // bibtotoc exists?
124                 fl_set_button(dialog_->check_bibtotoc,1);
125                 if (contains(bibstyle,',')) { // bibstyle exists?
126                         bibstyle = split(bibstyle,bibtotoc,',');
127                 } else {
128                         bibstyle = "";
129                 }
130
131                 fl_set_input(dialog_->input_style,bibstyle.c_str());
132
133         } else {
134                 fl_set_button(dialog_->check_bibtotoc,0);
135                 fl_set_input(dialog_->input_style,bibstyle.c_str());
136         }
137 }
138
139 namespace {
140
141 // Remove all duplicate entries in c.
142 // Taken stright out of Stroustrup
143 template<class C>
144 void eliminate_duplicates(C & c)
145 {
146         sort(c.begin(), c.end());
147         typename C::iterator p = std::unique(c.begin(), c.end());
148         c.erase(p, c.end());
149 }
150
151
152 string const unique_and_no_extensions(string const & str_in)
153 {
154         vector<string> dbase = getVectorFromString(str_in);
155         for (vector<string>::iterator it = dbase.begin();
156              it != dbase.end(); ++it) {
157                 *it = ChangeExtension(*it, "");
158         }
159         eliminate_duplicates(dbase);
160         return subst(getStringFromVector(dbase),",",", ");
161 }
162
163 } // namespace anon
164
165
166 void FormBibtex::apply()
167 {
168         string const db = fl_get_input(dialog_->input_database);
169         if (db.empty()) {
170                 // no database -> no bibtex-command and no options!
171                 controller().params().setContents("");
172                 controller().params().setOptions("");
173                 return;
174         }
175
176         controller().params().setContents(unique_and_no_extensions(db));
177
178         // empty is valid!
179         string bibstyle = fl_get_input(dialog_->input_style);
180         if (!bibstyle.empty()) {
181                 // save the BibTeX style without any ".bst" extension
182                 bibstyle = ChangeExtension(OnlyFilename(bibstyle), "");
183         }
184
185         bool const bibtotoc = fl_get_button(dialog_->check_bibtotoc);
186
187         if (bibtotoc && (!bibstyle.empty())) {
188                 // both bibtotoc and style
189                 controller().params().setOptions("bibtotoc,"+bibstyle);
190
191         } else if (bibtotoc) {
192                 // bibtotoc and no style
193                 controller().params().setOptions("bibtotoc");
194
195         } else if (!bibstyle.empty()) {
196                 // only style
197                 controller().params().setOptions(bibstyle);
198         }
199 }