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"
18 #include "BufferView.h"
20 #include "form_graphics.h"
21 #include "lyx_gui_misc.h"
23 #include "support/FileInfo.h"
24 #include "support/filetools.h"
29 extern string system_lyxdir;
30 extern string user_lyxdir;
31 extern string system_tempdir;
36 void GraphicxCB(FL_OBJECT * obj, long arg)
38 lyxerr << "GraphicxCB: obj = " << obj << " arg = " << arg << endl;
40 case 0: // The graphics file
41 lyxerr << "Set the graphics file in InsetGraphics" << endl;
43 case 1: // The file browser
46 case 2: // The Apply button
47 lyxerr << "Scan the form and set the "
48 "InsetGraphics accordingly." << endl;
50 case 3: // The OK button
51 GraphicxCB(obj, 2); // do the apply
52 GraphicxCB(obj, 4); // do the cancel
54 case 4: // The Cancel button
55 lyxerr << "Just hide the form and do nothing else!" << endl;
58 lyxerr << "Not implemented yet..." << endl;
61 lyxerr << "Unknown callback value!" << endl;
69 // This function is probably not good enough yet, and does need some
70 // arguemnts to tell what dir to start looking in.
72 static string current_figure_path = ".";
76 // Does user clipart directory exist?
77 string bufclip = AddName (user_lyxdir, "clipart");
78 FileInfo fileInfo(bufclip);
79 if (!(fileInfo.isOK() && fileInfo.isDir()))
80 // No - bail out to system clipart directory
81 bufclip = AddName (system_lyxdir, "clipart");
84 fileDlg.SetButton(0, _("Clipart"), bufclip);
89 string p = fileDlg.Select(_("Graphics"),
93 if (p.empty()) return p;
95 current_figure_path = OnlyPath(p);
97 if (p.find_first_of("#~$% ") != string::npos) {
98 WriteAlert(_("Filename can't contain any "
99 "of these characters:"),
100 // xgettext:no-c-format
101 _("space, '#', '~', '$' or '%'."));
110 InsetGraphics::InsetGraphics()
115 int InsetGraphics::ascent(BufferView *, LyXFont const &) const
122 int InsetGraphics::descent(BufferView *, LyXFont const &) const
124 // this is not true if viewport is used and clip is not.
129 int InsetGraphics::width(BufferView *, LyXFont const &) const
132 return bb.urx - bb.llx;
138 void InsetGraphics::draw(BufferView * bv, LyXFont const & font,
139 int baseline, float & x, bool) const
141 Painter & pain = bv->painter();
143 // This will draw the graphics. As for now we only draw a
144 // placeholder rectangele.
145 pain.rectangle(int(x), baseline - ascent(bv, font),
147 ascent(bv, font) + descent(bv, font));
151 void InsetGraphics::Edit(BufferView *, int, int, unsigned int)
153 lyxerr.debug() << "InsetGraphics::Edit" << endl;
156 form = create_form_Graphics();
157 fl_set_form_atclose(form->Graphics, CancelCloseBoxCB, 0);
158 fl_set_object_return(form->Angle, FL_RETURN_ALWAYS);
159 fl_set_object_return(form->Width, FL_RETURN_ALWAYS);
160 fl_set_object_return(form->Height, FL_RETURN_ALWAYS);
163 if (form->Graphics->visible) {
164 fl_raise_form(form->Graphics);
166 fl_show_form(form->Graphics, FL_PLACE_MOUSE | FL_PLACE_SIZE,
167 FL_FULLBORDER, _("Graphics"));
172 Inset::EDITABLE InsetGraphics::Editable() const
178 void InsetGraphics::Write(Buffer const *, ostream & os) const
180 // The question on the file format is still open.
182 // perhaps a format that is xml-parsable
183 //<graphics name="test.eps"/>
188 void InsetGraphics::Read(Buffer const *, LyXLex & /*lex*/)
190 // For now we only use a static file...
191 graphicsfile = "testfile.xpm";
192 //graphicscache.addFile(graphicsfile);
193 //bb = graphicscache.getBB(graphicsfile);
194 //pixmap = graphicscache.getPixmap(graphicsfile);
198 int InsetGraphics::Latex(Buffer const *, ostream & os,
199 bool /*fragile*/, bool/*fs*/) const
201 // MISSING: We have to decide how to do the order of the options
202 // that is depentant of order, like witdth, height, andlge. Should
203 // we rotate before scale? Should we let the user decide?
204 // bool rot_before_scale; ?
205 // Nothing to do if we don't have a graphics file
206 if (graphicsfile.empty()) return 0;
208 // We never used the starred form, we use the "clip" option instead.
209 string command("\\insetgraphics");
212 std::ostringstream options;
216 if (bb.isSet() && use_bb) {
218 << bb.llx << " " << bb.lly << " "
219 << bb.urx << " " << bb.ury << ",";
222 options << "hiresbb,";
224 if (viewport.isSet()) {
225 options << "viewport="
226 << viewport.llx << " " << viewport.lly << " "
227 << viewport.urx << " " << viewport.ury << ",";
231 << trim.llx << " " << trim.lly << " "
232 << trim.urx << " " << trim.ury << ",";
234 if (natheight.value() == 0) {
235 options << "natheight=" << natheight.asString() << ",";
237 if (natwidth.value() == 0) {
238 options << "natwidth=" << natwidth.asString() << ",";
241 options << "angle=" << angle << ",";
243 if (origin != DEFAULT) {
247 options << "origin=lt,";
250 options << "origin=lc,";
253 options << "origin=lB,";
256 options << "origin=lb,";
259 options << "origin=ct,";
262 options << "origin=c,";
265 options << "origin=cB,";
268 options << "origin=cb,";
271 options << "origin=rt,";
274 options << "origin=rc,";
277 options << "origin=rB,";
280 options << "origin=rb,";
284 if (g_width.value() != 0) {
285 options << "width=" << g_width.asString() << ",";
287 if (g_height.value() != 0) {
288 options << "height=" << g_height.asString() << ",";
290 if (totalheight.value() != 0) {
291 options << "totalheight=" << totalheight.asString() << ",";
293 if (keepaspectratio) {
294 options << "keepaspectratio,";
297 options << "scale=" << scale << ",";
306 options << "type=" << type << ",";
309 options << "ext=" << type << ",";
312 options << "read=" << type << ",";
314 if (!command.empty()) {
315 options << "command=" << type << ",";
318 string opts(options.str().c_str());
321 char * tmp = options.str();
325 opts = strip(opts, ',');
332 command += graphicsfile;
335 os << command << '\n';
341 int InsetGraphics::Ascii(Buffer const *, ostream &) const
347 int InsetGraphics::Linuxdoc(Buffer const *, ostream &) const
353 int InsetGraphics::DocBook(Buffer const *, ostream &) const
359 void InsetGraphics::Validate(LaTeXFeatures & /*features*/) const
361 //features.graphicx = true;
365 Inset * InsetGraphics::Clone() const
367 return new InsetGraphics;