]> git.lyx.org Git - lyx.git/blob - src/insets/insetinfo.C
3d4648532204f9a49d6c833cb5375ef82b91ac2c
[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-1999 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 "lyxparagraph.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 #ifndef USE_PAINTER
28 #include "lyxdraw.h"
29 #endif
30
31 /* Info, used for the Info boxes */
32
33 extern BufferView * current_view;
34
35
36 InsetInfo::InsetInfo()
37 {
38         form = 0;
39 }
40
41
42 InsetInfo::InsetInfo(string const & string)
43         : contents(string)
44 {
45         form = 0;
46 }
47
48
49 InsetInfo::~InsetInfo()
50 {
51         if (form){
52                 fl_hide_form(form);
53                 fl_free_form(form);
54                 form = 0;
55         }
56 }
57
58
59 #ifdef USE_PAINTER
60 int InsetInfo::ascent(Painter &, LyXFont const & font) const
61 {
62         return font.maxAscent() + 1;
63 }
64 #else
65 int InsetInfo::Ascent(LyXFont const & font) const
66 {
67         return font.maxAscent() + 1;
68 }
69 #endif
70
71
72 #ifdef USE_PAINTER
73 int InsetInfo::descent(Painter &, LyXFont const & font) const
74 {
75         return font.maxDescent() + 1;
76 }
77 #else
78 int InsetInfo::Descent(LyXFont const & font) const
79 {
80         return font.maxDescent() + 1;
81 }
82 #endif
83
84
85 #ifdef USE_PAINTER
86 int InsetInfo::width(Painter &, LyXFont const & font) const
87 {
88         return 6 + font.textWidth(_("Note"), strlen(_("Note")));
89 }
90 #else
91 int InsetInfo::Width(LyXFont const & font) const
92 {
93         return 6 + font.textWidth(_("Note"), strlen(_("Note")));
94 }
95 #endif
96
97
98 #ifdef USE_PAINTER
99 void InsetInfo::draw(Painter & pain, LyXFont const & f,
100                      int baseline, float & x) const
101 {
102         LyXFont font(f);
103         
104         /* Info-insets are never LaTeX, so just correct the font */
105         font.setLatex(LyXFont::OFF);
106
107         // Draw as "Note" in a yellow box
108         x += 1;
109         pain.fillRectangle(int(x), baseline - ascent(pain, font) + 1,
110                            width(pain, font) - 2,
111                            ascent(pain, font) + descent(pain, font) - 2);
112         pain.rectangle(int(x), baseline - ascent(pain, font) + 1,
113                        width(pain, font) - 2,
114                        ascent(pain, font) + descent(pain, font) - 2);
115         
116         pain.text(int(x + 2), baseline, _("Note"), font);
117         x +=  width(pain, font) - 1;
118 }
119 #else
120 void InsetInfo::Draw(LyXFont font, LyXScreen & scr,
121                      int baseline, float & x)
122 {
123         /* Info-insets are never LaTeX, so just correct the font */
124         font.setLatex(LyXFont::OFF);
125
126         // Draw as "Note" in a yellow box
127         x += 1;
128         scr.fillRectangle(gc_note,
129                           int(x), baseline - Ascent(font)+1,
130                           Width(font)-2, Ascent(font)+Descent(font)-2);
131         scr.drawRectangle(gc_note_frame,
132                           int(x), baseline- Ascent(font)+1,
133                           Width(font)-2, Ascent(font)+Descent(font)-2);
134         
135         scr.drawString(font, _("Note"), baseline, int(x+2));
136         x +=  Width(font) - 1;
137 }
138 #endif
139
140
141 void InsetInfo::Write(ostream & os)
142 {
143         os << "Info " << contents;
144 }
145
146
147 void InsetInfo::Read(LyXLex & lex)
148 {
149         string tmp = lex.GetString(); // should be "Info"
150         if (tmp != "Info")
151                 lyxerr << "ERROR (InsetInfo::Read): "
152                         "consistency check 1 failed." << endl;
153
154         while (lex.IsOK()) {
155                 if (!lex.EatLine())
156                         // blank line in the file being read
157                         // should we skip blank lines?
158                         continue;
159
160                 string const token = strip(lex.GetString());
161                 lyxerr[Debug::PARSER] << "Note: " << token << endl;
162                 
163                 if (token != "\\end_inset") {
164                         contents += token + '\n';
165                 }
166                 else // token == "\\end_inset"
167                         break;
168         }
169         // now remove the last '\n's
170         contents = strip(contents, '\n');
171 }
172       
173
174 int InsetInfo::Latex(ostream &, signed char /*fragile*/)
175 {
176         return 0;
177 }
178
179
180 int InsetInfo::Latex(string &, signed char /*fragile*/)
181 {
182         return 0;
183 }
184
185
186 int InsetInfo::Linuxdoc(string &)
187 {
188         return 0;
189 }
190
191
192 int InsetInfo::DocBook(string &)
193 {
194         return 0;
195 }
196
197
198 unsigned char InsetInfo::Editable() const
199 {
200         return 1;
201 }
202
203
204 void InsetInfo::CloseInfoCB(FL_OBJECT * ob, long)
205 {
206         InsetInfo * inset = static_cast<InsetInfo*>(ob->u_vdata);
207         string tmp = fl_get_input(inset->strobj);
208         Buffer * buffer = current_view->buffer();
209         if(tmp != inset->contents && !(buffer->isReadonly()) ) {
210                 buffer->markDirty();
211                 inset->contents = tmp;
212         }
213         if (inset->form) {
214                 fl_hide_form(inset->form);
215                 fl_free_form(inset->form);
216                 inset->form = 0;
217         }
218 }
219
220
221 // This is just a wrapper.
222 extern "C" void C_InsetInfo_CloseInfoCB(FL_OBJECT * ob, long data) 
223 {
224         InsetInfo::CloseInfoCB(ob, data);
225 }
226
227
228 void InsetInfo::Edit(int, int)
229 {
230         static int ow = -1, oh;
231
232         if(current_view->buffer()->isReadonly())
233                 WarnReadonly(current_view->buffer()->fileName());
234         
235         if (!form) {
236                 FL_OBJECT *obj;
237                 form = fl_bgn_form(FL_UP_BOX, 400, 180);
238                 strobj = obj = fl_add_input(FL_MULTILINE_INPUT, 10, 10, 380, 120, "");
239                 fl_set_object_color(obj, FL_MCOL, FL_MCOL);
240                 fl_set_object_resize(obj, FL_RESIZE_ALL);
241                 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
242                 obj = fl_add_button(FL_NORMAL_BUTTON, 130, 140, 120, 30, idex(_("Close|#C^[")));
243                 fl_set_object_resize(obj, FL_RESIZE_NONE);
244                 fl_set_object_gravity(obj, SouthEastGravity, SouthEastGravity);
245                 fl_set_object_callback(obj, C_InsetInfo_CloseInfoCB, 0);
246                 obj->u_vdata = this;
247                 fl_set_object_shortcut(obj, scex(_("Close|#C^[")), 1);
248                 fl_end_form();
249                 fl_set_form_atclose(form, CancelCloseBoxCB, 0);
250         }
251         fl_set_input(strobj, contents.c_str());
252         if (form->visible) {
253                 fl_raise_form(form);
254         } else {
255                 fl_show_form(form, FL_PLACE_MOUSE | FL_FREE_SIZE, FL_FULLBORDER, 
256                              _("Note"));
257                 if (ow < 0) {
258                         ow = form->w;
259                         oh = form->h;
260                 }
261                 fl_set_form_minsize(form, ow, oh);
262         }
263 }
264
265
266 Inset * InsetInfo::Clone() const
267 {
268         return new InsetInfo(contents);
269 }
270
271
272 Inset::Code InsetInfo::LyxCode() const
273 {
274         return Inset::IGNORE_CODE;
275 }