From 077ea832b5f42e53e48fd2e350bd0e232ff67727 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=BCrgen=20Vigna?= Date: Thu, 10 Aug 2000 13:15:05 +0000 Subject: [PATCH] Baruch's patch + some fixes to it. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@964 a592a061-630c-0410-9148-cb99ea01b6c8 --- ChangeLog | 24 ++++ src/graphics/EPS_Renderer.C | 60 ++++++++++ src/graphics/EPS_Renderer.h | 36 ++++++ src/graphics/GraphicsCache.C | 28 +++-- src/graphics/GraphicsCacheItem.C | 148 ++++++++---------------- src/graphics/GraphicsCacheItem.h | 60 ++++------ src/graphics/GraphicsCacheItem_pimpl.C | 149 +++++++++++++++++++++++++ src/graphics/GraphicsCacheItem_pimpl.h | 102 +++++++++++++++++ src/graphics/Makefile.am | 6 +- src/insets/insetgraphics.C | 57 +++++----- src/insets/insetgraphics.h | 15 ++- 11 files changed, 491 insertions(+), 194 deletions(-) create mode 100644 src/graphics/EPS_Renderer.C create mode 100644 src/graphics/EPS_Renderer.h create mode 100644 src/graphics/GraphicsCacheItem_pimpl.C create mode 100644 src/graphics/GraphicsCacheItem_pimpl.h diff --git a/ChangeLog b/ChangeLog index 8d41e3d2ae..b627ad8507 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,27 @@ +2000-08-10 Juergen Vigna + + * src/insets/insetgraphics.C (draw): fixed access to 0 cacheHandle. + (InsetGraphics): initialized cacheHandle to 0. + (draw): changed call to updateInset to status=CHANGE_IN_DRAW. + +2000-08-10 Baruch Even + + * src/graphics/GraphicsCache.h: + * src/graphics/GraphicsCache.C (addFile, removeFile): Changed to work + correctly as a cache. + + * src/graphics/GraphicsCacheItem.h: + * src/graphics/GraphicsCacheItem.C: Changed to the pimpl idiom to allow + reference counting. + + * src/graphics/GraphicsCacheItem_pimpl.h: + * src/graphics/GraphicsCacheItem_pimpl.C: The implementation of the + GraphicsCacheItem. + + * src/insets/insetgraphics.h: + * src/insets/insetgraphics.C: Changed from using a signal notification + to polling when image is not loaded. + 2000-08-10 Allan Rae * development/tools/makeLyXsigc.sh: Updated to allow Signal3. Note diff --git a/src/graphics/EPS_Renderer.C b/src/graphics/EPS_Renderer.C new file mode 100644 index 0000000000..ee0d1bd061 --- /dev/null +++ b/src/graphics/EPS_Renderer.C @@ -0,0 +1,60 @@ +// -*- C++ -*- +/* This file is part of + * ================================================= + * + * LyX, The Document Processor + * Copyright 1995 Matthias Ettrich. + * Copyright 1995-2000 The LyX Team. + * + * This file Copyright 2000 Baruch Even + * ================================================= */ + +#ifdef __GNUG__ +#pragma implementation +#endif + +#include +#include "EPS_Renderer.h" + +#include FORMS_H_LOCATION +#include +#include + +#include "support/LAssert.h" +#include "debug.h" + +using std::endl; +using std::ios; + +EPS_Renderer::EPS_Renderer() + : Renderer() +{} + +EPS_Renderer::~EPS_Renderer() +{} + +bool EPS_Renderer::renderImage() +{ + return false; +} + +bool EPS_Renderer::isImageFormatOK(string const & filename) const +{ + std::ifstream is(filename.c_str(), ios::in); + + // The signature of the file without the spaces. + static const char str[] = "%!PS"; + const char * ptr = str; + + do { + char c; + is >> c; + + if (c != *ptr) + return false; + + ++ptr; + } while (*ptr != '\0'); + + return true; +} diff --git a/src/graphics/EPS_Renderer.h b/src/graphics/EPS_Renderer.h new file mode 100644 index 0000000000..947146bc3c --- /dev/null +++ b/src/graphics/EPS_Renderer.h @@ -0,0 +1,36 @@ +// -*- C++ -*- +/* This file is part of + * ================================================= + * + * LyX, The Document Processor + * Copyright 1995 Matthias Ettrich. + * Copyright 1995-2000 The LyX Team. + * + * This file Copyright 2000 Baruch Even + * ================================================= */ + +#ifndef EPS_RENDERER_H +#define EPS_RENDERER_H + +#ifdef __GNUG__ +#pragma interface +#endif + +#include "graphics/Renderer.h" + +class EPS_Renderer : public Renderer { +public: + /// c-tor. + EPS_Renderer(); + /// d-tor. + virtual ~EPS_Renderer(); + + /// Load the EPS image and create a pixmap out of it. + virtual bool renderImage(); + +private: + /// Verify that filename is really an EPS file. + virtual bool isImageFormatOK(string const & filename) const; +}; + +#endif diff --git a/src/graphics/GraphicsCache.C b/src/graphics/GraphicsCache.C index 2c1d0915d6..b83a78f97a 100644 --- a/src/graphics/GraphicsCache.C +++ b/src/graphics/GraphicsCache.C @@ -46,37 +46,35 @@ GraphicsCache::~GraphicsCache() } -GraphicsCacheItem * +GraphicsCacheItem * GraphicsCache::addFile(string const & filename) { CacheType::const_iterator it = cache.find(filename); if (it != cache.end()) { - return (*it).second; + return new GraphicsCacheItem( *((*it).second) ); } GraphicsCacheItem * cacheItem = new GraphicsCacheItem(); - if (cacheItem == 0) { + if (cacheItem == 0) return 0; - } - bool result = cacheItem->setFilename(filename); - if (!result) - return 0; + cacheItem->setFilename(filename); cache[filename] = cacheItem; - - return cacheItem; + + // We do not want to return the main cache object, otherwise when the + // will destroy their copy they will destroy the main copy. + return new GraphicsCacheItem( *cacheItem ); } void GraphicsCache::removeFile(string const & filename) { - CacheType::const_iterator it = cache.find(filename); - - if (it != cache.end()) { - // INCOMPLETE! -// cache.erase(it); - } + // We do not destroy the GraphicsCacheItem since we are here because + // the last copy of it is being erased. + + if (cache.find(filename) != cache.end()) + cache.erase(filename); } diff --git a/src/graphics/GraphicsCacheItem.C b/src/graphics/GraphicsCacheItem.C index 3b1dce4904..2cea9589ad 100644 --- a/src/graphics/GraphicsCacheItem.C +++ b/src/graphics/GraphicsCacheItem.C @@ -15,134 +15,78 @@ #pragma implementation #endif -#include "GraphicsCacheItem.h" - -#include "graphics/XPM_Renderer.h" -#include "support/filetools.h" -#include "debug.h" -#include "support/LAssert.h" -#include // unlink - -#include - -#include FORMS_H_LOCATION - -using std::endl; -using std::map; +#include "graphics/GraphicsCache.h" +#include "graphics/GraphicsCacheItem.h" +#include "graphics/GraphicsCacheItem_pimpl.h" GraphicsCacheItem::GraphicsCacheItem() - : height_(-1), width_(-1), imageStatus_(Loading), - pixmap_(0), renderer(0) -{} + : pimpl(new GraphicsCacheItem_pimpl) +{ + pimpl->refCount = 1; +} GraphicsCacheItem::~GraphicsCacheItem() { - if (imageStatus_ == Loaded) { - XFreePixmap(fl_display, pixmap_); - } - - delete renderer; + destroy(); } bool GraphicsCacheItem::setFilename(string const & filename) { - imageStatus_ = Loading; - - renderer = new XPM_Renderer(); - if (renderXPM(filename)) - return true; - - return false; + filename_ = filename; + return pimpl->setFilename(filename); } -/*** Callback method ***/ - -typedef map CallbackMap; -static CallbackMap callbackMap; - -void -callback(string cmd, int retval) +GraphicsCacheItem::GraphicsCacheItem(GraphicsCacheItem const & gci) { - lyxerr << "callback, cmd="< imageDone; + private: /// Private c-tor so that only GraphicsCache can create an instance. GraphicsCacheItem(); + /// internal copy mechanism. + void copy(GraphicsCacheItem const &); + /// internal destroy mechanism. + void destroy(); + /// Set the filename this item will be pointing too. bool setFilename(string const & filename); - /// Create an XPM file version of the image. - bool renderXPM(string const & filename); - - /// Load the image from XPM to memory Pixmap - void loadXPMImage(); - /// friend class GraphicsCache; - /// The file name of the XPM file. - string xpmfile; - /// The image height - int height_; - /// The image width - int width_; - /// Is the pixmap loaded? - ImageStatus imageStatus_; - /// The image pixmap - Pixmap pixmap_; - /// The rendering object. - Renderer * renderer; - - /// The system caller, runs the convertor. - Systemcalls syscall; + GraphicsCacheItem_pimpl * pimpl; + + /// The filename we refer too, this is used when removing ourselves + /// from the cache. + string filename_; }; #endif diff --git a/src/graphics/GraphicsCacheItem_pimpl.C b/src/graphics/GraphicsCacheItem_pimpl.C new file mode 100644 index 0000000000..3fab0167b0 --- /dev/null +++ b/src/graphics/GraphicsCacheItem_pimpl.C @@ -0,0 +1,149 @@ +// -*- C++ -*- +/* This file is part of + * ================================================= + * + * LyX, The Document Processor + * Copyright 1995 Matthias Ettrich. + * Copyright 1995-2000 The LyX Team. + * + * This file Copyright 2000 Baruch Even + * ================================================= */ + +#include + +#ifdef __GNUG__ +#pragma implementation +#endif + +#include "GraphicsCacheItem.h" +#include "GraphicsCacheItem_pimpl.h" + +#include "graphics/XPM_Renderer.h" +#include "graphics/EPS_Renderer.h" +#include "support/filetools.h" +#include "debug.h" +#include "support/LAssert.h" +#include // unlink + +#include + +#include FORMS_H_LOCATION + +using std::endl; +using std::map; + + +GraphicsCacheItem_pimpl::GraphicsCacheItem_pimpl() + : height_(-1), width_(-1), imageStatus_(GraphicsCacheItem::Loading), + pixmap_(0), renderer(0), refCount(0) +{} + +GraphicsCacheItem_pimpl::~GraphicsCacheItem_pimpl() +{ + if (imageStatus_ == GraphicsCacheItem::Loaded) { + XFreePixmap(fl_display, pixmap_); + } + + delete renderer; +} + +bool +GraphicsCacheItem_pimpl::setFilename(string const & filename) +{ + imageStatus_ = GraphicsCacheItem::Loading; + + renderer = new XPM_Renderer(); + if (renderXPM(filename)) + return true; + + return false; +} + +/*** Callback method ***/ + +typedef map CallbackMap; +static CallbackMap callbackMap; + +void +callback(string cmd, int retval) +{ + lyxerr << "callback, cmd="<getImageStatus() == GraphicsCacheItem::Loaded) - return cachehandle->getHeight(); + if (pixmapInitialized) + return cacheHandle->getHeight(); else return 50; } @@ -215,34 +216,40 @@ int InsetGraphics::descent(BufferView *, LyXFont const &) const int InsetGraphics::width(BufferView *, LyXFont const &) const { - if (cachehandle && - cachehandle->getImageStatus() == GraphicsCacheItem::Loaded) - return cachehandle->getWidth(); + if (pixmapInitialized) + return cacheHandle->getWidth(); else return 50; } void InsetGraphics::draw(BufferView * bv, LyXFont const & font, - int baseline, float & x, bool) const + int baseline, float & x, bool) const { Painter & paint = bv->painter(); - - // This will draw the graphics. As for now we only draw a - // placeholder rectangele. - if (cachehandle && - cachehandle->getImageStatus() == GraphicsCacheItem::Loaded) { + // This will draw the graphics. If the graphics has not been loaded yet, + // we draw just a rectangle. + if (pixmapInitialized) { paint.pixmap(int(x)+2, baseline - ascent(bv, font), width(bv, font) - 4, - ascent(bv,font) + descent(bv,font), - cachehandle->getImage()); + ascent(bv,font) + descent(bv,font), + pixmap); } else { paint.rectangle(int(x)+2, baseline - ascent(bv, font), width(bv, font) - 4, ascent(bv, font) + descent(bv, font)); + // Check if the image is now ready. + if (cacheHandle && + (cacheHandle->getImageStatus() == GraphicsCacheItem::Loaded)) { + pixmap = cacheHandle->getImage(); + pixmapInitialized = true; + + // Tell BufferView we need to be updated! + bv->text->status = LyXText::CHANGED_IN_DRAW; + } } x += width(bv, font); @@ -251,7 +258,6 @@ void InsetGraphics::draw(BufferView * bv, LyXFont const & font, void InsetGraphics::Edit(BufferView *bv, int, int, unsigned int) { - bv_ = bv; bv->owner()->getDialogs() -> showGraphics(this); } @@ -584,8 +590,8 @@ int InsetGraphics::Latex(Buffer const *buf, ostream & os, } // How do we decide to what format should we export? -// cachehandle->export(ImageType::EPS); -// cachehandle->export(ImageType::PNG); +// cacheHandle->>export(ImageType::EPS); +// cacheHandle->>export(ImageType::PNG); return 1; } @@ -635,19 +641,10 @@ void InsetGraphics::updateInset() if (!params.filename.empty()) { temp = gc->addFile(params.filename); - if (temp) - temp->imageDone.connect(slot(this, &InsetGraphics::imageDone)); } - delete cachehandle; - cachehandle = temp; - -} - -void InsetGraphics::imageDone() -{ - if (bv_) - bv_->updateInset(this, false); + delete cacheHandle; + cacheHandle = temp; } bool InsetGraphics::setParams(InsetGraphicsParams const & params) diff --git a/src/insets/insetgraphics.h b/src/insets/insetgraphics.h index 9a28714bfb..4c64ffe6c9 100644 --- a/src/insets/insetgraphics.h +++ b/src/insets/insetgraphics.h @@ -21,6 +21,7 @@ #include "insets/lyxinset.h" #include "insets/insetgraphicsParams.h" +#include "graphics/GraphicsCacheItem.h" #include "LaTeXFeatures.h" @@ -33,7 +34,6 @@ using SigC::Object; #endif class Dialogs; -class GraphicsCacheItem; /// #ifdef SIGC_CXX_NAMESPACES @@ -103,15 +103,14 @@ private: /// Update the inset after parameter change. void updateInset(); - /// Get notified when the inline image processing has finished. - void imageDone(); - /// The graphics cache handle. - GraphicsCacheItem * cachehandle; - - /// Holds the buffer view that we are associated with. - BufferView * bv_; + GraphicsCacheItem * cacheHandle; + /// The pixmap + mutable Pixmap pixmap; + /// is the pixmap initialized? + mutable bool pixmapInitialized; + InsetGraphicsParams params; // Baruch Even (baruch.even@writeme.com) 2000-07-17 -- 2.39.2