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