]> git.lyx.org Git - lyx.git/blob - src/insets/InsetBibitem.cpp
829e15a52a22d642cc05dac70249dfa209adb3e4
[lyx.git] / src / insets / InsetBibitem.cpp
1 /**
2  * \file InsetBibitem.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Alejandro Aguilar Sierra
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "InsetBibitem.h"
14
15 #include "Biblio.h"
16 #include "Buffer.h"
17 #include "BufferParams.h"
18 #include "BufferView.h"
19 #include "Counters.h"
20 #include "DispatchResult.h"
21 #include "FuncRequest.h"
22 #include "Font.h"
23 #include "InsetIterator.h"
24 #include "Lexer.h"
25 #include "Paragraph.h"
26 #include "ParagraphList.h"
27
28 #include "support/lstrings.h"
29 #include "support/std_ostream.h"
30 #include "support/convert.h"
31
32
33 namespace lyx {
34
35 using support::prefixIs;
36
37 using std::max;
38 using std::string;
39 using std::auto_ptr;
40 using std::ostream;
41
42 int InsetBibitem::key_counter = 0;
43 docstring const key_prefix = from_ascii("key-");
44
45 InsetBibitem::InsetBibitem(InsetCommandParams const & p)
46         : InsetCommand(p, "bibitem")
47 {
48         if (getParam("key").empty())
49                 setParam("key", key_prefix + convert<docstring>(++key_counter));
50 }
51
52
53 auto_ptr<Inset> InsetBibitem::doClone() const
54 {
55         auto_ptr<InsetBibitem> b(new InsetBibitem(params()));
56         b->autolabel_ = autolabel_;
57         return auto_ptr<Inset>(b);
58 }
59
60
61 void InsetBibitem::doDispatch(Cursor & cur, FuncRequest & cmd)
62 {
63         switch (cmd.action) {
64
65         case LFUN_INSET_MODIFY: {
66                 InsetCommandParams p("bibitem");
67                 InsetCommandMailer::string2params("bibitem", to_utf8(cmd.argument()), p);
68                 if (p.getCmdName().empty()) {
69                         cur.noUpdate();
70                         break;
71                 }
72                 if (p["key"] != params()["key"])
73                         cur.bv().buffer()->changeRefsIfUnique(params()["key"],
74                                                        p["key"], Inset::CITE_CODE);
75                 setParams(p);
76         }
77
78         default:
79                 InsetCommand::doDispatch(cur, cmd);
80                 break;
81         }
82 }
83
84
85 void InsetBibitem::read(Buffer const & buf, Lexer & lex)
86 {
87         InsetCommand::read(buf, lex);
88
89         if (prefixIs(getParam("key"), key_prefix)) {
90                 int const key = convert<int>(getParam("key").substr(key_prefix.length()));
91                 key_counter = max(key_counter, key);
92         }
93 }
94
95
96 docstring const InsetBibitem::getBibLabel() const
97 {
98         docstring const & label = getParam("label");
99         return label.empty() ? autolabel_ : label;
100 }
101
102
103 docstring const InsetBibitem::getScreenLabel(Buffer const &) const
104 {
105         return getParam("key") + " [" + getBibLabel() + ']';
106 }
107
108
109 int InsetBibitem::plaintext(Buffer const &, odocstream & os,
110                             OutputParams const &) const
111 {
112         odocstringstream oss;
113         oss << '[' << getBibLabel() << "] ";
114
115         docstring const str = oss.str();
116         os << str;
117
118         return str.size();
119 }
120
121
122 // ale070405
123 docstring const bibitemWidest(Buffer const & buffer)
124 {
125         int w = 0;
126
127         InsetBibitem const * bitem = 0;
128
129         // FIXME: this font is used unitialized for now but should  be set to
130         // a proportional font. Here is what Georg Baum has to say about it:
131         /*
132         bibitemWidest() is supposed to find the bibitem with the widest label in the
133         output, because that is needed as an argument of the bibliography
134         environment to dtermine the correct indentation. To be 100% correct we
135         would need the metrics of the font that is used in the output, but usually
136         we don't have access to these.
137         In practice, any proportional font is probably good enough, since we don't
138         need to know the final with, we only need to know the which label is the
139         widest.
140         Unless there is an easy way to get the metrics of the output font I suggest
141         to use a hardcoded font like "Times" or so.
142
143         It is very important that the result of this function is the same both with
144         and without GUI. After thinking about this it is clear that no Font
145         metrics should be used here, since these come from the gui. If we can't
146         easily get the LaTeX font metrics we should make our own poor mans front
147         metrics replacement, e.g. by hardcoding the metrics of the standard TeX
148         font.
149         */
150         Font font;
151
152         ParagraphList::const_iterator it = buffer.paragraphs().begin();
153         ParagraphList::const_iterator end = buffer.paragraphs().end();
154
155         for (; it != end; ++it) {
156                 if (it->bibitem()) {
157                         docstring const label = it->bibitem()->getBibLabel();
158
159                         // FIXME: we can't be sure using the following that the GUI
160                         // version and the command-line version will give the same
161                         // result.
162                         //
163                         //int const wx = use_gui?
164                         //      theFontMetrics(font).width(label): label.size();
165                         //
166                         // So for now we just use the label size in order to be sure
167                         // that GUI and no-GUI gives the same bibitem (even if that is
168                         // potentially the wrong one.
169                         int const wx = label.size();
170
171                         if (wx > w) {
172                                 w = wx;
173                                 bitem = it->bibitem();
174                         }
175                 }
176         }
177
178         if (bitem && !bitem->getBibLabel().empty())
179                 return bitem->getBibLabel();
180
181         return from_ascii("99");
182 }
183
184
185 void InsetBibitem::fillWithBibKeys(Buffer const & buf,
186         biblio::BibKeyList & keys, InsetIterator const & it) const
187 {
188         string const key = to_utf8(getParam("key"));
189         biblio::BibTeXInfo keyvalmap;
190         keyvalmap[from_ascii("label")] = getParam("label");
191         DocIterator doc_it(it); 
192         doc_it.forwardPos();
193         keyvalmap [from_ascii("ref")] = doc_it.paragraph().asString(buf, false);
194         keyvalmap.isBibTeX = false;
195         keys[key] = keyvalmap;
196 }
197
198
199 /// Update the counters of this inset and of its contents
200 void InsetBibitem::updateLabels(Buffer const &buf, ParIterator const &) 
201 {
202         Counters & counters = buf.params().getTextClass().counters();
203         docstring const bibitem = from_ascii("bibitem");
204         if (counters.hasCounter(bibitem) && getParam("label").empty()) {
205                 counters.step(bibitem);
206                 autolabel_ = counters.theCounter(bibitem);
207         } else
208                 autolabel_ = from_ascii("??");
209         refresh();
210 }
211
212
213 } // namespace lyx