> Here is a question for you: Do you want to display true, embedded
> filename, or the may-not-exist external filename in the graphics
> dialog? The attached patch does the former, but in a ugly way:
>

Attached is an updated patch, which is even uglier because it tries to
display 'Embedded:/filename' in the graphic dialog when the file is
embedded. As you can imagine, back and forth translations are needed.

Bo
Index: src/insets/InsetGraphics.cpp
===================================================================
--- src/insets/InsetGraphics.cpp	(revision 20269)
+++ src/insets/InsetGraphics.cpp	(working copy)
@@ -248,8 +248,6 @@
 	params_.filename.set(file.availableFile(&buf), buf.filePath());
 	LYXERR(Debug::FILES) << " to " 
 		<< params_.filename.toFilesystemEncoding() << std::endl;
-	// FIXME: graphics dialog is not updated even if the underlying
-	// filename is updated. What should I do?
 }
 
 
Index: src/insets/InsetGraphicsParams.cpp
===================================================================
--- src/insets/InsetGraphicsParams.cpp	(revision 20269)
+++ src/insets/InsetGraphicsParams.cpp	(working copy)
@@ -157,6 +157,9 @@
 
 	if (!filename.empty()) {
 		// when we save, we still use the original filename
+		// This ensures that the external filename will be written 
+		// to .lyx file, but causes trouble in graphics dialog update
+		// In that dialog, true filename has to be displayed.
 		EmbeddedFiles::EmbeddedFileList::const_iterator it = 
 			buffer.embeddedFiles().find(filename.toFilesystemEncoding());
 		if (it != buffer.embeddedFiles().end())
Index: src/frontends/qt4/GuiGraphics.cpp
===================================================================
--- src/frontends/qt4/GuiGraphics.cpp	(revision 20269)
+++ src/frontends/qt4/GuiGraphics.cpp	(working copy)
@@ -56,6 +56,9 @@
 
 
 namespace lyx {
+
+using support::DocFileName;
+
 namespace frontend {
 
 
@@ -253,7 +256,7 @@
 void GuiGraphicsDialog::on_browsePB_clicked()
 {
 	docstring const str =
-		controller().browse(qstring_to_ucs4(filename->text()));
+		controller().browse(controller().translateFileName(qstring_to_ucs4(filename->text())));
 	if(!str.empty()){
 		filename->setText(toqstr(str));
 		changed();
@@ -422,9 +425,8 @@
 			break;
 	}
 
-	string const name =
-		igp.filename.outputFilename(controller().bufferFilepath());
-	filename->setText(toqstr(name));
+	// this file may be embedded
+	filename->setText(toqstr(controller().displayFileName()));
 
 	// set the bounding box values
 	if (igp.bb.empty()) {
@@ -569,7 +571,7 @@
 {
 	InsetGraphicsParams & igp = controller().params();
 
-	igp.filename.set(internal_path(fromqstr(filename->text())),
+	igp.filename.set(internal_path(controller().translateFileName(fromqstr(filename->text()))),
 			 controller().bufferFilepath());
 
 	// the bb section
@@ -663,7 +665,7 @@
 
 void GuiGraphicsDialog::getBB()
 {
-	string const fn = fromqstr(filename->text());
+	string const fn = controller().translateFileName(fromqstr(filename->text()));
 	if (!fn.empty()) {
 		string const bb = controller().readBB(fn);
 		if (!bb.empty()) {
Index: src/frontends/controllers/ControlEmbeddedFiles.cpp
===================================================================
--- src/frontends/controllers/ControlEmbeddedFiles.cpp	(revision 20269)
+++ src/frontends/controllers/ControlEmbeddedFiles.cpp	(working copy)
@@ -111,6 +111,9 @@
 		else
 			item.extract(&buffer());
 		item.updateInsets(&buffer());
+		// FIXME: unless we record the type of file item, we will
+		// need to update all possible dialogs (bibtex etc).
+		updateDialog("graphics");
 	}
 	if (embed)
 		dispatchMessage("Embed file " + item.outputFilename(buffer().filePath()));
Index: src/frontends/controllers/ControlGraphics.h
===================================================================
--- src/frontends/controllers/ControlGraphics.h	(revision 20269)
+++ src/frontends/controllers/ControlGraphics.h	(working copy)
@@ -61,6 +61,13 @@
 	bool isFilenameValid(std::string const & fname) const;
 	/// edit file
 	void editGraphics();
+	/// in case that the file is embedded, we need to display
+	/// its embedded filename in the form of Embed::
+	/// Note that params() always holds the external filename.
+	std::string const displayFileName() const;
+	/// translate Embedd:: filename to a valid one.
+	docstring const translateFileName(docstring const &) const;
+	std::string const translateFileName(std::string const &) const;
 
 private:
 	///
Index: src/frontends/controllers/ControlGraphics.cpp
===================================================================
--- src/frontends/controllers/ControlGraphics.cpp	(revision 20269)
+++ src/frontends/controllers/ControlGraphics.cpp	(working copy)
@@ -18,6 +18,8 @@
 #include "FuncRequest.h"
 #include "gettext.h"
 #include "LyXRC.h"
+#include "Buffer.h"
+#include "EmbeddedFiles.h"
 
 #include "graphics/GraphicsCache.h"
 #include "graphics/GraphicsCacheItem.h"
@@ -30,6 +32,7 @@
 #include "support/filetools.h"
 #include "support/Package.h"
 #include "support/types.h"
+#include "support/lstrings.h"
 
 #include <boost/filesystem/operations.hpp>
 
@@ -45,10 +48,13 @@
 using support::addName;
 using support::FileFilterList;
 using support::FileName;
+using support::DocFileName;
 using support::isFileReadable;
 using support::makeAbsPath;
 using support::package;
 using support::readBB_from_PSFile;
+using support::prefixIs;
+using support::subst;
 
 namespace frontend {
 
@@ -149,6 +155,38 @@
 }
 
 
+string const ControlGraphics::displayFileName() const
+{
+	EmbeddedFiles const & files = buffer().embeddedFiles();
+	DocFileName const & filename = params().filename;
+	if (!files.enabled())
+		return filename.outputFilename(bufferFilepath());
+	// otherwise, is it enabled?
+	EmbeddedFiles::EmbeddedFileList::const_iterator it = files.find(filename.absFilename());
+	if (it != files.end() && it->embedded())
+		return "Embedded:/" + it->inzipName();
+	return filename.outputFilename(bufferFilepath());
+}
+
+
+docstring const ControlGraphics::translateFileName(docstring const & filename) const
+{
+	if (prefixIs(filename, from_utf8("Embedded:/")))
+		return from_utf8(subst(to_utf8(filename), "Embedded:", buffer().temppath()));
+	else
+		return filename;
+}
+
+
+string const ControlGraphics::translateFileName(string const & filename) const
+{
+	if (prefixIs(filename, "Embedded:/"))
+		return subst(filename, "Embedded:", buffer().temppath());
+	else
+		return filename;
+}
+
+
 namespace {
 
 char const * const bb_units[] = { "bp", "cm", "mm", "in" };

Reply via email to