]> git.lyx.org Git - lyx.git/blob - src/insets/insetinfo.C
get rid of NO_LATEX, split some methods, small cleanup
[lyx.git] / src / insets / insetinfo.C
1 /* This file is part of
2  * ======================================================
3  * 
4  *           LyX, The Document Processor
5  *       
6  *          Copyright 1995 Matthias Ettrich
7  *          Copyright 1995-2001 The LyX Team.
8  *
9  * ====================================================== */
10
11 #include <config.h>
12
13 #include <cctype>
14
15 #ifdef __GNUG__
16 #pragma implementation
17 #endif
18
19 #include "insetinfo.h"
20 #include "paragraph.h"
21 #include "debug.h"
22 #include "gettext.h"
23 #include "lyx_gui_misc.h" // CancelCloseBoxCB
24 #include "buffer.h"
25 #include "support/lstrings.h"
26 #include "Painter.h"
27 #include "font.h"
28 #include "BufferView.h"
29
30 using std::ostream;
31 using std::endl;
32
33 /* Info, used for the Info boxes */
34
35 extern BufferView * current_view;
36
37
38 InsetInfo::InsetInfo()
39         : form(0), labelfont(LyXFont::ALL_SANE)
40 {
41         labelfont.decSize().decSize()
42                 .setColor(LColor::note);
43 }
44
45
46 InsetInfo::InsetInfo(string const & str)
47         : contents(str), form(0), labelfont(LyXFont::ALL_SANE)
48 {
49         labelfont.decSize().decSize()
50                 .setColor(LColor::note);
51 }
52
53
54 InsetInfo::~InsetInfo()
55 {
56         if (form) {
57                 fl_hide_form(form);
58                 fl_free_form(form);
59                 form = 0;
60         }
61 }
62
63
64 int InsetInfo::ascent(BufferView *, LyXFont const &) const
65 {
66         return lyxfont::maxAscent(labelfont) + 1;
67 }
68
69
70 int InsetInfo::descent(BufferView *, LyXFont const &) const
71 {
72         return lyxfont::maxDescent(labelfont) + 1;
73 }
74
75
76 int InsetInfo::width(BufferView *, LyXFont const &) const
77 {
78         return 6 + lyxfont::width(_("Note"), labelfont);
79 }
80
81
82 void InsetInfo::draw(BufferView * bv, LyXFont const &,
83                      int baseline, float & x, bool) const
84 {
85         Painter & pain = bv->painter();
86
87         // Draw as "Note" in a yellow box
88         x += 1;
89         pain.fillRectangle(int(x), baseline - ascent(bv, labelfont),
90                            width(bv, labelfont) - 2,
91                            ascent(bv, labelfont) + descent(bv, labelfont) - 2,
92                            LColor::notebg);
93         pain.rectangle(int(x), baseline - ascent(bv, labelfont),
94                        width(bv, labelfont) - 2,
95                        ascent(bv, labelfont) + descent(bv, labelfont) - 2,
96                        LColor::noteframe);
97         
98         pain.text(int(x + 2), baseline, _("Note"), labelfont);
99         x +=  width(bv, labelfont) - 1;
100 }
101
102
103 void InsetInfo::write(Buffer const *, ostream & os) const
104 {
105         os << "Info\n" << contents;
106 }
107
108
109 void InsetInfo::read(Buffer const *, LyXLex & lex)
110 {
111         string tmp = lex.GetString(); // should be "Info"
112         if (tmp != "Info")
113                 lyxerr << "ERROR (InsetInfo::Read): "
114                         "consistency check 1 failed." << endl;
115
116         while (lex.IsOK()) {
117                 if (!lex.EatLine())
118                         // blank line in the file being read
119                         // should we skip blank lines?
120                         continue;
121
122                 string const token = strip(lex.GetString());
123                 lyxerr[Debug::PARSER] << "Note: " << token << endl;
124                 
125                 if (token != "\\end_inset") {
126                         contents += token + '\n';
127                 }
128                 else // token == "\\end_inset"
129                         break;
130         }
131         // now remove the last '\n's
132         contents = strip(contents, '\n');
133 }
134       
135
136 int InsetInfo::latex(Buffer const *, ostream &,
137                      bool /*fragile*/, bool /*free_spc*/) const
138 {
139         return 0;
140 }
141
142
143 int InsetInfo::ascii(Buffer const *, ostream &, int) const
144 {
145         return 0;
146 }
147
148
149 int InsetInfo::linuxdoc(Buffer const *, ostream &) const
150 {
151         return 0;
152 }
153
154
155 int InsetInfo::docBook(Buffer const *, ostream &) const
156 {
157         return 0;
158 }
159
160
161 Inset::EDITABLE InsetInfo::editable() const
162 {
163         return IS_EDITABLE;
164 }
165
166
167 void InsetInfo::closeInfoCB(FL_OBJECT * ob, long)
168 {
169         InsetInfo * inset = static_cast<InsetInfo*>(ob->u_vdata);
170         string tmp = fl_get_input(inset->strobj);
171         Buffer * buffer = current_view->buffer();
172         if (tmp != inset->contents && !(buffer->isReadonly())) {
173                 buffer->markDirty();
174                 inset->contents = tmp;
175         }
176         if (inset->form) {
177                 fl_hide_form(inset->form);
178                 fl_free_form(inset->form);
179                 inset->form = 0;
180         }
181 }
182
183
184 // This is just a wrapper.
185 extern "C"
186 void C_InsetInfo_CloseInfoCB(FL_OBJECT * ob, long data) 
187 {
188         InsetInfo::closeInfoCB(ob, data);
189 }
190
191
192 string const InsetInfo::editMessage() const 
193 {
194         return _("Opened note");
195 }
196
197
198 void InsetInfo::edit(BufferView *bv, int, int, unsigned int)
199 {
200         static int ow = -1;
201         static int oh;
202
203         if (bv->buffer()->isReadonly())
204                 WarnReadonly(bv->buffer()->fileName());
205         
206         if (!form) {
207                 FL_OBJECT *obj;
208                 form = fl_bgn_form(FL_UP_BOX, 400, 180);
209                 strobj = obj = fl_add_input(FL_MULTILINE_INPUT, 10, 10, 380, 120, "");
210                 fl_set_object_color(obj, FL_MCOL, FL_MCOL);
211                 fl_set_object_resize(obj, FL_RESIZE_ALL);
212                 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
213                 obj = fl_add_button(FL_NORMAL_BUTTON, 130, 140, 120, 30, idex(_("Close|#C^[")));
214                 fl_set_object_resize(obj, FL_RESIZE_NONE);
215                 fl_set_object_gravity(obj, SouthEastGravity, SouthEastGravity);
216                 fl_set_object_callback(obj, C_InsetInfo_CloseInfoCB, 0);
217                 obj->u_vdata = this;
218                 fl_set_object_shortcut(obj, scex(_("Close|#C^[")), 1);
219                 fl_end_form();
220                 fl_set_form_atclose(form, CancelCloseBoxCB, 0);
221         }
222         fl_set_input(strobj, contents.c_str());
223         if (form->visible) {
224                 fl_raise_form(form);
225         } else {
226                 fl_show_form(form,
227                              FL_PLACE_MOUSE | FL_FREE_SIZE, FL_TRANSIENT,
228                              _("Note"));
229                 if (ow < 0) {
230                         ow = form->w;
231                         oh = form->h;
232                 }
233                 fl_set_form_minsize(form, ow, oh);
234         }
235 }
236
237
238 void InsetInfo::edit(BufferView * bv, bool)
239 {
240         edit(bv, 0, 0, 0);
241 }
242
243
244 Inset * InsetInfo::clone(Buffer const &, bool) const
245 {
246         return new InsetInfo(contents);
247 }
248
249
250 Inset::Code InsetInfo::lyxCode() const
251 {
252         return Inset::IGNORE_CODE;
253 }