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