2 * \file InsetBibitem.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Alejandro Aguilar Sierra
8 * Full author contact details are available in file CREDITS.
13 #include "InsetBibitem.h"
17 #include "BufferParams.h"
18 #include "BufferView.h"
20 #include "DispatchResult.h"
21 #include "FuncRequest.h"
23 #include "InsetIterator.h"
25 #include "Paragraph.h"
26 #include "ParagraphList.h"
28 #include "support/lstrings.h"
29 #include "support/std_ostream.h"
30 #include "support/convert.h"
35 using support::prefixIs;
42 int InsetBibitem::key_counter = 0;
43 docstring const key_prefix = from_ascii("key-");
45 InsetBibitem::InsetBibitem(InsetCommandParams const & p)
46 : InsetCommand(p, "bibitem")
48 if (getParam("key").empty())
49 setParam("key", key_prefix + convert<docstring>(++key_counter));
53 auto_ptr<Inset> InsetBibitem::doClone() const
55 auto_ptr<InsetBibitem> b(new InsetBibitem(params()));
56 b->autolabel_ = autolabel_;
57 return auto_ptr<Inset>(b);
61 void InsetBibitem::doDispatch(Cursor & cur, FuncRequest & cmd)
65 case LFUN_INSET_MODIFY: {
66 InsetCommandParams p("bibitem");
67 InsetCommandMailer::string2params("bibitem", to_utf8(cmd.argument()), p);
68 if (p.getCmdName().empty()) {
72 if (p["key"] != params()["key"])
73 cur.bv().buffer()->changeRefsIfUnique(params()["key"],
74 p["key"], Inset::CITE_CODE);
79 InsetCommand::doDispatch(cur, cmd);
85 void InsetBibitem::read(Buffer const & buf, Lexer & lex)
87 InsetCommand::read(buf, lex);
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);
96 docstring const InsetBibitem::getBibLabel() const
98 docstring const & label = getParam("label");
99 return label.empty() ? autolabel_ : label;
103 docstring const InsetBibitem::getScreenLabel(Buffer const &) const
105 return getParam("key") + " [" + getBibLabel() + ']';
109 int InsetBibitem::plaintext(Buffer const &, odocstream & os,
110 OutputParams const &) const
112 odocstringstream oss;
113 oss << '[' << getBibLabel() << "] ";
115 docstring const str = oss.str();
123 docstring const bibitemWidest(Buffer const & buffer)
127 InsetBibitem const * bitem = 0;
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:
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
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.
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
152 ParagraphList::const_iterator it = buffer.paragraphs().begin();
153 ParagraphList::const_iterator end = buffer.paragraphs().end();
155 for (; it != end; ++it) {
157 docstring const label = it->bibitem()->getBibLabel();
159 // FIXME: we can't be sure using the following that the GUI
160 // version and the command-line version will give the same
163 //int const wx = use_gui?
164 // theFontMetrics(font).width(label): label.size();
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();
173 bitem = it->bibitem();
178 if (bitem && !bitem->getBibLabel().empty())
179 return bitem->getBibLabel();
181 return from_ascii("99");
185 void InsetBibitem::fillWithBibKeys(Buffer const & buf,
186 biblio::BibKeyList & keys, InsetIterator const & it) const
188 string const key = to_utf8(getParam("key"));
189 biblio::BibTeXInfo keyvalmap;
190 keyvalmap[from_ascii("label")] = getParam("label");
191 DocIterator doc_it(it);
193 keyvalmap [from_ascii("ref")] = doc_it.paragraph().asString(buf, false);
194 keyvalmap.isBibTeX = false;
195 keys[key] = keyvalmap;
199 /// Update the counters of this inset and of its contents
200 void InsetBibitem::updateLabels(Buffer const &buf, ParIterator const &)
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);
208 autolabel_ = from_ascii("??");