]> git.lyx.org Git - features.git/blob - src/insets/InsetERT.cpp
Remove redundant code and introduce InsetCollapsable::setLabelColor().
[features.git] / src / insets / InsetERT.cpp
1 /**
2  * \file InsetERT.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Jürgen Vigna
7  * \author Lars Gullik Bjønnes
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "InsetERT.h"
15
16 #include "Buffer.h"
17 #include "BufferParams.h"
18 #include "BufferView.h"
19 #include "Cursor.h"
20 #include "debug.h"
21 #include "DispatchResult.h"
22 #include "FuncRequest.h"
23 #include "FuncStatus.h"
24 #include "gettext.h"
25 #include "Language.h"
26 #include "Layout.h"
27 #include "LyXAction.h"
28 #include "Lexer.h"
29 #include "TextClass.h"
30 #include "MetricsInfo.h"
31 #include "ParagraphParameters.h"
32 #include "Paragraph.h"
33
34 #include "frontends/alert.h"
35
36 #include <sstream>
37
38
39 namespace lyx {
40
41 using support::token;
42
43 using std::endl;
44 using std::min;
45
46 using std::istringstream;
47 using std::ostream;
48 using std::ostringstream;
49 using std::string;
50
51
52 void InsetERT::init()
53 {
54         setButtonLabel();
55         // FIXME: what to do with those?
56         //text_.current_font.setLanguage(latex_language);
57         //text_.real_current_font.setLanguage(latex_language);
58 }
59
60
61 InsetERT::InsetERT(BufferParams const & bp, CollapseStatus status)
62         : InsetCollapsable(bp, status)
63 {
64         setLayout(bp);
65         init();
66 }
67
68
69 InsetERT::InsetERT(InsetERT const & in)
70         : InsetCollapsable(in)
71 {
72         init();
73 }
74
75
76 Inset * InsetERT::clone() const
77 {
78         return new InsetERT(*this);
79 }
80
81
82 #if 0
83 InsetERT::InsetERT(BufferParams const & bp,
84                    Language const *, string const & contents, CollapseStatus status)
85         : InsetCollapsable(bp, status)
86 {
87         Font font(FONT_INHERIT, latex_language);
88         paragraphs().begin()->insert(0, contents, font);
89
90         // the init has to be after the initialization of the paragraph
91         // because of the label settings (draw_label for ert insets).
92         init();
93 }
94 #endif
95
96
97 InsetERT::~InsetERT()
98 {
99         InsetERTMailer(*this).hideDialog();
100 }
101
102
103 void InsetERT::write(Buffer const & buf, ostream & os) const
104 {
105         os << "ERT" << "\n";
106         InsetCollapsable::write(buf, os);
107 }
108
109
110 docstring const InsetERT::editMessage() const
111 {
112         return _("Opened ERT Inset");
113 }
114
115
116 int InsetERT::latex(Buffer const & buf, odocstream & os,
117                     OutputParams const & op) const
118 {
119         return InsetCollapsable::latex(buf, os, op);
120 }
121
122
123 int InsetERT::plaintext(Buffer const &, odocstream &,
124                         OutputParams const &) const
125 {
126         return 0; // do not output TeX code
127 }
128
129
130 int InsetERT::docbook(Buffer const &, odocstream & os,
131                       OutputParams const &) const
132 {
133         // FIXME can we do the same thing here as for LaTeX?
134         ParagraphList::const_iterator par = paragraphs().begin();
135         ParagraphList::const_iterator end = paragraphs().end();
136
137         int lines = 0;
138         while (par != end) {
139                 pos_type siz = par->size();
140                 for (pos_type i = 0; i < siz; ++i)
141                         os.put(par->getChar(i));
142                 ++par;
143                 if (par != end) {
144                         os << "\n";
145                         ++lines;
146                 }
147         }
148
149         return lines;
150 }
151
152
153 void InsetERT::doDispatch(Cursor & cur, FuncRequest & cmd)
154 {
155         BufferParams const & bp = cur.buffer().params();
156         LayoutPtr const layout =
157                         bp.getTextClass().defaultLayout();
158         //lyxerr << "\nInsetERT::doDispatch (begin): cmd: " << cmd << endl;
159         switch (cmd.action) {
160
161         case LFUN_MOUSE_PRESS:
162                 if (cmd.button() != mouse_button::button3)
163                         InsetCollapsable::doDispatch(cur, cmd);
164                 else
165                         // This makes the cursor leave the
166                         // inset when it collapses on mouse-3
167                         cur.undispatched();
168                 break;
169
170         case LFUN_QUOTE_INSERT: {
171                 // We need to bypass the fancy quotes in Text
172                 FuncRequest f(LFUN_SELF_INSERT, "\"");
173                 dispatch(cur, f);
174                 break;
175         }
176         case LFUN_INSET_MODIFY: {
177                 InsetCollapsable::CollapseStatus st;
178                 InsetERTMailer::string2params(to_utf8(cmd.argument()), st);
179                 setStatus(cur, st);
180                 break;
181         }
182         default:
183                 // Force any new text to latex_language
184                 // FIXME: This should only be necessary in init(), but
185                 // new paragraphs that are created by pressing enter at the
186                 // start of an existing paragraph get the buffer language
187                 // and not latex_language, so we take this brute force
188                 // approach.
189                 cur.current_font.fontInfo() = layout->font;
190                 cur.real_current_font.fontInfo() = layout->font;
191                 InsetCollapsable::doDispatch(cur, cmd);
192                 break;
193         }
194 }
195
196
197 bool InsetERT::getStatus(Cursor & cur, FuncRequest const & cmd,
198         FuncStatus & status) const
199 {
200         switch (cmd.action) {
201                 case LFUN_CLIPBOARD_PASTE:
202                 case LFUN_INSET_MODIFY:
203                 case LFUN_PASTE:
204                 case LFUN_PRIMARY_SELECTION_PASTE:
205                 case LFUN_QUOTE_INSERT:
206                         status.enabled(true);
207                         return true;
208
209                 // this one is difficult to get right. As a half-baked
210                 // solution, we consider only the first action of the sequence
211                 case LFUN_COMMAND_SEQUENCE: {
212                         // argument contains ';'-terminated commands
213                         string const firstcmd = token(to_utf8(cmd.argument()), ';', 0);
214                         FuncRequest func(lyxaction.lookupFunc(firstcmd));
215                         func.origin = cmd.origin;
216                         return getStatus(cur, func, status);
217                 }
218
219                 default:
220                         return InsetCollapsable::getStatus(cur, cmd, status);
221         }
222 }
223
224
225 void InsetERT::setButtonLabel()
226 {
227         // FIXME UNICODE
228         if (decoration() == Classic)
229                 setLabel(isOpen() ? _("ERT") : getNewLabel(_("ERT")));
230         else
231                 setLabel(getNewLabel(_("ERT")));
232 }
233
234
235 bool InsetERT::insetAllowed(InsetCode /* code */) const
236 {
237         return false;
238 }
239
240
241 void InsetERT::draw(PainterInfo & pi, int x, int y) const
242 {
243         const_cast<InsetERT &>(*this).setButtonLabel();
244         InsetCollapsable::draw(pi, x, y);
245 }
246
247
248 bool InsetERT::showInsetDialog(BufferView * bv) const
249 {
250         InsetERTMailer(const_cast<InsetERT &>(*this)).showDialog(bv);
251         return true;
252 }
253
254
255 string const InsetERTMailer::name_("ert");
256
257 InsetERTMailer::InsetERTMailer(InsetERT & inset)
258         : inset_(inset)
259 {}
260
261
262 string const InsetERTMailer::inset2string(Buffer const &) const
263 {
264         return params2string(inset_.status());
265 }
266
267
268 void InsetERTMailer::string2params(string const & in,
269                                    InsetCollapsable::CollapseStatus & status)
270 {
271         status = InsetCollapsable::Collapsed;
272         if (in.empty())
273                 return;
274
275         istringstream data(in);
276         Lexer lex(0,0);
277         lex.setStream(data);
278
279         string name;
280         lex >> name;
281         if (name != name_)
282                 return print_mailer_error("InsetERTMailer", in, 1, name_);
283
284         int s;
285         lex >> s;
286         if (lex)
287                 status = static_cast<InsetCollapsable::CollapseStatus>(s);
288 }
289
290
291 string const
292 InsetERTMailer::params2string(InsetCollapsable::CollapseStatus status)
293 {
294         ostringstream data;
295         data << name_ << ' ' << status;
296         return data.str();
297 }
298
299
300 } // namespace lyx