]> git.lyx.org Git - lyx.git/blob - src/insets/insetbibitem.C
revert change from previous commit and remove the FontMetrics access completely in...
[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 "support/lstrings.h"
25 #include "support/std_ostream.h"
26 #include "support/convert.h"
27
28 using lyx::docstring;
29 using lyx::support::prefixIs;
30
31 using std::max;
32 using std::string;
33 using std::auto_ptr;
34 using std::ostream;
35
36 int InsetBibitem::key_counter = 0;
37 string const key_prefix = "key-";
38
39 InsetBibitem::InsetBibitem(InsetCommandParams const & p)
40         : InsetCommand(p, "bibitem"), counter(1)
41 {
42         if (getContents().empty())
43                 setContents(key_prefix + convert<string>(++key_counter));
44 }
45
46
47 auto_ptr<InsetBase> InsetBibitem::doClone() const
48 {
49         auto_ptr<InsetBibitem> b(new InsetBibitem(params()));
50         b->setCounter(counter);
51         return auto_ptr<InsetBase>(b);
52 }
53
54
55 void InsetBibitem::doDispatch(LCursor & cur, FuncRequest & cmd)
56 {
57         switch (cmd.action) {
58
59         case LFUN_INSET_MODIFY: {
60                 InsetCommandParams p;
61                 InsetCommandMailer::string2params("bibitem", lyx::to_utf8(cmd.argument()), p);
62                 if (p.getCmdName().empty()) {
63                         cur.noUpdate();
64                         break;
65                 }
66                 if (p.getContents() != params().getContents()) 
67                         cur.bv().buffer()->changeRefsIfUnique(params().getContents(),
68                                                        p.getContents(), InsetBase::CITE_CODE);
69                 setParams(p);
70         }
71
72         default:
73                 InsetCommand::doDispatch(cur, cmd);
74                 break;
75         }
76 }
77
78
79 void InsetBibitem::setCounter(int c)
80 {
81         counter = c;
82 }
83
84
85 // I'm sorry but this is still necessary because \bibitem is used also
86 // as a LyX 2.x command, and lyxlex is not enough smart to understand
87 // real LaTeX commands. Yes, that could be fixed, but would be a waste
88 // of time cause LyX3 won't use lyxlex anyway.  (ale)
89 void InsetBibitem::write(Buffer const &, std::ostream & os) const
90 {
91         os << "\n\\bibitem ";
92         if (!getOptions().empty())
93                 os << '[' << getOptions() << ']';
94         os << '{' << getContents() << "}\n";
95 }
96
97
98 // This is necessary here because this is written without begin_inset
99 // This should be changed!!! (Jug)
100 void InsetBibitem::read(Buffer const &, LyXLex & lex)
101 {
102         if (lex.eatLine())
103                 scanCommand(lex.getString());
104         else
105                 lex.printError("InsetCommand: Parse error: `$$Token'");
106
107         if (prefixIs(getContents(), key_prefix)) {
108                 int const key = convert<int>(getContents().substr(key_prefix.length()));
109                 key_counter = max(key_counter, key);
110         }
111 }
112
113
114 docstring const InsetBibitem::getBibLabel() const
115 {
116         // FIXME UNICODE
117         return getOptions().empty() ?
118                 convert<docstring>(counter) :
119                 lyx::from_utf8(getOptions());
120 }
121
122
123 docstring const InsetBibitem::getScreenLabel(Buffer const &) const
124 {
125         // FIXME UNICODE
126         return lyx::from_utf8(getContents()) + " [" + getBibLabel() + ']';
127 }
128
129
130 int InsetBibitem::plaintext(Buffer const &, lyx::odocstream & os,
131                             OutputParams const &) const
132 {
133         os << '[' << getCounter() << "] ";
134         return 0;
135 }
136
137
138 // ale070405
139 docstring const bibitemWidest(Buffer const & buffer)
140 {
141         int w = 0;
142
143         InsetBibitem const * bitem = 0;
144
145         // FIXME: this font is used unitialized for now but should  be set to
146         // a proportional font. Here is what Georg Baum has to say about it:
147         /*
148         bibitemWidest() is supposed to find the bibitem with the widest label in the 
149         output, because that is needed as an argument of the bibliography 
150         environment to dtermine the correct indentation. To be 100% correct we 
151         would need the metrics of the font that is used in the output, but usually 
152         we don't have access to these.
153         In practice, any proportional font is probably good enough, since we don't 
154         need to know the final with, we only need to know the which label is the 
155         widest.
156         Unless there is an easy way to get the metrics of the output font I suggest 
157         to use a hardcoded font like "Times" or so.
158
159         It is very important that the result of this function is the same both with 
160         and without GUI. After thinking about this it is clear that no LyXFont 
161         metrics should be used here, since these come from the gui. If we can't 
162         easily get the LaTeX font metrics we should make our own poor mans front 
163         metrics replacement, e.g. by hardcoding the metrics of the standard TeX 
164         font.
165         */
166         LyXFont font;
167
168         ParagraphList::const_iterator it = buffer.paragraphs().begin();
169         ParagraphList::const_iterator end = buffer.paragraphs().end();
170
171         for (; it != end; ++it) {
172                 if (it->bibitem()) {
173                         docstring const label = it->bibitem()->getBibLabel();
174             
175                         // FIXME: we can't be sure using the following that the GUI
176                         // version and the command-line version will give the same 
177                         // result.
178                         //
179                         //int const wx = lyx::use_gui?
180                         //      theFontMetrics(font).width(label): label.size();
181                         //
182                         // So for now we just use the label size in order to be sure
183                         // that GUI and no-GUI gives the same bibitem (even if that is 
184                         // potentially the wrong one.
185                         int const wx = label.size();
186
187                         if (wx > w) {
188                                 w = wx;
189                                 bitem = it->bibitem();
190                         }
191                 }
192         }
193
194         if (bitem && !bitem->getBibLabel().empty())
195                 return bitem->getBibLabel();
196
197         return lyx::from_ascii("99");
198 }