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"
18 #include "paragraph.h"
20 #include "frontends/font_metrics.h"
22 #include "support/lstrings.h"
23 #include "support/tostr.h"
25 using lyx::support::prefixIs;
26 using lyx::support::strToInt;
33 int InsetBibitem::key_counter = 0;
34 string const key_prefix = "key-";
37 InsetBibitem::InsetBibitem(InsetCommandParams const & p)
38 : InsetCommand(p), counter(1)
40 if (getContents().empty())
41 setContents(key_prefix + tostr(++key_counter));
45 InsetBibitem::~InsetBibitem()
47 InsetCommandMailer("bibitem", *this).hideDialog();
51 auto_ptr<InsetBase> InsetBibitem::clone() const
53 InsetBibitem * b = new InsetBibitem(params());
54 b->setCounter(counter);
55 return auto_ptr<InsetBase>(b);
59 dispatch_result InsetBibitem::localDispatch(FuncRequest const & cmd)
64 InsetCommandMailer("bibitem", *this).showDialog(cmd.view());
67 case LFUN_INSET_MODIFY: {
69 InsetCommandMailer::string2params(cmd.argument, p);
70 if (p.getCmdName().empty())
73 cmd.view()->updateInset(this);
74 cmd.view()->fitCursor();
79 return InsetCommand::localDispatch(cmd);
84 void InsetBibitem::setCounter(int c)
90 // I'm sorry but this is still necessary because \bibitem is used also
91 // as a LyX 2.x command, and lyxlex is not enough smart to understand
92 // real LaTeX commands. Yes, that could be fixed, but would be a waste
93 // of time cause LyX3 won't use lyxlex anyway. (ale)
94 void InsetBibitem::write(Buffer const &, std::ostream & os) const
97 if (!getOptions().empty())
98 os << '[' << getOptions() << ']';
99 os << '{' << getContents() << "}\n";
103 // This is necessary here because this is written without begin_inset
104 // This should be changed!!! (Jug)
105 void InsetBibitem::read(Buffer const &, LyXLex & lex)
108 scanCommand(lex.getString());
110 lex.printError("InsetCommand: Parse error: `$$Token'");
113 if (prefixIs(getContents(), key_prefix)) {
114 int key = strToInt(getContents().substr(key_prefix.length()));
115 key_counter = max(key_counter, key);
120 string const InsetBibitem::getBibLabel() const
122 return getOptions().empty() ? tostr(counter) : getOptions();
126 string const InsetBibitem::getScreenLabel(Buffer const &) const
128 return getContents() + " [" + getBibLabel() + ']';
132 // ale070405 This function maybe shouldn't be here. We'll fix this at 0.13.
133 int bibitemMaxWidth(BufferView * bv, LyXFont const &)
136 // Ha, now we are mainly at 1.2.0 and it is still here (Jug)
137 // Does look like a hack? It is! (but will change at 0.13)
138 ParagraphList::iterator it = bv->buffer()->paragraphs().begin();
139 ParagraphList::iterator end = bv->buffer()->paragraphs().end();
140 for (; it != end; ++it) {
142 #warning metrics broken!
143 int const wx = it->bibitem()->width();
153 string const bibitemWidest(Buffer const & buffer)
156 // Does look like a hack? It is! (but will change at 0.13)
158 InsetBibitem const * bitem = 0;
161 ParagraphList::const_iterator it = buffer.paragraphs().begin();
162 ParagraphList::const_iterator end = buffer.paragraphs().end();
164 for (; it != end; ++it) {
167 font_metrics::width(it->bibitem()->getBibLabel(),
171 bitem = it->bibitem();
176 if (bitem && !bitem->getBibLabel().empty())
177 return bitem->getBibLabel();