]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormBibtex.C
Bugfixes: checkboxes to radiobuttons (from J�rgen S) and remove a little
[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 "helper_funcs.h"
23 #include "support/lstrings.h"
24 #include "support/filetools.h"
25
26
27 typedef FormCB<ControlBibtex, FormDB<FD_form_bibtex> > base_class;
28
29 FormBibtex::FormBibtex(ControlBibtex & c)
30         : base_class(c, _("BibTeX Database"))
31 {}
32
33
34 void FormBibtex::build()
35 {
36         dialog_.reset(build_bibtex());
37
38         fl_set_input_return(dialog_->database, FL_RETURN_CHANGED);
39         fl_set_input_return(dialog_->style, FL_RETURN_CHANGED);
40
41         // Manage the ok, apply, restore and cancel/close buttons
42         bc().setOK(dialog_->button_ok);
43         bc().setCancel(dialog_->button_cancel);
44
45         bc().addReadOnly(dialog_->database_browse);
46         bc().addReadOnly(dialog_->database);
47         bc().addReadOnly(dialog_->style_browse);
48         bc().addReadOnly(dialog_->style);
49         bc().addReadOnly(dialog_->radio_bibtotoc);
50 }
51
52
53 ButtonPolicy::SMInput FormBibtex::input(FL_OBJECT * ob, long)
54 {
55         if (ob == dialog_->database_browse) {
56                 // When browsing, take the first file only 
57                 string const in_name = fl_get_input(dialog_->database);
58                 string out_name = 
59                         controller().Browse("",
60                                             "Select Database",
61                                             "*.bib| BibTeX Databases (*.bib)");
62                 if (!out_name.empty()) {
63                         // add the database to any existing ones
64                         if (!in_name.empty())
65                                 out_name = in_name + ", " + out_name;
66
67                         fl_freeze_form(form()); 
68                         fl_set_input(dialog_->database, out_name.c_str());
69                         fl_unfreeze_form(form()); 
70                 }
71         }
72
73         if (ob == dialog_->style_browse) {
74                 string const in_name = fl_get_input(dialog_->style);
75                 string out_name = 
76                         controller().Browse(in_name,
77                                             "Select BibTeX-Style",
78                                             "*.bst| BibTeX Styles (*.bst)");
79                 if (!out_name.empty()) {
80                         fl_freeze_form(form()); 
81                         fl_set_input(dialog_->style, out_name.c_str());
82                         fl_unfreeze_form(form()); 
83                 }
84         }
85   
86         if (!compare(fl_get_input(dialog_->database),"")) {
87                 return ButtonPolicy::SMI_NOOP;
88         }
89
90         return ButtonPolicy::SMI_VALID;
91 }
92
93
94 void FormBibtex::update()
95 {
96         fl_set_input(dialog_->database,
97                      controller().params().getContents().c_str());
98         string bibtotoc = "bibtotoc";
99         string bibstyle (controller().params().getOptions().c_str());
100         if (prefixIs(bibstyle,bibtotoc)) { // bibtotoc exists?
101                 fl_set_button(dialog_->radio_bibtotoc,1);
102                 if (contains(bibstyle,',')) { // bibstyle exists?
103                         bibstyle = split(bibstyle,bibtotoc,',');
104                 } else {
105                         bibstyle = "";
106                 }
107
108                 fl_set_input(dialog_->style,bibstyle.c_str());
109
110         } else {
111                 fl_set_button(dialog_->radio_bibtotoc,0);
112                 fl_set_input(dialog_->style,bibstyle.c_str());
113         }
114 }
115
116 namespace {
117
118 // Remove all duplicate entries in c.
119 // Taken stright out of Stroustrup
120 template<class C> void eliminate_duplicates(C & c)
121 {
122         std::sort(c.begin(), c.end()); // sort
123         typename C::iterator p = std::unique(c.begin(), c.end()); // compact
124         c.erase(p, c.end()); // shrink
125 }
126
127 string const unique_and_no_extensions(string const & str_in)
128 {
129         std::vector<string> dbase = getVectorFromString(str_in);
130         for (std::vector<string>::iterator it = dbase.begin();
131              it != dbase.end(); ++it) {
132                 *it = ChangeExtension(*it, "");
133         }
134         eliminate_duplicates(dbase);
135         return subst(getStringFromVector(dbase),",",", ");
136 }
137  
138 } // namespace anon
139
140
141 void FormBibtex::apply()
142 {
143         string const db = fl_get_input(dialog_->database);
144         if (db.empty()) {
145                 // no database -> no bibtex-command and no options!
146                 controller().params().setContents("");
147                 controller().params().setOptions("");
148                 return;
149         }
150         
151         controller().params().setContents(unique_and_no_extensions(db));
152
153         // empty is valid!
154         string bibstyle = fl_get_input(dialog_->style);
155         if (!bibstyle.empty()) {
156                 // save the BibTeX style without any ".bst" extension
157                 bibstyle = ChangeExtension(OnlyFilename(bibstyle), "");
158         }
159
160         bool const bibtotoc = fl_get_button(dialog_->radio_bibtotoc);
161         
162         if (bibtotoc && (!bibstyle.empty())) {
163                 // both bibtotoc and style
164                 controller().params().setOptions("bibtotoc,"+bibstyle);
165
166         } else if (bibtotoc) {
167                 // bibtotoc and no style
168                 controller().params().setOptions("bibtotoc");
169
170         } else if (!bibstyle.empty()){
171                 // only style
172                 controller().params().setOptions(bibstyle);
173         }
174 }