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 "dispatchresult.h"
17 #include "funcrequest.h"
20 #include "paragraph.h"
21 #include "ParagraphList.h"
23 #include "frontends/font_metrics.h"
25 #include "support/lstrings.h"
26 #include "support/std_ostream.h"
27 #include "support/convert.h"
30 using lyx::support::prefixIs;
37 int InsetBibitem::key_counter = 0;
38 string const key_prefix = "key-";
41 InsetBibitem::InsetBibitem(InsetCommandParams const & p)
42 : InsetCommand(p, "bibitem"), counter(1)
44 if (getContents().empty())
45 setContents(key_prefix + convert<string>(++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: {
63 InsetCommandMailer::string2params("bibitem", lyx::to_utf8(cmd.argument()), p);
64 if (!p.getCmdName().empty())
72 InsetCommand::doDispatch(cur, cmd);
78 void InsetBibitem::setCounter(int c)
84 // I'm sorry but this is still necessary because \bibitem is used also
85 // as a LyX 2.x command, and lyxlex is not enough smart to understand
86 // real LaTeX commands. Yes, that could be fixed, but would be a waste
87 // of time cause LyX3 won't use lyxlex anyway. (ale)
88 void InsetBibitem::write(Buffer const &, std::ostream & os) const
91 if (!getOptions().empty())
92 os << '[' << getOptions() << ']';
93 os << '{' << getContents() << "}\n";
97 // This is necessary here because this is written without begin_inset
98 // This should be changed!!! (Jug)
99 void InsetBibitem::read(Buffer const &, LyXLex & lex)
102 scanCommand(lex.getString());
104 lex.printError("InsetCommand: Parse error: `$$Token'");
106 if (prefixIs(getContents(), key_prefix)) {
107 int const key = convert<int>(getContents().substr(key_prefix.length()));
108 key_counter = max(key_counter, key);
113 string const InsetBibitem::getBibLabel() const
115 return getOptions().empty() ? convert<string>(counter) : getOptions();
119 string const InsetBibitem::getScreenLabel(Buffer const &) const
121 return getContents() + " [" + getBibLabel() + ']';
124 int InsetBibitem::plaintext(Buffer const &, ostream & os,
125 OutputParams const &) const
127 os << '[' << getCounter() << "] ";
133 string const bibitemWidest(Buffer const & buffer)
136 // Does look like a hack? It is! (but will change at 0.13)
138 InsetBibitem const * bitem = 0;
141 ParagraphList::const_iterator it = buffer.paragraphs().begin();
142 ParagraphList::const_iterator end = buffer.paragraphs().end();
144 for (; it != end; ++it) {
146 string const label = it->bibitem()->getBibLabel();
147 docstring const dlab(label.begin(), label.end());
150 font_metrics::width(dlab, font);
153 bitem = it->bibitem();
158 if (bitem && !bitem->getBibLabel().empty())
159 return bitem->getBibLabel();