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