]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/ControlBibtex.C
Fix display of the style name in the BibTeX Bibliography panel
[lyx.git] / src / frontends / controllers / ControlBibtex.C
1 /**
2  * \file ControlBibtex.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author John Levon
7  * \author Angus Leeming
8  * \author Herbert Voß
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "ControlBibtex.h"
16 #include "biblio.h"
17
18 #include "buffer.h"
19 #include "bufferparams.h"
20
21 #include "lyxrc.h"
22 #include "helper_funcs.h"
23 #include "tex_helpers.h"
24 #include "gettext.h"
25
26 #include "support/filefilterlist.h"
27 #include "support/filetools.h"
28 #include "support/lstrings.h"
29
30 using std::pair;
31 using std::string;
32 using std::vector;
33
34
35 namespace lyx {
36
37 using support::contains;
38 using support::FileFilterList;
39 using support::onlyFilename;
40 using support::prefixIs;
41 using support::split;
42
43 namespace frontend {
44
45
46 ControlBibtex::ControlBibtex(Dialog & d)
47         : ControlCommand(d, "bibtex", "bibtex")
48 {}
49
50
51 docstring const ControlBibtex::browseBib(docstring const & in_name) const
52 {
53         // FIXME UNICODE
54         pair<docstring, docstring> dir1(_("Documents|#o#O"),
55                                   lyx::from_utf8(lyxrc.document_path));
56         FileFilterList const filter(_("BibTeX Databases (*.bib)"));
57         return browseRelFile(in_name, lyx::from_utf8(kernel().bufferFilepath()),
58                              _("Select a BibTeX database to add"),
59                              filter, false, dir1);
60 }
61
62
63 docstring const ControlBibtex::browseBst(docstring const & in_name) const
64 {
65         // FIXME UNICODE
66         pair<docstring, docstring> dir1(_("Documents|#o#O"),
67                                   lyx::from_utf8(lyxrc.document_path));
68         FileFilterList const filter(_("BibTeX Styles (*.bst)"));
69         return browseRelFile(in_name, lyx::from_utf8(kernel().bufferFilepath()),
70                              _("Select a BibTeX style"), filter, false, dir1);
71 }
72
73
74 void ControlBibtex::getBibStyles(vector<string> & data) const
75 {
76         data.clear();
77
78         getTexFileList("bstFiles.lst", data);
79         // test, if we have a valid list, otherwise run rescan
80         if (data.empty()) {
81                 rescanBibStyles();
82                 getTexFileList("bstFiles.lst", data);
83         }
84         vector<string>::iterator it  = data.begin();
85         vector<string>::iterator end = data.end();
86         for (; it != end; ++it) {
87                 *it = onlyFilename(*it);
88         }
89         // sort on filename only (no path)
90         std::sort(data.begin(), data.end());
91 }
92
93
94 void ControlBibtex::getBibFiles(vector<string> & data) const
95 {
96         data.clear();
97
98         getTexFileList("bibFiles.lst", data);
99         // test, if we have a valid list, otherwise run rescan
100         if (data.empty()) {
101                 rescanBibStyles();
102                 getTexFileList("bibFiles.lst", data);
103         }
104         vector<string>::iterator it  = data.begin();
105         vector<string>::iterator end = data.end();
106         for (; it != end; ++it) {
107                 *it = onlyFilename(*it);
108         }
109         // sort on filename only (no path)
110         std::sort(data.begin(), data.end());
111 }
112
113
114 void ControlBibtex::rescanBibStyles() const
115 {
116         rescanTexStyles();
117 }
118
119
120 bool ControlBibtex::usingBibtopic() const
121 {
122         return kernel().buffer().params().use_bibtopic;
123 }
124
125
126 bool ControlBibtex::bibtotoc() const
127 {
128         return prefixIs(lyx::to_utf8(params()["options"]), "bibtotoc");
129 }
130
131
132 string const ControlBibtex::getStylefile() const
133 {
134         // the different bibtex packages have (and need) their
135         // own "plain" stylefiles
136         biblio::CiteEngine_enum const & engine =
137                 biblio::getEngine(kernel().buffer());
138         docstring defaultstyle;
139         switch (engine) {
140         case biblio::ENGINE_BASIC:
141                 defaultstyle = lyx::from_ascii("plain");
142                 break;
143         case biblio::ENGINE_NATBIB_AUTHORYEAR:
144                 defaultstyle = lyx::from_ascii("plainnat");
145                 break;
146         case biblio::ENGINE_NATBIB_NUMERICAL:
147                 defaultstyle = lyx::from_ascii("plainnat");
148                 break;
149         case biblio::ENGINE_JURABIB:
150                 defaultstyle = lyx::from_ascii("jurabib");
151                 break;
152         }
153
154         docstring bst = params()["options"];
155         if (bibtotoc()){
156                 // bibstyle exists?
157                 if (contains(bst, ',')) {
158                         docstring bibtotoc = lyx::from_ascii("bibtotoc");
159                         bst = split(bst, bibtotoc, ',');
160                 } else
161                         bst.erase();
162         }
163
164         // propose default style file for new insets
165         // existing insets might have (legally) no bst files
166         // (if the class already provides a style)
167         if (bst.empty() && params()["bibfiles"].empty())
168                 bst = defaultstyle;
169
170         // FIXME UNICODE
171         return lyx::to_utf8(bst);
172 }
173
174 } // namespace frontend
175 } // namespace lyx