]> git.lyx.org Git - lyx.git/blob - src/insets/insetbibitem.C
Rename LatexRunParams::fragile as moving_arg.
[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 #include <config.h>
11
12 #include "insetbibitem.h"
13 #include "buffer.h"
14 #include "BufferView.h"
15 #include "funcrequest.h"
16 #include "lyxlex.h"
17
18 #include "frontends/font_metrics.h"
19
20 #include "support/tostr.h"
21 #include "support/lstrings.h"
22
23
24 using std::max;
25
26
27 int InsetBibitem::key_counter = 0;
28 const string key_prefix = "key-";
29
30
31 InsetBibitem::InsetBibitem(InsetCommandParams const & p)
32         : InsetCommand(p), counter(1)
33 {
34         if (getContents().empty())
35                 setContents(key_prefix + tostr(++key_counter));
36 }
37
38
39 InsetBibitem::~InsetBibitem()
40 {
41         InsetCommandMailer mailer("bibitem", *this);
42         mailer.hideDialog();
43 }
44
45
46 Inset * InsetBibitem::clone(Buffer const &, bool) const
47 {
48         InsetBibitem * b = new InsetBibitem(params());
49         b->setCounter(counter);
50         return b;
51 }
52
53
54 dispatch_result InsetBibitem::localDispatch(FuncRequest const & cmd)
55 {
56         switch (cmd.action) {
57
58         case LFUN_INSET_EDIT:
59                 InsetCommandMailer("bibitem", *this).showDialog(cmd.view());
60                 return DISPATCHED;
61
62         case LFUN_INSET_MODIFY: {
63                 InsetCommandParams p;
64                 InsetCommandMailer::string2params(cmd.argument, p);
65                 if (p.getCmdName().empty())
66                         return DISPATCHED;
67
68                 if (view() && p.getContents() != params().getContents()) {
69                         view()->ChangeCitationsIfUnique(params().getContents(),
70                                                         p.getContents());
71                 }
72
73                 setParams(p);
74                 cmd.view()->updateInset(this);
75
76                 // We need to do a redraw because the maximum
77                 // InsetBibitem width could have changed
78 #warning please check you mean repaint() not update(),
79 #warning and whether the repaint() is needed at all
80                 cmd.view()->repaint();
81                 cmd.view()->fitCursor();
82                 return DISPATCHED;
83         }
84
85         default:
86                 return InsetCommand::localDispatch(cmd);
87         }
88 }
89
90
91 void InsetBibitem::setCounter(int c)
92 {
93         counter = c;
94 }
95
96
97 // I'm sorry but this is still necessary because \bibitem is used also
98 // as a LyX 2.x command, and lyxlex is not enough smart to understand
99 // real LaTeX commands. Yes, that could be fixed, but would be a waste
100 // of time cause LyX3 won't use lyxlex anyway.  (ale)
101 void InsetBibitem::write(Buffer const *, std::ostream & os) const
102 {
103         os << "\n\\bibitem ";
104         if (!getOptions().empty())
105                 os << '[' << getOptions() << ']';
106         os << '{' << getContents() << "}\n";
107 }
108
109
110 // This is necessary here because this is written without begin_inset
111 // This should be changed!!! (Jug)
112 void InsetBibitem::read(Buffer const *, LyXLex & lex)
113 {
114         if (lex.eatLine()) {
115                 scanCommand(lex.getString());
116         } else {
117                 lex.printError("InsetCommand: Parse error: `$$Token'");
118         }
119
120         if (prefixIs(getContents(), key_prefix)) {
121                 int key = strToInt(getContents().substr(key_prefix.length()));
122                 key_counter = max(key_counter, key);
123         }
124 }
125
126
127 string const InsetBibitem::getBibLabel() const
128 {
129         return getOptions().empty() ? tostr(counter) : getOptions();
130 }
131
132
133 string const InsetBibitem::getScreenLabel(Buffer const *) const
134 {
135         return getContents() + " [" + getBibLabel() + ']';
136 }
137
138
139 // ale070405 This function maybe shouldn't be here. We'll fix this at 0.13.
140 int bibitemMaxWidth(BufferView * bv, LyXFont const & font)
141 {
142         int w = 0;
143         // Ha, now we are mainly at 1.2.0 and it is still here (Jug)
144         // Does look like a hack? It is! (but will change at 0.13)
145         ParagraphList::iterator it = bv->buffer()->paragraphs.begin();
146         ParagraphList::iterator end = bv->buffer()->paragraphs.end();
147         for (; it != end; ++it) {
148                 if (it->bibitem()) {
149                         int const wx = it->bibitem()->width(bv, font);
150                         if (wx > w)
151                                 w = wx;
152                 }
153         }
154         return w;
155 }
156
157
158 // ale070405
159 string const bibitemWidest(Buffer const * buffer)
160 {
161         int w = 0;
162         // Does look like a hack? It is! (but will change at 0.13)
163
164         InsetBibitem * bitem = 0;
165         LyXFont font;
166
167         ParagraphList::iterator it = buffer->paragraphs.begin();
168         ParagraphList::iterator end = buffer->paragraphs.end();
169         for (; it != end; ++it) {
170                 if (it->bibitem()) {
171                         int const wx =
172                                 font_metrics::width(it->bibitem()->getBibLabel(),
173                                                     font);
174                         if (wx > w) {
175                                 w = wx;
176                                 bitem = it->bibitem();
177                         }
178                 }
179         }
180
181         if (bitem && !bitem->getBibLabel().empty())
182                 return bitem->getBibLabel();
183
184         return "99";
185 }