]> git.lyx.org Git - features.git/blob - src/insets/insetgraphics.C
read the Changelog
[features.git] / src / insets / insetgraphics.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 #ifdef __GNUG__
12 #pragma implementation
13 #endif
14
15 #include <config.h>
16
17 #include "insets/insetgraphics.h"
18 #include "BufferView.h"
19 #include "Painter.h"
20 #include "form_graphics.h"
21 #include "lyx_gui_misc.h"
22 #include "filedlg.h"
23 #include "support/FileInfo.h"
24 #include "support/filetools.h"
25
26 using std::ostream;
27 using std::endl;
28
29 extern string system_lyxdir;
30 extern string user_lyxdir;
31 extern string system_tempdir;
32
33 string browseFile();
34
35 extern "C"
36 void GraphicxCB(FL_OBJECT * obj, long arg) 
37 {
38         lyxerr << "GraphicxCB: obj = " << obj << " arg = " << arg << endl;
39         switch (arg) {
40         case 0: // The graphics file
41                 lyxerr << "Set the graphics file in InsetGraphics" << endl;
42                 break;
43         case 1: // The file browser
44                 browseFile();
45                 break;
46         case 2: // The Apply button
47                 lyxerr << "Scan the form and set the "
48                         "InsetGraphics accordingly." << endl;
49                 break;
50         case 3: // The OK button
51                 GraphicxCB(obj, 2); // do the apply
52                 GraphicxCB(obj, 4); // do the cancel
53                 break;
54         case 4: // The Cancel button
55                 lyxerr << "Just hide the form and do nothing else!" << endl;
56                 break;
57         case 99:
58                 lyxerr << "Not implemented yet..." << endl;
59                 break;
60         default:
61                 lyxerr << "Unknown callback value!" << endl;
62                 break;
63         }
64 }
65
66
67 string browseFile()
68 {
69         // This function is probably not good enough yet, and does need some
70         // arguemnts to tell what dir to start looking in.
71         
72         static string current_figure_path = ".";
73
74         LyXFileDlg fileDlg;
75
76         // Does user clipart directory exist?
77         string bufclip = AddName (user_lyxdir, "clipart");      
78         FileInfo fileInfo(bufclip);
79         if (!(fileInfo.isOK() && fileInfo.isDir()))
80                 // No - bail out to system clipart directory
81                 bufclip = AddName (system_lyxdir, "clipart");   
82
83
84         fileDlg.SetButton(0, _("Clipart"), bufclip); 
85
86         bool error = false;
87         string buf;
88         do {
89                 string p = fileDlg.Select(_("Graphics"),
90                                    current_figure_path,
91                                    "*ps", string());
92
93                 if (p.empty()) return p;
94
95                 current_figure_path = OnlyPath(p);
96
97                 if (p.find_first_of("#~$% ") != string::npos) {
98                         WriteAlert(_("Filename can't contain any "
99                                      "of these characters:"),
100                                    // xgettext:no-c-format
101                                    _("space, '#', '~', '$' or '%'.")); 
102                         error = true;
103                 }
104         } while (error);
105
106         return buf;
107 }
108
109
110 InsetGraphics::InsetGraphics()
111         : form(0)
112 {}
113
114
115 int InsetGraphics::ascent(BufferView *, LyXFont const &) const 
116 {
117         
118         return 100;
119 }
120
121
122 int InsetGraphics::descent(BufferView *, LyXFont const &) const 
123 {
124         // this is not true if viewport is used and clip is not.
125         return 1;
126 }
127
128
129 int InsetGraphics::width(BufferView *, LyXFont const &) const 
130 {
131         if (bb.isSet()) {
132                 return bb.urx - bb.llx;
133         }
134         return 100;
135 }
136
137
138 void InsetGraphics::draw(BufferView * bv, LyXFont const & font,
139                          int baseline, float & x, bool) const
140 {
141         Painter & pain = bv->painter();
142
143         // This will draw the graphics. As for now we only draw a
144         // placeholder rectangele.
145         pain.rectangle(int(x), baseline - ascent(bv, font),
146                        width(bv, font),
147                        ascent(bv, font) + descent(bv, font));
148 }
149
150
151 void InsetGraphics::Edit(BufferView *, int, int, unsigned int)
152 {
153         lyxerr.debug() << "InsetGraphics::Edit" << endl;
154
155         if (!form) {
156                 form = create_form_Graphics();
157                 fl_set_form_atclose(form->Graphics, CancelCloseBoxCB, 0);
158                 fl_set_object_return(form->Angle, FL_RETURN_ALWAYS);
159                 fl_set_object_return(form->Width, FL_RETURN_ALWAYS);
160                 fl_set_object_return(form->Height, FL_RETURN_ALWAYS);
161         }
162
163         if (form->Graphics->visible) {
164                 fl_raise_form(form->Graphics);
165         } else {
166                 fl_show_form(form->Graphics, FL_PLACE_MOUSE | FL_PLACE_SIZE,
167                              FL_FULLBORDER, _("Graphics"));
168         }
169 }
170
171
172 Inset::EDITABLE InsetGraphics::Editable() const
173 {
174         return IS_EDITABLE;
175 }
176
177
178 void InsetGraphics::Write(Buffer const *, ostream & os) const
179 {
180         // The question on the file format is still open.
181         // Suggestions?
182         // perhaps a format that is xml-parsable
183         //<graphics name="test.eps"/>
184         os << "GRAPHICS\n";
185 }
186
187
188 void InsetGraphics::Read(Buffer const *, LyXLex & /*lex*/) 
189 {
190         // For now we only use a static file...
191         graphicsfile = "testfile.xpm";
192         //graphicscache.addFile(graphicsfile);
193         //bb = graphicscache.getBB(graphicsfile);
194         //pixmap = graphicscache.getPixmap(graphicsfile);
195 }
196
197
198 int InsetGraphics::Latex(Buffer const *, ostream & os,
199                          bool /*fragile*/, bool/*fs*/) const
200 {
201         // MISSING: We have to decide how to do the order of the options
202         // that is depentant of order, like witdth, height, andlge. Should
203         // we rotate before scale? Should we let the user decide?
204         // bool rot_before_scale; ?
205         // Nothing to do if we don't have a graphics file
206         if (graphicsfile.empty()) return 0;
207
208         // We never used the starred form, we use the "clip" option instead.
209         string command("\\insetgraphics");
210         
211 #ifdef HAVE_SSTREAM
212         std::ostringstream options;
213 #else
214         ostrstream options;
215 #endif
216         if (bb.isSet() && use_bb) {
217                 options << "bb="
218                         << bb.llx << " " << bb.lly << " "
219                         << bb.urx << " " << bb.ury << ",";
220         }
221         if (hiresbb) {
222                 options << "hiresbb,";
223         }
224         if (viewport.isSet()) {
225                 options << "viewport="
226                         << viewport.llx << " " << viewport.lly << " "
227                         << viewport.urx << " " << viewport.ury << ",";
228         }
229         if (trim.isSet()) {
230                 options << "trim="
231                         << trim.llx << " " << trim.lly << " "
232                         << trim.urx << " " << trim.ury << ",";
233         }
234         if (natheight.value() == 0) {
235                 options << "natheight=" << natheight.asString() << ",";
236         }
237         if (natwidth.value() == 0) {
238                 options << "natwidth=" << natwidth.asString() << ",";
239         }
240         if (angle != 0.0) {
241                 options << "angle=" << angle << ",";
242         }
243         if (origin != DEFAULT) {
244                 switch(origin) {
245                 case DEFAULT: break;
246                 case LEFTTOP:
247                         options << "origin=lt,";
248                         break;
249                 case LEFTCENTER:
250                         options << "origin=lc,";
251                         break;
252                 case LEFTBASELINE:
253                         options << "origin=lB,";
254                         break;
255                 case LEFTBOTTOM:
256                         options << "origin=lb,";
257                         break;
258                 case CENTERTOP:
259                         options << "origin=ct,";
260                         break;
261                 case CENTER:
262                         options << "origin=c,";
263                         break;
264                 case CENTERBASELINE:
265                         options << "origin=cB,";
266                         break;
267                 case CENTERBOTTOM:
268                         options << "origin=cb,";
269                         break;
270                 case RIGHTTOP:
271                         options << "origin=rt,";
272                         break;
273                 case RIGHTCENTER:
274                         options << "origin=rc,";
275                         break;
276                 case RIGHTBASELINE:
277                         options << "origin=rB,";
278                         break;
279                 case RIGHTBOTTOM:
280                         options << "origin=rb,";
281                         break;
282                 }
283         }
284         if (g_width.value() != 0) {
285                 options << "width=" << g_width.asString() << ",";
286         }
287         if (g_height.value() != 0) {
288                 options << "height=" << g_height.asString() << ",";
289         }
290         if (totalheight.value() != 0) {
291                 options << "totalheight=" << totalheight.asString() << ",";
292         }
293         if (keepaspectratio) {
294                 options << "keepaspectratio,";
295         }
296         if (scale != 0.0) {
297                 options << "scale=" << scale << ",";
298         }
299         if (clip) {
300                 options << "clip,";
301         }
302         if (draft) {
303                 options << "draft,";
304         }
305         if (!type.empty()) {
306                 options << "type=" << type << ",";
307         }
308         if (!ext.empty()) {
309                 options << "ext=" << type << ",";
310         }
311         if (!read.empty()) {
312                 options << "read=" << type << ",";
313         }
314         if (!command.empty()) {
315                 options << "command=" << type << ",";
316         }
317 #ifdef HAVE_SSTREAM
318         string opts(options.str().c_str());
319 #else
320         options << '\0';
321         char * tmp = options.str();
322         string opts(tmp);
323         delete [] tmp;
324 #endif
325         opts = strip(opts, ',');
326         if (!opts.empty()) {
327                 command += "[";
328                 command += opts;
329                 command += "]";
330         }
331         command += "{";
332         command += graphicsfile;
333         command += "}";
334
335         os << command << '\n';
336         
337         return 1;
338 }
339
340
341 int InsetGraphics::Ascii(Buffer const *, ostream &) const
342 {
343         return 0;
344 }
345
346
347 int InsetGraphics::Linuxdoc(Buffer const *, ostream &) const
348 {
349         return 0;
350 }
351
352
353 int InsetGraphics::DocBook(Buffer const *, ostream &) const
354 {
355         return 0;
356 }
357
358
359 void InsetGraphics::Validate(LaTeXFeatures & /*features*/) const
360 {
361         //features.graphicx = true;
362 }
363
364
365 Inset * InsetGraphics::Clone() const
366 {
367         return new InsetGraphics;
368 }
369
370