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 "dispatchresult.h"
18 #include "funcrequest.h"
21 #include "paragraph.h"
23 #include "frontends/font_metrics.h"
25 #include "support/lstrings.h"
26 #include "support/tostr.h"
28 using lyx::support::prefixIs;
29 using lyx::support::strToInt;
36 int InsetBibitem::key_counter = 0;
37 string const key_prefix = "key-";
40 InsetBibitem::InsetBibitem(InsetCommandParams const & p)
41 : InsetCommand(p), counter(1)
43 if (getContents().empty())
44 setContents(key_prefix + tostr(++key_counter));
48 InsetBibitem::~InsetBibitem()
50 InsetCommandMailer("bibitem", *this).hideDialog();
54 auto_ptr<InsetBase> InsetBibitem::clone() const
56 auto_ptr<InsetBibitem> b(new InsetBibitem(params()));
57 b->setCounter(counter);
58 return auto_ptr<InsetBase>(b);
63 InsetBibitem::priv_dispatch(FuncRequest const & cmd,
64 idx_type & idx, pos_type & pos)
68 case LFUN_MOUSE_PRESS:
69 InsetCommandMailer("bibitem", *this).showDialog(cmd.view());
70 return DispatchResult(true, true);
72 case LFUN_INSET_MODIFY: {
74 InsetCommandMailer::string2params(cmd.argument, p);
75 if (p.getCmdName().empty())
76 return DispatchResult(true, true);
79 cmd.view()->fitCursor();
80 return DispatchResult(true, true);
84 return InsetCommand::priv_dispatch(cmd, idx, pos);
89 void InsetBibitem::setCounter(int c)
95 // I'm sorry but this is still necessary because \bibitem is used also
96 // as a LyX 2.x command, and lyxlex is not enough smart to understand
97 // real LaTeX commands. Yes, that could be fixed, but would be a waste
98 // of time cause LyX3 won't use lyxlex anyway. (ale)
99 void InsetBibitem::write(Buffer const &, std::ostream & os) const
101 os << "\n\\bibitem ";
102 if (!getOptions().empty())
103 os << '[' << getOptions() << ']';
104 os << '{' << getContents() << "}\n";
108 // This is necessary here because this is written without begin_inset
109 // This should be changed!!! (Jug)
110 void InsetBibitem::read(Buffer const &, LyXLex & lex)
113 scanCommand(lex.getString());
115 lex.printError("InsetCommand: Parse error: `$$Token'");
118 if (prefixIs(getContents(), key_prefix)) {
119 int key = strToInt(getContents().substr(key_prefix.length()));
120 key_counter = max(key_counter, key);
125 string const InsetBibitem::getBibLabel() const
127 return getOptions().empty() ? tostr(counter) : getOptions();
131 string const InsetBibitem::getScreenLabel(Buffer const &) const
133 return getContents() + " [" + getBibLabel() + ']';
137 // ale070405 This function maybe shouldn't be here. We'll fix this at 0.13.
138 int bibitemMaxWidth(BufferView * bv, LyXFont const &)
141 // Ha, now we are mainly at 1.2.0 and it is still here (Jug)
142 // Does look like a hack? It is! (but will change at 0.13)
143 ParagraphList::iterator it = bv->buffer()->paragraphs().begin();
144 ParagraphList::iterator end = bv->buffer()->paragraphs().end();
145 for (; it != end; ++it) {
147 #warning metrics broken!
148 int const wx = it->bibitem()->width();
158 string const bibitemWidest(Buffer const & buffer)
161 // Does look like a hack? It is! (but will change at 0.13)
163 InsetBibitem const * bitem = 0;
166 ParagraphList::const_iterator it = buffer.paragraphs().begin();
167 ParagraphList::const_iterator end = buffer.paragraphs().end();
169 for (; it != end; ++it) {
172 font_metrics::width(it->bibitem()->getBibLabel(),
176 bitem = it->bibitem();
181 if (bitem && !bitem->getBibLabel().empty())
182 return bitem->getBibLabel();