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