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"
15 #include "BufferView.h"
16 #include "funcrequest.h"
19 #include "frontends/font_metrics.h"
21 #include "support/lstrings.h"
22 #include "support/tostr.h"
24 using namespace lyx::support;
30 int InsetBibitem::key_counter = 0;
31 string const key_prefix = "key-";
34 InsetBibitem::InsetBibitem(InsetCommandParams const & p)
35 : InsetCommand(p), counter(1)
37 if (getContents().empty())
38 setContents(key_prefix + tostr(++key_counter));
42 InsetBibitem::~InsetBibitem()
44 InsetCommandMailer("bibitem", *this).hideDialog();
48 auto_ptr<InsetBase> InsetBibitem::clone() const
50 InsetBibitem * b = new InsetBibitem(params());
51 b->setCounter(counter);
52 return auto_ptr<InsetBase>(b);
56 dispatch_result InsetBibitem::localDispatch(FuncRequest const & cmd)
61 InsetCommandMailer("bibitem", *this).showDialog(cmd.view());
64 case LFUN_INSET_MODIFY: {
66 InsetCommandMailer::string2params(cmd.argument, p);
67 if (p.getCmdName().empty())
70 cmd.view()->updateInset(this);
71 cmd.view()->fitCursor();
76 return InsetCommand::localDispatch(cmd);
81 void InsetBibitem::setCounter(int c)
87 // I'm sorry but this is still necessary because \bibitem is used also
88 // as a LyX 2.x command, and lyxlex is not enough smart to understand
89 // real LaTeX commands. Yes, that could be fixed, but would be a waste
90 // of time cause LyX3 won't use lyxlex anyway. (ale)
91 void InsetBibitem::write(Buffer const &, std::ostream & os) const
94 if (!getOptions().empty())
95 os << '[' << getOptions() << ']';
96 os << '{' << getContents() << "}\n";
100 // This is necessary here because this is written without begin_inset
101 // This should be changed!!! (Jug)
102 void InsetBibitem::read(Buffer const &, LyXLex & lex)
105 scanCommand(lex.getString());
107 lex.printError("InsetCommand: Parse error: `$$Token'");
110 if (prefixIs(getContents(), key_prefix)) {
111 int key = strToInt(getContents().substr(key_prefix.length()));
112 key_counter = max(key_counter, key);
117 string const InsetBibitem::getBibLabel() const
119 return getOptions().empty() ? tostr(counter) : getOptions();
123 string const InsetBibitem::getScreenLabel(Buffer const &) const
125 return getContents() + " [" + getBibLabel() + ']';
129 // ale070405 This function maybe shouldn't be here. We'll fix this at 0.13.
130 int bibitemMaxWidth(BufferView * bv, LyXFont const &)
133 // Ha, now we are mainly at 1.2.0 and it is still here (Jug)
134 // Does look like a hack? It is! (but will change at 0.13)
135 ParagraphList::iterator it = bv->buffer()->paragraphs.begin();
136 ParagraphList::iterator end = bv->buffer()->paragraphs.end();
137 for (; it != end; ++it) {
139 #warning metrics broken!
140 int const wx = it->bibitem()->width();
150 string const bibitemWidest(Buffer const & buffer)
153 // Does look like a hack? It is! (but will change at 0.13)
155 InsetBibitem const * bitem = 0;
158 ParagraphList::const_iterator it = buffer.paragraphs.begin();
159 ParagraphList::const_iterator end = buffer.paragraphs.end();
161 for (; it != end; ++it) {
164 font_metrics::width(it->bibitem()->getBibLabel(),
168 bitem = it->bibitem();
173 if (bitem && !bitem->getBibLabel().empty())
174 return bitem->getBibLabel();