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-";
30 InsetBibitem::InsetBibitem(InsetCommandParams const & p)
31 : InsetCommand(p), counter(1)
33 if (getContents().empty())
34 setContents(key_prefix + tostr(++key_counter));
38 InsetBibitem::~InsetBibitem()
40 InsetCommandMailer mailer("bibitem", *this);
45 Inset * InsetBibitem::clone(Buffer const &, bool) const
47 InsetBibitem * b = new InsetBibitem(params());
48 b->setCounter(counter);
53 dispatch_result InsetBibitem::localDispatch(FuncRequest const & cmd)
55 Inset::RESULT result = UNDISPATCHED;
58 case LFUN_INSET_MODIFY: {
60 InsetCommandMailer::string2params(cmd.argument, p);
61 if (p.getCmdName().empty())
64 if (view() && p.getContents() != params().getContents()) {
65 view()->ChangeCitationsIfUnique(params().getContents(),
70 cmd.view()->updateInset(this);
72 // We need to do a redraw because the maximum
73 // InsetBibitem width could have changed
74 #warning please check you mean repaint() not update(),
75 #warning and whether the repaint() is needed at all
76 cmd.view()->repaint();
77 cmd.view()->fitCursor();
83 result = InsetCommand::localDispatch(cmd);
90 void InsetBibitem::setCounter(int c)
96 // I'm sorry but this is still necessary because \bibitem is used also
97 // as a LyX 2.x command, and lyxlex is not enough smart to understand
98 // real LaTeX commands. Yes, that could be fixed, but would be a waste
99 // of time cause LyX3 won't use lyxlex anyway. (ale)
100 void InsetBibitem::write(Buffer const *, std::ostream & os) const
102 os << "\n\\bibitem ";
103 if (!getOptions().empty())
104 os << '[' << getOptions() << ']';
105 os << '{' << getContents() << "}\n";
109 // This is necessary here because this is written without begin_inset
110 // This should be changed!!! (Jug)
111 void InsetBibitem::read(Buffer const *, LyXLex & lex)
114 scanCommand(lex.getString());
116 lex.printError("InsetCommand: Parse error: `$$Token'");
119 if (prefixIs(getContents(), key_prefix)) {
120 int key = strToInt(getContents().substr(key_prefix.length()));
121 key_counter = max(key_counter, key);
126 string const InsetBibitem::getBibLabel() const
128 return getOptions().empty() ? tostr(counter) : getOptions();
132 string const InsetBibitem::getScreenLabel(Buffer const *) const
134 return getContents() + " [" + getBibLabel() + ']';
138 void InsetBibitem::edit(BufferView * bv, int, int, mouse_button::state)
140 InsetCommandMailer mailer("bibitem", *this);
141 mailer.showDialog(bv);
145 void InsetBibitem::edit(BufferView * bv, bool)
147 edit(bv, 0, 0, mouse_button::none);
151 // ale070405 This function maybe shouldn't be here. We'll fix this at 0.13.
152 int bibitemMaxWidth(BufferView * bv, LyXFont const & font)
155 // Ha, now we are mainly at 1.2.0 and it is still here (Jug)
156 // Does look like a hack? It is! (but will change at 0.13)
157 ParagraphList::iterator it = bv->buffer()->paragraphs.begin();
158 ParagraphList::iterator end = bv->buffer()->paragraphs.end();
159 for (; it != end; ++it) {
161 int const wx = it->bibitem()->width(bv, font);
171 string const bibitemWidest(Buffer const * buffer)
174 // Does look like a hack? It is! (but will change at 0.13)
176 InsetBibitem * bitem = 0;
179 ParagraphList::iterator it = buffer->paragraphs.begin();
180 ParagraphList::iterator end = buffer->paragraphs.end();
181 for (; it != end; ++it) {
184 font_metrics::width(it->bibitem()->getBibLabel(),
188 bitem = it->bibitem();
193 if (bitem && !bitem->getBibLabel().empty())
194 return bitem->getBibLabel();