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