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