]> git.lyx.org Git - lyx.git/blob - src/insets/insetinfo.C
last updates from 1.0.4, no more updates expected from that branch now
[lyx.git] / src / insets / insetinfo.C
1 /* This file is part of
2  * ======================================================
3  * 
4  *           LyX, The Document Processor
5  *       
6  *          Copyright (C) 1995 Matthias Ettrich
7  *          Copyright (C) 1995-1998 The LyX Team.
8  *
9  *======================================================*/
10
11 #include <config.h>
12 #include <ctype.h>
13
14 #ifdef __GNUG__
15 #pragma implementation
16 #endif
17
18 #include "insetinfo.h"
19 #include "lyxparagraph.h"
20 #include "error.h"
21 #include "lyxdraw.h"
22 #include "gettext.h"
23 #include "lyx_gui_misc.h" // CancelCloseBoxCB
24 #include "buffer.h"
25
26 //      $Id: insetinfo.C,v 1.1 1999/09/27 18:44:39 larsbj Exp $ 
27
28 #if !defined(lint) && !defined(WITH_WARNINGS)
29 static char vcid[] = "$Id: insetinfo.C,v 1.1 1999/09/27 18:44:39 larsbj Exp $";
30 #endif /* lint */
31
32 extern BufferView *current_view;
33
34 /* Info, used for the Info boxes */
35
36 InsetInfo::InsetInfo()
37 {
38         form = NULL;
39 }
40
41
42 InsetInfo::InsetInfo(LString const & string)
43         : contents(string)
44 {
45         form = NULL;
46 }
47
48
49 InsetInfo::~InsetInfo()
50 {
51         if (form){
52                 fl_hide_form(form);
53                 fl_free_form(form);
54                 form = NULL;
55         }
56 }
57
58
59 int InsetInfo::Ascent(LyXFont const &font) const
60 {
61         return font.maxAscent()+1;
62 }
63
64
65 int InsetInfo::Descent(LyXFont const &font) const
66 {
67         return font.maxDescent()+1;
68 }
69
70
71 int InsetInfo::Width(LyXFont const &font) const
72 {
73         return 6 + font.textWidth(_("Note"), strlen(_("Note")));
74 }
75
76
77 void InsetInfo::Draw(LyXFont font, LyXScreen &scr,
78                      int baseline, float &x)
79 {
80         /* Info-insets are never LaTeX, so just correct the font */
81         font.setLatex(LyXFont::OFF);
82
83         // Draw as "Note" in a yellow box
84         x += 1;
85         scr.fillRectangle(gc_note,
86                           int(x), baseline - Ascent(font)+1,
87                           Width(font)-2, Ascent(font)+Descent(font)-2);
88         scr.drawRectangle(gc_note_frame,
89                           int(x), baseline- Ascent(font)+1,
90                           Width(font)-2, Ascent(font)+Descent(font)-2);
91         
92         scr.drawString(font, _("Note"), baseline, int(x+2));
93         x +=  Width(font) - 1;
94 }
95
96
97 void InsetInfo::Write(FILE *file)
98 {
99         fprintf(file, "Info %s", contents.c_str());
100 }
101
102
103 void InsetInfo::Read(LyXLex &lex)
104 {
105         LString tmp = lex.GetString(); // should be "Info"
106         if (tmp != "Info")
107                 lyxerr.print("ERROR (InsetInfo::Read): "
108                               "consistency check 1 failed.");
109
110         while (lex.IsOK()) {
111                 if (!lex.EatLine())
112                         // blank line in the file being read
113                         // should we skip blank lines?
114                         continue;
115
116                 LString const token = lex.GetString().strip();
117                 lyxerr.debug("Note: " + token, Error::LEX_PARSER);
118                 
119                 if (token != "\\end_inset") {
120                         contents += token + '\n';
121                 }
122                 else // token == "\\end_inset"
123                         break;
124         }
125         // now remove the last '\n's
126         contents.strip('\n');
127 }
128       
129
130 int InsetInfo::Latex(FILE *, signed char /*fragile*/)
131 {
132         return 0;
133 }
134
135
136 int InsetInfo::Latex(LString &, signed char /*fragile*/)
137 {
138         return 0;
139 }
140
141
142 int InsetInfo::Linuxdoc(LString &)
143 {
144         return 0;
145 }
146
147
148 int InsetInfo::DocBook(LString &)
149 {
150         return 0;
151 }
152
153
154 unsigned char InsetInfo::Editable() const
155 {
156         return 1;
157 }
158
159
160 void InsetInfo::CloseInfoCB(FL_OBJECT *, long data)
161 {
162         InsetInfo *inset = (InsetInfo*) data;
163 //      inset->contents = fl_get_input(inset->strobj);
164         LString tmp = fl_get_input(inset->strobj);
165         Buffer *buffer = current_view->currentBuffer();
166         if(tmp != inset->contents && !(buffer->isReadonly()) ) {
167                 buffer->markDirty();
168                 inset->contents = tmp;
169         }
170         if (inset->form) {
171                 fl_hide_form(inset->form);
172                 fl_free_form(inset->form);
173                 inset->form = NULL;
174         }
175 }
176
177
178 void InsetInfo::Edit(int, int)
179 {
180         if(current_view->currentBuffer()->isReadonly())
181                 WarnReadonly();
182         
183         if (!form) {
184                 FL_OBJECT *obj;
185                 form = fl_bgn_form(FL_UP_BOX,400,180);
186                 strobj = obj = fl_add_input(FL_MULTILINE_INPUT,10,10,380,120,"");
187                 fl_set_object_color(obj,FL_MCOL,FL_MCOL);
188                 fl_set_object_resize(obj, FL_RESIZE_ALL);
189                 fl_set_object_gravity(obj, NorthWestGravity, SouthEastGravity);
190                 obj = fl_add_button(FL_NORMAL_BUTTON,130,140,120,30,idex(_("Close|#C^[")));
191                 fl_set_object_resize(obj, FL_RESIZE_NONE);
192                 fl_set_object_gravity(obj, SouthWestGravity, SouthEastGravity);
193                 fl_set_object_callback(obj, CloseInfoCB, (long)this);
194                 fl_set_object_shortcut(obj, scex(_("Close|#C^[")), (long)this);
195                 fl_end_form();
196                 fl_set_form_atclose(form, CancelCloseBoxCB, NULL);
197         }
198         fl_set_input(strobj, contents.c_str());
199         if (form->visible) {
200                 fl_raise_form(form);
201         } else {
202                 fl_show_form(form,FL_PLACE_MOUSE | FL_FREE_SIZE, FL_FULLBORDER, 
203                              _("Note"));
204         }
205 }
206
207
208 Inset* InsetInfo::Clone()
209 {
210         InsetInfo *result = new InsetInfo(contents);
211         return result;
212 }
213
214
215 Inset::Code InsetInfo::LyxCode() const
216 {
217         return Inset::IGNORE_CODE;
218 }