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