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
12 #include "insetbibitem.h"
14 #include "BufferView.h"
17 #include "frontends/font_metrics.h"
19 #include "support/lstrings.h"
25 int InsetBibitem::key_counter = 0;
26 const string key_prefix = "key-";
28 InsetBibitem::InsetBibitem(InsetCommandParams const & p)
29 : InsetCommand(p), counter(1)
31 if (getContents().empty())
32 setContents(key_prefix + tostr(++key_counter));
36 InsetBibitem::~InsetBibitem()
38 InsetCommandMailer mailer("bibitem", *this);
43 Inset * InsetBibitem::clone(Buffer const &, bool) const
45 InsetBibitem * b = new InsetBibitem(params());
46 b->setCounter(counter);
51 void InsetBibitem::setCounter(int c)
57 // I'm sorry but this is still necessary because \bibitem is used also
58 // as a LyX 2.x command, and lyxlex is not enough smart to understand
59 // real LaTeX commands. Yes, that could be fixed, but would be a waste
60 // of time cause LyX3 won't use lyxlex anyway. (ale)
61 void InsetBibitem::write(Buffer const *, std::ostream & os) const
64 if (!getOptions().empty())
65 os << '[' << getOptions() << ']';
66 os << '{' << getContents() << "}\n";
70 // This is necessary here because this is written without begin_inset
71 // This should be changed!!! (Jug)
72 void InsetBibitem::read(Buffer const *, LyXLex & lex)
75 scanCommand(lex.getString());
77 lex.printError("InsetCommand: Parse error: `$$Token'");
80 if (prefixIs(getContents(), key_prefix)) {
81 int key = strToInt(getContents().substr(key_prefix.length()));
82 key_counter = max(key_counter, key);
87 string const InsetBibitem::getBibLabel() const
89 return getOptions().empty() ? tostr(counter) : getOptions();
93 string const InsetBibitem::getScreenLabel(Buffer const *) const
95 return getContents() + " [" + getBibLabel() + ']';
99 void InsetBibitem::edit(BufferView *, int, int, mouse_button::state)
101 InsetCommandMailer mailer("bibitem", *this);
106 void InsetBibitem::edit(BufferView * bv, bool)
108 edit(bv, 0, 0, mouse_button::none);
112 // ale070405 This function maybe shouldn't be here. We'll fix this at 0.13.
113 int bibitemMaxWidth(BufferView * bv, LyXFont const & font)
116 // Ha, now we are mainly at 1.2.0 and it is still here (Jug)
117 // Does look like a hack? It is! (but will change at 0.13)
118 ParagraphList::iterator it = bv->buffer()->paragraphs.begin();
119 ParagraphList::iterator end = bv->buffer()->paragraphs.end();
120 for (; it != end; ++it) {
122 int const wx = it->bibitem()->width(bv, font);
132 string const bibitemWidest(Buffer const * buffer)
135 // Does look like a hack? It is! (but will change at 0.13)
137 InsetBibitem * bitem = 0;
140 ParagraphList::iterator it = buffer->paragraphs.begin();
141 ParagraphList::iterator end = buffer->paragraphs.end();
142 for (; it != end; ++it) {
145 font_metrics::width(it->bibitem()->getBibLabel(),
149 bitem = it->bibitem();
154 if (bitem && !bitem->getBibLabel().empty())
155 return bitem->getBibLabel();