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 namespace lyx::support;
31 int InsetBibitem::key_counter = 0;
32 string const key_prefix = "key-";
35 InsetBibitem::InsetBibitem(InsetCommandParams const & p)
36 : InsetCommand(p), counter(1)
38 if (getContents().empty())
39 setContents(key_prefix + tostr(++key_counter));
43 InsetBibitem::~InsetBibitem()
45 InsetCommandMailer("bibitem", *this).hideDialog();
49 auto_ptr<InsetBase> InsetBibitem::clone() const
51 InsetBibitem * b = new InsetBibitem(params());
52 b->setCounter(counter);
53 return auto_ptr<InsetBase>(b);
57 dispatch_result InsetBibitem::localDispatch(FuncRequest const & cmd)
62 InsetCommandMailer("bibitem", *this).showDialog(cmd.view());
65 case LFUN_INSET_MODIFY: {
67 InsetCommandMailer::string2params(cmd.argument, p);
68 if (p.getCmdName().empty())
71 cmd.view()->updateInset(this);
72 cmd.view()->fitCursor();
77 return InsetCommand::localDispatch(cmd);
82 void InsetBibitem::setCounter(int c)
88 // I'm sorry but this is still necessary because \bibitem is used also
89 // as a LyX 2.x command, and lyxlex is not enough smart to understand
90 // real LaTeX commands. Yes, that could be fixed, but would be a waste
91 // of time cause LyX3 won't use lyxlex anyway. (ale)
92 void InsetBibitem::write(Buffer const &, std::ostream & os) const
95 if (!getOptions().empty())
96 os << '[' << getOptions() << ']';
97 os << '{' << getContents() << "}\n";
101 // This is necessary here because this is written without begin_inset
102 // This should be changed!!! (Jug)
103 void InsetBibitem::read(Buffer const &, LyXLex & lex)
106 scanCommand(lex.getString());
108 lex.printError("InsetCommand: Parse error: `$$Token'");
111 if (prefixIs(getContents(), key_prefix)) {
112 int key = strToInt(getContents().substr(key_prefix.length()));
113 key_counter = max(key_counter, key);
118 string const InsetBibitem::getBibLabel() const
120 return getOptions().empty() ? tostr(counter) : getOptions();
124 string const InsetBibitem::getScreenLabel(Buffer const &) const
126 return getContents() + " [" + getBibLabel() + ']';
130 // ale070405 This function maybe shouldn't be here. We'll fix this at 0.13.
131 int bibitemMaxWidth(BufferView * bv, LyXFont const &)
134 // Ha, now we are mainly at 1.2.0 and it is still here (Jug)
135 // Does look like a hack? It is! (but will change at 0.13)
136 ParagraphList::iterator it = bv->buffer()->paragraphs().begin();
137 ParagraphList::iterator end = bv->buffer()->paragraphs().end();
138 for (; it != end; ++it) {
140 #warning metrics broken!
141 int const wx = it->bibitem()->width();
151 string const bibitemWidest(Buffer const & buffer)
154 // Does look like a hack? It is! (but will change at 0.13)
156 InsetBibitem const * bitem = 0;
159 ParagraphList::const_iterator it = buffer.paragraphs().begin();
160 ParagraphList::const_iterator end = buffer.paragraphs().end();
162 for (; it != end; ++it) {
165 font_metrics::width(it->bibitem()->getBibLabel(),
169 bitem = it->bibitem();
174 if (bitem && !bitem->getBibLabel().empty())
175 return bitem->getBibLabel();