]> git.lyx.org Git - lyx.git/blob - src/insets/insetert.C
376621a801d9cb28f5f7f2bd7092c0435a78597c
[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 #include "lyxtext.h"
26
27 using std::ostream;
28
29 void InsetERT::init()
30 {
31         setButtonLabel();
32         labelfont = LyXFont(LyXFont::ALL_SANE);
33         labelfont.decSize();
34         labelfont.decSize();
35         labelfont.setColor(LColor::latex);
36         setInsetName("ERT");
37 }
38
39
40 InsetERT::InsetERT() : InsetCollapsable()
41 {
42         init();
43 }
44
45
46 InsetERT::InsetERT(InsetERT const & in, bool same_id)
47         : InsetCollapsable(in, same_id)
48 {
49         init();
50 }
51
52
53 Inset * InsetERT::clone(Buffer const &, bool same_id) const
54 {
55         return new InsetERT(*const_cast<InsetERT *>(this), same_id);
56 }
57
58
59 InsetERT::InsetERT(string const & contents, bool collapsed)
60         : InsetCollapsable(collapsed)
61 {
62         LyXFont font(LyXFont::ALL_INHERIT);
63         font.setFamily(LyXFont::TYPEWRITER_FAMILY);
64         font.setColor(LColor::latex);
65         string::const_iterator cit = contents.begin();
66         string::const_iterator end = contents.end();
67         Paragraph::size_type pos = 0;
68         for (; cit != end; ++cit) {
69                 inset.paragraph()->insertChar(pos++, *cit, font);
70         }
71         // the init has to be after the initialization of the paragraph
72         // because of the label settings (draw_label for ert insets).
73         init();
74 }
75
76
77 void InsetERT::read(Buffer const * buf, LyXLex & lex)
78 {
79         InsetCollapsable::read(buf, lex);
80
81         setButtonLabel();
82 }
83
84
85 void InsetERT::write(Buffer const * buf, ostream & os) const 
86 {
87         os << getInsetName() << "\n";
88         InsetCollapsable::write(buf, os);
89 }
90
91
92 string const InsetERT::editMessage() const 
93 {
94         return _("Opened ERT Inset");
95 }
96
97
98 bool InsetERT::insertInset(BufferView *, Inset *)
99 {
100         return false;
101 }
102
103
104 void InsetERT::setFont(BufferView *, LyXFont const &, bool, bool selectall)
105 {
106         // if selectall is activated then the fontchange was an outside general
107         // fontchange and this messages is not needed
108         if (!selectall)
109                 WriteAlert(_("Impossible Operation!"),
110                            _("Not permitted to change font-types inside ERT-insets!"),
111                            _("Sorry."));
112 }
113
114
115 void InsetERT::edit(BufferView * bv, int x, int y, unsigned int button)
116 {
117         InsetCollapsable::edit(bv, x, y, button);
118         set_latex_font(bv);
119 }
120
121
122 void InsetERT::edit(BufferView * bv, bool front)
123 {
124         InsetCollapsable::edit(bv, front);
125         set_latex_font(bv);
126 }
127
128
129 void InsetERT::insetButtonRelease(BufferView * bv,
130                                   int x, int y, int button)
131 {
132         if ((x >= 0)  && (x < button_length) &&
133             (y >= button_top_y) &&  (y <= button_bottom_y))
134         {
135                 if (button == 2) {
136                         inlined(bv, !inlined());
137                         return;
138                 }
139                 if (collapsed_) {
140                         setLabel(_("ERT"));
141                 } else {
142                         setLabel(get_new_label());
143                 }
144                 if (collapsed_) {
145                         collapsed_ = false;
146                         inset.insetButtonRelease(bv, 0, 0, button);
147                         inset.setUpdateStatus(bv, InsetText::FULL);
148                         bv->updateInset(this, true);
149                 } else {
150                         collapsed_ = true;
151                         bv->unlockInset(this);
152                         bv->updateInset(this, true);
153                 }
154         } else if (!collapsed_ && (y > button_bottom_y)) {
155                 LyXFont font(LyXFont::ALL_SANE);
156                 int yy = ascent(bv, font) + y -
157                     (ascent_collapsed() +
158                      descent_collapsed() +
159                      inset.ascent(bv, font));
160                 inset.insetButtonRelease(bv, x, yy, button);
161         }
162 }
163
164
165 int InsetERT::latex(Buffer const *, std::ostream & os, bool /*fragile*/,
166                     bool /*free_spc*/) const
167 {
168         Paragraph * par = inset.paragraph();
169         while (par) {
170                 Paragraph::size_type siz = inset.paragraph()->size();
171                 for (Paragraph::size_type i = 0; i != siz; ++i) {
172                         char c = inset.paragraph()->getChar(i);
173                         switch (c) {
174                         case Paragraph::META_NEWLINE:
175                                 os << '\n';
176                                 break;
177                         default:
178                                 os << c;
179                                 break;
180                         }
181                 }
182                 par = par->next();
183         }
184         
185         return 1;
186 }
187
188
189 int InsetERT::ascii(Buffer const *,
190                     std::ostream &, int /*linelen*/) const 
191 {
192         return 0;
193 }
194
195
196 int InsetERT::linuxdoc(Buffer const *, std::ostream &) const
197 {
198         return 0;
199 }
200
201
202 int InsetERT::docBook(Buffer const *, std::ostream &) const
203 {
204         return 0;
205 }
206
207
208 UpdatableInset::RESULT
209 InsetERT::localDispatch(BufferView * bv, kb_action action, string const & arg)
210 {
211         UpdatableInset::RESULT result = DISPATCHED_NOUPDATE;
212
213         if (!inset.paragraph()->size()) {
214                 set_latex_font(bv);
215         }
216
217         switch(action) {
218         case LFUN_LAYOUT:
219                 bv->owner()->setLayout(inset.paragraph()->getLayout());
220                 break;
221         default:
222                 result = InsetCollapsable::localDispatch(bv, action, arg);
223         }
224         switch(action) {
225         case LFUN_BREAKPARAGRAPH:
226         case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
227                 set_latex_font(bv);
228                 break;
229         
230         default:
231                 break;
232         }
233         return result;
234 }
235
236
237 string const InsetERT::get_new_label() const
238 {
239         string la;
240         Paragraph::size_type const max_length = 15;
241
242         Paragraph::size_type const p_siz = inset.paragraph()->size();
243         Paragraph::size_type const n = std::min(max_length, p_siz);
244         int i = 0;
245         int j = 0;
246         for(; i < n && j < p_siz; ++j) {
247                 if (inset.paragraph()->isInset(j))
248                         continue;
249                 la += inset.paragraph()->getChar(j);
250                 ++i;
251         }
252         if (i > 0 && j < p_siz) {
253                 la += "...";
254         }
255         if (la.empty()) {
256                 la = _("ERT");
257         }
258         return la;
259 }
260
261
262 void InsetERT::setButtonLabel() 
263 {
264         if (collapsed_) {
265                 setLabel(get_new_label());
266         } else {
267                 setLabel(_("ERT"));
268         }
269 }
270
271
272 bool InsetERT::checkInsertChar(LyXFont & font)
273 {
274         LyXFont f(LyXFont::ALL_INHERIT);
275         font = f;
276         font.setFamily(LyXFont::TYPEWRITER_FAMILY);
277         font.setColor(LColor::latex);
278         return true;
279 }
280
281
282 void InsetERT::inlined(BufferView * bv, bool flag)
283 {
284         if (flag != inset.getAutoBreakRows())
285                 return;
286         
287         inset.setAutoBreakRows(!flag);
288         bv->updateInset(this, true);
289 }
290
291 void InsetERT::draw(BufferView * bv, LyXFont const & f, 
292                     int baseline, float & x, bool cleared) const
293 {
294         Painter & pain = bv->painter();
295
296         button_length = width_collapsed();
297         button_top_y = -ascent(bv, f);
298         button_bottom_y = -ascent(bv, f) + ascent_collapsed() +
299                 descent_collapsed();
300
301         if (!isOpen()) {
302                 draw_collapsed(pain, baseline, x);
303                 x += TEXT_TO_INSET_OFFSET;
304                 return;
305         }
306
307         float old_x = x;
308
309         if (!owner())
310                 x += static_cast<float>(scroll());
311
312         if (!cleared && (inset.need_update == InsetText::FULL ||
313                          inset.need_update == InsetText::INIT ||
314                          top_x != int(x) ||
315                          top_baseline != baseline))
316         {
317                 // we don't need anymore to clear here we just have to tell
318                 // the underlying LyXText that it should do the RowClear!
319                 inset.setUpdateStatus(bv, InsetText::FULL);
320                 bv->text->status(bv, LyXText::CHANGED_IN_DRAW);
321                 return;
322         }
323
324         top_x = int(x);
325         top_baseline = baseline;
326
327         int const bl = baseline - ascent(bv, f) + ascent_collapsed();
328
329         if (inlined()) {
330                 inset.draw(bv, f, baseline, x, cleared);
331         } else {
332                 draw_collapsed(pain, bl, old_x);
333                 inset.draw(bv, f, 
334                                    bl + descent_collapsed() + inset.ascent(bv, f),
335                                    x, cleared);
336         }
337         need_update = NONE;
338 }
339
340
341 void InsetERT::set_latex_font(BufferView * bv)
342 {
343         LyXFont font(LyXFont::ALL_INHERIT);
344
345         font.setFamily(LyXFont::TYPEWRITER_FAMILY);
346         font.setColor(LColor::latex);
347         inset.setFont(bv, font);
348 }