]> git.lyx.org Git - lyx.git/blobdiff - src/graphics/GraphicsCacheItem.C
remove more forms.h cruft
[lyx.git] / src / graphics / GraphicsCacheItem.C
index 3b1dce490451526f8d44699e1eb298e64467bd1f..8a066ff5ff58ebda8c8163b5deb5393747b2e97a 100644 (file)
@@ -1,10 +1,9 @@
-// -*- C++ -*-
 /* This file is part of
  * =================================================
  * 
  *          LyX, The Document Processor
  *          Copyright 1995 Matthias Ettrich.
- *          Copyright 1995-2000 The LyX Team.
+ *          Copyright 1995-2001 The LyX Team.
  *
  *          This file Copyright 2000 Baruch Even
  * ================================================= */
 #pragma implementation
 #endif
 
-#include "GraphicsCacheItem.h"
-
-#include "graphics/XPM_Renderer.h"
+#include "graphics/GraphicsCache.h"
+#include "graphics/GraphicsCacheItem.h"
+#include "frontends/support/LyXImage.h"
+#include "graphics/ImageLoaderXPM.h"
+#include "converter.h"
 #include "support/filetools.h"
+#include "support/lyxlib.h"
+#include "lyx_gui_misc.h"
 #include "debug.h"
 #include "support/LAssert.h"
-#include <unistd.h> // unlink
-
-#include <map>
-
-#include FORMS_H_LOCATION
+#include "gettext.h"
 
 using std::endl;
-using std::map;
 
-GraphicsCacheItem::GraphicsCacheItem()
-       : height_(-1), width_(-1), imageStatus_(Loading),
-         pixmap_(0), renderer(0)
-{}
-
-GraphicsCacheItem::~GraphicsCacheItem()
+GraphicsCacheItem::GraphicsCacheItem(string const & filename)
+       : imageStatus_(GraphicsCacheItem::Loading)
 {
-       if (imageStatus_ == Loaded) {
-               XFreePixmap(fl_display, pixmap_);
-       }
-
-       delete renderer;
+       filename_ = filename;
+       
+       bool success = convertImage(filename);
+       // For now we do it synchronously
+       if (success) 
+               imageConverted(0);
+       else
+               imageStatus_ = ErrorConverting;
 }
 
-bool
-GraphicsCacheItem::setFilename(string const & filename)
-{
-       imageStatus_ = Loading;
 
-       renderer = new XPM_Renderer();
-       if (renderXPM(filename))
-               return true;
-       
-       return false;
-}
+GraphicsCacheItem::~GraphicsCacheItem()
+{}
 
-/*** Callback method ***/
 
-typedef map<string, GraphicsCacheItem*> CallbackMap;
-static CallbackMap callbackMap;
+GraphicsCacheItem::ImageStatus 
+GraphicsCacheItem::getImageStatus() const { return imageStatus_; }
 
-void
-callback(string cmd, int retval)
-{
-       lyxerr << "callback, cmd="<<cmd<<", retval="<<retval<<endl;
 
-       GraphicsCacheItem * item = callbackMap[cmd];
-       callbackMap.erase(cmd);
-       
-       item->imageConverted(retval);
-}
+LyXImage * 
+GraphicsCacheItem::getImage() const { return image_.get(); }
+
 
 void
 GraphicsCacheItem::imageConverted(int retval)
 {
-       lyxerr << "imageConverted, retval="<<retval<<endl;
+       lyxerr << "imageConverted, retval=" << retval << endl;
 
        if (retval) {
-               imageStatus_ = ErrorConverting;
+               lyxerr << "(GraphicsCacheItem::imageConverter) "
+                       "Error converting image." << endl;
+               imageStatus_ = GraphicsCacheItem::ErrorConverting;
                return;
        }
 
-       // Do the actual image loading from XPM to memory.
-       loadXPMImage(); 
+       // Do the actual image loading from file to memory.
+       loadImage();    
 }
 
-/**********************/
 
-bool
-GraphicsCacheItem::renderXPM(string const & filename)
+namespace {
+
+string const findTargetFormat(string const & from)
 {
-       // Create the command to do the conversion, this depends on ImageMagicks
-       // convert program.
-       string command = "convert ";
-       command += filename;
-       command += " XPM:";
+       typedef ImageLoader::FormatList FormatList;
+       FormatList formats = ImageLoaderXPM().loadableFormats();
+       lyx::Assert(formats.size() > 0); // There must be a format to load from.
+       
+       FormatList::const_iterator iter = formats.begin();
+       FormatList::const_iterator end  = formats.end();
+
+       for (; iter != end; ++iter) {
+               if (converters.IsReachable(from, *iter))
+                       break;
+       }
+       if (iter == end) {
+               // We do not know how to convert the image to something loadable.
+               lyxerr << "ERROR: Do not know how to convert image." << std::endl;
+
+               string const first(_("Cannot convert image to display format"));
+               string const second1(_("Need converter from "));
+               string const second2(_(" to "));
+               string const second(second1 + from + second2 + formats[0]);
+
+               WriteAlert(first, second);
+               
+               return string();
+       }
+
+       return (*iter);
+}
+
+} // anon namespace
 
+       
+bool
+GraphicsCacheItem::convertImage(string const & filename)
+{
+       string const from = GetExtension(filename);
+       string const to = findTargetFormat(from);
+       if (to.empty()) 
+               return false;
+       
        // Take only the filename part of the file, without path or extension.
        string temp = OnlyFilename(filename);
-       temp = ChangeExtension(filename , string());
+       temp = ChangeExtension(filename, string());
        
        // Add some stuff to have it a unique temp file.
-       xpmfile = TmpFileName(string(), temp);
-       xpmfile = ChangeExtension(xpmfile, ".xpm");     
-       
-       command += xpmfile;
+       // This tempfile is deleted in loadImage after it is loaded to memory.
+       tempfile = lyx::tempName(string(), temp);
+       // Remove the temp file, we only want the name...
+       lyx::unlink(tempfile);
 
-       // Set the callback mapping to point to us.
-       callbackMap[command] = this;
-
-       // Run the convertor.
-       // There is a problem with running it asyncronously, it doesn't return
-       // to call the callback, so until the Systemcalls mechanism is fixed
-       // I use the syncronous method.
-       lyxerr << "Launching convert to xpm, command="<<command<<endl;
-//     syscall.startscript(Systemcalls::DontWait, command, &callback);
-       syscall.startscript(Systemcalls::Wait, command, &callback);
+       converters.Convert(0, filename, tempfile, from, to);
 
        return true;
 }
 
+
 // This function gets called from the callback after the image has been
 // converted successfully.
 void
-GraphicsCacheItem::loadXPMImage()
+GraphicsCacheItem::loadImage()
 {
-       if (! renderer->setFilename(xpmfile)) {
-               return;
-       }
+       lyxerr << "Loading XPM Image... ";
 
-       if (renderer->renderImage()) {
-               pixmap_ = renderer->getPixmap();
-               width_ = renderer->getWidth();
-               height_ = renderer->getHeight();
-               imageStatus_ = Loaded;
+       ImageLoaderXPM imageLoader;
+       if (imageLoader.loadImage(tempfile) == ImageLoader::OK) {
+               lyxerr << "Success." << endl;
+               image_.reset(imageLoader.getImage());
+               imageStatus_ = GraphicsCacheItem::Loaded;
        } else {
-               imageStatus_ = ErrorReading;
+               lyxerr << "Loading " << tempfile << "Failed" << endl;
+               imageStatus_ = GraphicsCacheItem::ErrorReading;
        }
 
-       imageDone.emit();
-
        // remove the xpm file now.
-       ::unlink(xpmfile.c_str());
+       lyx::unlink(tempfile);
        // and remove the reference to the filename.
-       xpmfile = string();
+       tempfile = string();
 }