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