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