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