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