]> git.lyx.org Git - lyx.git/blob - src/insets/insetinfo.C
clear()->erase() ; lots of using directives for cxx
[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(Painter & pain, LyXFont const & f,
79                      int baseline, float & x) const
80 {
81         LyXFont font(f);
82         
83         /* Info-insets are never LaTeX, so just correct the font */
84         font.setLatex(LyXFont::OFF).setColor(LColor::note);
85         
86         // Draw as "Note" in a yellow box
87         x += 1;
88         pain.fillRectangle(int(x), baseline - ascent(pain, font) + 1,
89                            width(pain, font) - 2,
90                            ascent(pain, font) + descent(pain, font) - 2,
91                            LColor::notebg);
92         pain.rectangle(int(x), baseline - ascent(pain, font) + 1,
93                        width(pain, font) - 2,
94                        ascent(pain, font) + descent(pain, font) - 2,
95                        LColor::noteframe);
96         
97         pain.text(int(x + 2), baseline, _("Note"), font);
98         x +=  width(pain, font) - 1;
99 }
100
101
102 void InsetInfo::Write(ostream & os) const
103 {
104         os << "Info " << contents;
105 }
106
107
108 void InsetInfo::Read(LyXLex & lex)
109 {
110         string tmp = lex.GetString(); // should be "Info"
111         if (tmp != "Info")
112                 lyxerr << "ERROR (InsetInfo::Read): "
113                         "consistency check 1 failed." << endl;
114
115         while (lex.IsOK()) {
116                 if (!lex.EatLine())
117                         // blank line in the file being read
118                         // should we skip blank lines?
119                         continue;
120
121                 string const token = strip(lex.GetString());
122                 lyxerr[Debug::PARSER] << "Note: " << token << endl;
123                 
124                 if (token != "\\end_inset") {
125                         contents += token + '\n';
126                 }
127                 else // token == "\\end_inset"
128                         break;
129         }
130         // now remove the last '\n's
131         contents = strip(contents, '\n');
132 }
133       
134
135 int InsetInfo::Latex(ostream &,
136                      bool /*fragile*/, bool /*free_spc*/) const
137 {
138         return 0;
139 }
140
141
142 int InsetInfo::Ascii(ostream &) const
143 {
144         return 0;
145 }
146
147
148 int InsetInfo::Linuxdoc(ostream &) const
149 {
150         return 0;
151 }
152
153
154 int InsetInfo::DocBook(ostream &) const
155 {
156         return 0;
157 }
158
159
160 Inset::EDITABLE InsetInfo::Editable() const
161 {
162         return IS_EDITABLE;
163 }
164
165
166 void InsetInfo::CloseInfoCB(FL_OBJECT * ob, long)
167 {
168         InsetInfo * inset = static_cast<InsetInfo*>(ob->u_vdata);
169         string tmp = fl_get_input(inset->strobj);
170         Buffer * buffer = current_view->buffer();
171         if(tmp != inset->contents && !(buffer->isReadonly()) ) {
172                 buffer->markDirty();
173                 inset->contents = tmp;
174         }
175         if (inset->form) {
176                 fl_hide_form(inset->form);
177                 fl_free_form(inset->form);
178                 inset->form = 0;
179         }
180 }
181
182
183 // This is just a wrapper.
184 extern "C" void C_InsetInfo_CloseInfoCB(FL_OBJECT * ob, long data) 
185 {
186         InsetInfo::CloseInfoCB(ob, data);
187 }
188
189
190 char const * InsetInfo::EditMessage() const 
191 {
192         return _("Opened note");
193 }
194
195
196 void InsetInfo::Edit(BufferView *bv, int, int, unsigned int)
197 {
198         static int ow = -1, oh;
199
200         if(bv->buffer()->isReadonly())
201                 WarnReadonly(bv->buffer()->fileName());
202         
203         if (!form) {
204                 FL_OBJECT *obj;
205                 form = fl_bgn_form(FL_UP_BOX, 400, 180);
206                 strobj = obj = fl_add_input(FL_MULTILINE_INPUT, 10, 10, 380, 120, "");
207                 fl_set_object_color(obj, FL_MCOL, FL_MCOL);
208                 fl_set_object_resize(obj, FL_RESIZE_ALL);
209                 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
210                 obj = fl_add_button(FL_NORMAL_BUTTON, 130, 140, 120, 30, idex(_("Close|#C^[")));
211                 fl_set_object_resize(obj, FL_RESIZE_NONE);
212                 fl_set_object_gravity(obj, SouthEastGravity, SouthEastGravity);
213                 fl_set_object_callback(obj, C_InsetInfo_CloseInfoCB, 0);
214                 obj->u_vdata = this;
215                 fl_set_object_shortcut(obj, scex(_("Close|#C^[")), 1);
216                 fl_end_form();
217                 fl_set_form_atclose(form, CancelCloseBoxCB, 0);
218         }
219         fl_set_input(strobj, contents.c_str());
220         if (form->visible) {
221                 fl_raise_form(form);
222         } else {
223                 fl_show_form(form, FL_PLACE_MOUSE | FL_FREE_SIZE, FL_FULLBORDER, 
224                              _("Note"));
225                 if (ow < 0) {
226                         ow = form->w;
227                         oh = form->h;
228                 }
229                 fl_set_form_minsize(form, ow, oh);
230         }
231 }
232
233
234 Inset * InsetInfo::Clone() const
235 {
236         return new InsetInfo(contents);
237 }
238
239
240 Inset::Code InsetInfo::LyxCode() const
241 {
242         return Inset::IGNORE_CODE;
243 }