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