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"
52 // FIXME: currently we need the second '|' to prevent mis-interpretation!
53 // All supported graphic formats with their file-extension and the
54 // gzip-ext for zipped (e)ps-files.
55 string const grfx_pattern =
56 "*.(agr|bmp|eps|epsi|fits|gif|jpg|obj|pdf|pbm|pgm|png|"
57 "ppm|ps|tif|tiff|xbm|xpm|xwd|gz)|";
62 ControlGraphics::ControlGraphics(LyXView & lv, Dialogs & d)
63 : ControlInset<InsetGraphics, InsetGraphicsParams>(lv, d)
65 d_.showGraphics.connect(SigC::slot(this, &ControlGraphics::showInset));
69 InsetGraphicsParams const ControlGraphics::getParams(string const &)
71 return InsetGraphicsParams();
75 InsetGraphicsParams const
76 ControlGraphics::getParams(InsetGraphics const & inset)
78 return inset.params();
82 void ControlGraphics::applyParamsToInset()
84 // Set the parameters in the inset, it also returns true if the new
85 // parameters are different from what was in the inset already.
86 bool changed = inset()->setParams(params(), lv_.buffer()->filePath());
87 // Tell LyX we've got a change, and mark the document dirty,
89 lv_.view()->updateInset(inset(), changed);
93 void ControlGraphics::applyParamsNoInset()
97 // We need these in the file browser.
98 extern string system_lyxdir;
99 extern string user_lyxdir;
102 string const ControlGraphics::Browse(string const & in_name)
104 string const title = _("Select graphics file");
106 // Does user clipart directory exist?
107 string clipdir = AddName (user_lyxdir, "clipart");
108 FileInfo fileInfo(clipdir);
109 if (!(fileInfo.isOK() && fileInfo.isDir()))
110 // No - bail out to system clipart directory
111 clipdir = AddName (system_lyxdir, "clipart");
112 pair<string, string> dir1(_("Clipart|#C#c"), clipdir);
113 pair<string, string> dir2(_("Documents|#o#O"), string(lyxrc.document_path));
114 // Show the file browser dialog
115 return browseRelFile(&lv_, in_name, lv_.buffer()->filePath(),
116 title, ::grfx_pattern, dir1, dir2);
120 string const ControlGraphics::readBB(string const & file)
122 string const abs_file = MakeAbsPath(file, lv_.buffer()->filePath());
124 string const from = getExtFromContents(abs_file);
125 // Check if we have a Postscript file, then it's easy
126 if (contains(from, "ps"))
127 return readBB_from_PSFile(abs_file);
129 // we don't, so ask the Graphics Cache if it has loaded the file
130 grfx::GCache & gc = grfx::GCache::get();
132 tostr(gc.raw_width(abs_file)) + ' ' +
133 tostr(gc.raw_height(abs_file)));
137 bool ControlGraphics::isFilenameValid(string const & fname) const
139 // It may be that the filename is relative.
140 string const name = MakeAbsPath(fname, lv_.buffer()->filePath());
141 return IsFileReadable(name);