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