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