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"
25 extern string system_lyxdir;
26 extern string user_lyxdir;
27 extern string system_tempdir;
31 void GraphicxCB(FL_OBJECT * obj, long arg)
33 lyxerr << "GraphicxCB: obj = " << obj << " arg = " << arg << endl;
35 case 0: // The graphics file
36 lyxerr << "Set the graphics file in InsetGraphics" << endl;
38 case 1: // The file browser
41 case 2: // The Apply button
42 lyxerr << "Scan the form and set the "
43 "InsetGraphics accordingly." << endl;
45 case 3: // The OK button
46 GraphicxCB(obj, 2); // do the apply
47 GraphicxCB(obj, 4); // do the cancel
49 case 4: // The Cancel button
50 lyxerr << "Just hide the form and do nothing else!" << endl;
53 lyxerr << "Not implemented yet..." << endl;
56 lyxerr << "Unknown callback value!" << endl;
64 // This function is probably not good enough yet, and does need some
65 // arguemnts to tell what dir to start looking in.
67 static string current_figure_path = ".";
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");
79 fileDlg.SetButton(0, _("Clipart"), bufclip);
84 string p = fileDlg.Select(_("Graphics"),
88 if (p.empty()) return p;
90 current_figure_path = OnlyPath(p);
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 '%'."));
105 InsetGraphics::InsetGraphics()
110 int InsetGraphics::ascent(Painter &, LyXFont const &) const
117 int InsetGraphics::descent(Painter &, LyXFont const &) const
119 // this is not true if viewport is used and clip is not.
124 int InsetGraphics::width(Painter &, LyXFont const &) const
127 return bb.urx - bb.llx;
133 void InsetGraphics::draw(Painter & pain, LyXFont const & font,
134 int baseline, float & x) const
136 // This will draw the graphics. As for now we only draw a
137 // placeholder rectangele.
138 pain.rectangle(x, baseline - ascent(pain, font),
140 ascent(pain, font) + descent(pain, font));
144 void InsetGraphics::Edit(BufferView *, int, int, unsigned int)
146 lyxerr.debug() << "InsetGraphics::Edit" << endl;
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);
156 if (form->Graphics->visible) {
157 fl_raise_form(form->Graphics);
159 fl_show_form(form->Graphics, FL_PLACE_MOUSE | FL_PLACE_SIZE,
160 FL_FULLBORDER, _("Graphics"));
165 Inset::EDITABLE InsetGraphics::Editable() const
171 void InsetGraphics::Write(ostream & os) const
173 // The question on the file format is still open.
175 // perhaps a format that is xml-parsable
176 //<graphics name="test.eps"/>
181 void InsetGraphics::Read(LyXLex & /*lex*/)
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);
191 int InsetGraphics::Latex(ostream & os, signed char /*fragile*/, bool/*fs*/) const
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;
200 // We never used the starred form, we use the "clip" option instead.
201 string command("\\insetgraphics");
204 ostringstream options;
208 if (bb.isSet() && use_bb) {
210 << bb.llx << " " << bb.lly << " "
211 << bb.urx << " " << bb.ury << ",";
214 options << "hiresbb,";
216 if (viewport.isSet()) {
217 options << "viewport="
218 << viewport.llx << " " << viewport.lly << " "
219 << viewport.urx << " " << viewport.ury << ",";
223 << trim.llx << " " << trim.lly << " "
224 << trim.urx << " " << trim.ury << ",";
226 if (natheight.value() == 0) {
227 options << "natheight=" << natheight.asString() << ",";
229 if (natwidth.value() == 0) {
230 options << "natwidth=" << natwidth.asString() << ",";
233 options << "angle=" << angle << ",";
235 if (origin != DEFAULT) {
239 options << "origin=lt,";
242 options << "origin=lc,";
245 options << "origin=lB,";
248 options << "origin=lb,";
251 options << "origin=ct,";
254 options << "origin=c,";
257 options << "origin=cB,";
260 options << "origin=cb,";
263 options << "origin=rt,";
266 options << "origin=rc,";
269 options << "origin=rB,";
272 options << "origin=rb,";
276 if (g_width.value() != 0) {
277 options << "width=" << g_width.asString() << ",";
279 if (g_height.value() != 0) {
280 options << "height=" << g_height.asString() << ",";
282 if (totalheight.value() != 0) {
283 options << "totalheight=" << totalheight.asString() << ",";
285 if (keepaspectratio) {
286 options << "keepaspectratio,";
289 options << "scale=" << scale << ",";
298 options << "type=" << type << ",";
301 options << "ext=" << type << ",";
304 options << "read=" << type << ",";
306 if (!command.empty()) {
307 options << "command=" << type << ",";
310 string opts(options.str().c_str());
313 char * tmp = options.str();
317 opts = strip(opts, ',');
324 command += graphicsfile;
327 os << command << '\n';
333 int InsetGraphics::Linuxdoc(ostream &) const
339 int InsetGraphics::DocBook(ostream &) const
345 void InsetGraphics::Validate(LaTeXFeatures & /*features*/) const
347 //features.graphicx = true;
351 Inset * InsetGraphics::Clone() const
353 return new InsetGraphics;