]> git.lyx.org Git - lyx.git/blob - src/insets/InsetBibitem.cpp
The counters labelstring patch. Part 2: Use the new code.
[lyx.git] / src / insets / InsetBibitem.cpp
1 /**
2  * \file InsetBibitem.cpp
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 "debug.h"
16
17 #include "Biblio.h"
18 #include "Buffer.h"
19 #include "BufferParams.h"
20 #include "BufferView.h"
21 #include "Counters.h"
22 #include "DispatchResult.h"
23 #include "FuncRequest.h"
24 #include "Font.h"
25 #include "InsetIterator.h"
26 #include "Lexer.h"
27 #include "Paragraph.h"
28 #include "ParagraphList.h"
29
30 #include "support/lstrings.h"
31 #include "support/std_ostream.h"
32 #include "support/convert.h"
33
34
35 namespace lyx {
36
37 using support::prefixIs;
38
39 using std::max;
40 using std::string;
41 using std::auto_ptr;
42 using std::ostream;
43
44 int InsetBibitem::key_counter = 0;
45 docstring const key_prefix = from_ascii("key-");
46
47 InsetBibitem::InsetBibitem(InsetCommandParams const & p)
48         : InsetCommand(p, "bibitem")
49 {
50         if (getParam("key").empty())
51                 setParam("key", key_prefix + convert<docstring>(++key_counter));
52 }
53
54
55 auto_ptr<Inset> InsetBibitem::doClone() const
56 {
57         auto_ptr<InsetBibitem> b(new InsetBibitem(params()));
58         b->autolabel_ = autolabel_;
59         return auto_ptr<Inset>(b);
60 }
61
62
63 void InsetBibitem::doDispatch(Cursor & cur, FuncRequest & cmd)
64 {
65         switch (cmd.action) {
66
67         case LFUN_INSET_MODIFY: {
68                 InsetCommandParams p("bibitem");
69                 InsetCommandMailer::string2params("bibitem", to_utf8(cmd.argument()), p);
70                 if (p.getCmdName().empty()) {
71                         cur.noUpdate();
72                         break;
73                 }
74                 if (p["key"] != params()["key"])
75                         cur.bv().buffer()->changeRefsIfUnique(params()["key"],
76                                                        p["key"], Inset::CITE_CODE);
77                 setParams(p);
78         }
79
80         default:
81                 InsetCommand::doDispatch(cur, cmd);
82                 break;
83         }
84 }
85
86
87 void InsetBibitem::read(Buffer const & buf, Lexer & lex)
88 {
89         InsetCommand::read(buf, lex);
90
91         if (prefixIs(getParam("key"), key_prefix)) {
92                 int const key = convert<int>(getParam("key").substr(key_prefix.length()));
93                 key_counter = max(key_counter, key);
94         }
95 }
96
97
98 docstring const InsetBibitem::getBibLabel() const
99 {
100         docstring const & label = getParam("label");
101         return label.empty() ? autolabel_ : label;
102 }
103
104
105 docstring const InsetBibitem::getScreenLabel(Buffer const &) const
106 {
107         return getParam("key") + " [" + getBibLabel() + ']';
108 }
109
110
111 int InsetBibitem::plaintext(Buffer const &, odocstream & os,
112                             OutputParams const &) const
113 {
114         odocstringstream oss;
115         oss << '[' << getBibLabel() << "] ";
116
117         docstring const str = oss.str();
118         os << str;
119
120         return str.size();
121 }
122
123
124 // ale070405
125 docstring const bibitemWidest(Buffer const & buffer)
126 {
127         int w = 0;
128
129         InsetBibitem const * bitem = 0;
130
131         // FIXME: this font is used unitialized for now but should  be set to
132         // a proportional font. Here is what Georg Baum has to say about it:
133         /*
134         bibitemWidest() is supposed to find the bibitem with the widest label in the
135         output, because that is needed as an argument of the bibliography
136         environment to dtermine the correct indentation. To be 100% correct we
137         would need the metrics of the font that is used in the output, but usually
138         we don't have access to these.
139         In practice, any proportional font is probably good enough, since we don't
140         need to know the final with, we only need to know the which label is the
141         widest.
142         Unless there is an easy way to get the metrics of the output font I suggest
143         to use a hardcoded font like "Times" or so.
144
145         It is very important that the result of this function is the same both with
146         and without GUI. After thinking about this it is clear that no Font
147         metrics should be used here, since these come from the gui. If we can't
148         easily get the LaTeX font metrics we should make our own poor mans front
149         metrics replacement, e.g. by hardcoding the metrics of the standard TeX
150         font.
151         */
152         Font font;
153
154         ParagraphList::const_iterator it = buffer.paragraphs().begin();
155         ParagraphList::const_iterator end = buffer.paragraphs().end();
156
157         for (; it != end; ++it) {
158                 if (it->bibitem()) {
159                         docstring const label = it->bibitem()->getBibLabel();
160
161                         // FIXME: we can't be sure using the following that the GUI
162                         // version and the command-line version will give the same
163                         // result.
164                         //
165                         //int const wx = use_gui?
166                         //      theFontMetrics(font).width(label): label.size();
167                         //
168                         // So for now we just use the label size in order to be sure
169                         // that GUI and no-GUI gives the same bibitem (even if that is
170                         // potentially the wrong one.
171                         int const wx = label.size();
172
173                         if (wx > w) {
174                                 w = wx;
175                                 bitem = it->bibitem();
176                         }
177                 }
178         }
179
180         if (bitem && !bitem->getBibLabel().empty())
181                 return bitem->getBibLabel();
182
183         return from_ascii("99");
184 }
185
186
187 void InsetBibitem::fillWithBibKeys(Buffer const & buf,
188         biblio::BibKeyList & keys, InsetIterator const & it) const
189 {
190         string const key = to_utf8(getParam("key"));
191         biblio::BibTeXInfo keyvalmap;
192         keyvalmap[from_ascii("label")] = getParam("label");
193         DocIterator doc_it(it); 
194         doc_it.forwardPos();
195         keyvalmap [from_ascii("ref")] = doc_it.paragraph().asString(buf, false);
196         keyvalmap.isBibTeX = false;
197         keys[key] = keyvalmap;
198 }
199
200
201 /// Update the counters of this inset and of its contents
202 void InsetBibitem::updateLabels(Buffer const &buf, ParIterator const & pit) 
203 {
204         lyxerr << "update! " << to_utf8(getParam("key")) << std::endl;
205         Counters & counters = buf.params().getTextClass().counters();
206         docstring const bibitem = from_ascii("bibitem");
207         if (counters.hasCounter(bibitem) && getParam("label").empty()) {
208                 counters.step(bibitem);
209                 autolabel_ = counters.theCounter(bibitem);
210         } else
211                 autolabel_ = from_ascii("??");
212         refresh();
213 }
214
215
216 } // namespace lyx