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 "paragraph.h"
21 #include "frontends/font_metrics.h"
23 #include "support/lstrings.h"
24 #include "support/tostr.h"
26 using lyx::support::prefixIs;
27 using lyx::support::strToInt;
34 int InsetBibitem::key_counter = 0;
35 string const key_prefix = "key-";
38 InsetBibitem::InsetBibitem(InsetCommandParams const & p)
39 : InsetCommand(p), counter(1)
41 if (getContents().empty())
42 setContents(key_prefix + tostr(++key_counter));
46 InsetBibitem::~InsetBibitem()
48 InsetCommandMailer("bibitem", *this).hideDialog();
52 auto_ptr<InsetBase> InsetBibitem::clone() const
54 InsetBibitem * b = new InsetBibitem(params());
55 b->setCounter(counter);
56 return auto_ptr<InsetBase>(b);
61 InsetBibitem::priv_dispatch(FuncRequest const & cmd,
62 idx_type & idx, pos_type & pos)
67 InsetCommandMailer("bibitem", *this).showDialog(cmd.view());
70 case LFUN_INSET_MODIFY: {
72 InsetCommandMailer::string2params(cmd.argument, p);
73 if (p.getCmdName().empty())
76 cmd.view()->updateInset(this);
77 cmd.view()->fitCursor();
82 return InsetCommand::priv_dispatch(cmd, idx, pos);
87 void InsetBibitem::setCounter(int c)
93 // I'm sorry but this is still necessary because \bibitem is used also
94 // as a LyX 2.x command, and lyxlex is not enough smart to understand
95 // real LaTeX commands. Yes, that could be fixed, but would be a waste
96 // of time cause LyX3 won't use lyxlex anyway. (ale)
97 void InsetBibitem::write(Buffer const &, std::ostream & os) const
100 if (!getOptions().empty())
101 os << '[' << getOptions() << ']';
102 os << '{' << getContents() << "}\n";
106 // This is necessary here because this is written without begin_inset
107 // This should be changed!!! (Jug)
108 void InsetBibitem::read(Buffer const &, LyXLex & lex)
111 scanCommand(lex.getString());
113 lex.printError("InsetCommand: Parse error: `$$Token'");
116 if (prefixIs(getContents(), key_prefix)) {
117 int key = strToInt(getContents().substr(key_prefix.length()));
118 key_counter = max(key_counter, key);
123 string const InsetBibitem::getBibLabel() const
125 return getOptions().empty() ? tostr(counter) : getOptions();
129 string const InsetBibitem::getScreenLabel(Buffer const &) const
131 return getContents() + " [" + getBibLabel() + ']';
135 // ale070405 This function maybe shouldn't be here. We'll fix this at 0.13.
136 int bibitemMaxWidth(BufferView * bv, LyXFont const &)
139 // Ha, now we are mainly at 1.2.0 and it is still here (Jug)
140 // Does look like a hack? It is! (but will change at 0.13)
141 ParagraphList::iterator it = bv->buffer()->paragraphs().begin();
142 ParagraphList::iterator end = bv->buffer()->paragraphs().end();
143 for (; it != end; ++it) {
145 #warning metrics broken!
146 int const wx = it->bibitem()->width();
156 string const bibitemWidest(Buffer const & buffer)
159 // Does look like a hack? It is! (but will change at 0.13)
161 InsetBibitem const * bitem = 0;
164 ParagraphList::const_iterator it = buffer.paragraphs().begin();
165 ParagraphList::const_iterator end = buffer.paragraphs().end();
167 for (; it != end; ++it) {
170 font_metrics::width(it->bibitem()->getBibLabel(),
174 bitem = it->bibitem();
179 if (bitem && !bitem->getBibLabel().empty())
180 return bitem->getBibLabel();