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