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