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.
13 #include "insetbibitem.h"
16 #include "BufferView.h"
17 #include "funcrequest.h"
20 #include "paragraph.h"
22 #include "frontends/font_metrics.h"
24 #include "support/lstrings.h"
25 #include "support/tostr.h"
27 using lyx::support::prefixIs;
28 using lyx::support::strToInt;
35 int InsetBibitem::key_counter = 0;
36 string const key_prefix = "key-";
39 InsetBibitem::InsetBibitem(InsetCommandParams const & p)
40 : InsetCommand(p), counter(1)
42 if (getContents().empty())
43 setContents(key_prefix + tostr(++key_counter));
47 InsetBibitem::~InsetBibitem()
49 InsetCommandMailer("bibitem", *this).hideDialog();
53 auto_ptr<InsetBase> InsetBibitem::clone() const
55 InsetBibitem * b = new InsetBibitem(params());
56 b->setCounter(counter);
57 return auto_ptr<InsetBase>(b);
62 InsetBibitem::priv_dispatch(FuncRequest const & cmd,
63 idx_type & idx, pos_type & pos)
68 InsetCommandMailer("bibitem", *this).showDialog(cmd.view());
71 case LFUN_INSET_MODIFY: {
73 InsetCommandMailer::string2params(cmd.argument, p);
74 if (p.getCmdName().empty())
77 cmd.view()->updateInset(this);
78 cmd.view()->fitCursor();
83 return InsetCommand::priv_dispatch(cmd, idx, pos);
88 void InsetBibitem::setCounter(int c)
94 // I'm sorry but this is still necessary because \bibitem is used also
95 // as a LyX 2.x command, and lyxlex is not enough smart to understand
96 // real LaTeX commands. Yes, that could be fixed, but would be a waste
97 // of time cause LyX3 won't use lyxlex anyway. (ale)
98 void InsetBibitem::write(Buffer const &, std::ostream & os) const
100 os << "\n\\bibitem ";
101 if (!getOptions().empty())
102 os << '[' << getOptions() << ']';
103 os << '{' << getContents() << "}\n";
107 // This is necessary here because this is written without begin_inset
108 // This should be changed!!! (Jug)
109 void InsetBibitem::read(Buffer const &, LyXLex & lex)
112 scanCommand(lex.getString());
114 lex.printError("InsetCommand: Parse error: `$$Token'");
117 if (prefixIs(getContents(), key_prefix)) {
118 int key = strToInt(getContents().substr(key_prefix.length()));
119 key_counter = max(key_counter, key);
124 string const InsetBibitem::getBibLabel() const
126 return getOptions().empty() ? tostr(counter) : getOptions();
130 string const InsetBibitem::getScreenLabel(Buffer const &) const
132 return getContents() + " [" + getBibLabel() + ']';
136 // ale070405 This function maybe shouldn't be here. We'll fix this at 0.13.
137 int bibitemMaxWidth(BufferView * bv, LyXFont const &)
140 // Ha, now we are mainly at 1.2.0 and it is still here (Jug)
141 // Does look like a hack? It is! (but will change at 0.13)
142 ParagraphList::iterator it = bv->buffer()->paragraphs().begin();
143 ParagraphList::iterator end = bv->buffer()->paragraphs().end();
144 for (; it != end; ++it) {
146 #warning metrics broken!
147 int const wx = it->bibitem()->width();
157 string const bibitemWidest(Buffer const & buffer)
160 // Does look like a hack? It is! (but will change at 0.13)
162 InsetBibitem const * bitem = 0;
165 ParagraphList::const_iterator it = buffer.paragraphs().begin();
166 ParagraphList::const_iterator end = buffer.paragraphs().end();
168 for (; it != end; ++it) {
171 font_metrics::width(it->bibitem()->getBibLabel(),
175 bitem = it->bibitem();
180 if (bitem && !bitem->getBibLabel().empty())
181 return bitem->getBibLabel();