]> git.lyx.org Git - lyx.git/blob - src/insets/insetbibitem.C
Introduce wide streams. This fixes the remaining problems of plain text
[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.h"
23
24 #include "frontends/FontMetrics.h"
25
26 #include "support/lstrings.h"
27 #include "support/std_ostream.h"
28 #include "support/convert.h"
29
30 using lyx::docstring;
31 using lyx::support::prefixIs;
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", lyx::to_utf8(cmd.argument()), p);
65                 if (p.getCmdName().empty()) {
66                         cur.noUpdate();
67                         break;
68                 }
69                 if (p.getContents() != params().getContents()) 
70                         cur.bv().buffer()->changeRefsIfUnique(params().getContents(),
71                                                        p.getContents(), InsetBase::CITE_CODE);
72                 setParams(p);
73         }
74
75         default:
76                 InsetCommand::doDispatch(cur, cmd);
77                 break;
78         }
79 }
80
81
82 void InsetBibitem::setCounter(int c)
83 {
84         counter = c;
85 }
86
87
88 // I'm sorry but this is still necessary because \bibitem is used also
89 // as a LyX 2.x command, and lyxlex is not enough smart to understand
90 // real LaTeX commands. Yes, that could be fixed, but would be a waste
91 // of time cause LyX3 won't use lyxlex anyway.  (ale)
92 void InsetBibitem::write(Buffer const &, std::ostream & os) const
93 {
94         os << "\n\\bibitem ";
95         if (!getOptions().empty())
96                 os << '[' << getOptions() << ']';
97         os << '{' << getContents() << "}\n";
98 }
99
100
101 // This is necessary here because this is written without begin_inset
102 // This should be changed!!! (Jug)
103 void InsetBibitem::read(Buffer const &, LyXLex & lex)
104 {
105         if (lex.eatLine())
106                 scanCommand(lex.getString());
107         else
108                 lex.printError("InsetCommand: Parse error: `$$Token'");
109
110         if (prefixIs(getContents(), key_prefix)) {
111                 int const key = convert<int>(getContents().substr(key_prefix.length()));
112                 key_counter = max(key_counter, key);
113         }
114 }
115
116
117 docstring const InsetBibitem::getBibLabel() const
118 {
119         // FIXME UNICODE
120         return getOptions().empty() ?
121                 convert<docstring>(counter) :
122                 lyx::from_utf8(getOptions());
123 }
124
125
126 docstring const InsetBibitem::getScreenLabel(Buffer const &) const
127 {
128         // FIXME UNICODE
129         return lyx::from_utf8(getContents()) + " [" + getBibLabel() + ']';
130 }
131
132
133 int InsetBibitem::plaintext(Buffer const &, lyx::odocstream & os,
134                             OutputParams const &) const
135 {
136         os << '[' << getCounter() << "] ";
137         return 0;
138 }
139
140
141 // ale070405
142 docstring const bibitemWidest(Buffer const & buffer)
143 {
144         int w = 0;
145         // Does look like a hack? It is! (but will change at 0.13)
146
147         InsetBibitem const * bitem = 0;
148         // FIXME font is used unitialized, is that correct?
149         LyXFont font;
150         lyx::frontend::FontMetrics const & fm = theFontMetrics(font);
151
152         ParagraphList::const_iterator it = buffer.paragraphs().begin();
153         ParagraphList::const_iterator end = buffer.paragraphs().end();
154
155         for (; it != end; ++it) {
156                 if (it->bibitem()) {
157                         docstring const label = it->bibitem()->getBibLabel();
158                     
159                         int const wx =
160                                 fm.width(label);
161                         if (wx > w) {
162                                 w = wx;
163                                 bitem = it->bibitem();
164                         }
165                 }
166         }
167
168         if (bitem && !bitem->getBibLabel().empty())
169                 return bitem->getBibLabel();
170
171         return lyx::from_ascii("99");
172 }