]> git.lyx.org Git - lyx.git/blob - src/insets/InsetERT.cpp
Change the "empty layout" to the "plain layout", to try to avoid confusion.
[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 "Lexer.h"
26 #include "LyXAction.h"
27 #include "MetricsInfo.h"
28 #include "ParagraphParameters.h"
29 #include "Paragraph.h"
30 #include "TextClass.h"
31
32 #include "frontends/alert.h"
33 #include "frontends/Application.h"
34
35 #include "support/debug.h"
36 #include "support/gettext.h"
37 #include "support/lstrings.h"
38
39 #include <sstream>
40
41 using namespace std;
42 using namespace lyx::support;
43
44 namespace lyx {
45
46 InsetERT::InsetERT(Buffer const & buf, CollapseStatus status)
47         : InsetCollapsable(buf, status)
48 {}
49
50
51 InsetERT::~InsetERT()
52 {
53         hideDialogs("ert", this);
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().plainLayout();
108         //lyxerr << "\nInsetERT::doDispatch (begin): cmd: " << cmd << endl;
109         switch (cmd.action) {
110         case LFUN_QUOTE_INSERT: {
111                 // We need to bypass the fancy quotes in Text
112                 FuncRequest f(LFUN_SELF_INSERT, "\"");
113                 dispatch(cur, f);
114                 break;
115         }
116         case LFUN_INSET_MODIFY: {
117                 setStatus(cur, string2params(to_utf8(cmd.argument())));
118                 break;
119         }
120         default:
121                 // Force any new text to latex_language
122                 // FIXME: This should not be necessary but
123                 // new paragraphs that are created by pressing enter at the
124                 // start of an existing paragraph get the buffer language
125                 // and not latex_language, so we take this brute force
126                 // approach.
127                 cur.current_font.fontInfo() = layout.font;
128                 cur.real_current_font.fontInfo() = layout.font;
129                 InsetCollapsable::doDispatch(cur, cmd);
130                 break;
131         }
132 }
133
134
135 bool InsetERT::getStatus(Cursor & cur, FuncRequest const & cmd,
136         FuncStatus & status) const
137 {
138         switch (cmd.action) {
139                 case LFUN_CLIPBOARD_PASTE:
140                 case LFUN_INSET_MODIFY:
141                 case LFUN_PASTE:
142                 case LFUN_PRIMARY_SELECTION_PASTE:
143                 case LFUN_QUOTE_INSERT:
144                         status.setEnabled(true);
145                         return true;
146
147                 // this one is difficult to get right. As a half-baked
148                 // solution, we consider only the first action of the sequence
149                 case LFUN_COMMAND_SEQUENCE: {
150                         // argument contains ';'-terminated commands
151                         string const firstcmd = token(to_utf8(cmd.argument()), ';', 0);
152                         FuncRequest func(lyxaction.lookupFunc(firstcmd));
153                         func.origin = cmd.origin;
154                         return getStatus(cur, func, status);
155                 }
156
157                 default:
158                         return InsetCollapsable::getStatus(cur, cmd, status);
159         }
160 }
161
162
163 void InsetERT::setButtonLabel()
164 {
165         if (decoration() == InsetLayout::Classic)
166                 setLabel(isOpen() ? _("ERT") : getNewLabel(_("ERT")));
167         else
168                 setLabel(getNewLabel(_("ERT")));
169 }
170
171
172 bool InsetERT::insetAllowed(InsetCode /* code */) const
173 {
174         return false;
175 }
176
177
178 void InsetERT::draw(PainterInfo & pi, int x, int y) const
179 {
180         const_cast<InsetERT &>(*this).setButtonLabel();
181         InsetCollapsable::draw(pi, x, y);
182 }
183
184
185 bool InsetERT::showInsetDialog(BufferView * bv) const
186 {
187         bv->showDialog("ert", params2string(status()), 
188                 const_cast<InsetERT *>(this));
189         return true;
190 }
191
192
193 InsetCollapsable::CollapseStatus InsetERT::string2params(string const & in)
194 {
195         if (in.empty())
196                 return Collapsed;
197         istringstream data(in);
198         Lexer lex;
199         lex.setStream(data);
200         lex.setContext("InsetERT::string2params");
201         lex >> "ert";
202         int s;
203         lex >> s;
204         return static_cast<CollapseStatus>(s);
205 }
206
207
208 string InsetERT::params2string(CollapseStatus status)
209 {
210         ostringstream data;
211         data << "ert" << ' ' << status;
212         return data.str();
213 }
214
215
216 } // namespace lyx