]> git.lyx.org Git - lyx.git/blob - src/insets/insetinfo.C
Various fixes in insettext/tabular to make update work selective.
[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-2000 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 #include "font.h"
28
29 using std::ostream;
30 using std::endl;
31
32 /* Info, used for the Info boxes */
33
34 extern BufferView * current_view;
35
36
37 InsetInfo::InsetInfo()
38 {
39         form = 0;
40 }
41
42
43 InsetInfo::InsetInfo(string const & string)
44         : contents(string)
45 {
46         form = 0;
47 }
48
49
50 InsetInfo::~InsetInfo()
51 {
52         if (form){
53                 fl_hide_form(form);
54                 fl_free_form(form);
55                 form = 0;
56         }
57 }
58
59
60 int InsetInfo::ascent(Painter &, LyXFont const & font) const
61 {
62         return lyxfont::maxAscent(font) + 1;
63 }
64
65
66 int InsetInfo::descent(Painter &, LyXFont const & font) const
67 {
68         return lyxfont::maxDescent(font) + 1;
69 }
70
71
72 int InsetInfo::width(Painter &, LyXFont const & font) const
73 {
74         return 6 + lyxfont::width(_("Note"), font);
75 }
76
77
78 void InsetInfo::draw(BufferView * bv, LyXFont const & f,
79                      int baseline, float & x) const
80 {
81         Painter & pain = bv->painter();
82         LyXFont font(f);
83         
84         /* Info-insets are never LaTeX, so just correct the font */
85         font.setLatex(LyXFont::OFF).setColor(LColor::note);
86         
87         // Draw as "Note" in a yellow box
88         x += 1;
89         pain.fillRectangle(int(x), baseline - ascent(pain, font) + 1,
90                            width(pain, font) - 2,
91                            ascent(pain, font) + descent(pain, font) - 2,
92                            LColor::notebg);
93         pain.rectangle(int(x), baseline - ascent(pain, font) + 1,
94                        width(pain, font) - 2,
95                        ascent(pain, font) + descent(pain, font) - 2,
96                        LColor::noteframe);
97         
98         pain.text(int(x + 2), baseline, _("Note"), font);
99         x +=  width(pain, font) - 1;
100 }
101
102
103 void InsetInfo::Write(Buffer const *, ostream & os) const
104 {
105         os << "Info " << 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 &) 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" void C_InsetInfo_CloseInfoCB(FL_OBJECT * ob, long data) 
186 {
187         InsetInfo::CloseInfoCB(ob, data);
188 }
189
190
191 char const * InsetInfo::EditMessage() const 
192 {
193         return _("Opened note");
194 }
195
196
197 void InsetInfo::Edit(BufferView *bv, int, int, unsigned int)
198 {
199         static int ow = -1, oh;
200
201         if(bv->buffer()->isReadonly())
202                 WarnReadonly(bv->buffer()->fileName());
203         
204         if (!form) {
205                 FL_OBJECT *obj;
206                 form = fl_bgn_form(FL_UP_BOX, 400, 180);
207                 strobj = obj = fl_add_input(FL_MULTILINE_INPUT, 10, 10, 380, 120, "");
208                 fl_set_object_color(obj, FL_MCOL, FL_MCOL);
209                 fl_set_object_resize(obj, FL_RESIZE_ALL);
210                 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
211                 obj = fl_add_button(FL_NORMAL_BUTTON, 130, 140, 120, 30, idex(_("Close|#C^[")));
212                 fl_set_object_resize(obj, FL_RESIZE_NONE);
213                 fl_set_object_gravity(obj, SouthEastGravity, SouthEastGravity);
214                 fl_set_object_callback(obj, C_InsetInfo_CloseInfoCB, 0);
215                 obj->u_vdata = this;
216                 fl_set_object_shortcut(obj, scex(_("Close|#C^[")), 1);
217                 fl_end_form();
218                 fl_set_form_atclose(form, CancelCloseBoxCB, 0);
219         }
220         fl_set_input(strobj, contents.c_str());
221         if (form->visible) {
222                 fl_raise_form(form);
223         } else {
224                 fl_show_form(form, FL_PLACE_MOUSE | FL_FREE_SIZE, FL_FULLBORDER, 
225                              _("Note"));
226                 if (ow < 0) {
227                         ow = form->w;
228                         oh = form->h;
229                 }
230                 fl_set_form_minsize(form, ow, oh);
231         }
232 }
233
234
235 Inset * InsetInfo::Clone() const
236 {
237         return new InsetInfo(contents);
238 }
239
240
241 Inset::Code InsetInfo::LyxCode() const
242 {
243         return Inset::IGNORE_CODE;
244 }