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/tostr.h"
21 #include "support/lstrings.h"
27 int InsetBibitem::key_counter = 0;
28 string const key_prefix = "key-";
31 InsetBibitem::InsetBibitem(InsetCommandParams const & p)
32 : InsetCommand(p), counter(1)
34 if (getContents().empty())
35 setContents(key_prefix + tostr(++key_counter));
39 InsetBibitem::~InsetBibitem()
41 InsetCommandMailer mailer("bibitem", *this);
46 InsetBase * InsetBibitem::clone() const
48 InsetBibitem * b = new InsetBibitem(params());
49 b->setCounter(counter);
54 dispatch_result InsetBibitem::localDispatch(FuncRequest const & cmd)
59 InsetCommandMailer("bibitem", *this).showDialog(cmd.view());
62 case LFUN_INSET_MODIFY: {
64 InsetCommandMailer::string2params(cmd.argument, p);
65 if (p.getCmdName().empty())
68 if (view() && p.getContents() != params().getContents()) {
69 view()->ChangeCitationsIfUnique(params().getContents(),
74 cmd.view()->updateInset(this);
76 // We need to do a redraw because the maximum
77 // InsetBibitem width could have changed
78 #warning please check you mean repaint() not update(),
79 #warning and whether the repaint() is needed at all
80 cmd.view()->repaint();
81 cmd.view()->fitCursor();
86 return InsetCommand::localDispatch(cmd);
91 void InsetBibitem::setCounter(int c)
97 // I'm sorry but this is still necessary because \bibitem is used also
98 // as a LyX 2.x command, and lyxlex is not enough smart to understand
99 // real LaTeX commands. Yes, that could be fixed, but would be a waste
100 // of time cause LyX3 won't use lyxlex anyway. (ale)
101 void InsetBibitem::write(Buffer const *, std::ostream & os) const
103 os << "\n\\bibitem ";
104 if (!getOptions().empty())
105 os << '[' << getOptions() << ']';
106 os << '{' << getContents() << "}\n";
110 // This is necessary here because this is written without begin_inset
111 // This should be changed!!! (Jug)
112 void InsetBibitem::read(Buffer const *, LyXLex & lex)
115 scanCommand(lex.getString());
117 lex.printError("InsetCommand: Parse error: `$$Token'");
120 if (prefixIs(getContents(), key_prefix)) {
121 int key = strToInt(getContents().substr(key_prefix.length()));
122 key_counter = max(key_counter, key);
127 string const InsetBibitem::getBibLabel() const
129 return getOptions().empty() ? tostr(counter) : getOptions();
133 string const InsetBibitem::getScreenLabel(Buffer const *) const
135 return getContents() + " [" + getBibLabel() + ']';
139 // ale070405 This function maybe shouldn't be here. We'll fix this at 0.13.
140 int bibitemMaxWidth(BufferView * bv, LyXFont const & font)
143 // Ha, now we are mainly at 1.2.0 and it is still here (Jug)
144 // Does look like a hack? It is! (but will change at 0.13)
145 ParagraphList::iterator it = bv->buffer()->paragraphs.begin();
146 ParagraphList::iterator end = bv->buffer()->paragraphs.end();
147 for (; it != end; ++it) {
149 int const wx = it->bibitem()->width(bv, font);
159 string const bibitemWidest(Buffer const * buffer)
162 // Does look like a hack? It is! (but will change at 0.13)
164 InsetBibitem const * bitem = 0;
167 ParagraphList::const_iterator it = buffer->paragraphs.begin();
168 ParagraphList::const_iterator end = buffer->paragraphs.end();
170 for (; it != end; ++it) {
173 font_metrics::width(it->bibitem()->getBibLabel(),
177 bitem = it->bibitem();
182 if (bitem && !bitem->getBibLabel().empty())
183 return bitem->getBibLabel();