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"
27 extern string system_lyxdir;
28 extern string user_lyxdir;
29 extern string system_tempdir;
33 extern "C" void GraphicxCB(FL_OBJECT * obj, long arg)
35 lyxerr << "GraphicxCB: obj = " << obj << " arg = " << arg << endl;
37 case 0: // The graphics file
38 lyxerr << "Set the graphics file in InsetGraphics" << endl;
40 case 1: // The file browser
43 case 2: // The Apply button
44 lyxerr << "Scan the form and set the "
45 "InsetGraphics accordingly." << endl;
47 case 3: // The OK button
48 GraphicxCB(obj, 2); // do the apply
49 GraphicxCB(obj, 4); // do the cancel
51 case 4: // The Cancel button
52 lyxerr << "Just hide the form and do nothing else!" << endl;
55 lyxerr << "Not implemented yet..." << endl;
58 lyxerr << "Unknown callback value!" << endl;
66 // This function is probably not good enough yet, and does need some
67 // arguemnts to tell what dir to start looking in.
69 static string current_figure_path = ".";
73 // Does user clipart directory exist?
74 string bufclip = AddName (user_lyxdir, "clipart");
75 FileInfo fileInfo(bufclip);
76 if (!(fileInfo.isOK() && fileInfo.isDir()))
77 // No - bail out to system clipart directory
78 bufclip = AddName (system_lyxdir, "clipart");
81 fileDlg.SetButton(0, _("Clipart"), bufclip);
86 string p = fileDlg.Select(_("Graphics"),
90 if (p.empty()) return p;
92 current_figure_path = OnlyPath(p);
94 if (p.find_first_of("#~$% ") != string::npos) {
95 WriteAlert(_("Filename can't contain any "
96 "of these characters:"),
97 // xgettext:no-c-format
98 _("space, '#', '~', '$' or '%'."));
107 InsetGraphics::InsetGraphics()
112 int InsetGraphics::ascent(Painter &, LyXFont const &) const
119 int InsetGraphics::descent(Painter &, LyXFont const &) const
121 // this is not true if viewport is used and clip is not.
126 int InsetGraphics::width(Painter &, LyXFont const &) const
129 return bb.urx - bb.llx;
135 void InsetGraphics::draw(Painter & pain, LyXFont const & font,
136 int baseline, float & x) const
138 // This will draw the graphics. As for now we only draw a
139 // placeholder rectangele.
140 pain.rectangle(x, baseline - ascent(pain, font),
142 ascent(pain, font) + descent(pain, font));
146 void InsetGraphics::Edit(BufferView *, int, int, unsigned int)
148 lyxerr.debug() << "InsetGraphics::Edit" << endl;
151 form = create_form_Graphics();
152 fl_set_form_atclose(form->Graphics, CancelCloseBoxCB, 0);
153 fl_set_object_return(form->Angle, FL_RETURN_ALWAYS);
154 fl_set_object_return(form->Width, FL_RETURN_ALWAYS);
155 fl_set_object_return(form->Height, FL_RETURN_ALWAYS);
158 if (form->Graphics->visible) {
159 fl_raise_form(form->Graphics);
161 fl_show_form(form->Graphics, FL_PLACE_MOUSE | FL_PLACE_SIZE,
162 FL_FULLBORDER, _("Graphics"));
167 Inset::EDITABLE InsetGraphics::Editable() const
173 void InsetGraphics::Write(ostream & os) const
175 // The question on the file format is still open.
177 // perhaps a format that is xml-parsable
178 //<graphics name="test.eps"/>
183 void InsetGraphics::Read(LyXLex & /*lex*/)
185 // For now we only use a static file...
186 graphicsfile = "testfile.xpm";
187 //graphicscache.addFile(graphicsfile);
188 //bb = graphicscache.getBB(graphicsfile);
189 //pixmap = graphicscache.getPixmap(graphicsfile);
193 int InsetGraphics::Latex(ostream & os,
194 signed char /*fragile*/, bool/*fs*/) const
196 // MISSING: We have to decide how to do the order of the options
197 // that is depentant of order, like witdth, height, andlge. Should
198 // we rotate before scale? Should we let the user decide?
199 // bool rot_before_scale; ?
200 // Nothing to do if we don't have a graphics file
201 if (graphicsfile.empty()) return 0;
203 // We never used the starred form, we use the "clip" option instead.
204 string command("\\insetgraphics");
207 ostringstream options;
211 if (bb.isSet() && use_bb) {
213 << bb.llx << " " << bb.lly << " "
214 << bb.urx << " " << bb.ury << ",";
217 options << "hiresbb,";
219 if (viewport.isSet()) {
220 options << "viewport="
221 << viewport.llx << " " << viewport.lly << " "
222 << viewport.urx << " " << viewport.ury << ",";
226 << trim.llx << " " << trim.lly << " "
227 << trim.urx << " " << trim.ury << ",";
229 if (natheight.value() == 0) {
230 options << "natheight=" << natheight.asString() << ",";
232 if (natwidth.value() == 0) {
233 options << "natwidth=" << natwidth.asString() << ",";
236 options << "angle=" << angle << ",";
238 if (origin != DEFAULT) {
242 options << "origin=lt,";
245 options << "origin=lc,";
248 options << "origin=lB,";
251 options << "origin=lb,";
254 options << "origin=ct,";
257 options << "origin=c,";
260 options << "origin=cB,";
263 options << "origin=cb,";
266 options << "origin=rt,";
269 options << "origin=rc,";
272 options << "origin=rB,";
275 options << "origin=rb,";
279 if (g_width.value() != 0) {
280 options << "width=" << g_width.asString() << ",";
282 if (g_height.value() != 0) {
283 options << "height=" << g_height.asString() << ",";
285 if (totalheight.value() != 0) {
286 options << "totalheight=" << totalheight.asString() << ",";
288 if (keepaspectratio) {
289 options << "keepaspectratio,";
292 options << "scale=" << scale << ",";
301 options << "type=" << type << ",";
304 options << "ext=" << type << ",";
307 options << "read=" << type << ",";
309 if (!command.empty()) {
310 options << "command=" << type << ",";
313 string opts(options.str().c_str());
316 char * tmp = options.str();
320 opts = strip(opts, ',');
327 command += graphicsfile;
330 os << command << '\n';
336 int InsetGraphics::Linuxdoc(ostream &) const
342 int InsetGraphics::DocBook(ostream &) const
348 void InsetGraphics::Validate(LaTeXFeatures & /*features*/) const
350 //features.graphicx = true;
354 Inset * InsetGraphics::Clone() const
356 return new InsetGraphics;