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