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