]> git.lyx.org Git - lyx.git/blob - src/insets/insetbibitem.C
prevent crash when inserting minipage in table cell,
[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 using namespace lyx::support;
24
25 using std::max;
26 using std::auto_ptr;
27
28
29 int InsetBibitem::key_counter = 0;
30 string const key_prefix = "key-";
31
32
33 InsetBibitem::InsetBibitem(InsetCommandParams const & p)
34         : InsetCommand(p), counter(1)
35 {
36         if (getContents().empty())
37                 setContents(key_prefix + tostr(++key_counter));
38 }
39
40
41 InsetBibitem::~InsetBibitem()
42 {
43         InsetCommandMailer mailer("bibitem", *this);
44         mailer.hideDialog();
45 }
46
47
48 auto_ptr<InsetBase> InsetBibitem::clone() const
49 {
50         InsetBibitem * b = new InsetBibitem(params());
51         b->setCounter(counter);
52         return auto_ptr<InsetBase>(b);
53 }
54
55
56 dispatch_result InsetBibitem::localDispatch(FuncRequest const & cmd)
57 {
58         switch (cmd.action) {
59
60         case LFUN_INSET_EDIT:
61                 InsetCommandMailer("bibitem", *this).showDialog(cmd.view());
62                 return DISPATCHED;
63
64         case LFUN_INSET_MODIFY: {
65                 InsetCommandParams p;
66                 InsetCommandMailer::string2params(cmd.argument, p);
67                 if (p.getCmdName().empty())
68                         return DISPATCHED;
69                 setParams(p);
70                 cmd.view()->updateInset(this);
71                 cmd.view()->fitCursor();
72                 return DISPATCHED;
73         }
74
75         default:
76                 return InsetCommand::localDispatch(cmd);
77         }
78 }
79
80
81 void InsetBibitem::setCounter(int c)
82 {
83         counter = c;
84 }
85
86
87 // I'm sorry but this is still necessary because \bibitem is used also
88 // as a LyX 2.x command, and lyxlex is not enough smart to understand
89 // real LaTeX commands. Yes, that could be fixed, but would be a waste
90 // of time cause LyX3 won't use lyxlex anyway.  (ale)
91 void InsetBibitem::write(Buffer const *, std::ostream & os) const
92 {
93         os << "\n\\bibitem ";
94         if (!getOptions().empty())
95                 os << '[' << getOptions() << ']';
96         os << '{' << getContents() << "}\n";
97 }
98
99
100 // This is necessary here because this is written without begin_inset
101 // This should be changed!!! (Jug)
102 void InsetBibitem::read(Buffer const *, LyXLex & lex)
103 {
104         if (lex.eatLine()) {
105                 scanCommand(lex.getString());
106         } else {
107                 lex.printError("InsetCommand: Parse error: `$$Token'");
108         }
109
110         if (prefixIs(getContents(), key_prefix)) {
111                 int key = strToInt(getContents().substr(key_prefix.length()));
112                 key_counter = max(key_counter, key);
113         }
114 }
115
116
117 string const InsetBibitem::getBibLabel() const
118 {
119         return getOptions().empty() ? tostr(counter) : getOptions();
120 }
121
122
123 string const InsetBibitem::getScreenLabel(Buffer const *) const
124 {
125         return getContents() + " [" + getBibLabel() + ']';
126 }
127
128
129 // ale070405 This function maybe shouldn't be here. We'll fix this at 0.13.
130 int bibitemMaxWidth(BufferView * bv, LyXFont const &)
131 {
132         int w = 0;
133         // Ha, now we are mainly at 1.2.0 and it is still here (Jug)
134         // Does look like a hack? It is! (but will change at 0.13)
135         ParagraphList::iterator it = bv->buffer()->paragraphs.begin();
136         ParagraphList::iterator end = bv->buffer()->paragraphs.end();
137         for (; it != end; ++it) {
138                 if (it->bibitem()) {
139 #warning metrics broken!
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 }