]> git.lyx.org Git - features.git/blob - src/insets/InsetERT.cpp
this feels good...
[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 "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 #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         if (theApp())
54                 theApp()->hideDialogs("ert", this);
55 }
56
57
58 void InsetERT::write(ostream & os) const
59 {
60         os << "ERT" << "\n";
61         InsetCollapsable::write(os);
62 }
63
64
65 docstring InsetERT::editMessage() const
66 {
67         return _("Opened ERT Inset");
68 }
69
70
71 int InsetERT::latex(odocstream & os, OutputParams const & op) const
72 {
73         return InsetCollapsable::latex(os, op);
74 }
75
76
77 int InsetERT::plaintext(odocstream &, OutputParams const &) const
78 {
79         return 0; // do not output TeX code
80 }
81
82
83 int InsetERT::docbook(odocstream & os, OutputParams const &) const
84 {
85         // FIXME can we do the same thing here as for LaTeX?
86         ParagraphList::const_iterator par = paragraphs().begin();
87         ParagraphList::const_iterator end = paragraphs().end();
88
89         int lines = 0;
90         while (par != end) {
91                 pos_type siz = par->size();
92                 for (pos_type i = 0; i < siz; ++i)
93                         os.put(par->getChar(i));
94                 ++par;
95                 if (par != end) {
96                         os << "\n";
97                         ++lines;
98                 }
99         }
100
101         return lines;
102 }
103
104
105 void InsetERT::doDispatch(Cursor & cur, FuncRequest & cmd)
106 {
107         BufferParams const & bp = cur.buffer().params();
108         Layout const layout = bp.documentClass().emptyLayout();
109         //lyxerr << "\nInsetERT::doDispatch (begin): cmd: " << cmd << endl;
110         switch (cmd.action) {
111         case LFUN_QUOTE_INSERT: {
112                 // We need to bypass the fancy quotes in Text
113                 FuncRequest f(LFUN_SELF_INSERT, "\"");
114                 dispatch(cur, f);
115                 break;
116         }
117         case LFUN_INSET_MODIFY: {
118                 InsetCollapsable::CollapseStatus st;
119                 InsetERT::string2params(to_utf8(cmd.argument()), st);
120                 setStatus(cur, st);
121                 break;
122         }
123         default:
124                 // Force any new text to latex_language
125                 // FIXME: This should not be necessary but
126                 // new paragraphs that are created by pressing enter at the
127                 // start of an existing paragraph get the buffer language
128                 // and not latex_language, so we take this brute force
129                 // approach.
130                 cur.current_font.fontInfo() = layout.font;
131                 cur.real_current_font.fontInfo() = layout.font;
132                 InsetCollapsable::doDispatch(cur, cmd);
133                 break;
134         }
135 }
136
137
138 bool InsetERT::getStatus(Cursor & cur, FuncRequest const & cmd,
139         FuncStatus & status) const
140 {
141         switch (cmd.action) {
142                 case LFUN_CLIPBOARD_PASTE:
143                 case LFUN_INSET_MODIFY:
144                 case LFUN_PASTE:
145                 case LFUN_PRIMARY_SELECTION_PASTE:
146                 case LFUN_QUOTE_INSERT:
147                         status.enabled(true);
148                         return true;
149
150                 // this one is difficult to get right. As a half-baked
151                 // solution, we consider only the first action of the sequence
152                 case LFUN_COMMAND_SEQUENCE: {
153                         // argument contains ';'-terminated commands
154                         string const firstcmd = token(to_utf8(cmd.argument()), ';', 0);
155                         FuncRequest func(lyxaction.lookupFunc(firstcmd));
156                         func.origin = cmd.origin;
157                         return getStatus(cur, func, status);
158                 }
159
160                 default:
161                         return InsetCollapsable::getStatus(cur, cmd, status);
162         }
163 }
164
165
166 void InsetERT::setButtonLabel()
167 {
168         if (decoration() == InsetLayout::Classic)
169                 setLabel(isOpen() ? _("ERT") : getNewLabel(_("ERT")));
170         else
171                 setLabel(getNewLabel(_("ERT")));
172 }
173
174
175 bool InsetERT::insetAllowed(InsetCode /* code */) const
176 {
177         return false;
178 }
179
180
181 void InsetERT::draw(PainterInfo & pi, int x, int y) const
182 {
183         const_cast<InsetERT &>(*this).setButtonLabel();
184         InsetCollapsable::draw(pi, x, y);
185 }
186
187
188 bool InsetERT::showInsetDialog(BufferView * bv) const
189 {
190         bv->showDialog("ert", params2string(status()), 
191                 const_cast<InsetERT *>(this));
192         return true;
193 }
194
195
196 void InsetERT::string2params(string const & in, CollapseStatus & status)
197 {
198         status = InsetCollapsable::Collapsed;
199         if (in.empty())
200                 return;
201
202         istringstream data(in);
203         Lexer lex(0,0);
204         lex.setStream(data);
205
206         string name;
207         lex >> name;
208         if (name != "ert") {
209                 LYXERR0("InsetERT::string2params(" << in << ")\n"
210                                           "Expected arg 1 to be \"ert\"\n");
211                 return;
212         }
213
214         int s;
215         lex >> s;
216         if (lex)
217                 status = static_cast<InsetCollapsable::CollapseStatus>(s);
218 }
219
220
221 string InsetERT::params2string(CollapseStatus status)
222 {
223         ostringstream data;
224         data << "ert" << ' ' << status;
225         return data.str();
226 }
227
228
229 } // namespace lyx