]> git.lyx.org Git - lyx.git/blob - src/insets/insetert.C
9ecca22ac56abda724c807d1fcf7d5fdf72b3801
[lyx.git] / src / insets / insetert.C
1 /* This file is part of
2  * ======================================================
3  * 
4  *           LyX, The Document Processor
5  *       
6  *          Copyright 1998 The LyX Team.
7  *
8  *======================================================*/
9
10 #include <config.h>
11
12 #ifdef __GNUG__
13 #pragma implementation
14 #endif
15
16 #include "insetert.h"
17 #include "gettext.h"
18 #include "lyxfont.h"
19 #include "buffer.h"
20 #include "insets/insettext.h"
21 #include "support/LOstream.h"
22 #include "lyx_gui_misc.h"
23 #include "BufferView.h"
24 #include "LyXView.h"
25
26 using std::ostream;
27
28 void InsetERT::init()
29 {
30 #if 0
31         setLabel(_("666"), true);
32 #else
33         setLabel(_("666"));
34 #endif
35         labelfont = LyXFont(LyXFont::ALL_SANE);
36         labelfont.decSize();
37         labelfont.decSize();
38 #ifndef NO_LATEX
39         labelfont.setLatex(LyXFont::ON);
40 #else
41         labelfont.setColor(LColor::latex);
42 #endif
43         setInsetName("ERT");
44 }
45
46
47 InsetERT::InsetERT() : InsetCollapsable()
48 {
49         init();
50 }
51
52
53 #if 0
54 InsetERT::InsetERT(InsetERT const & in, bool same_id)
55         : InsetCollapsable(in, same_id)
56 {
57 }
58 #endif
59
60
61 InsetERT::InsetERT(string const & contents, bool collapsed)
62         : InsetCollapsable(collapsed)
63 {
64         LyXFont font(LyXFont::ALL_INHERIT);
65         font.setFamily(LyXFont::TYPEWRITER_FAMILY);
66         font.setColor(LColor::latex);
67         string::const_iterator cit = contents.begin();
68         string::const_iterator end = contents.end();
69         Paragraph::size_type pos = 0;
70         for (; cit != end; ++cit) {
71                 inset.paragraph()->insertChar(pos++, *cit, font);
72         }
73         // the init has to be after the initialization of the paragraph
74         // because of the label settings (draw_label for ert insets).
75         init();
76 }
77
78
79 void InsetERT::read(Buffer const * buf, LyXLex & lex)
80 {
81         InsetCollapsable::read(buf, lex);
82         if (collapsed_) {
83                 setLabel(get_new_label());
84         } else {
85                 setLabel(_("666"));
86         }
87 }
88
89
90 void InsetERT::write(Buffer const * buf, ostream & os) const 
91 {
92         os << getInsetName() << "\n";
93         InsetCollapsable::write(buf, os);
94 }
95
96
97 string const InsetERT::editMessage() const 
98 {
99         return _("Opened ERT Inset");
100 }
101
102
103 bool InsetERT::insertInset(BufferView *, Inset *)
104 {
105         return false;
106 }
107
108
109 void InsetERT::setFont(BufferView *, LyXFont const &, bool, bool selectall)
110 {
111         // if selectall is activated then the fontchange was an outside general
112         // fontchange and this messages is not needed
113         if (!selectall)
114                 WriteAlert(_("Impossible Operation!"),
115                            _("Not permitted to change font-types inside ERT-insets!"),
116                            _("Sorry."));
117 }
118
119
120 void InsetERT::edit(BufferView * bv, int x, int y, unsigned int button)
121 {
122         InsetCollapsable::edit(bv, x, y, button);
123 #ifndef NO_LATEX
124         LyXFont font(LyXFont::ALL_SANE);
125         font.setLatex (LyXFont::ON);
126 #else
127         LyXFont font(LyXFont::ALL_INHERIT);
128         font.setFamily(LyXFont::TYPEWRITER_FAMILY);
129         font.setColor(LColor::latex);
130 #endif
131         inset.setFont(bv, font);
132 }
133
134
135 void InsetERT::edit(BufferView * bv, bool)
136 {
137         edit(bv, 0, 0, 0);
138 }
139
140
141 void InsetERT::insetButtonRelease(BufferView * bv,
142                                   int x, int y, int button)
143 {
144         if ((x >= 0)  && (x < button_length) &&
145             (y >= button_top_y) &&  (y <= button_bottom_y)) {
146                 if (collapsed_) {
147                         setLabel(_("666"));
148                 } else {
149                         setLabel(get_new_label());
150                 }
151         }
152         InsetCollapsable::insetButtonRelease(bv, x, y, button);
153 }
154
155
156 int InsetERT::latex(Buffer const *, std::ostream & os, bool /*fragile*/,
157                     bool /*free_spc*/) const
158 {
159         Paragraph * par = inset.paragraph();
160         while (par) {
161                 Paragraph::size_type siz = inset.paragraph()->size();
162                 for (Paragraph::size_type i = 0; i != siz; ++i) {
163                         char c = inset.paragraph()->getChar(i);
164                         switch (c) {
165                         case Paragraph::META_NEWLINE:
166                                 os << '\n';
167                                 break;
168                         default:
169                                 os << c;
170                                 break;
171                         }
172                 }
173                 par = par->next();
174         }
175         
176         return 1;
177 }
178
179
180 int InsetERT::ascii(Buffer const *,
181                     std::ostream &, int /*linelen*/) const 
182 {
183         return 0;
184 }
185
186
187 int InsetERT::linuxdoc(Buffer const *, std::ostream &) const
188 {
189         return 0;
190 }
191
192
193 int InsetERT::docBook(Buffer const *, std::ostream &) const
194 {
195         return 0;
196 }
197
198
199 UpdatableInset::RESULT
200 InsetERT::localDispatch(BufferView * bv, kb_action action, string const & arg)
201 {
202         UpdatableInset::RESULT result = DISPATCHED_NOUPDATE;
203         
204         switch(action) {
205         case LFUN_LAYOUT:
206                 bv->owner()->setLayout(inset.paragraph()->getLayout());
207                 break;
208         default:
209                 result = InsetCollapsable::localDispatch(bv, action, arg);
210         }
211         switch(action) {
212         case LFUN_BREAKPARAGRAPH:
213         case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
214         {
215 #ifndef NO_LATEX
216                 LyXFont font(LyXFont::ALL_SANE);
217                 font.setLatex (LyXFont::ON);
218 #else
219                 LyXFont font(LyXFont::ALL_INHERIT);
220                 font.setFamily(LyXFont::TYPEWRITER_FAMILY);
221                 font.setColor(LColor::latex);
222 #endif
223                 inset.setFont(bv, font);
224         }
225         break;
226         
227         default:
228                 break;
229         }
230         return result;
231 }
232
233
234 string const InsetERT::get_new_label() const
235 {
236         string la;
237         Paragraph::size_type const max_length = 15;
238
239         int const n = std::min(max_length, inset.paragraph()->size());
240         int i = 0;
241         int j = 0;
242         for(; i < n && j < inset.paragraph()->size(); ++j) {
243                 if (inset.paragraph()->isInset(j))
244                         continue;
245                 la += inset.paragraph()->getChar(j);
246                 ++i;
247         }
248         if ((i > 0) && (j < inset.paragraph()->size()))
249                 la += "...";
250         if (la.empty())
251                 la = _("666");
252         return la;
253 }