1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2000 the LyX Team.
9 * ====================================================== */
12 #pragma implementation
17 #include "insets/insetgraphics.h"
19 #include "form_graphics.h"
20 #include "lyx_gui_misc.h"
22 #include "support/FileInfo.h"
23 #include "support/filetools.h"
28 extern string system_lyxdir;
29 extern string user_lyxdir;
30 extern string system_tempdir;
34 extern "C" void GraphicxCB(FL_OBJECT * obj, long arg)
36 lyxerr << "GraphicxCB: obj = " << obj << " arg = " << arg << endl;
38 case 0: // The graphics file
39 lyxerr << "Set the graphics file in InsetGraphics" << endl;
41 case 1: // The file browser
44 case 2: // The Apply button
45 lyxerr << "Scan the form and set the "
46 "InsetGraphics accordingly." << endl;
48 case 3: // The OK button
49 GraphicxCB(obj, 2); // do the apply
50 GraphicxCB(obj, 4); // do the cancel
52 case 4: // The Cancel button
53 lyxerr << "Just hide the form and do nothing else!" << endl;
56 lyxerr << "Not implemented yet..." << endl;
59 lyxerr << "Unknown callback value!" << endl;
67 // This function is probably not good enough yet, and does need some
68 // arguemnts to tell what dir to start looking in.
70 static string current_figure_path = ".";
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");
82 fileDlg.SetButton(0, _("Clipart"), bufclip);
87 string p = fileDlg.Select(_("Graphics"),
91 if (p.empty()) return p;
93 current_figure_path = OnlyPath(p);
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 '%'."));
108 InsetGraphics::InsetGraphics()
113 int InsetGraphics::ascent(Painter &, LyXFont const &) const
120 int InsetGraphics::descent(Painter &, LyXFont const &) const
122 // this is not true if viewport is used and clip is not.
127 int InsetGraphics::width(Painter &, LyXFont const &) const
130 return bb.urx - bb.llx;
136 void InsetGraphics::draw(Painter & pain, LyXFont const & font,
137 int baseline, float & x) const
139 // This will draw the graphics. As for now we only draw a
140 // placeholder rectangele.
141 pain.rectangle(int(x), baseline - ascent(pain, font),
143 ascent(pain, font) + descent(pain, font));
147 void InsetGraphics::Edit(BufferView *, int, int, unsigned int)
149 lyxerr.debug() << "InsetGraphics::Edit" << endl;
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);
159 if (form->Graphics->visible) {
160 fl_raise_form(form->Graphics);
162 fl_show_form(form->Graphics, FL_PLACE_MOUSE | FL_PLACE_SIZE,
163 FL_FULLBORDER, _("Graphics"));
168 Inset::EDITABLE InsetGraphics::Editable() const
174 void InsetGraphics::Write(ostream & os) const
176 // The question on the file format is still open.
178 // perhaps a format that is xml-parsable
179 //<graphics name="test.eps"/>
184 void InsetGraphics::Read(LyXLex & /*lex*/)
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);
194 int InsetGraphics::Latex(ostream & os,
195 bool /*fragile*/, bool/*fs*/) const
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;
204 // We never used the starred form, we use the "clip" option instead.
205 string command("\\insetgraphics");
208 std::ostringstream options;
212 if (bb.isSet() && use_bb) {
214 << bb.llx << " " << bb.lly << " "
215 << bb.urx << " " << bb.ury << ",";
218 options << "hiresbb,";
220 if (viewport.isSet()) {
221 options << "viewport="
222 << viewport.llx << " " << viewport.lly << " "
223 << viewport.urx << " " << viewport.ury << ",";
227 << trim.llx << " " << trim.lly << " "
228 << trim.urx << " " << trim.ury << ",";
230 if (natheight.value() == 0) {
231 options << "natheight=" << natheight.asString() << ",";
233 if (natwidth.value() == 0) {
234 options << "natwidth=" << natwidth.asString() << ",";
237 options << "angle=" << angle << ",";
239 if (origin != DEFAULT) {
243 options << "origin=lt,";
246 options << "origin=lc,";
249 options << "origin=lB,";
252 options << "origin=lb,";
255 options << "origin=ct,";
258 options << "origin=c,";
261 options << "origin=cB,";
264 options << "origin=cb,";
267 options << "origin=rt,";
270 options << "origin=rc,";
273 options << "origin=rB,";
276 options << "origin=rb,";
280 if (g_width.value() != 0) {
281 options << "width=" << g_width.asString() << ",";
283 if (g_height.value() != 0) {
284 options << "height=" << g_height.asString() << ",";
286 if (totalheight.value() != 0) {
287 options << "totalheight=" << totalheight.asString() << ",";
289 if (keepaspectratio) {
290 options << "keepaspectratio,";
293 options << "scale=" << scale << ",";
302 options << "type=" << type << ",";
305 options << "ext=" << type << ",";
308 options << "read=" << type << ",";
310 if (!command.empty()) {
311 options << "command=" << type << ",";
314 string opts(options.str().c_str());
317 char * tmp = options.str();
321 opts = strip(opts, ',');
328 command += graphicsfile;
331 os << command << '\n';
337 int InsetGraphics::Ascii(ostream &) const
343 int InsetGraphics::Linuxdoc(ostream &) const
349 int InsetGraphics::DocBook(ostream &) const
355 void InsetGraphics::Validate(LaTeXFeatures & /*features*/) const
357 //features.graphicx = true;
361 Inset * InsetGraphics::Clone() const
363 return new InsetGraphics;