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