]> git.lyx.org Git - lyx.git/blob - src/insets/insetbibitem.C
Fix bug 2744:
[lyx.git] / src / insets / insetbibitem.C
1 /**
2  * \file insetbibitem.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Alejandro Aguilar Sierra
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "insetbibitem.h"
14
15 #include "buffer.h"
16 #include "BufferView.h"
17 #include "dispatchresult.h"
18 #include "funcrequest.h"
19 #include "lyxfont.h"
20 #include "lyxlex.h"
21 #include "paragraph.h"
22 #include "ParagraphList.h"
23
24 #include "frontends/Application.h"
25 #include "frontends/FontLoader.h"
26 #include "frontends/FontMetrics.h"
27
28 #include "support/lstrings.h"
29 #include "support/std_ostream.h"
30 #include "support/convert.h"
31
32 using lyx::docstring;
33 using lyx::support::prefixIs;
34
35 using std::max;
36 using std::string;
37 using std::auto_ptr;
38 using std::ostream;
39
40 int InsetBibitem::key_counter = 0;
41 string const key_prefix = "key-";
42
43
44 InsetBibitem::InsetBibitem(InsetCommandParams const & p)
45         : InsetCommand(p, "bibitem"), counter(1)
46 {
47         if (getContents().empty())
48                 setContents(key_prefix + convert<string>(++key_counter));
49 }
50
51
52 auto_ptr<InsetBase> InsetBibitem::doClone() const
53 {
54         auto_ptr<InsetBibitem> b(new InsetBibitem(params()));
55         b->setCounter(counter);
56         return auto_ptr<InsetBase>(b);
57 }
58
59
60 void InsetBibitem::doDispatch(LCursor & cur, FuncRequest & cmd)
61 {
62         switch (cmd.action) {
63
64         case LFUN_INSET_MODIFY: {
65                 InsetCommandParams p;
66                 InsetCommandMailer::string2params("bibitem", lyx::to_utf8(cmd.argument()), p);
67                 if (p.getCmdName().empty()) {
68                         cur.noUpdate();
69                         break;
70                 }
71                 if (p.getContents() != params().getContents()) 
72                         cur.bv().buffer()->changeRefsIfUnique(params().getContents(),
73                                                        p.getContents(), InsetBase::CITE_CODE);
74                 setParams(p);
75         }
76
77         default:
78                 InsetCommand::doDispatch(cur, cmd);
79                 break;
80         }
81 }
82
83
84 void InsetBibitem::setCounter(int c)
85 {
86         counter = c;
87 }
88
89
90 // I'm sorry but this is still necessary because \bibitem is used also
91 // as a LyX 2.x command, and lyxlex is not enough smart to understand
92 // real LaTeX commands. Yes, that could be fixed, but would be a waste
93 // of time cause LyX3 won't use lyxlex anyway.  (ale)
94 void InsetBibitem::write(Buffer const &, std::ostream & os) const
95 {
96         os << "\n\\bibitem ";
97         if (!getOptions().empty())
98                 os << '[' << getOptions() << ']';
99         os << '{' << getContents() << "}\n";
100 }
101
102
103 // This is necessary here because this is written without begin_inset
104 // This should be changed!!! (Jug)
105 void InsetBibitem::read(Buffer const &, LyXLex & lex)
106 {
107         if (lex.eatLine())
108                 scanCommand(lex.getString());
109         else
110                 lex.printError("InsetCommand: Parse error: `$$Token'");
111
112         if (prefixIs(getContents(), key_prefix)) {
113                 int const key = convert<int>(getContents().substr(key_prefix.length()));
114                 key_counter = max(key_counter, key);
115         }
116 }
117
118
119 string const InsetBibitem::getBibLabel() const
120 {
121         return getOptions().empty() ? convert<string>(counter) : getOptions();
122 }
123
124
125 string const InsetBibitem::getScreenLabel(Buffer const &) const
126 {
127         return getContents() + " [" + getBibLabel() + ']';
128 }
129
130 int InsetBibitem::plaintext(Buffer const &, ostream & os,
131                             OutputParams const &) const
132 {
133         os << '[' << getCounter() << "] ";
134         return 0;
135 }
136
137
138 // ale070405
139 string const bibitemWidest(Buffer const & buffer)
140 {
141         int w = 0;
142         // Does look like a hack? It is! (but will change at 0.13)
143
144         InsetBibitem const * bitem = 0;
145         // FIXME font is used unitialized, is that correct?
146         LyXFont font;
147         lyx::frontend::FontMetrics const & fm =
148                 theApp->fontLoader().metrics(font);
149
150         ParagraphList::const_iterator it = buffer.paragraphs().begin();
151         ParagraphList::const_iterator end = buffer.paragraphs().end();
152
153         for (; it != end; ++it) {
154                 if (it->bibitem()) {
155                         string const label = it->bibitem()->getBibLabel();
156                         docstring const dlab(label.begin(), label.end());
157                     
158                         int const wx =
159                                 fm.width(dlab);
160                         if (wx > w) {
161                                 w = wx;
162                                 bitem = it->bibitem();
163                         }
164                 }
165         }
166
167         if (bitem && !bitem->getBibLabel().empty())
168                 return bitem->getBibLabel();
169
170         return "99";
171 }