]> git.lyx.org Git - lyx.git/blob - src/insets/insetinfo.C
some reindentation, revert workarea xpos++, constify, remove all traces of LyXParagra...
[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         : form(0), labelfont(LyXFont::ALL_SANE)
39 {
40         labelfont.decSize().decSize()
41                 .setColor(LColor::note).setLatex(LyXFont::OFF);
42 }
43
44
45 InsetInfo::InsetInfo(string const & str)
46         : contents(str), form(0), labelfont(LyXFont::ALL_SANE)
47 {
48         labelfont.decSize().decSize()
49                 .setColor(LColor::note).setLatex(LyXFont::OFF);
50 }
51
52
53 InsetInfo::~InsetInfo()
54 {
55         if (form) {
56                 fl_hide_form(form);
57                 fl_free_form(form);
58                 form = 0;
59         }
60 }
61
62
63 int InsetInfo::ascent(BufferView *, LyXFont const &) const
64 {
65         return lyxfont::maxAscent(labelfont) + 1;
66 }
67
68
69 int InsetInfo::descent(BufferView *, LyXFont const &) const
70 {
71         return lyxfont::maxDescent(labelfont) + 1;
72 }
73
74
75 int InsetInfo::width(BufferView *, LyXFont const &) const
76 {
77         return 6 + lyxfont::width(_("Note"), labelfont);
78 }
79
80
81 void InsetInfo::draw(BufferView * bv, LyXFont const &,
82                      int baseline, float & x, bool) const
83 {
84         Painter & pain = bv->painter();
85
86         // Draw as "Note" in a yellow box
87         x += 1;
88         pain.fillRectangle(int(x), baseline - ascent(bv, labelfont),
89                            width(bv, labelfont) - 2,
90                            ascent(bv, labelfont) + descent(bv, labelfont) - 2,
91                            LColor::notebg);
92         pain.rectangle(int(x), baseline - ascent(bv, labelfont),
93                        width(bv, labelfont) - 2,
94                        ascent(bv, labelfont) + descent(bv, labelfont) - 2,
95                        LColor::noteframe);
96         
97         pain.text(int(x + 2), baseline, _("Note"), labelfont);
98         x +=  width(bv, labelfont) - 1;
99 }
100
101
102 void InsetInfo::Write(Buffer const *, ostream & os) const
103 {
104         os << "Info\n" << contents;
105 }
106
107
108 void InsetInfo::Read(Buffer const *, 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(Buffer const *, ostream &,
136                      bool /*fragile*/, bool /*free_spc*/) const
137 {
138         return 0;
139 }
140
141
142 int InsetInfo::Ascii(Buffer const *, ostream &, int) const
143 {
144         return 0;
145 }
146
147
148 int InsetInfo::Linuxdoc(Buffer const *, ostream &) const
149 {
150         return 0;
151 }
152
153
154 int InsetInfo::DocBook(Buffer const *, 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"
185 void C_InsetInfo_CloseInfoCB(FL_OBJECT * ob, long data) 
186 {
187         InsetInfo::CloseInfoCB(ob, data);
188 }
189
190
191 string 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,
225                              FL_PLACE_MOUSE | FL_FREE_SIZE, FL_TRANSIENT,
226                              _("Note"));
227                 if (ow < 0) {
228                         ow = form->w;
229                         oh = form->h;
230                 }
231                 fl_set_form_minsize(form, ow, oh);
232         }
233 }
234
235
236 Inset * InsetInfo::Clone(Buffer const &) const
237 {
238         return new InsetInfo(contents);
239 }
240
241
242 Inset::Code InsetInfo::LyxCode() const
243 {
244         return Inset::IGNORE_CODE;
245 }