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