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 "frontends/font_metrics.h"
26 #include "support/lstrings.h"
27 #include "support/std_ostream.h"
28 #include "support/convert.h"
31 using lyx::support::prefixIs;
38 int InsetBibitem::key_counter = 0;
39 string const key_prefix = "key-";
42 InsetBibitem::InsetBibitem(InsetCommandParams const & p)
43 : InsetCommand(p, "bibitem"), counter(1)
45 if (getContents().empty())
46 setContents(key_prefix + convert<string>(++key_counter));
50 auto_ptr<InsetBase> InsetBibitem::doClone() const
52 auto_ptr<InsetBibitem> b(new InsetBibitem(params()));
53 b->setCounter(counter);
54 return auto_ptr<InsetBase>(b);
58 void InsetBibitem::doDispatch(LCursor & cur, FuncRequest & cmd)
62 case LFUN_INSET_MODIFY: {
64 InsetCommandMailer::string2params("bibitem", lyx::to_utf8(cmd.argument()), p);
65 if (!p.getCmdName().empty())
73 InsetCommand::doDispatch(cur, cmd);
79 void InsetBibitem::setCounter(int c)
85 // I'm sorry but this is still necessary because \bibitem is used also
86 // as a LyX 2.x command, and lyxlex is not enough smart to understand
87 // real LaTeX commands. Yes, that could be fixed, but would be a waste
88 // of time cause LyX3 won't use lyxlex anyway. (ale)
89 void InsetBibitem::write(Buffer const &, std::ostream & os) const
92 if (!getOptions().empty())
93 os << '[' << getOptions() << ']';
94 os << '{' << getContents() << "}\n";
98 // This is necessary here because this is written without begin_inset
99 // This should be changed!!! (Jug)
100 void InsetBibitem::read(Buffer const &, LyXLex & lex)
103 scanCommand(lex.getString());
105 lex.printError("InsetCommand: Parse error: `$$Token'");
107 if (prefixIs(getContents(), key_prefix)) {
108 int const key = convert<int>(getContents().substr(key_prefix.length()));
109 key_counter = max(key_counter, key);
114 string const InsetBibitem::getBibLabel() const
116 return getOptions().empty() ? convert<string>(counter) : getOptions();
120 string const InsetBibitem::getScreenLabel(Buffer const &) const
122 return getContents() + " [" + getBibLabel() + ']';
125 int InsetBibitem::plaintext(Buffer const &, ostream & os,
126 OutputParams const &) const
128 os << '[' << getCounter() << "] ";
134 string const bibitemWidest(Buffer const & buffer)
137 // Does look like a hack? It is! (but will change at 0.13)
139 InsetBibitem const * bitem = 0;
142 ParagraphList::const_iterator it = buffer.paragraphs().begin();
143 ParagraphList::const_iterator end = buffer.paragraphs().end();
145 for (; it != end; ++it) {
147 string const label = it->bibitem()->getBibLabel();
148 docstring const dlab(label.begin(), label.end());
151 font_metrics::width(dlab, font);
154 bitem = it->bibitem();
159 if (bitem && !bitem->getBibLabel().empty())
160 return bitem->getBibLabel();