]> 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 cfd8f19ebe82401bdeb0a1ac9f3cb97d0e9c3730..f409c02b0e0d2a715eb656e2ec2583a29e882115 100644 (file)
@@ -4,9 +4,9 @@
  * Licence details can be found in the file COPYING.
  *
  * \author Angus Leeming
- * \author Herbert Voss
+ * \author Herbert Voß
  *
- * Full author contact details are available in file CREDITS
+ * Full author contact details are available in file CREDITS.
  */
 
 #include <config.h>
@@ -15,8 +15,6 @@
 
 #include "helper_funcs.h"
 
-#include "buffer.h"
-#include "BufferView.h"
 #include "funcrequest.h"
 #include "gettext.h"
 #include "lyxrc.h"
 #include "graphics/GraphicsImage.h"
 
 #include "insets/insetgraphics.h"
-#include "insets/insetgraphicsParams.h"
 
-#include "support/tostr.h"
+#include "support/convert.h"
+#include "support/filefilterlist.h"
 #include "support/filetools.h"
-#include "support/FileInfo.h"
+#include "support/package.h"
+#include "support/types.h"
 
-using namespace lyx::support;
+#include <boost/filesystem/operations.hpp>
 
-using std::pair;
 using std::make_pair;
+using std::string;
+using std::pair;
 using std::vector;
 
-// We need these in the file browser.
-extern string system_lyxdir;
-extern string user_lyxdir;
+namespace fs = boost::filesystem;
+
+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)
@@ -51,11 +60,8 @@ ControlGraphics::ControlGraphics(Dialog & parent)
 bool ControlGraphics::initialiseParams(string const & data)
 {
        InsetGraphicsParams params;
-       InsetGraphicsMailer::string2params(data, params);
+       InsetGraphicsMailer::string2params(data, kernel().buffer(), params);
        params_.reset(new InsetGraphicsParams(params));
-       // make relative for good UI
-       params_->filename = MakeRelPath(params_->filename,
-               kernel().buffer()->filePath());
        return true;
 }
 
@@ -69,36 +75,35 @@ void ControlGraphics::clearParams()
 void ControlGraphics::dispatchParams()
 {
        InsetGraphicsParams tmp_params(params());
-       // core requires absolute path during runtime
-       tmp_params.filename = MakeAbsPath(tmp_params.filename,
-               kernel().buffer()->filePath());
-       string const lfun = InsetGraphicsMailer::params2string(tmp_params);
-       kernel().dispatch(FuncRequest(LFUN_INSET_APPLY, lfun));
+       string const lfun =
+               InsetGraphicsMailer::params2string(tmp_params, kernel().buffer());
+       kernel().dispatch(FuncRequest(getLfun(), lfun));
 }
 
 
-string const ControlGraphics::Browse(string const & in_name)
+string const ControlGraphics::browse(string const & in_name) const
 {
        string const title = _("Select graphics file");
 
        // Does user clipart directory exist?
-       string clipdir = AddName (user_lyxdir, "clipart");
-       FileInfo fileInfo(clipdir);
-       if (!(fileInfo.isOK() && fileInfo.isDir()))
+       string clipdir = AddName (package().user_support(), "clipart");
+       if (!(fs::exists(clipdir) && fs::is_directory(clipdir)))
                // No - bail out to system clipart directory
-               clipdir = AddName (system_lyxdir, "clipart");
+               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().buffer()->filePath(),
-                            title, "*.*", false, dir1, dir2);
+       return browseRelFile(in_name, kernel().bufferFilepath(),
+                            title,
+                            FileFilterList(),
+                            false, dir1, dir2);
 }
 
 
 string const ControlGraphics::readBB(string const & file)
 {
        string const abs_file =
-               MakeAbsPath(file, kernel().buffer()->filePath());
+               MakeAbsPath(file, kernel().bufferFilepath());
 
        // try to get it from the file, if possible. Zipped files are
        // unzipped in the readBB_from_PSFile-Function
@@ -110,9 +115,9 @@ string const ControlGraphics::readBB(string const & file)
        int width = 0;
        int height = 0;
 
-       lyx::graphics::Cache & gc = lyx::graphics::Cache::get();
+       graphics::Cache & gc = graphics::Cache::get();
        if (gc.inCache(abs_file)) {
-               lyx::graphics::Image const * image = gc.item(abs_file)->image();
+               graphics::Image const * image = gc.item(abs_file)->image();
 
                if (image) {
                        width  = image->getWidth();
@@ -120,19 +125,28 @@ string const ControlGraphics::readBB(string const & file)
                }
        }
 
-       return ("0 0 " + tostr(width) + ' ' + tostr(height));
+       return ("0 0 " + convert<string>(width) + ' ' + convert<string>(height));
 }
 
 
 bool ControlGraphics::isFilenameValid(string const & fname) const
 {
        // It may be that the filename is relative.
-       string const name = MakeAbsPath(fname, kernel().buffer()->filePath());
+       string const name = MakeAbsPath(fname, kernel().bufferFilepath());
        return IsFileReadable(name);
 }
 
 
-namespace frnt {
+void ControlGraphics::editGraphics()
+{
+       BOOST_ASSERT(params_.get());
+
+       dialog().view().apply();
+       string const lfun =
+               InsetGraphicsMailer::params2string(params(), kernel().buffer());
+       kernel().dispatch(FuncRequest(LFUN_GRAPHICS_EDIT, lfun));
+}
+
 
 namespace {
 
@@ -153,9 +167,9 @@ char const * const rorigin_lyx_strs[] = {
 // use. Note that they can/should be translated.
 char const * const rorigin_gui_strs[] = {
        N_("Default"),
-       N_("Top left"), N_("Bottom left"), N_("Left baseline"),
-       N_("Center"), N_("Top center"), N_("Bottom center"), N_("Center baseline"),
-       N_("Top right"), N_("Bottom right"), N_("Right baseline") };
+       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 *);
 
@@ -175,7 +189,7 @@ vector<RotationOriginPair> getRotationOriginData()
                return data;
 
        data.resize(rorigin_size);
-       for (lyx::size_type i = 0; i < rorigin_size; ++i) {
+       for (size_type i = 0; i < rorigin_size; ++i) {
                data[i] = make_pair(_(rorigin_gui_strs[i]),
                                    rorigin_lyx_strs[i]);
        }
@@ -183,4 +197,5 @@ vector<RotationOriginPair> getRotationOriginData()
        return data;
 }
 
-} // namespace frnt
+} // namespace frontend
+} // namespace lyx