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"
18 #include "frontends/LyXView.h"
19 #include "frontends/Dialogs.h"
21 #include "support/lstrings.h"
27 int InsetBibitem::key_counter = 0;
28 const string key_prefix = "key-";
30 InsetBibitem::InsetBibitem(InsetCommandParams const & p)
31 : InsetCommand(p), counter(1)
33 if (getContents().empty())
34 setContents(key_prefix + tostr(++key_counter));
38 Inset * InsetBibitem::clone(Buffer const &, bool) const
40 InsetBibitem * b = new InsetBibitem(params());
41 b->setCounter(counter);
46 void InsetBibitem::setCounter(int c)
52 // I'm sorry but this is still necessary because \bibitem is used also
53 // as a LyX 2.x command, and lyxlex is not enough smart to understand
54 // real LaTeX commands. Yes, that could be fixed, but would be a waste
55 // of time cause LyX3 won't use lyxlex anyway. (ale)
56 void InsetBibitem::write(Buffer const *, std::ostream & os) const
59 if (!getOptions().empty())
60 os << '[' << getOptions() << ']';
61 os << '{' << getContents() << "}\n";
65 // This is necessary here because this is written without begin_inset
66 // This should be changed!!! (Jug)
67 void InsetBibitem::read(Buffer const *, LyXLex & lex)
70 scanCommand(lex.getString());
72 lex.printError("InsetCommand: Parse error: `$$Token'");
75 if (prefixIs(getContents(), key_prefix)) {
76 int key = strToInt(getContents().substr(key_prefix.length()));
77 key_counter = max(key_counter, key);
82 string const InsetBibitem::getBibLabel() const
84 return getOptions().empty() ? tostr(counter) : getOptions();
88 string const InsetBibitem::getScreenLabel(Buffer const *) const
90 return getContents() + " [" + getBibLabel() + ']';
94 void InsetBibitem::edit(BufferView * bv, int, int, mouse_button::state)
96 bv->owner()->getDialogs().showBibitem(this);
100 void InsetBibitem::edit(BufferView * bv, bool)
102 edit(bv, 0, 0, mouse_button::none);
106 // ale070405 This function maybe shouldn't be here. We'll fix this at 0.13.
107 int bibitemMaxWidth(BufferView * bv, LyXFont const & font)
110 // Ha, now we are mainly at 1.2.0 and it is still here (Jug)
111 // Does look like a hack? It is! (but will change at 0.13)
112 ParagraphList::iterator it = bv->buffer()->paragraphs.begin();
113 ParagraphList::iterator end = bv->buffer()->paragraphs.end();
114 for (; it != end; ++it) {
116 int const wx = it->bibitem()->width(bv, font);
126 string const bibitemWidest(Buffer const * buffer)
129 // Does look like a hack? It is! (but will change at 0.13)
131 InsetBibitem * bitem = 0;
134 ParagraphList::iterator it = buffer->paragraphs.begin();
135 ParagraphList::iterator end = buffer->paragraphs.end();
136 for (; it != end; ++it) {
139 font_metrics::width(it->bibitem()->getBibLabel(),
143 bitem = it->bibitem();
148 if (bitem && !bitem->getBibLabel().empty())
149 return bitem->getBibLabel();