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 "BufferView.h"
17 #include "dispatchresult.h"
18 #include "funcrequest.h"
21 #include "paragraph.h"
22 #include "ParagraphList.h"
24 #include "support/lstrings.h"
25 #include "support/std_ostream.h"
26 #include "support/convert.h"
31 using support::prefixIs;
38 int InsetBibitem::key_counter = 0;
39 docstring const key_prefix = from_ascii("key-");
41 InsetBibitem::InsetBibitem(InsetCommandParams const & p)
42 : InsetCommand(p, "bibitem"), counter(1)
44 if (getParam("key").empty())
45 setParam("key", key_prefix + convert<docstring>(++key_counter));
49 auto_ptr<InsetBase> InsetBibitem::doClone() const
51 auto_ptr<InsetBibitem> b(new InsetBibitem(params()));
52 b->setCounter(counter);
53 return auto_ptr<InsetBase>(b);
57 void InsetBibitem::doDispatch(LCursor & cur, FuncRequest & cmd)
61 case LFUN_INSET_MODIFY: {
62 InsetCommandParams p("bibitem");
63 InsetCommandMailer::string2params("bibitem", to_utf8(cmd.argument()), p);
64 if (p.getCmdName().empty()) {
68 if (p["key"] != params()["key"])
69 cur.bv().buffer()->changeRefsIfUnique(params()["key"],
70 p["key"], InsetBase::CITE_CODE);
75 InsetCommand::doDispatch(cur, cmd);
81 void InsetBibitem::setCounter(int c)
87 void InsetBibitem::read(Buffer const & buf, LyXLex & lex)
89 InsetCommand::read(buf, lex);
91 if (prefixIs(getParam("key"), key_prefix)) {
92 int const key = convert<int>(getParam("key").substr(key_prefix.length()));
93 key_counter = max(key_counter, key);
98 docstring const InsetBibitem::getBibLabel() const
100 docstring const & label = getParam("label");
101 return label.empty() ? convert<docstring>(counter) : label;
105 docstring const InsetBibitem::getScreenLabel(Buffer const &) const
107 return getParam("key") + " [" + getBibLabel() + ']';
111 int InsetBibitem::plaintext(Buffer const &, odocstream & os,
112 OutputParams const &) const
114 os << '[' << getCounter() << "] ";
120 docstring const bibitemWidest(Buffer const & buffer)
124 InsetBibitem const * bitem = 0;
126 // FIXME: this font is used unitialized for now but should be set to
127 // a proportional font. Here is what Georg Baum has to say about it:
129 bibitemWidest() is supposed to find the bibitem with the widest label in the
130 output, because that is needed as an argument of the bibliography
131 environment to dtermine the correct indentation. To be 100% correct we
132 would need the metrics of the font that is used in the output, but usually
133 we don't have access to these.
134 In practice, any proportional font is probably good enough, since we don't
135 need to know the final with, we only need to know the which label is the
137 Unless there is an easy way to get the metrics of the output font I suggest
138 to use a hardcoded font like "Times" or so.
140 It is very important that the result of this function is the same both with
141 and without GUI. After thinking about this it is clear that no LyXFont
142 metrics should be used here, since these come from the gui. If we can't
143 easily get the LaTeX font metrics we should make our own poor mans front
144 metrics replacement, e.g. by hardcoding the metrics of the standard TeX
149 ParagraphList::const_iterator it = buffer.paragraphs().begin();
150 ParagraphList::const_iterator end = buffer.paragraphs().end();
152 for (; it != end; ++it) {
154 docstring const label = it->bibitem()->getBibLabel();
156 // FIXME: we can't be sure using the following that the GUI
157 // version and the command-line version will give the same
160 //int const wx = use_gui?
161 // theFontMetrics(font).width(label): label.size();
163 // So for now we just use the label size in order to be sure
164 // that GUI and no-GUI gives the same bibitem (even if that is
165 // potentially the wrong one.
166 int const wx = label.size();
170 bitem = it->bibitem();
175 if (bitem && !bitem->getBibLabel().empty())
176 return bitem->getBibLabel();
178 return from_ascii("99");