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