2003-02-21 09:20:18 +00:00
|
|
|
/**
|
|
|
|
* \file insetbibitem.C
|
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
|
|
|
*
|
|
|
|
* \author Alejandro Aguilar Sierra
|
|
|
|
*
|
2003-08-23 00:17:00 +00:00
|
|
|
* Full author contact details are available in file CREDITS.
|
2003-02-21 09:20:18 +00:00
|
|
|
*/
|
2003-10-21 16:15:14 +00:00
|
|
|
|
2003-02-21 09:20:18 +00:00
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include "insetbibitem.h"
|
2003-09-05 09:01:27 +00:00
|
|
|
|
2003-02-21 09:20:18 +00:00
|
|
|
#include "buffer.h"
|
|
|
|
#include "BufferView.h"
|
2003-10-29 10:47:21 +00:00
|
|
|
#include "dispatchresult.h"
|
2003-02-26 19:28:38 +00:00
|
|
|
#include "funcrequest.h"
|
2003-09-16 13:43:30 +00:00
|
|
|
#include "lyxfont.h"
|
2003-02-21 09:20:18 +00:00
|
|
|
#include "lyxlex.h"
|
2003-09-06 17:23:08 +00:00
|
|
|
#include "paragraph.h"
|
2004-03-25 09:16:36 +00:00
|
|
|
#include "ParagraphList_fwd.h"
|
2003-02-21 09:20:18 +00:00
|
|
|
|
|
|
|
#include "frontends/font_metrics.h"
|
|
|
|
|
|
|
|
#include "support/lstrings.h"
|
2004-08-14 18:41:27 +00:00
|
|
|
#include "support/std_ostream.h"
|
2005-01-06 16:39:35 +00:00
|
|
|
#include "support/convert.h"
|
2003-02-21 09:20:18 +00:00
|
|
|
|
2003-09-09 22:13:45 +00:00
|
|
|
using lyx::support::prefixIs;
|
2003-02-21 09:20:18 +00:00
|
|
|
|
|
|
|
using std::max;
|
2003-10-06 15:43:21 +00:00
|
|
|
using std::string;
|
2003-07-25 17:11:25 +00:00
|
|
|
using std::auto_ptr;
|
2004-08-14 18:41:27 +00:00
|
|
|
using std::ostream;
|
2003-02-21 09:20:18 +00:00
|
|
|
|
|
|
|
int InsetBibitem::key_counter = 0;
|
2003-06-28 01:23:11 +00:00
|
|
|
string const key_prefix = "key-";
|
2003-02-21 09:20:18 +00:00
|
|
|
|
2003-05-16 07:44:00 +00:00
|
|
|
|
2003-02-21 09:20:18 +00:00
|
|
|
InsetBibitem::InsetBibitem(InsetCommandParams const & p)
|
2003-12-11 15:23:15 +00:00
|
|
|
: InsetCommand(p, "bibitem"), counter(1)
|
2003-02-21 09:20:18 +00:00
|
|
|
{
|
|
|
|
if (getContents().empty())
|
2005-01-06 15:40:49 +00:00
|
|
|
setContents(key_prefix + convert<string>(++key_counter));
|
2003-02-21 09:20:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-11-23 23:04:52 +00:00
|
|
|
auto_ptr<InsetBase> InsetBibitem::doClone() const
|
2003-02-21 09:20:18 +00:00
|
|
|
{
|
2003-11-03 17:47:28 +00:00
|
|
|
auto_ptr<InsetBibitem> b(new InsetBibitem(params()));
|
2003-02-21 09:20:18 +00:00
|
|
|
b->setCounter(counter);
|
2003-07-25 17:11:25 +00:00
|
|
|
return auto_ptr<InsetBase>(b);
|
2003-02-21 09:20:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-11-24 21:58:42 +00:00
|
|
|
void InsetBibitem::doDispatch(LCursor & cur, FuncRequest & cmd)
|
2003-02-26 19:28:38 +00:00
|
|
|
{
|
2003-03-07 21:44:48 +00:00
|
|
|
switch (cmd.action) {
|
2003-05-16 07:44:00 +00:00
|
|
|
|
2003-03-07 21:44:48 +00:00
|
|
|
case LFUN_INSET_MODIFY: {
|
|
|
|
InsetCommandParams p;
|
2003-12-11 15:23:15 +00:00
|
|
|
InsetCommandMailer::string2params("bibitem", cmd.argument, p);
|
2004-11-30 01:59:49 +00:00
|
|
|
if (!p.getCmdName().empty())
|
|
|
|
setParams(p);
|
2005-05-17 11:11:45 +00:00
|
|
|
else
|
|
|
|
cur.noUpdate();
|
2004-02-16 11:58:51 +00:00
|
|
|
break;
|
2003-03-07 21:44:48 +00:00
|
|
|
}
|
2003-05-16 07:44:00 +00:00
|
|
|
|
2003-03-07 21:44:48 +00:00
|
|
|
default:
|
2004-11-24 21:58:42 +00:00
|
|
|
InsetCommand::doDispatch(cur, cmd);
|
2004-02-16 11:58:51 +00:00
|
|
|
break;
|
2003-02-26 19:28:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-02-21 09:20:18 +00:00
|
|
|
void InsetBibitem::setCounter(int c)
|
|
|
|
{
|
|
|
|
counter = c;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// I'm sorry but this is still necessary because \bibitem is used also
|
|
|
|
// as a LyX 2.x command, and lyxlex is not enough smart to understand
|
|
|
|
// real LaTeX commands. Yes, that could be fixed, but would be a waste
|
|
|
|
// of time cause LyX3 won't use lyxlex anyway. (ale)
|
2003-08-28 07:41:31 +00:00
|
|
|
void InsetBibitem::write(Buffer const &, std::ostream & os) const
|
2003-02-21 09:20:18 +00:00
|
|
|
{
|
|
|
|
os << "\n\\bibitem ";
|
|
|
|
if (!getOptions().empty())
|
|
|
|
os << '[' << getOptions() << ']';
|
|
|
|
os << '{' << getContents() << "}\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// This is necessary here because this is written without begin_inset
|
|
|
|
// This should be changed!!! (Jug)
|
2003-08-28 07:41:31 +00:00
|
|
|
void InsetBibitem::read(Buffer const &, LyXLex & lex)
|
2003-02-21 09:20:18 +00:00
|
|
|
{
|
2004-11-30 01:59:49 +00:00
|
|
|
if (lex.eatLine())
|
2003-02-21 09:20:18 +00:00
|
|
|
scanCommand(lex.getString());
|
2004-11-30 01:59:49 +00:00
|
|
|
else
|
2003-02-21 09:20:18 +00:00
|
|
|
lex.printError("InsetCommand: Parse error: `$$Token'");
|
|
|
|
|
|
|
|
if (prefixIs(getContents(), key_prefix)) {
|
2005-01-27 21:05:44 +00:00
|
|
|
int const key = convert<int>(getContents().substr(key_prefix.length()));
|
2003-02-21 09:20:18 +00:00
|
|
|
key_counter = max(key_counter, key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
string const InsetBibitem::getBibLabel() const
|
|
|
|
{
|
2005-01-06 15:40:49 +00:00
|
|
|
return getOptions().empty() ? convert<string>(counter) : getOptions();
|
2003-02-21 09:20:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-08-28 07:41:31 +00:00
|
|
|
string const InsetBibitem::getScreenLabel(Buffer const &) const
|
2003-02-21 09:20:18 +00:00
|
|
|
{
|
|
|
|
return getContents() + " [" + getBibLabel() + ']';
|
|
|
|
}
|
|
|
|
|
2004-08-14 18:41:27 +00:00
|
|
|
int InsetBibitem::plaintext(Buffer const &, ostream & os,
|
|
|
|
OutputParams const &) const
|
|
|
|
{
|
|
|
|
os << '[' << getCounter() << "] ";
|
|
|
|
return 0;
|
|
|
|
}
|
2003-02-21 09:20:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
// ale070405
|
2003-08-28 07:41:31 +00:00
|
|
|
string const bibitemWidest(Buffer const & buffer)
|
2003-02-21 09:20:18 +00:00
|
|
|
{
|
|
|
|
int w = 0;
|
|
|
|
// Does look like a hack? It is! (but will change at 0.13)
|
|
|
|
|
2003-05-27 22:41:04 +00:00
|
|
|
InsetBibitem const * bitem = 0;
|
2003-02-21 09:20:18 +00:00
|
|
|
LyXFont font;
|
|
|
|
|
2003-09-09 09:47:59 +00:00
|
|
|
ParagraphList::const_iterator it = buffer.paragraphs().begin();
|
|
|
|
ParagraphList::const_iterator end = buffer.paragraphs().end();
|
2003-05-28 06:47:15 +00:00
|
|
|
|
2003-02-21 09:20:18 +00:00
|
|
|
for (; it != end; ++it) {
|
|
|
|
if (it->bibitem()) {
|
|
|
|
int const wx =
|
|
|
|
font_metrics::width(it->bibitem()->getBibLabel(),
|
|
|
|
font);
|
|
|
|
if (wx > w) {
|
|
|
|
w = wx;
|
|
|
|
bitem = it->bibitem();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bitem && !bitem->getBibLabel().empty())
|
|
|
|
return bitem->getBibLabel();
|
|
|
|
|
|
|
|
return "99";
|
|
|
|
}
|