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