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