1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 2001 The LyX Team.
8 * ======================================================
10 * \file ControlGraphics.C
11 * \author Angus Leeming <a.leeming@ic.ac.uk>
12 * \author Herbert Voss <voss@perce.de>
19 #pragma implementation
23 #include "ButtonControllerBase.h"
24 #include "ControlGraphics.h"
25 #include "ControlInset.tmpl"
27 #include "helper_funcs.h"
30 #include "BufferView.h"
36 #include "graphics/GraphicsCache.h"
38 #include "insets/insetgraphics.h"
39 #include "insets/insetgraphicsParams.h"
41 #include "support/lstrings.h"
42 #include "support/filetools.h"
43 #include "support/FileInfo.h"
50 ControlGraphics::ControlGraphics(LyXView & lv, Dialogs & d)
51 : ControlInset<InsetGraphics, InsetGraphicsParams>(lv, d)
53 d_.showGraphics.connect(SigC::slot(this, &ControlGraphics::showInset));
57 InsetGraphicsParams const ControlGraphics::getParams(string const &)
59 return InsetGraphicsParams();
63 InsetGraphicsParams const
64 ControlGraphics::getParams(InsetGraphics const & inset)
66 return inset.params();
70 void ControlGraphics::applyParamsToInset()
72 // Set the parameters in the inset, it also returns true if the new
73 // parameters are different from what was in the inset already.
74 bool changed = inset()->setParams(params(), lv_.buffer()->filePath());
75 // Tell LyX we've got a change, and mark the document dirty,
77 lv_.view()->updateInset(inset(), changed);
81 void ControlGraphics::applyParamsNoInset()
85 // We need these in the file browser.
86 extern string system_lyxdir;
87 extern string user_lyxdir;
90 string const ControlGraphics::Browse(string const & in_name)
92 string const title = _("Select graphics file");
93 // FIXME: currently we need the second '|' to prevent mis-interpretation
94 string const pattern = "*.(ps|eps|png|jpeg|jpg|gif|gz)|";
96 // Does user clipart directory exist?
97 string clipdir = AddName (user_lyxdir, "clipart");
98 FileInfo fileInfo(clipdir);
99 if (!(fileInfo.isOK() && fileInfo.isDir()))
100 // No - bail out to system clipart directory
101 clipdir = AddName (system_lyxdir, "clipart");
102 pair<string, string> dir1(_("Clipart|#C#c"), clipdir);
103 pair<string, string> dir2(_("Documents|#o#O"), string(lyxrc.document_path));
104 // Show the file browser dialog
105 return browseRelFile(&lv_, in_name, lv_.buffer()->filePath(),
106 title, pattern, dir1, dir2);
110 string const ControlGraphics::readBB(string const & file)
112 string const abs_file = MakeAbsPath(file, lv_.buffer()->filePath());
114 string const from = getExtFromContents(abs_file);
115 // Check if we have a Postscript file, then it's easy
116 if (contains(from, "ps"))
117 return readBB_from_PSFile(abs_file);
119 // we don't, so ask the Graphics Cache if it has loaded the file
120 grfx::GCache & gc = grfx::GCache::get();
122 tostr(gc.raw_width(abs_file)) + ' ' +
123 tostr(gc.raw_height(abs_file)));
127 bool ControlGraphics::isFilenameValid(string const & fname) const
129 // It may be that the filename is relative.
130 string const name = MakeAbsPath(fname, lv_.buffer()->filePath());
131 return IsFileReadable(name);