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"
15 #include "funcrequest.h"
18 #include "frontends/font_metrics.h"
20 #include "support/lstrings.h"
26 int InsetBibitem::key_counter = 0;
27 const string key_prefix = "key-";
29 InsetBibitem::InsetBibitem(InsetCommandParams const & p)
30 : InsetCommand(p), counter(1)
32 if (getContents().empty())
33 setContents(key_prefix + tostr(++key_counter));
37 InsetBibitem::~InsetBibitem()
39 InsetCommandMailer mailer("bibitem", *this);
44 Inset * InsetBibitem::clone(Buffer const &, bool) const
46 InsetBibitem * b = new InsetBibitem(params());
47 b->setCounter(counter);
52 dispatch_result InsetBibitem::localDispatch(FuncRequest const & cmd)
54 if (cmd.action != LFUN_INSET_APPLY)
58 InsetCommandMailer::string2params(cmd.argument, p);
59 if (p.getCmdName().empty())
62 if (view() && p.getContents() != params().getContents()) {
63 view()->ChangeCitationsIfUnique(params().getContents(),
69 view()->updateInset(this, true);
75 void InsetBibitem::setCounter(int c)
81 // I'm sorry but this is still necessary because \bibitem is used also
82 // as a LyX 2.x command, and lyxlex is not enough smart to understand
83 // real LaTeX commands. Yes, that could be fixed, but would be a waste
84 // of time cause LyX3 won't use lyxlex anyway. (ale)
85 void InsetBibitem::write(Buffer const *, std::ostream & os) const
88 if (!getOptions().empty())
89 os << '[' << getOptions() << ']';
90 os << '{' << getContents() << "}\n";
94 // This is necessary here because this is written without begin_inset
95 // This should be changed!!! (Jug)
96 void InsetBibitem::read(Buffer const *, LyXLex & lex)
99 scanCommand(lex.getString());
101 lex.printError("InsetCommand: Parse error: `$$Token'");
104 if (prefixIs(getContents(), key_prefix)) {
105 int key = strToInt(getContents().substr(key_prefix.length()));
106 key_counter = max(key_counter, key);
111 string const InsetBibitem::getBibLabel() const
113 return getOptions().empty() ? tostr(counter) : getOptions();
117 string const InsetBibitem::getScreenLabel(Buffer const *) const
119 return getContents() + " [" + getBibLabel() + ']';
123 void InsetBibitem::edit(BufferView *, int, int, mouse_button::state)
125 InsetCommandMailer mailer("bibitem", *this);
130 void InsetBibitem::edit(BufferView * bv, bool)
132 edit(bv, 0, 0, mouse_button::none);
136 // ale070405 This function maybe shouldn't be here. We'll fix this at 0.13.
137 int bibitemMaxWidth(BufferView * bv, LyXFont const & font)
140 // Ha, now we are mainly at 1.2.0 and it is still here (Jug)
141 // Does look like a hack? It is! (but will change at 0.13)
142 ParagraphList::iterator it = bv->buffer()->paragraphs.begin();
143 ParagraphList::iterator end = bv->buffer()->paragraphs.end();
144 for (; it != end; ++it) {
146 int const wx = it->bibitem()->width(bv, font);
156 string const bibitemWidest(Buffer const * buffer)
159 // Does look like a hack? It is! (but will change at 0.13)
161 InsetBibitem * bitem = 0;
164 ParagraphList::iterator it = buffer->paragraphs.begin();
165 ParagraphList::iterator end = buffer->paragraphs.end();
166 for (; it != end; ++it) {
169 font_metrics::width(it->bibitem()->getBibLabel(),
173 bitem = it->bibitem();
178 if (bitem && !bitem->getBibLabel().empty())
179 return bitem->getBibLabel();