]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/controllers/ControlGraphics.C
fix crash due to invalidated iterator
[lyx.git] / src / frontends / controllers / ControlGraphics.C
index bdca7e622901e4b5a1428a55072ed98532b3c5bd..f409c02b0e0d2a715eb656e2ec2583a29e882115 100644 (file)
-/* This file is part of
- * ====================================================== 
- *
- *           LyX, The Document Processor
- *
- *           Copyright 2001 The LyX Team.
+/**
+ * \file ControlGraphics.C
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
  *
- * ======================================================
+ * \author Angus Leeming
+ * \author Herbert Voß
  *
- * \file ControlGraphics.C
- * \author Angus Leeming <a.leeming@ic.ac.uk>
- * \author Herbert Voss <voss@perce.de>
+ * Full author contact details are available in file CREDITS.
  */
 
-#include <config.h> 
-#include <fstream>
-
-#ifdef __GNUG__
-#pragma implementation
-#endif 
+#include <config.h>
 
-#include "ViewBase.h"
-#include "ButtonControllerBase.h"
 #include "ControlGraphics.h"
-#include "ControlInset.tmpl"
-#include "buffer.h"
-#include "Dialogs.h"
-#include "LyXView.h"
+
+#include "helper_funcs.h"
+
+#include "funcrequest.h"
 #include "gettext.h"
 #include "lyxrc.h"
 
+#include "graphics/GraphicsCache.h"
+#include "graphics/GraphicsCacheItem.h"
+#include "graphics/GraphicsImage.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/lstrings.h"
-#include "support/filetools.h" // for AddName, zippedFile
-#include "frontends/Alert.h"
-#include "BufferView.h"
+#include "support/convert.h"
+#include "support/filefilterlist.h"
+#include "support/filetools.h"
+#include "support/package.h"
+#include "support/types.h"
+
+#include <boost/filesystem/operations.hpp>
 
-using std::pair;
 using std::make_pair;
-using std::ifstream;
+using std::string;
+using std::pair;
+using std::vector;
+
+namespace fs = boost::filesystem;
 
-ControlGraphics::ControlGraphics(LyXView & lv, Dialogs & d)
-       : ControlInset<InsetGraphics, InsetGraphicsParams>(lv, d)
+namespace lyx {
+
+using support::AddName;
+using support::FileFilterList;
+using support::IsFileReadable;
+using support::MakeAbsPath;
+using support::package;
+using support::readBB_from_PSFile;
+
+namespace frontend {
+
+
+ControlGraphics::ControlGraphics(Dialog & parent)
+       : Dialog::Controller(parent)
+{}
+
+
+bool ControlGraphics::initialiseParams(string const & data)
 {
-       d_.showGraphics.connect(SigC::slot(this, &ControlGraphics::showInset));
+       InsetGraphicsParams params;
+       InsetGraphicsMailer::string2params(data, kernel().buffer(), params);
+       params_.reset(new InsetGraphicsParams(params));
+       return true;
 }
 
 
-InsetGraphicsParams const ControlGraphics::getParams(string const &)
+void ControlGraphics::clearParams()
 {
-       return InsetGraphicsParams();
+       params_.reset();
 }
 
 
-InsetGraphicsParams const
-ControlGraphics::getParams(InsetGraphics const & inset)
+void ControlGraphics::dispatchParams()
 {
-       return inset.params();
+       InsetGraphicsParams tmp_params(params());
+       string const lfun =
+               InsetGraphicsMailer::params2string(tmp_params, kernel().buffer());
+       kernel().dispatch(FuncRequest(getLfun(), lfun));
 }
 
 
-void ControlGraphics::applyParamsToInset()
+string const ControlGraphics::browse(string const & in_name) const
 {
-       // Set the parameters in the inset, it also returns true if the new
-       // parameters are different from what was in the inset already.
-       bool changed = inset()->setParams(params());
-       // Tell LyX we've got a change, and mark the document dirty,
-       // if it changed.
-       lv_.view()->updateInset(inset(), changed);
+       string const title = _("Select graphics file");
+
+       // Does user clipart directory exist?
+       string clipdir = AddName (package().user_support(), "clipart");
+       if (!(fs::exists(clipdir) && fs::is_directory(clipdir)))
+               // No - bail out to system clipart directory
+               clipdir = AddName (package().system_support(), "clipart");
+       pair<string, string> dir1(_("Clipart|#C#c"), clipdir);
+       pair<string, string> dir2(_("Documents|#o#O"), string(lyxrc.document_path));
+       // Show the file browser dialog
+       return browseRelFile(in_name, kernel().bufferFilepath(),
+                            title,
+                            FileFilterList(),
+                            false, dir1, dir2);
 }
 
 
-void ControlGraphics::applyParamsNoInset()
-{}
+string const ControlGraphics::readBB(string const & file)
+{
+       string const abs_file =
+               MakeAbsPath(file, kernel().bufferFilepath());
+
+       // try to get it from the file, if possible. Zipped files are
+       // unzipped in the readBB_from_PSFile-Function
+       string const bb = readBB_from_PSFile(abs_file);
+       if (!bb.empty())
+               return bb;
+
+       // we don't, so ask the Graphics Cache if it has loaded the file
+       int width = 0;
+       int height = 0;
+
+       graphics::Cache & gc = graphics::Cache::get();
+       if (gc.inCache(abs_file)) {
+               graphics::Image const * image = gc.item(abs_file)->image();
+
+               if (image) {
+                       width  = image->getWidth();
+                       height = image->getHeight();
+               }
+       }
 
+       return ("0 0 " + convert<string>(width) + ' ' + convert<string>(height));
+}
 
-// We need these in the file browser.
-extern string system_lyxdir;
-extern string user_lyxdir;
 
-string const ControlGraphics::Browse(string const & in_name)
+bool ControlGraphics::isFilenameValid(string const & fname) const
 {
-       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, dir2);
+       // It may be that the filename is relative.
+       string const name = MakeAbsPath(fname, kernel().bufferFilepath());
+       return IsFileReadable(name);
 }
 
-string const ControlGraphics::readBB(string const & file)
+
+void ControlGraphics::editGraphics()
 {
-// 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();
+       BOOST_ASSERT(params_.get());
+
+       dialog().view().apply();
+       string const lfun =
+               InsetGraphicsMailer::params2string(params(), kernel().buffer());
+       kernel().dispatch(FuncRequest(LFUN_GRAPHICS_EDIT, lfun));
 }
 
 
-void ControlGraphics::checkFilename(string const & fname) {
-       if (!IsFileReadable(fname))
-               Alert::alert(_("Warning!"),
-                            _("Filename") + ' '
-                            + fname + _(" does not exist!"));
+namespace {
+
+char const * const bb_units[] = { "bp", "cm", "mm", "in" };
+size_t const bb_size = sizeof(bb_units) / sizeof(char *);
+
+// These are the strings that are stored in the LyX file and which
+// correspond to the LaTeX identifiers shown in the comments at the
+// end of each line.
+char const * const rorigin_lyx_strs[] = {
+       // the LaTeX default is leftBaseline
+       "",
+       "leftTop",  "leftBottom", "leftBaseline", // lt lb lB
+       "center", "centerTop", "centerBottom", "centerBaseline", // c ct cb cB
+       "rightTop", "rightBottom", "rightBaseline" }; // rt rb rB
+
+// These are the strings, corresponding to the above, that the GUI should
+// use. Note that they can/should be translated.
+char const * const rorigin_gui_strs[] = {
+       N_("Default"),
+       N_("Top left"), N_("Bottom left"), N_("Baseline left"),
+       N_("Center"), N_("Top center"), N_("Bottom center"), N_("Baseline center"),
+       N_("Top right"), N_("Bottom right"), N_("Baseline right") };
 
+size_t const rorigin_size = sizeof(rorigin_lyx_strs) / sizeof(char *);
+
+} // namespace anon
+
+
+vector<string> const getBBUnits()
+{
+       return vector<string> (bb_units, bb_units + bb_size);
 }
+
+
+vector<RotationOriginPair> getRotationOriginData()
+{
+       static vector<RotationOriginPair> data;
+       if (!data.empty())
+               return data;
+
+       data.resize(rorigin_size);
+       for (size_type i = 0; i < rorigin_size; ++i) {
+               data[i] = make_pair(_(rorigin_gui_strs[i]),
+                                   rorigin_lyx_strs[i]);
+       }
+
+       return data;
+}
+
+} // namespace frontend
+} // namespace lyx