]> git.lyx.org Git - lyx.git/blob - src/insets/InsetERT.cpp
Fix GRAPHICS_EDIT of InsetGraphics
[lyx.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 "DispatchResult.h"
21 #include "FuncRequest.h"
22 #include "FuncStatus.h"
23 #include "Language.h"
24 #include "Layout.h"
25 #include "LyXAction.h"
26 #include "Lexer.h"
27 #include "TextClass.h"
28 #include "MetricsInfo.h"
29 #include "ParagraphParameters.h"
30 #include "Paragraph.h"
31
32 #include "frontends/alert.h"
33
34 #include "support/debug.h"
35 #include "support/gettext.h"
36 #include "support/lstrings.h"
37
38 #include <sstream>
39
40 using namespace std;
41 using namespace lyx::support;
42
43 namespace lyx {
44
45
46 InsetERT::InsetERT(Buffer const & buf, CollapseStatus status)
47         : InsetCollapsable(buf, status)
48 {}
49
50
51 InsetERT::~InsetERT()
52 {
53         InsetERTMailer(*this).hideDialog();
54 }
55
56
57 void InsetERT::write(ostream & os) const
58 {
59         os << "ERT" << "\n";
60         InsetCollapsable::write(os);
61 }
62
63
64 docstring InsetERT::editMessage() const
65 {
66         return _("Opened ERT Inset");
67 }
68
69
70 int InsetERT::latex(odocstream & os, OutputParams const & op) const
71 {
72         return InsetCollapsable::latex(os, op);
73 }
74
75
76 int InsetERT::plaintext(odocstream &, OutputParams const &) const
77 {
78         return 0; // do not output TeX code
79 }
80
81
82 int InsetERT::docbook(odocstream & os, OutputParams const &) const
83 {
84         // FIXME can we do the same thing here as for LaTeX?
85         ParagraphList::const_iterator par = paragraphs().begin();
86         ParagraphList::const_iterator end = paragraphs().end();
87
88         int lines = 0;
89         while (par != end) {
90                 pos_type siz = par->size();
91                 for (pos_type i = 0; i < siz; ++i)
92                         os.put(par->getChar(i));
93                 ++par;
94                 if (par != end) {
95                         os << "\n";
96                         ++lines;
97                 }
98         }
99
100         return lines;
101 }
102
103
104 void InsetERT::doDispatch(Cursor & cur, FuncRequest & cmd)
105 {
106         BufferParams const & bp = cur.buffer().params();
107         Layout const layout = bp.documentClass().emptyLayout();
108         //lyxerr << "\nInsetERT::doDispatch (begin): cmd: " << cmd << endl;
109         switch (cmd.action) {
110
111         case LFUN_MOUSE_PRESS:
112                 if (cmd.button() != mouse_button::button3)
113                         InsetCollapsable::doDispatch(cur, cmd);
114                 else
115                         // This makes the cursor leave the
116                         // inset when it collapses on mouse-3
117                         cur.undispatched();
118                 break;
119
120         case LFUN_QUOTE_INSERT: {
121                 // We need to bypass the fancy quotes in Text
122                 FuncRequest f(LFUN_SELF_INSERT, "\"");
123                 dispatch(cur, f);
124                 break;
125         }
126         case LFUN_INSET_MODIFY: {
127                 InsetCollapsable::CollapseStatus st;
128                 InsetERTMailer::string2params(to_utf8(cmd.argument()), st);
129                 setStatus(cur, st);
130                 break;
131         }
132         default:
133                 // Force any new text to latex_language
134                 // FIXME: This should not be necessary but
135                 // new paragraphs that are created by pressing enter at the
136                 // start of an existing paragraph get the buffer language
137                 // and not latex_language, so we take this brute force
138                 // approach.
139                 cur.current_font.fontInfo() = layout.font;
140                 cur.real_current_font.fontInfo() = layout.font;
141                 InsetCollapsable::doDispatch(cur, cmd);
142                 break;
143         }
144 }
145
146
147 bool InsetERT::getStatus(Cursor & cur, FuncRequest const & cmd,
148         FuncStatus & status) const
149 {
150         switch (cmd.action) {
151                 case LFUN_CLIPBOARD_PASTE:
152                 case LFUN_INSET_MODIFY:
153                 case LFUN_PASTE:
154                 case LFUN_PRIMARY_SELECTION_PASTE:
155                 case LFUN_QUOTE_INSERT:
156                         status.enabled(true);
157                         return true;
158
159                 // this one is difficult to get right. As a half-baked
160                 // solution, we consider only the first action of the sequence
161                 case LFUN_COMMAND_SEQUENCE: {
162                         // argument contains ';'-terminated commands
163                         string const firstcmd = token(to_utf8(cmd.argument()), ';', 0);
164                         FuncRequest func(lyxaction.lookupFunc(firstcmd));
165                         func.origin = cmd.origin;
166                         return getStatus(cur, func, status);
167                 }
168
169                 default:
170                         return InsetCollapsable::getStatus(cur, cmd, status);
171         }
172 }
173
174
175 void InsetERT::setButtonLabel()
176 {
177         if (decoration() == InsetLayout::Classic)
178                 setLabel(isOpen() ? _("ERT") : getNewLabel(_("ERT")));
179         else
180                 setLabel(getNewLabel(_("ERT")));
181 }
182
183
184 bool InsetERT::insetAllowed(InsetCode /* code */) const
185 {
186         return false;
187 }
188
189
190 void InsetERT::draw(PainterInfo & pi, int x, int y) const
191 {
192         const_cast<InsetERT &>(*this).setButtonLabel();
193         InsetCollapsable::draw(pi, x, y);
194 }
195
196
197 bool InsetERT::showInsetDialog(BufferView * bv) const
198 {
199         InsetERTMailer(const_cast<InsetERT &>(*this)).showDialog(bv);
200         return true;
201 }
202
203
204 string const InsetERTMailer::name_("ert");
205
206 InsetERTMailer::InsetERTMailer(InsetERT & inset)
207         : inset_(inset)
208 {}
209
210
211 string const InsetERTMailer::inset2string(Buffer const &) const
212 {
213         return params2string(inset_.status());
214 }
215
216
217 void InsetERTMailer::string2params(string const & in,
218                                    InsetCollapsable::CollapseStatus & status)
219 {
220         status = InsetCollapsable::Collapsed;
221         if (in.empty())
222                 return;
223
224         istringstream data(in);
225         Lexer lex(0,0);
226         lex.setStream(data);
227
228         string name;
229         lex >> name;
230         if (name != name_)
231                 return print_mailer_error("InsetERTMailer", in, 1, name_);
232
233         int s;
234         lex >> s;
235         if (lex)
236                 status = static_cast<InsetCollapsable::CollapseStatus>(s);
237 }
238
239
240 string const
241 InsetERTMailer::params2string(InsetCollapsable::CollapseStatus status)
242 {
243         ostringstream data;
244         data << name_ << ' ' << status;
245         return data.str();
246 }
247
248
249 } // namespace lyx