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"
16 #include "BufferParams.h"
17 #include "BufferView.h"
19 #include "DispatchResult.h"
20 #include "FuncRequest.h"
21 #include "InsetIterator.h"
22 #include "InsetList.h"
24 #include "Paragraph.h"
25 #include "ParagraphList.h"
26 #include "TextClass.h"
28 #include "support/lstrings.h"
29 #include "support/docstream.h"
30 #include "support/convert.h"
35 using namespace lyx::support;
40 int InsetBibitem::key_counter = 0;
41 docstring const key_prefix = from_ascii("key-");
44 InsetBibitem::InsetBibitem(InsetCommandParams const & p)
45 : InsetCommand(p, "bibitem")
47 if (getParam("key").empty())
48 setParam("key", key_prefix + convert<docstring>(++key_counter));
52 ParamInfo const & InsetBibitem::findInfo(string const & /* cmdName */)
54 static ParamInfo param_info_;
55 if (param_info_.empty()) {
56 param_info_.add("label", ParamInfo::LATEX_OPTIONAL);
57 param_info_.add("key", ParamInfo::LATEX_REQUIRED);
63 Inset * InsetBibitem::clone() const
65 InsetBibitem * b = new InsetBibitem(params());
66 b->autolabel_ = autolabel_;
71 void InsetBibitem::doDispatch(Cursor & cur, FuncRequest & cmd)
75 case LFUN_INSET_MODIFY: {
76 InsetCommandParams p(BIBITEM_CODE);
77 InsetCommandMailer::string2params("bibitem", to_utf8(cmd.argument()), p);
78 if (p.getCmdName().empty()) {
82 if (p["key"] != params()["key"])
83 cur.bv().buffer().changeRefsIfUnique(params()["key"],
89 InsetCommand::doDispatch(cur, cmd);
95 void InsetBibitem::read(Lexer & lex)
97 InsetCommand::read(lex);
99 if (prefixIs(getParam("key"), key_prefix)) {
100 int const key = convert<int>(getParam("key").substr(key_prefix.length()));
101 key_counter = max(key_counter, key);
106 docstring InsetBibitem::bibLabel() const
108 docstring const & label = getParam("label");
109 return label.empty() ? autolabel_ : label;
113 docstring InsetBibitem::screenLabel() const
115 return getParam("key") + " [" + bibLabel() + ']';
119 int InsetBibitem::plaintext(odocstream & os, OutputParams const &) const
121 odocstringstream oss;
122 oss << '[' << bibLabel() << "] ";
124 docstring const str = oss.str();
132 docstring const bibitemWidest(Buffer const & buffer)
136 InsetBibitem const * bitem = 0;
138 // FIXME: this font is used unitialized for now but should be set to
139 // a proportional font. Here is what Georg Baum has to say about it:
141 bibitemWidest() is supposed to find the bibitem with the widest label in the
142 output, because that is needed as an argument of the bibliography
143 environment to dtermine the correct indentation. To be 100% correct we
144 would need the metrics of the font that is used in the output, but usually
145 we don't have access to these.
146 In practice, any proportional font is probably good enough, since we don't
147 need to know the final with, we only need to know the which label is the
149 Unless there is an easy way to get the metrics of the output font I suggest
150 to use a hardcoded font like "Times" or so.
152 It is very important that the result of this function is the same both with
153 and without GUI. After thinking about this it is clear that no Font
154 metrics should be used here, since these come from the gui. If we can't
155 easily get the LaTeX font metrics we should make our own poor mans font
156 metrics replacement, e.g. by hardcoding the metrics of the standard TeX
160 ParagraphList::const_iterator it = buffer.paragraphs().begin();
161 ParagraphList::const_iterator end = buffer.paragraphs().end();
163 for (; it != end; ++it) {
164 if (it->insetList().empty())
166 Inset * inset = it->insetList().begin()->inset;
167 if (inset->lyxCode() != BIBITEM_CODE)
170 bitem = static_cast<InsetBibitem const *>(inset);
171 docstring const label = bitem->bibLabel();
173 // FIXME: we can't be sure using the following that the GUI
174 // version and the command-line version will give the same
177 //int const wx = use_gui?
178 // theFontMetrics(font).width(label): label.size();
180 // So for now we just use the label size in order to be sure
181 // that GUI and no-GUI gives the same bibitem (even if that is
182 // potentially the wrong one.
183 int const wx = label.size();
189 if (bitem && !bitem->bibLabel().empty())
190 return bitem->bibLabel();
192 return from_ascii("99");
196 void InsetBibitem::fillWithBibKeys(BiblioInfo & keys, InsetIterator const & it) const
198 docstring const key = getParam("key");
199 BibTeXInfo keyvalmap(false);
200 keyvalmap[from_ascii("label")] = getParam("label");
201 DocIterator doc_it(it);
203 keyvalmap[from_ascii("ref")] = doc_it.paragraph().asString(false);
204 keys[key] = keyvalmap;
208 /// Update the counters of this inset and of its contents
209 void InsetBibitem::updateLabels(ParIterator const &)
211 Counters & counters = buffer().params().documentClass().counters();
212 docstring const bibitem = from_ascii("bibitem");
213 if (counters.hasCounter(bibitem) && getParam("label").empty()) {
214 counters.step(bibitem);
215 autolabel_ = counters.theCounter(bibitem);
217 autolabel_ = from_ascii("??");