/* This file is part of
- * ======================================================
+ * ======================================================
*
* LyX, The Document Processor
*
*
* \file ControlGraphics.C
* \author Angus Leeming <a.leeming@ic.ac.uk>
+ * \author Herbert Voss <voss@perce.de>
*/
-#include <config.h>
+#include <config.h>
+#include <fstream>
#ifdef __GNUG__
#pragma implementation
-#endif
+#endif
#include "ViewBase.h"
#include "ButtonControllerBase.h"
#include "ControlGraphics.h"
+#include "ControlInset.tmpl"
#include "buffer.h"
+#include "BufferView.h"
#include "Dialogs.h"
#include "LyXView.h"
#include "gettext.h"
+#include "lyxrc.h"
#include "insets/insetgraphics.h"
#include "insets/insetgraphicsParams.h" // need operator!=()
#include "support/FileInfo.h" // for FileInfo
-#include "helper_funcs.h" // for browseFile
-#include "support/filetools.h" // for AddName
+#include "helper_funcs.h"
+#include "support/lstrings.h"
+#include "support/filetools.h" // for AddName, zippedFile
using std::pair;
using std::make_pair;
+using std::ifstream;
ControlGraphics::ControlGraphics(LyXView & lv, Dialogs & d)
: ControlInset<InsetGraphics, InsetGraphicsParams>(lv, d)
InsetGraphicsParams const
ControlGraphics::getParams(InsetGraphics const & inset)
{
- return inset.getParams();
+ return inset.params();
}
extern string system_lyxdir;
extern string user_lyxdir;
+
string const ControlGraphics::Browse(string const & in_name)
{
- string const title = N_("Graphics");
- // FIXME: currently we need the second '|' to prevent mis-interpretation
- string const pattern = "*.(ps|png)|";
-
- // Does user clipart directory exist?
- string clipdir = AddName (user_lyxdir, "clipart");
- FileInfo fileInfo(clipdir);
- if (!(fileInfo.isOK() && fileInfo.isDir()))
- // No - bail out to system clipart directory
- clipdir = AddName (system_lyxdir, "clipart");
- pair<string, string> dir1(N_("Clipart"), clipdir);
-
+ string const title = N_("Select graphics file");
+ // FIXME: currently we need the second '|' to prevent mis-interpretation
+ string const pattern = "*.(ps|eps|png|jpeg|jpg|gif|gz)|";
+
+ // Does user clipart directory exist?
+ string clipdir = AddName (user_lyxdir, "clipart");
+ FileInfo fileInfo(clipdir);
+ if (!(fileInfo.isOK() && fileInfo.isDir()))
+ // No - bail out to system clipart directory
+ clipdir = AddName (system_lyxdir, "clipart");
+ pair<string, string> dir1(N_("Clipart|#C#c"), clipdir);
+ pair<string, string> dir2(N_("Documents|#o#O"), string(lyxrc.document_path));
// Show the file browser dialog
- return browseFile(&lv_, in_name, title, pattern, dir1,
- make_pair(string(), string()));
+ return browseRelFile(&lv_, in_name, lv_.buffer()->filePath(),
+ title, pattern, dir1, dir2);
+}
+
+
+string const ControlGraphics::readBB(string const & file)
+{
+// in a file it's an entry like %%BoundingBox:23 45 321 345
+// the first number can following without a space, so we have
+// to check a bit more.
+// on the other hand some plot programs write the bb at the
+// end of the file. Than we have in the header a
+// %%BoundingBox: (atend)
+// In this case we must check until the end.
+ string file_ = file;
+ if (zippedFile(file_))
+ file_ = unzipFile(file_);
+ std::ifstream is(file_.c_str());
+ if (!contains(getExtFromContents(file_),"ps")) // bb exists?
+ return string();
+ while (is) {
+ string s;
+ is >> s;
+ if (contains(s,"%%BoundingBox:")) {
+ string a, b, c, d;
+ is >> a >> b >> c >> d;
+ if (is && !contains(a,"atend")) { // bb at the end?
+ if (s != "%%BoundingBox:")
+ return (s.substr(14)+" "+a+" "+b+" "+c+" ");
+ else
+ return (a+" "+b+" "+c+" "+d+" ");
+ }
+ }
+ }
+ return string();
+}
+
+
+bool ControlGraphics::isFilenameValid(string const & fname) const
+{
+ // It may be that the filename is relative.
+ string const name = MakeAbsPath(fname, lv_.buffer()->filePath());
+ return IsFileReadable(name);
}