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;
35 extern "C" void GraphicxCB(FL_OBJECT * obj, long arg)
37 lyxerr << "GraphicxCB: obj = " << obj << " arg = " << arg << endl;
39 case 0: // The graphics file
40 lyxerr << "Set the graphics file in InsetGraphics" << endl;
42 case 1: // The file browser
45 case 2: // The Apply button
46 lyxerr << "Scan the form and set the "
47 "InsetGraphics accordingly." << endl;
49 case 3: // The OK button
50 GraphicxCB(obj, 2); // do the apply
51 GraphicxCB(obj, 4); // do the cancel
53 case 4: // The Cancel button
54 lyxerr << "Just hide the form and do nothing else!" << endl;
57 lyxerr << "Not implemented yet..." << endl;
60 lyxerr << "Unknown callback value!" << endl;
68 // This function is probably not good enough yet, and does need some
69 // arguemnts to tell what dir to start looking in.
71 static string current_figure_path = ".";
75 // Does user clipart directory exist?
76 string bufclip = AddName (user_lyxdir, "clipart");
77 FileInfo fileInfo(bufclip);
78 if (!(fileInfo.isOK() && fileInfo.isDir()))
79 // No - bail out to system clipart directory
80 bufclip = AddName (system_lyxdir, "clipart");
83 fileDlg.SetButton(0, _("Clipart"), bufclip);
88 string p = fileDlg.Select(_("Graphics"),
92 if (p.empty()) return p;
94 current_figure_path = OnlyPath(p);
96 if (p.find_first_of("#~$% ") != string::npos) {
97 WriteAlert(_("Filename can't contain any "
98 "of these characters:"),
99 // xgettext:no-c-format
100 _("space, '#', '~', '$' or '%'."));
109 InsetGraphics::InsetGraphics()
114 int InsetGraphics::ascent(Painter &, LyXFont const &) const
121 int InsetGraphics::descent(Painter &, LyXFont const &) const
123 // this is not true if viewport is used and clip is not.
128 int InsetGraphics::width(Painter &, LyXFont const &) const
131 return bb.urx - bb.llx;
137 void InsetGraphics::draw(BufferView * bv, LyXFont const & font,
138 int baseline, float & x, bool) const
140 Painter & pain = bv->painter();
142 // This will draw the graphics. As for now we only draw a
143 // placeholder rectangele.
144 pain.rectangle(int(x), baseline - ascent(pain, font),
146 ascent(pain, font) + descent(pain, font));
150 void InsetGraphics::Edit(BufferView *, int, int, unsigned int)
152 lyxerr.debug() << "InsetGraphics::Edit" << endl;
155 form = create_form_Graphics();
156 fl_set_form_atclose(form->Graphics, CancelCloseBoxCB, 0);
157 fl_set_object_return(form->Angle, FL_RETURN_ALWAYS);
158 fl_set_object_return(form->Width, FL_RETURN_ALWAYS);
159 fl_set_object_return(form->Height, FL_RETURN_ALWAYS);
162 if (form->Graphics->visible) {
163 fl_raise_form(form->Graphics);
165 fl_show_form(form->Graphics, FL_PLACE_MOUSE | FL_PLACE_SIZE,
166 FL_FULLBORDER, _("Graphics"));
171 Inset::EDITABLE InsetGraphics::Editable() const
177 void InsetGraphics::Write(Buffer const *, ostream & os) const
179 // The question on the file format is still open.
181 // perhaps a format that is xml-parsable
182 //<graphics name="test.eps"/>
187 void InsetGraphics::Read(Buffer const *, LyXLex & /*lex*/)
189 // For now we only use a static file...
190 graphicsfile = "testfile.xpm";
191 //graphicscache.addFile(graphicsfile);
192 //bb = graphicscache.getBB(graphicsfile);
193 //pixmap = graphicscache.getPixmap(graphicsfile);
197 int InsetGraphics::Latex(Buffer const *, ostream & os,
198 bool /*fragile*/, bool/*fs*/) const
200 // MISSING: We have to decide how to do the order of the options
201 // that is depentant of order, like witdth, height, andlge. Should
202 // we rotate before scale? Should we let the user decide?
203 // bool rot_before_scale; ?
204 // Nothing to do if we don't have a graphics file
205 if (graphicsfile.empty()) return 0;
207 // We never used the starred form, we use the "clip" option instead.
208 string command("\\insetgraphics");
211 std::ostringstream options;
215 if (bb.isSet() && use_bb) {
217 << bb.llx << " " << bb.lly << " "
218 << bb.urx << " " << bb.ury << ",";
221 options << "hiresbb,";
223 if (viewport.isSet()) {
224 options << "viewport="
225 << viewport.llx << " " << viewport.lly << " "
226 << viewport.urx << " " << viewport.ury << ",";
230 << trim.llx << " " << trim.lly << " "
231 << trim.urx << " " << trim.ury << ",";
233 if (natheight.value() == 0) {
234 options << "natheight=" << natheight.asString() << ",";
236 if (natwidth.value() == 0) {
237 options << "natwidth=" << natwidth.asString() << ",";
240 options << "angle=" << angle << ",";
242 if (origin != DEFAULT) {
246 options << "origin=lt,";
249 options << "origin=lc,";
252 options << "origin=lB,";
255 options << "origin=lb,";
258 options << "origin=ct,";
261 options << "origin=c,";
264 options << "origin=cB,";
267 options << "origin=cb,";
270 options << "origin=rt,";
273 options << "origin=rc,";
276 options << "origin=rB,";
279 options << "origin=rb,";
283 if (g_width.value() != 0) {
284 options << "width=" << g_width.asString() << ",";
286 if (g_height.value() != 0) {
287 options << "height=" << g_height.asString() << ",";
289 if (totalheight.value() != 0) {
290 options << "totalheight=" << totalheight.asString() << ",";
292 if (keepaspectratio) {
293 options << "keepaspectratio,";
296 options << "scale=" << scale << ",";
305 options << "type=" << type << ",";
308 options << "ext=" << type << ",";
311 options << "read=" << type << ",";
313 if (!command.empty()) {
314 options << "command=" << type << ",";
317 string opts(options.str().c_str());
320 char * tmp = options.str();
324 opts = strip(opts, ',');
331 command += graphicsfile;
334 os << command << '\n';
340 int InsetGraphics::Ascii(Buffer const *, ostream &) const
346 int InsetGraphics::Linuxdoc(Buffer const *, ostream &) const
352 int InsetGraphics::DocBook(Buffer const *, ostream &) const
358 void InsetGraphics::Validate(LaTeXFeatures & /*features*/) const
360 //features.graphicx = true;
364 Inset * InsetGraphics::Clone() const
366 return new InsetGraphics;