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 Inset::RESULT result = UNDISPATCHED;
57 case LFUN_INSET_MODIFY: {
59 InsetCommandMailer::string2params(cmd.argument, p);
60 if (p.getCmdName().empty())
63 if (view() && p.getContents() != params().getContents()) {
64 view()->ChangeCitationsIfUnique(params().getContents(),
69 cmd.view()->updateInset(this);
71 // We need to do a redraw because the maximum
72 // InsetBibitem width could have changed
73 #warning please check you mean repaint() not update(),
74 #warning and whether the repaint() is needed at all
75 cmd.view()->repaint();
76 cmd.view()->fitCursor();
82 result = InsetCommand::localDispatch(cmd);
89 void InsetBibitem::setCounter(int c)
95 // I'm sorry but this is still necessary because \bibitem is used also
96 // as a LyX 2.x command, and lyxlex is not enough smart to understand
97 // real LaTeX commands. Yes, that could be fixed, but would be a waste
98 // of time cause LyX3 won't use lyxlex anyway. (ale)
99 void InsetBibitem::write(Buffer const *, std::ostream & os) const
101 os << "\n\\bibitem ";
102 if (!getOptions().empty())
103 os << '[' << getOptions() << ']';
104 os << '{' << getContents() << "}\n";
108 // This is necessary here because this is written without begin_inset
109 // This should be changed!!! (Jug)
110 void InsetBibitem::read(Buffer const *, LyXLex & lex)
113 scanCommand(lex.getString());
115 lex.printError("InsetCommand: Parse error: `$$Token'");
118 if (prefixIs(getContents(), key_prefix)) {
119 int key = strToInt(getContents().substr(key_prefix.length()));
120 key_counter = max(key_counter, key);
125 string const InsetBibitem::getBibLabel() const
127 return getOptions().empty() ? tostr(counter) : getOptions();
131 string const InsetBibitem::getScreenLabel(Buffer const *) const
133 return getContents() + " [" + getBibLabel() + ']';
137 void InsetBibitem::edit(BufferView * bv, int, int, mouse_button::state)
139 InsetCommandMailer mailer("bibitem", *this);
140 mailer.showDialog(bv);
144 void InsetBibitem::edit(BufferView * bv, bool)
146 edit(bv, 0, 0, mouse_button::none);
150 // ale070405 This function maybe shouldn't be here. We'll fix this at 0.13.
151 int bibitemMaxWidth(BufferView * bv, LyXFont const & font)
154 // Ha, now we are mainly at 1.2.0 and it is still here (Jug)
155 // Does look like a hack? It is! (but will change at 0.13)
156 ParagraphList::iterator it = bv->buffer()->paragraphs.begin();
157 ParagraphList::iterator end = bv->buffer()->paragraphs.end();
158 for (; it != end; ++it) {
160 int const wx = it->bibitem()->width(bv, font);
170 string const bibitemWidest(Buffer const * buffer)
173 // Does look like a hack? It is! (but will change at 0.13)
175 InsetBibitem * bitem = 0;
178 ParagraphList::iterator it = buffer->paragraphs.begin();
179 ParagraphList::iterator end = buffer->paragraphs.end();
180 for (; it != end; ++it) {
183 font_metrics::width(it->bibitem()->getBibLabel(),
187 bitem = it->bibitem();
192 if (bitem && !bitem->getBibLabel().empty())
193 return bitem->getBibLabel();