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