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 const string 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 Inset * InsetBibitem::clone(Buffer const &) const
48 InsetBibitem * b = new InsetBibitem(params());
49 b->setCounter(counter);
54 // Inset * InsetBibitem::clone(Buffer const &, bool) const
56 // InsetBibitem * b = new InsetBibitem(params());
57 // b->setCounter(counter);
62 dispatch_result InsetBibitem::localDispatch(FuncRequest const & cmd)
67 InsetCommandMailer("bibitem", *this).showDialog(cmd.view());
70 case LFUN_INSET_MODIFY: {
72 InsetCommandMailer::string2params(cmd.argument, p);
73 if (p.getCmdName().empty())
76 if (view() && p.getContents() != params().getContents()) {
77 view()->ChangeCitationsIfUnique(params().getContents(),
82 cmd.view()->updateInset(this);
84 // We need to do a redraw because the maximum
85 // InsetBibitem width could have changed
86 #warning please check you mean repaint() not update(),
87 #warning and whether the repaint() is needed at all
88 cmd.view()->repaint();
89 cmd.view()->fitCursor();
94 return InsetCommand::localDispatch(cmd);
99 void InsetBibitem::setCounter(int c)
105 // I'm sorry but this is still necessary because \bibitem is used also
106 // as a LyX 2.x command, and lyxlex is not enough smart to understand
107 // real LaTeX commands. Yes, that could be fixed, but would be a waste
108 // of time cause LyX3 won't use lyxlex anyway. (ale)
109 void InsetBibitem::write(Buffer const *, std::ostream & os) const
111 os << "\n\\bibitem ";
112 if (!getOptions().empty())
113 os << '[' << getOptions() << ']';
114 os << '{' << getContents() << "}\n";
118 // This is necessary here because this is written without begin_inset
119 // This should be changed!!! (Jug)
120 void InsetBibitem::read(Buffer const *, LyXLex & lex)
123 scanCommand(lex.getString());
125 lex.printError("InsetCommand: Parse error: `$$Token'");
128 if (prefixIs(getContents(), key_prefix)) {
129 int key = strToInt(getContents().substr(key_prefix.length()));
130 key_counter = max(key_counter, key);
135 string const InsetBibitem::getBibLabel() const
137 return getOptions().empty() ? tostr(counter) : getOptions();
141 string const InsetBibitem::getScreenLabel(Buffer const *) const
143 return getContents() + " [" + getBibLabel() + ']';
147 // ale070405 This function maybe shouldn't be here. We'll fix this at 0.13.
148 int bibitemMaxWidth(BufferView * bv, LyXFont const & font)
151 // Ha, now we are mainly at 1.2.0 and it is still here (Jug)
152 // Does look like a hack? It is! (but will change at 0.13)
153 ParagraphList::iterator it = bv->buffer()->paragraphs.begin();
154 ParagraphList::iterator end = bv->buffer()->paragraphs.end();
155 for (; it != end; ++it) {
157 int const wx = it->bibitem()->width(bv, font);
167 string const bibitemWidest(Buffer const * buffer)
170 // Does look like a hack? It is! (but will change at 0.13)
172 InsetBibitem * bitem = 0;
175 ParagraphList::iterator it = buffer->paragraphs.begin();
176 ParagraphList::iterator end = buffer->paragraphs.end();
177 for (; it != end; ++it) {
180 font_metrics::width(it->bibitem()->getBibLabel(),
184 bitem = it->bibitem();
189 if (bitem && !bitem->getBibLabel().empty())
190 return bitem->getBibLabel();