]> git.lyx.org Git - lyx.git/blobdiff - src/insets/figinset.C
clear()->erase() ; lots of using directives for cxx
[lyx.git] / src / insets / figinset.C
index 664b08f9cf84a4e936fc5ef8deffbe5c36428951..e39691c7d8349d694a374407e8916a30360e3428 100644 (file)
 
 #include <config.h>
 
+#include <fstream>
+#include <queue>
+#include <list>
+#include <algorithm>
+#include <vector>
+
 #include <unistd.h>
 #include <csignal>
 #include <sys/wait.h>
 #include <cstdlib>
 #include <cctype>
 #include <cmath>
-#include <fstream>
-#include <queue>
-#include <list>
-#include <algorithm>
-#include <vector>
-
-using std::ofstream;
-using std::ifstream;
-using std::queue;
-using std::list;
-using std::vector;
-using std::find;
-using std::flush;
-using std::endl;
 
 #include "figinset.h"
 #include "lyx.h"
@@ -66,15 +58,29 @@ using std::endl;
 #include "support/FileInfo.h"
 #include "support/lyxlib.h"
 #include "Painter.h"
+#include "font.h"
+#include "bufferview_funcs.h"
+#include "ColorHandler.h"
+
+using std::ostream;
+using std::istream;
+using std::ofstream;
+using std::ifstream;
+using std::queue;
+using std::list;
+using std::vector;
+using std::find;
+using std::flush;
+using std::endl;
+#ifdef HAVE_SSTREAM
+using std::ostringstream;
+#endif
 
 extern BufferView * current_view;
 extern FL_OBJECT * figinset_canvas;
 
 extern char ** environ; // is this only redundtant on linux systems? Lgb.
 
-extern void ProhibitInput();
-extern void AllowInput();
-
 static float const DEG2PI = 57.295779513;
 
 struct queue_element {
@@ -107,7 +113,6 @@ static int gs_allcolors;            // number of all colors
 
 static list<int> pidwaitlist; // pid wait list
 
-
 static
 GC createGC()
 {
@@ -145,12 +150,27 @@ void addpidwait(int pid)
 }
 
 
+static
+string make_tmp(int pid)
+{
+       return system_tempdir + "/~lyxgs" + tostr(pid) + ".ps";
+}
+
+
+static
+void kill_gs(int pid, int sig)
+{
+       if (lyxerr.debugging()) 
+               lyxerr << "Killing gs " << pid << endl;
+       lyx::kill(pid, sig);
+       unlink(make_tmp(pid).c_str());
+}
+
+
 extern "C" // static
 int GhostscriptMsg(FL_OBJECT *, Window, int, int,
                   XEvent * ev, void *)
 {
-       char tmp[128];
-
        XClientMessageEvent * e = reinterpret_cast<XClientMessageEvent*>(ev);
 
        if(lyxerr.debugging()) {
@@ -160,7 +180,6 @@ int GhostscriptMsg(FL_OBJECT *, Window, int, int,
 
        // just kill gs, that way it will work for sure
        // This loop looks like S**T so it probably is...
-       //for (bitmaps_type::size_type i = 0; i < bitmaps.size(); ++i)
        for (bitmaps_type::iterator it = bitmaps.begin();
             it != bitmaps.end(); ++it)
                if (static_cast<long>((*it)->bitmap) ==
@@ -270,31 +289,13 @@ int GhostscriptMsg(FL_OBJECT *, Window, int, int,
                                        }
                                }
                          noim:
-                               if (lyxerr.debugging()) {
-                                       lyxerr << "Killing gs " 
-                                              << p->gspid << endl;
-                               }
-                               lyx::kill(p->gspid, SIGHUP);
-
-                               sprintf(tmp, "%s/~lyxgs%d.ps",
-                                       system_tempdir.c_str(), 
-                                       p->gspid);
-                               unlink(tmp);
+                               kill_gs(p->gspid, SIGHUP);
                                if (forkstat == 0) {
                                        XCloseDisplay(tmpdisp);
                                        _exit(0);
                                }
                        } else {
-                               if (lyxerr.debugging()) {
-                                       lyxerr << "Killing gs " 
-                                              << p->gspid << endl;
-                               }
-                               lyx::kill(p->gspid, SIGHUP);
-
-                               sprintf(tmp, "%s/~lyxgs%d.ps", 
-                                       system_tempdir.c_str(),
-                                       p->gspid);
-                               unlink(tmp);
+                               kill_gs(p->gspid, SIGHUP);
                        }
                        break;
                }
@@ -403,15 +404,17 @@ void InitFigures()
        gs_color = false;
        if (lyxrc.use_gui) {
                fl_add_canvas_handler(figinset_canvas, ClientMessage,
-                                                       GhostscriptMsg, current_view->owner()->getMainForm());
+                                     GhostscriptMsg,
+                                     current_view->owner()->getMainForm());
 
                local_gc_copy = createGC();
 
-               Visual * vi = DefaultVisual(fl_display, DefaultScreen(fl_display));
+               Visual * vi = DefaultVisual(fl_display,
+                                           DefaultScreen(fl_display));
                if (lyxerr.debugging()) {
                        printf("Visual ID: %ld, class: %d, bprgb: %d, mapsz: %d\n", 
-                                                vi->visualid, vi->c_class, 
-                                                vi->bits_per_rgb, vi->map_entries);
+                              vi->visualid, vi->c_class, 
+                              vi->bits_per_rgb, vi->map_entries);
                }
                color_visual = ( (vi->c_class == StaticColor) ||
                                 (vi->c_class == PseudoColor) ||
@@ -455,12 +458,9 @@ void freefigdata(figdata * tmpdata)
 
        if (tmpdata->gspid > 0) {
                int pid = tmpdata->gspid;
-               char buf[128];
                // kill ghostscript and unlink it's files
                tmpdata->gspid = -1;
-               lyx::kill(pid, SIGKILL);
-               sprintf(buf, "%s/~lyxgs%d.ps", system_tempdir.c_str(), pid);
-               unlink(buf);
+               kill_gs(pid, SIGKILL);
        }
 
        if (tmpdata->bitmap) XFreePixmap(fl_display, tmpdata->bitmap);
@@ -472,6 +472,10 @@ void freefigdata(figdata * tmpdata)
 static
 void runqueue()
 {
+       // This _have_ to be set before the fork!
+       unsigned long background_pixel =
+               lyxColorHandler->colorPixel(LColor::background);
+       
        // run queued requests for ghostscript, if any
        if (!gsrunning && gs_color && !gs_xcolor) {
                // here alloc all colors, so that gs will use only
@@ -481,7 +485,7 @@ void runqueue()
        }
        
        while (gsrunning < MAXGS) {
-               char tbuf[384], tbuf2[80];
+               //char tbuf[384]; //, tbuf2[80];
                Atom * prop;
                int nprop, i;
 
@@ -499,7 +503,7 @@ void runqueue()
                        continue;
                }
 
-               int pid = fork();
+               int pid = ::fork();
                
                if (pid == -1) {
                        if (lyxerr.debugging()) {
@@ -511,15 +515,13 @@ void runqueue()
                        return;
                }
                if (pid == 0) { // child
-                       char ** env, rbuf[80], gbuf[40];
+                       char ** env;
                        int ne = 0;
                        Display * tempdisp = XOpenDisplay(XDisplayName(0));
 
                        // create translation file
-                       sprintf(tbuf, "%s/~lyxgs%d.ps", system_tempdir.c_str(),
-                               int(getpid()));
-                       
-                       ofstream ofs(tbuf);
+                       ofstream ofs;
+                       ofs.open(make_tmp(getpid()).c_str());
                        ofs << "gsave clippath pathbbox grestore\n"
                            << "4 dict begin\n"
                            << "/ury exch def /urx exch def /lly exch def "
@@ -536,29 +538,49 @@ void runqueue()
                        ofs.close(); // Don't remove this.
 
                        // gs process - set ghostview environment first
-                       sprintf(tbuf2, "GHOSTVIEW=%ld %ld", fl_get_canvas_id(
-                               figinset_canvas), p->data->bitmap);
-
+#ifdef HAVE_SSTREAM
+                       ostringstream t2;
+#else
+                       char tbuf2[80];
+                       ostrstream t2(tbuf2, sizeof(tbuf2));
+#endif
+                       t2 << "GHOSTVIEW=" << fl_get_canvas_id(figinset_canvas)
+                          << ' ' << p->data->bitmap;
+#ifndef HAVE_SSTREAM
+                       t2 << '\0';
+#endif
                        // now set up ghostview property on a window
-                       sprintf(tbuf, "0 0 0 0 %d %d 72 72 0 0 0 0",
-                               p->data->wid, p->data->hgh);
                        // #warning BUG seems that the only bug here
                        // might be the hardcoded dpi.. Bummer!
+#ifdef HAVE_SSTREAM
+                       ostringstream t1;
+#else
+                       char tbuf[384];
+                       ostrstream t1(tbuf, sizeof(tbuf));
+#endif
+                       t1 << "0 0 0 0 " << p->data->wid << ' '
+                          << p->data->hgh << " 72 72 0 0 0 0";
+#ifndef HAVE_SSTREAM
+                       t1 << '\0';
+#endif
                        
                        if (lyxerr.debugging()) {
                                lyxerr << "Will set GHOSTVIEW property to ["
-                                      << tbuf << "]" << endl;
+                                      << t1.str() << "]" << endl;
                        }
                        // wait until property is deleted if executing multiple
                        // ghostscripts
+                       XGrabServer(tempdisp);
                        for (;;) {
                                // grab server to prevent other child
                                // interfering with setting GHOSTVIEW property
+                               // The grabbing goes on for too long, is it
+                               // really needed? (Lgb)
+                               // I moved most of the grabs... (Lgb)
                                if (lyxerr.debugging()) {
                                        lyxerr << "Grabbing the server"
                                               << endl;
                                }
-                               XGrabServer(tempdisp);
                                prop = XListProperties(tempdisp,
                                                       fl_get_canvas_id(
                                        figinset_canvas), &nprop);
@@ -576,9 +598,6 @@ void runqueue()
                                }
                                XFree(reinterpret_cast<char *>(prop)); // jc:
                                if (err) break;
-                               // release the server
-                               XUngrabServer(tempdisp);
-                               XFlush(tempdisp);
                                // ok, property found, we must wait until
                                // ghostscript deletes it
                                if (lyxerr.debugging()) {
@@ -587,81 +606,125 @@ void runqueue()
                                               << "] GHOSTVIEW property"
                                                " found. Waiting." << endl;
                                }
-
-                               sleep(1);
+                               XUngrabServer(tempdisp);
+                               XFlush(tempdisp);
+                               ::sleep(1);
+                               XGrabServer(tempdisp);
                        }
-
+#ifdef HAVE_SSTREAM
                        XChangeProperty(tempdisp, 
                                        fl_get_canvas_id(figinset_canvas),
                                        XInternAtom(tempdisp, "GHOSTVIEW", false),
                                        XInternAtom(tempdisp, "STRING", false),
                                        8, PropModeAppend, 
-                                       reinterpret_cast<unsigned char*>(tbuf),
-                                       strlen(tbuf));
+                                       reinterpret_cast<unsigned char*>(const_cast<char*>(t1.str().c_str())),
+                                       t1.str().size());
+#else
                        
+                       XChangeProperty(tempdisp, 
+                                       fl_get_canvas_id(figinset_canvas),
+                                       XInternAtom(tempdisp, "GHOSTVIEW", false),
+                                       XInternAtom(tempdisp, "STRING", false),
+                                       8, PropModeAppend, 
+                                       reinterpret_cast<unsigned char*>(const_cast<char*>(t1.str())),
+                                       ::strlen(t1.str()));
+#endif
+                       XUngrabServer(tempdisp);
+                       XFlush(tempdisp);
+
+#ifdef HAVE_SSTREAM
+                       ostringstream t3;
+#else
+                       //char tbuf[384];
+                       ostrstream t3(tbuf, sizeof(tbuf));
+#endif
                        switch (p->data->flags & 3) {
-                       case 0: tbuf[0] = 'H'; break; // Hidden
-                       case 1: tbuf[0] = 'M'; break; // Mono
-                       case 2: tbuf[0] = 'G'; break; // Gray
+                       case 0: t3 << 'H'; break; // Hidden
+                       case 1: t3 << 'M'; break; // Mono
+                       case 2: t3 << 'G'; break; // Gray
                        case 3:
                                if (color_visual) 
-                                       tbuf[0] = 'C'; // Color
+                                       t3 << 'C'; // Color
                                else 
-                                       tbuf[0] = 'G'; // Gray
+                                       t3 << 'G'; // Gray
                                break;
                        }
+       
+                       t3 << ' ' << BlackPixelOfScreen(DefaultScreenOfDisplay(tempdisp))
+                          << ' ' << background_pixel;
+#ifndef HAVE_SSTREAM
+                       t3 << '\0';
+#endif
 
-                       sprintf(tbuf+1, " %ld %ld", BlackPixelOfScreen(
-                               DefaultScreenOfDisplay(fl_display)),
-                               fl_get_pixel(FL_WHITE));
-
+                       XGrabServer(tempdisp);
+#ifdef HAVE_SSTREAM
+                       XChangeProperty(tempdisp, 
+                                       fl_get_canvas_id(figinset_canvas),
+                                       XInternAtom(tempdisp,
+                                                   "GHOSTVIEW_COLORS", false),
+                                       XInternAtom(tempdisp, "STRING", false),
+                                       8, PropModeReplace, 
+                                       reinterpret_cast<unsigned char*>(const_cast<char*>(t3.str().c_str())),
+                                       t3.str().size());
+#else
                        XChangeProperty(tempdisp, 
                                        fl_get_canvas_id(figinset_canvas),
                                        XInternAtom(tempdisp,
                                                    "GHOSTVIEW_COLORS", false),
                                        XInternAtom(tempdisp, "STRING", false),
                                        8, PropModeReplace, 
-                                       reinterpret_cast<unsigned char*>(tbuf),
-                                       strlen(tbuf));
+                                       reinterpret_cast<unsigned char*>(const_cast<char*>(t3.str())),
+                                       ::strlen(t3.str()));
+#endif
                        XUngrabServer(tempdisp);
                        XFlush(tempdisp);
+                       
                        if (lyxerr.debugging()) {
                                lyxerr << "Releasing the server" << endl;
                        }
                        XCloseDisplay(tempdisp);
 
                        // set up environment
-                       while (environ[ne]) ++ne;
+                       while (environ[ne])
+                               ++ne;
                        typedef char * char_p;
                        env = new char_p[ne + 2];
+#ifdef HAVE_SSTREAM
+                       string tmp = t2.str().c_str();
+                       env[0] = new char[tmp.size() + 1];
+                       std::copy(tmp.begin(), tmp.end(), env[0]);
+                       env[0][tmp.size()] = '\0';
+#else
                        env[0] = tbuf2;
-                       memcpy(&env[1], environ, sizeof(char*) * (ne + 1));
+#endif
+                       ::memcpy(&env[1], environ, sizeof(char*) * (ne + 1));
                        environ = env;
 
                        // now make gs command
                        // close(0);
                        // close(1); do NOT close. If GS writes out
                        // errors it would hang. (Matthias 290596) 
-                       sprintf(rbuf, "-r%gx%g", p->rx, p->ry);
-                       sprintf(gbuf, "-g%dx%d", p->data->wid, p->data->hgh);
+
+                       string rbuf = "-r" + tostr(p->rx) + "x" + tostr(p->ry);
+                       string gbuf = "-g" + tostr(p->data->wid) + "x" + tostr(p->data->hgh);
+
                        // now chdir into dir with .eps file, to be on the safe
                        // side
-                       chdir(OnlyPath(p->data->fname).c_str());
+                       ::chdir(OnlyPath(p->data->fname).c_str());
                        // make temp file name
-                       sprintf(tbuf, "%s/~lyxgs%d.ps", system_tempdir.c_str(),
-                               int(getpid()));
+                       string tmpf = make_tmp(getpid());
                        if (lyxerr.debugging()) {
-                               lyxerr << "starting gs " << tbuf << " "
+                               lyxerr << "starting gs " << tmpf << " "
                                       << p->data->fname
                                       << ", pid: " << getpid() << endl;
                        }
 
-                       int err = execlp(lyxrc.ps_command.c_str(), 
+                       int err = ::execlp(lyxrc.ps_command.c_str(), 
                                         lyxrc.ps_command.c_str(), 
                                         "-sDEVICE=x11",
                                         "-dNOPAUSE", "-dQUIET",
                                         "-dSAFER", 
-                                        rbuf, gbuf, tbuf
+                                        rbuf.c_str(), gbuf.c_str(), tmpf.c_str()
                                         p->data->fname.c_str(), 
                                         "showpage.ps", "quit.ps", "-", 0);
                        // if we are still there, an error occurred.
@@ -670,7 +733,7 @@ void runqueue()
                        lyxerr.debug() << "Cmd: " 
                                       << lyxrc.ps_command
                                       << " -sDEVICE=x11 "
-                                      << tbuf << ' '
+                                      << tmpf.c_str() << ' '
                                       << p->data->fname << endl;
                        _exit(0);       // no gs?
                }
@@ -707,12 +770,13 @@ void addwait(int psx, int psy, int pswid, int pshgh, figdata * data)
 
 static
 figdata * getfigdata(int wid, int hgh, string const & fname, 
-                           int psx, int psy, int pswid, int pshgh, 
-                           int raw_wid, int raw_hgh, float angle, char flags)
+                    int psx, int psy, int pswid, int pshgh, 
+                    int raw_wid, int raw_hgh, float angle, char flags)
 {
        /* first search for an exact match with fname and width/height */
 
-       if (fname.empty()) return 0;
+       if (fname.empty() || !IsFileReadable(fname)) 
+               return 0;
 
        for (bitmaps_type::iterator it = bitmaps.begin();
             it != bitmaps.end(); ++it) {
@@ -817,11 +881,7 @@ void sigchldchecker(pid_t pid, int * status)
                                p->broken = false;
                        } else {
                                // remove temporary files
-                               char tmp[128];
-                               sprintf(tmp, "%s/~lyxgs%d.ps", 
-                                       system_tempdir.c_str(),
-                                       p->gspid);
-                               unlink(tmp);
+                               unlink(make_tmp(p->gspid).c_str());
                                p->gspid = -1;
                                p->broken = true;
                        }
@@ -914,7 +974,7 @@ void UnregisterFigure(InsetFig * fi)
                        fl_hide_form(tmpfig->inset->form->Figure);
                }
 #if FL_REVISION == 89
-#warning Reactivate this free_form calls
+               // CHECK Reactivate this free_form calls
 #else
                fl_free_form(tmpfig->inset->form->Figure);
                free(tmpfig->inset->form); // Why free?
@@ -928,22 +988,6 @@ void UnregisterFigure(InsetFig * fi)
 }
 
 
-static
-string NextToken(istream & is)
-{
-       string token;
-       char c;
-       if (!is.eof()) {
-               do {
-                       is.get(c);
-                       token += c;
-               } while (!is.eof() && !isspace(c));
-               token.erase(token.length() - 1); // remove the isspace
-       }
-       return token;
-}
-
-
 InsetFig::InsetFig(int tmpx, int tmpy, Buffer * o)
        : owner(o)
 {
@@ -1019,17 +1063,22 @@ void InsetFig::draw(Painter & pain, LyXFont const & f,
                if (figure && figure->data) {
                        if (figure->data->broken)  msg = _("[render error]");
                        else if (figure->data->reading) msg = _("[rendering ... ]");
-               } else 
-                       if (fname.empty()) msg = _("[no file]");
-                       else if ((flags & 3) == 0) msg = _("[not displayed]");
-                       else if (lyxrc.ps_command.empty()) msg = _("[no ghostscript]");
+               } 
+               else if (fname.empty()) 
+                       msg = _("[no file]");
+               else if (!IsFileReadable(fname))
+                       msg = _("[bad file name]");
+               else if ((flags & 3) == 0) 
+                       msg = _("[not displayed]");
+               else if (lyxrc.ps_command.empty()) 
+                       msg = _("[no ghostscript]");
                
                if (!msg) msg = _("[unknown error]");
                
                font.setFamily(LyXFont::SANS_FAMILY);
                font.setSize(LyXFont::SIZE_FOOTNOTE);
                string justname = OnlyFilename (fname);
-               pain.text(int(x + 8), baseline - font.maxAscent() - 4,
+               pain.text(int(x + 8), baseline - lyxfont::maxAscent(font) - 4,
                          justname, font);
                
                font.setSize(LyXFont::SIZE_TINY);
@@ -1143,7 +1192,7 @@ void InsetFig::Read(LyXLex & lex)
 
 
 int InsetFig::Latex(ostream & os,
-                   signed char /* fragile*/, bool /* fs*/) const
+                   bool /* fragile*/, bool /* fs*/) const
 {
        Regenerate();
        if (!cmd.empty()) os << cmd << " ";
@@ -1151,6 +1200,12 @@ int InsetFig::Latex(ostream & os,
 }
 
 
+int InsetFig::Ascii(ostream &) const
+{
+       return 0;
+}
+
+
 int InsetFig::Linuxdoc(ostream &) const
 {
        return 0;
@@ -1162,7 +1217,7 @@ int InsetFig::DocBook(ostream & os) const
        string figurename = fname;
 
        if(suffixIs(figurename, ".eps"))
-               figurename.erase(fname.length() - 5);
+               figurename.erase(fname.length() - 4);
 
        os << "@<graphic fileref=\"" << figurename << "\"></graphic>";
        return 0;
@@ -1188,6 +1243,12 @@ bool InsetFig::Deletable() const
 }
 
 
+char const * InsetFig::EditMessage() const 
+{
+       return _("Opened figure");
+}
+
+
 void InsetFig::Edit(BufferView * bv, int, int, unsigned int)
 {
        lyxerr.debug() << "Editing InsetFig." << endl;
@@ -1244,7 +1305,8 @@ Inset * InsetFig::Clone() const
        tmp->pswid = pswid;
        tmp->pshgh = pshgh;
        tmp->fname = fname;
-       if (!fname.empty() && (flags & 3) && !lyxrc.ps_command.empty()
+       if (!fname.empty() && IsFileReadable(fname) 
+           && (flags & 3) && !lyxrc.ps_command.empty()
            && lyxrc.use_gui) { 
                // do not display if there is
                // "do not display" chosen (Matthias 260696)
@@ -1266,6 +1328,31 @@ Inset::Code InsetFig::LyxCode() const
 }
 
 
+static
+string stringify(InsetFig::HWTYPE hw, float f, string suffix)
+{
+       string res;
+       switch (hw) {
+               case InsetFig::DEF:
+                       break;
+               case InsetFig::CM:// \resizebox*{h-length}{v-length}{text}
+                       res = tostr(f) + "cm";
+                       break;
+               case InsetFig::IN: 
+                       res = tostr(f) + "in";
+                       break;
+               case InsetFig::PER_PAGE:
+                       res = tostr(f/100) + "\\text" + suffix;
+                       break;
+               case InsetFig::PER_COL:
+                       // Doesn't occur for htype...
+                       res = tostr(f/100) + "\\column" + suffix;
+                       break;
+       }
+       return res;
+}
+
+
 void InsetFig::Regenerate() const
 {
        string cmdbuf;
@@ -1283,69 +1370,8 @@ void InsetFig::Regenerate() const
        string fname2 = MakeRelPath(fname, buf1);
 
        string gcmd = "\\includegraphics{" + fname2 + '}';
-       
-       switch (wtype) {
-       case DEF:
-               break;
-       case CM:{// \resizebox*{h-length}{v-length}{text}
-               char buf[10];
-               sprintf(buf, "%g", xwid); // should find better
-               resizeW = buf;
-               resizeW += "cm";
-               break;
-       }
-       case IN: {
-               char buf[10];
-               sprintf(buf, "%g", xwid);
-               resizeW = buf;
-               resizeW += "in";
-               break;
-       }
-       case PER_PAGE:{
-               char buf[10];
-               sprintf(buf, "%g", xwid/100);
-               resizeW = buf;
-               resizeW += "\\textwidth";
-               break;
-       }
-       case PER_COL:{
-               char buf[10];
-               sprintf(buf, "%g", xwid/100);
-               resizeW = buf;
-               resizeW += "\\columnwidth";
-               break;
-       }
-       }
-
-       switch (htype) {
-       case DEF:
-               break;
-       case CM: {
-               char buf[10];
-               sprintf(buf, "%g", xhgh);
-               resizeH = buf;
-               resizeH += "cm";
-               break;
-       }
-       case IN:{
-               char buf[10];
-               sprintf(buf, "%g", xhgh);
-               resizeH = buf;
-               resizeH += "in";
-               break;
-       }
-       case PER_PAGE: {
-               char buf[10];
-               sprintf(buf, "%g", xhgh/100);
-               resizeH = buf;
-               resizeH += "\\textheight";
-               break;
-       }
-       case PER_COL: {
-                // Doesn't occur; case exists to suppress compiler warnings.
-               break;
-       }
-       }
+       resizeW = stringify(wtype, xwid, "width");
+       resizeH = stringify(htype, xhgh, "height");
 
        if (!resizeW.empty() || !resizeH.empty()) {
                recmd = "\\resizebox*{";
@@ -1363,12 +1389,8 @@ void InsetFig::Regenerate() const
        
        
        if (angle != 0) {
-               char buf[10];
-               sprintf(buf, "%g", angle);
                // \rotatebox{angle}{text}
-               rotate = "\\rotatebox{";
-               rotate += buf;
-               rotate += "}{";
+               rotate = "\\rotatebox{" + tostr(angle) + "}{";
        }
 
        cmdbuf = recmd;
@@ -1401,8 +1423,8 @@ void InsetFig::TempRegenerate()
        float txhgh = atof(fl_get_input(form->Height));
 
        if (!tfname || !*tfname) {
-               cmd = "\\fbox{\\rule[-0.5in]{0pt}{1in}";
-               cmd += _("empty figure path");
+               cmd = "\\fbox{\\rule[-0.5in]{0pt}{1in}";
+               cmd += _("empty figure path");
                cmd += '}';
                return;
        }
@@ -1411,69 +1433,9 @@ void InsetFig::TempRegenerate()
        string fname2 = MakeRelPath(tfname, buf1);
        // \includegraphics*[<llx,lly>][<urx,ury>]{file}
        string gcmd = "\\includegraphics{" + fname2 + '}';
-       
-       switch (twtype) {
-       case DEF:
-               break;
-       case CM: {// \resizebox*{h-length}{v-length}{text}
-               char buf[10];
-               sprintf(buf, "%g", txwid); // should find better
-               resizeW = buf;
-               resizeW += "cm";
-               break;
-       }
-       case IN: {
-               char buf[10];
-               sprintf(buf, "%g", txwid);
-               resizeW = buf;
-               resizeW += "in";
-               break;
-       }
-       case PER_PAGE: {
-               char buf[10];
-               sprintf(buf, "%g", txwid/100);
-               resizeW = buf;
-               resizeW += "\\textwidth";
-               break;
-       }
-       case PER_COL: {
-               char buf[10];
-               sprintf(buf, "%g", txwid/100);
-               resizeW = buf;
-               resizeW += "\\columnwidth";
-               break;
-       }
-       }
 
-       switch (thtype) {
-       case DEF:
-               break;
-       case CM: {
-               char buf[10];
-               sprintf(buf, "%g", txhgh);
-               resizeH = buf;
-               resizeH += "cm";
-               break;
-       }
-       case IN: {
-               char buf[10];
-               sprintf(buf, "%g", txhgh);
-               resizeH = buf;
-               resizeH += "in";
-               break;
-       }
-       case PER_PAGE: {
-               char buf[10];
-               sprintf(buf, "%g", txhgh/100);
-               resizeH = buf;
-               resizeH += "\\textheight";
-               break;
-       }
-       case PER_COL: {
-                // Doesn't occur; case exists to suppress compiler warnings.
-               break;
-       }
-       }
+       resizeW = stringify(twtype, txwid, "width");    
+       resizeH = stringify(thtype, txhgh, "height");   
 
        // \resizebox*{h-length}{v-length}{text}
        if (!resizeW.empty() || !resizeH.empty()) {
@@ -1491,17 +1453,11 @@ void InsetFig::TempRegenerate()
        }
        
        if (tangle != 0) {
-               char buf[10];
-               sprintf(buf, "%g", tangle);
                // \rotatebox{angle}{text}
-               rotate = "\\rotatebox{";
-               rotate += buf;
-               rotate += "}{";
+               rotate = "\\rotatebox{" + tostr(tangle) + "}{";
        }
 
-       cmdbuf = recmd;
-       cmdbuf += rotate;
-       cmdbuf += gcmd;
+       cmdbuf = recmd + rotate + gcmd;
        if (!rotate.empty()) cmdbuf += '}';
        if (!recmd.empty()) cmdbuf += '}';
        if (psubfigure && !tsubcap.empty()) {
@@ -1526,7 +1482,7 @@ void InsetFig::Recompute()
        /* now recompute wid and hgh, and if that is changed, set changed */
        /* this depends on chosen size of the picture and its bbox */
        // This will be redone in 0.13 ... (hen)
-       if (!fname.empty()) {
+       if (!fname.empty() && IsFileReadable(fname)) {
                // say, total width is 595 pts, as A4 in TeX, thats in 1/72" */
 
                newx = frame_wid;
@@ -1605,7 +1561,7 @@ void InsetFig::Recompute()
                figdata * pf = figure->data;
 
                // get new data
-               if (!fname.empty() && (flags & 3)
+               if (!fname.empty() && IsFileReadable(fname) && (flags & 3)
                    && !lyxrc.ps_command.empty()) {
                        // do not display if there is "do not display"
                        // chosen (Matthias 260696)
@@ -1656,11 +1612,8 @@ void InsetFig::GetPSSizes()
                        break;
                }
                if (c == '%' && lastchar == '%') {
-                       p = NextToken(ifs);
+                       ifs >> p;
                        if (p.empty()) break;
-                       // we should not use this, with it we cannot
-                       // discover bounding box and end of file.
-                       //if (strcmp(p, "EndComments") == 0) break;
                        lyxerr.debug() << "Token: `" << p << "'" << endl;
                        if (p == "BoundingBox:") {
                                float fpsx, fpsy, fpswid, fpshgh;
@@ -1815,7 +1768,7 @@ void InsetFig::CallbackFig(long arg)
                        } else {
                                if (!fname.empty()) {
                                        changedfname = true;
-                                       fname.clear();
+                                       fname.erase();
                                }
                        }
                        subcaption = fl_get_input(form->Subcaption);
@@ -1832,7 +1785,7 @@ void InsetFig::CallbackFig(long arg)
                                fl_set_focus_object(form->Figure, form->OkBtn);
                                fl_hide_form(form->Figure);
 #if FL_REVISION == 89
-#warning Reactivate this free_form calls
+                               // CHECK Reactivate this free_form calls
 #else
                                fl_free_form(form->Figure);
                                free(form); // Why free?
@@ -1849,8 +1802,8 @@ void InsetFig::CallbackFig(long arg)
                fl_set_focus_object(form->Figure, form->OkBtn);
                fl_hide_form(form->Figure);
 #if FL_REVISION == 89
-#warning Reactivate this free_form calls
-#warning Jug, is this still a problem?
+               // CHECK Reactivate this free_form calls
+               // Jug, is this still a problem?
 #else
                fl_free_form(form->Figure);
                free(form); // Why free?
@@ -1939,8 +1892,6 @@ void EnableFigurePanel(FD_Figure * const form)
 
 void InsetFig::RestoreForm()
 {
-       char buf[32];
-
        EnableFigurePanel(form);
 
        twtype = wtype;
@@ -1978,12 +1929,9 @@ void InsetFig::RestoreForm()
        fl_set_button(form->Subfigure, (subfigure != 0));
        pflags = flags;
        psubfigure = subfigure;
-       sprintf(buf, "%g", xwid);
-       fl_set_input(form->Width, buf);
-       sprintf(buf, "%g", xhgh);
-       fl_set_input(form->Height, buf);
-       sprintf(buf, "%g", angle);
-       fl_set_input(form->Angle, buf);
+       fl_set_input(form->Width, tostr(xwid).c_str());
+       fl_set_input(form->Height, tostr(xhgh).c_str());
+       fl_set_input(form->Angle, tostr(angle).c_str());
        if (!fname.empty()){
                string buf1 = OnlyPath(owner->fileName());
                string fname2 = MakeRelPath(fname, buf1);
@@ -2057,7 +2005,7 @@ void InsetFig::BrowseFile()
 
        bool error = false;
        do {
-               ProhibitInput();
+               ProhibitInput(current_view);
                if (once) {
                        p = fileDlg.Select(_("EPS Figure"),
                                           current_figure_path,
@@ -2066,7 +2014,7 @@ void InsetFig::BrowseFile()
                        p = fileDlg.Select(_("EPS Figure"), buf,
                                           "*ps", string());
                }
-               AllowInput();
+               AllowInput(current_view);
 
                if (p.empty()) return;
 
@@ -2075,14 +2023,13 @@ void InsetFig::BrowseFile()
                once = 1;
                
                if (contains(p, "#") || contains(p, "~") || contains(p, "$")
-                   || contains(p, "%") || contains(p, " ")) 
-                       {
-                               WriteAlert(_("Filename can't contain any "
-                                            "of these characters:"),
-                                          // xgettext:no-c-format
-                                          _("space, '#', '~', '$' or '%'.")); 
-                               error = true;
-                       }
+                   || contains(p, "%") || contains(p, " ")) {
+                       WriteAlert(_("Filename can't contain any "
+                                    "of these characters:"),
+                                  // xgettext:no-c-format
+                                  _("space, '#', '~', '$' or '%'.")); 
+                       error = true;
+               }
        } while (error);
 
        if (form) fl_set_input(form->EpsFile, buf.c_str());