]> git.lyx.org Git - lyx.git/blobdiff - src/insets/figinset.C
pos=string::npos for regex not found, use handcoded transform in lstring.C, fix the...
[lyx.git] / src / insets / figinset.C
index b1442aeb580746c47b6b22eef52aa761f7276355..549c766d35b25b6a0d8325f4a3c3d77e54c96cd0 100644 (file)
@@ -40,9 +40,16 @@ extern long int background_pixels;
 #include <cctype>
 #include <cmath>
 #include <fstream>
+#include <queue>
+#include <list>
+#include <algorithm>
 using std::ofstream;
+using std::ifstream;
+using std::queue;
+using std::list;
+using std::find;
+using std::flush;
 
-#include "form1.h"
 #include "figinset.h"
 #include "lyx.h"
 #include "lyx_main.h"
@@ -57,6 +64,7 @@ using std::ofstream;
 #include "gettext.h"
 #include "lyx_gui_misc.h" // CancelCloseBoxCB
 #include "support/FileInfo.h"
+#include "support/lyxlib.h"
 
 extern BufferView * current_view;
 #if 0
@@ -78,31 +86,27 @@ void PutInsetIntoInsetUpdateList(Inset * inset);
 extern void ProhibitInput();
 extern void AllowInput();
 
-#define DEG2PI 57.295779513
-#define figallocchunk 32
+static float const DEG2PI = 57.295779513;
+static int const figallocchunk = 32;
 
 static int figinsref = 0;      /* number of figures */
 static int figarrsize = 0;     /* current max number of figures */
 static int bmpinsref = 0;      /* number of bitmaps */
 static int bmparrsize = 0;     /* current max number of bitmaps */
 
-struct queue {
-       float rx, ry;           /* resolution x and y */
-       int ofsx, ofsy;         /* x and y translation */
-       figdata * data;         /* we are doing it for this data */
-       queue * next;           /* next item in queue */
+struct queue_element {
+       float rx, ry;          // resolution x and y
+       int ofsx, ofsy;        // x and y translation
+       figdata * data;        // we are doing it for this data
 };
 
-struct pidwait {
-       int pid;                /* pid to wait for */
-       pidwait * next; /* next */
-};
-
-#define MAXGS 3                        /* maximum 3 gs's at a time */
+static int const MAXGS = 3;                    /* maximum 3 gs's at a time */
 
 static Figref ** figures;      /* all the figures */
 static figdata ** bitmaps;     /* all the bitmaps */
-static queue * gsqueue = 0;    /* queue for ghostscripting */
+
+static queue<queue_element> gsqueue; // queue for ghostscripting
+
 static int gsrunning = 0;      /* currently so many gs's are running */
 static bool bitmap_waiting = false; /* bitmaps are waiting finished */
 static char bittable[256];     /* bit reversion table */
@@ -116,33 +120,28 @@ static int gs_spc;                        // shades per color
 static bool gs_gray;                   // is grayscale?
 static int gs_allcolors;               // number of all colors
 
-static pidwait * pw = 0;               // pid wait list
-
+static list<int> pidwaitlist; // pid wait list
 
-extern FD_form_main * fd_form_main;
 extern Colormap color_map;
 
 void addpidwait(int pid)
 {
        // adds pid to pid wait list
-       register pidwait * p = new pidwait;
-
-       p->pid = pid;
-       p->next = pw;
-       pw = p;
+       pidwaitlist.push_back(pid);
 
        if (lyxerr.debugging()) {
-               lyxerr << "Pids to wait for: " << p->pid << endl;
-               while (p->next) {
-                       p = p->next;
-                       lyxerr << p->pid << endl;
+               lyxerr << "Pids to wait for: \n";
+               for (list<int>::const_iterator cit = pidwaitlist.begin();
+                    cit != pidwaitlist.end(); ++cit) {
+                       lyxerr << (*cit) << '\n';
                }
+               lyxerr << flush;
        }
 }
 
 
 extern "C" int GhostscriptMsg(FL_OBJECT *, Window, int, int,
-                  XEvent * ev, void *)
+                             XEvent * ev, void *)
 {
        char tmp[128];
 
@@ -154,6 +153,7 @@ extern "C" 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 (int i = 0; i < bmpinsref; ++i)
                if ((long)bitmaps[i]->bitmap == (long)e->data.l[1]) {
                        // found the one
@@ -161,7 +161,8 @@ extern "C" int GhostscriptMsg(FL_OBJECT *, Window, int, int,
                        p->gsdone = true;
 
                        // first update p->bitmap, if necessary
-                       if (p->bitmap != None && p->flags > (1|8) && gs_color && p->wid) {
+                       if (p->bitmap != None
+                           && p->flags > (1|8) && gs_color && p->wid) {
                                // query current colormap and re-render
                                // the pixmap with proper colors
                                //XColor * cmap;
@@ -173,8 +174,10 @@ extern "C" int GhostscriptMsg(FL_OBJECT *, Window, int, int,
                                Display * tmpdisp;
                                GC gc = getGC(gc_copy);
 
-                               XGetWindowAttributes(fl_display, fl_get_canvas_id(
-                                       figinset_canvas), &wa);
+                               XGetWindowAttributes(fl_display,
+                                                    fl_get_canvas_id(
+                                                            figinset_canvas),
+                                                    &wa);
                                XFlush(fl_display);
                                if (lyxerr.debugging()) {
                                        lyxerr << "Starting image translation "
@@ -191,15 +194,15 @@ extern "C" int GhostscriptMsg(FL_OBJECT *, Window, int, int,
                                                << "Cannot fork, using slow "
                                                "method for pixmap translation." << endl;
                                        tmpdisp = fl_display;
-                               } else if (forkstat > 0) {
+                               } else if (forkstat > 0) { // parent
                                        // register child
                                        if (lyxerr.debugging()) {
                                                lyxerr << "Spawned child "
                                                       << forkstat << endl;
                                        }
                                        addpidwait(forkstat);
-                                       break; // in parent process
-                               } else {
+                                       break;
+                               } else {  // child
                                        tmpdisp = XOpenDisplay(XDisplayName(0));
                                        XFlush(tmpdisp);
                                }
@@ -217,44 +220,46 @@ extern "C" int GhostscriptMsg(FL_OBJECT *, Window, int, int,
                                }
                                {
                                // query current colormap
-                               //cmap = (XColor *) malloc(gs_allcolors*sizeof(XColor));
-                               XColor * cmap = new XColor[gs_allcolors];
-                               for (i = 0; i < gs_allcolors; ++i) cmap[i].pixel = i;
-                               XQueryColors(tmpdisp, color_map, cmap, gs_allcolors);
-                               XFlush(tmpdisp);
-                               wid1 = p->wid - 1;
+                                       XColor * cmap = new XColor[gs_allcolors];
+                                       for (i = 0; i < gs_allcolors; ++i) cmap[i].pixel = i;
+                                       XQueryColors(tmpdisp, color_map, cmap, gs_allcolors);
+                                       XFlush(tmpdisp);
+                                       wid1 = p->wid - 1;
                                // now we process all the image
-                               for (y = 0; y < p->hgh; ++y) {
-                                       for (int x = 0; x < wid; ++x) {
-                                               XColor * pc = cmap +
-                                                       XGetPixel(im, x, y);
-                                               XFlush(tmpdisp);
-                                               XPutPixel(im, x, y,
-                                                         gs_pixels[((pc->red+6553)*
-                                                                              spc1/65535)*spc2+((pc->green+6553)*
-                                                                                                spc1/65535)*gs_spc+((pc->blue+6553)*
-                                                                                                                    spc1/65535)]);
-                                               XFlush(tmpdisp);
+                                       for (y = 0; y < p->hgh; ++y) {
+                                               for (int x = 0; x < wid; ++x) {
+                                                       XColor * pc = cmap +
+                                                               XGetPixel(im, x, y);
+                                                       XFlush(tmpdisp);
+                                                       XPutPixel(im, x, y,
+                                                                 gs_pixels[((pc->red+6553)*
+                                                                            spc1/65535)*spc2+((pc->green+6553)*
+                                                                                              spc1/65535)*gs_spc+((pc->blue+6553)*
+                                                                                                                  spc1/65535)]);
+                                                       XFlush(tmpdisp);
+                                               }
                                        }
-                               }
                                // This must be correct.
-                               delete [] cmap;
-                               if (lyxerr.debugging()) {
-                                       lyxerr << "Putting image back" << endl;
-                               }
-                               XPutImage(tmpdisp, p->bitmap, gc, im, 0, 0,
-                                         0, 0, p->wid, p->hgh);
-                               XDestroyImage(im);
-                               if (lyxerr.debugging()) {
-                                       lyxerr << "Done translation" << endl;
-                               }
+                                       delete [] cmap;
+                                       if (lyxerr.debugging()) {
+                                               lyxerr << "Putting image back"
+                                                      << endl;
+                                       }
+                                       XPutImage(tmpdisp, p->bitmap,
+                                                 gc, im, 0, 0,
+                                                 0, 0, p->wid, p->hgh);
+                                       XDestroyImage(im);
+                                       if (lyxerr.debugging()) {
+                                               lyxerr << "Done translation"
+                                                      << endl;
+                                       }
                                }
                          noim:
                                if (lyxerr.debugging()) {
                                        lyxerr << "Killing gs " 
                                               << p->gspid << endl;
                                }
-                               kill(p->gspid, SIGHUP);
+                               lyx::kill(p->gspid, SIGHUP);
 
                                sprintf(tmp, "%s/~lyxgs%d.ps",
                                        system_tempdir.c_str(), 
@@ -269,7 +274,7 @@ extern "C" int GhostscriptMsg(FL_OBJECT *, Window, int, int,
                                        lyxerr << "Killing gs " 
                                               << p->gspid << endl;
                                }
-                               kill(p->gspid, SIGHUP);
+                               lyx::kill(p->gspid, SIGHUP);
 
                                sprintf(tmp, "%s/~lyxgs%d.ps", 
                                        system_tempdir.c_str(),
@@ -285,9 +290,6 @@ extern "C" int GhostscriptMsg(FL_OBJECT *, Window, int, int,
 static void AllocColors(int num)
 // allocate color cube numxnumxnum, if possible
 {
-       XColor xcol;
-       int i;
-
        if (lyxerr.debugging()) {
                lyxerr << "Allocating color cube " << num
                       << 'x' << num << 'x' << num << endl;
@@ -299,10 +301,11 @@ static void AllocColors(int num)
                return;
        }
        if (num > 5) num = 5;
-       for (i = 0; i < num*num*num; ++i) {
-               xcol.red = 65535*(i/(num*num))/(num-1);
-               xcol.green = 65535*((i/num) % num)/(num-1);
-               xcol.blue = 65535*(i % num)/(num-1);
+       XColor xcol;
+       for (int i = 0; i < num * num * num; ++i) {
+               xcol.red = 65535 * (i / (num * num)) / (num - 1);
+               xcol.green = 65535 * ((i / num) % num) / (num - 1);
+               xcol.blue = 65535 * (i % num) / (num - 1);
                xcol.flags = DoRed | DoGreen | DoBlue;
                if (!XAllocColor(fl_display, color_map, &xcol)) {
                        if (i) XFreeColors(fl_display, color_map,
@@ -311,7 +314,7 @@ static void AllocColors(int num)
                                lyxerr << "Cannot allocate color cube "
                                       << num << endl;;
                        }
-                       AllocColors(num-1);
+                       AllocColors(num - 1);
                        return;
                }
                gs_pixels[i] = xcol.pixel;
@@ -319,18 +322,16 @@ static void AllocColors(int num)
        gs_color = true;
        gs_gray = false;
        gs_spc = num;
-       gs_num_pixels = num*num*num;
+       gs_num_pixels = num * num * num;
 }
 
 
-static void AllocGrays(int num)
 // allocate grayscale ramp
+static
+void AllocGrays(int num)
 {
-       XColor xcol;
-       int i;
-
        if (lyxerr.debugging()) {
-               lyxerr << "Allocating grayscale ramp "
+               lyxerr << "Allocating grayscale colormap "
                       << num << endl;
        }
 
@@ -340,8 +341,9 @@ static void AllocGrays(int num)
                return;
        }
        if (num > 128) num = 128;
-       for (i = 0; i < num; ++i) {
-               xcol.red = xcol.green = xcol.blue = 65535*i/(num-1);
+       XColor xcol;
+       for (int i = 0; i < num; ++i) {
+               xcol.red = xcol.green = xcol.blue = 65535 * i / (num - 1);
                xcol.flags = DoRed | DoGreen | DoBlue;
                if (!XAllocColor(fl_display, color_map, &xcol)) {
                        if (i) XFreeColors(fl_display, color_map,
@@ -350,7 +352,7 @@ static void AllocGrays(int num)
                                lyxerr << "Cannot allocate grayscale " 
                                       << num << endl;
                        }
-                       AllocGrays(num/2);
+                       AllocGrays(num / 2);
                        return;
                }
                gs_pixels[i] = xcol.pixel;
@@ -363,22 +365,26 @@ static void AllocGrays(int num)
 
 void InitFigures()
 {
-       unsigned int i, j, k;
-       Visual *vi;
-
        bmparrsize = figarrsize = figallocchunk;
-       figures = (Figref**) malloc(sizeof(Figref*)*figallocchunk);
-       bitmaps = (figdata**) malloc(sizeof(figdata*)*figallocchunk);
-
-       for (i = 0; i < 256; ++i) {
+       typedef Figref * Figref_p;
+       figures = new Figref_p[figallocchunk];
+       //figures = static_cast<Figref**>
+       //      (malloc(sizeof(Figref*) * figallocchunk));
+       typedef figdata * figdata_p;
+       bitmaps = new figdata_p[figallocchunk];
+       //bitmaps = static_cast<figdata**>
+       //(malloc(sizeof(figdata*) * figallocchunk));
+
+       unsigned int k;
+       for (unsigned int i = 0; i < 256; ++i) {
                k = 0;
-               for (j = 0; j < 8; ++j)
+               for (unsigned int j = 0; j < 8; ++j)
                        if (i & (1 << (7-j))) k |= 1 << j;
-               bittable[i] = (char) ~k;
+               bittable[i] = char(~k);
        }
 
        fl_add_canvas_handler(figinset_canvas, ClientMessage,
-                             GhostscriptMsg, fd_form_main);
+                             GhostscriptMsg, current_view->owner()->getMainForm());
 
        // now we have to init color_map
        if (!color_map) color_map = DefaultColormap(fl_display,
@@ -387,16 +393,16 @@ void InitFigures()
        // first get visual
        gs_color = false;
 
-       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);
        }
        color_visual = ( (vi->c_class == StaticColor) ||
-               (vi->c_class == PseudoColor) ||
-               (vi->c_class == TrueColor) ||
-               (vi->c_class == DirectColor) );
+                        (vi->c_class == PseudoColor) ||
+                        (vi->c_class == TrueColor) ||
+                        (vi->c_class == DirectColor) );
        if ((vi->c_class & 1) == 0) return;
        // now allocate colors
        if (vi->c_class == GrayScale) {
@@ -405,7 +411,7 @@ void InitFigures()
        } else {
                // allocate normal color
                int i = 5;
-               while (i*i*i*2 > vi->map_entries) --i;
+               while (i * i * i * 2 > vi->map_entries) --i;
                AllocColors(i);
        }
        gs_allcolors = vi->map_entries;
@@ -414,8 +420,10 @@ void InitFigures()
 
 void DoneFigures()
 {
-       free(figures);
-       free(bitmaps);
+       //free(figures);
+       //free(bitmaps);
+       delete[] figures;
+       delete[] bitmaps;
        figarrsize = 0;
        bmparrsize = 0;
 
@@ -433,7 +441,7 @@ void DoneFigures()
 }
 
 
-int FindBmpIndex(figdata *tmpdata)
+int FindBmpIndex(figdata * tmpdata)
 {
        int i = 0;
        while (i < bmpinsref) {
@@ -457,7 +465,7 @@ static void chpixmap(Pixmap, int, int)
 }
 
 
-static void freefigdata(figdata *tmpdata)
+static void freefigdata(figdata * tmpdata)
 {
        tmpdata->ref--;
        if (tmpdata->ref) return;
@@ -469,7 +477,7 @@ static void freefigdata(figdata *tmpdata)
                chpixmap(tmpdata->bitmap, tmpdata->wid, tmpdata->hgh);
                // kill ghostscript and unlink it's files
                tmpdata->gspid = -1;
-               kill(pid, SIGKILL);
+               lyx::kill(pid, SIGKILL);
                sprintf(buf, "%s/~lyxgs%d.ps", system_tempdir.c_str(), pid);
                unlink(buf);
        }
@@ -479,7 +487,7 @@ static void freefigdata(figdata *tmpdata)
        int i = FindBmpIndex(tmpdata);
        --bmpinsref;
        while (i < bmpinsref) {
-               bitmaps[i] = bitmaps[i+1];
+               bitmaps[i] = bitmaps[i + 1];
                ++i;
        }
 }
@@ -500,7 +508,7 @@ static void runqueue()
                Atom * prop;
                int nprop, i;
 
-               if (!gsqueue) {
+               if (gsqueue.empty()) {
                        if (!gsrunning && gs_xcolor) {
                                // de-allocate rest of colors
                                // *****
@@ -508,10 +516,9 @@ static void runqueue()
                        }
                        return;
                }
-               queue * p = gsqueue;
-
+               queue_element * p = &gsqueue.front();
                if (!p->data) {
-                       delete p;
+                       gsqueue.pop();
                        continue;
                }
 
@@ -558,7 +565,8 @@ static void runqueue()
                        // 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!
+                       // #warning BUG seems that the only bug here
+                       // might be the hardcoded dpi.. Bummer!
                        
                        if (lyxerr.debugging()) {
                                lyxerr << "Will set GHOSTVIEW property to ["
@@ -567,35 +575,37 @@ static void runqueue()
                        // wait until property is deleted if executing multiple
                        // ghostscripts
                        for (;;) {
-                               // grab server to prevent other child interfering
-                               // with setting GHOSTVIEW property
+                               // grab server to prevent other child
+                               // interfering with setting GHOSTVIEW property
                                if (lyxerr.debugging()) {
-                                       lyxerr << "Grabbing the server" << endl;
+                                       lyxerr << "Grabbing the server"
+                                              << endl;
                                }
                                XGrabServer(tempdisp);
-                               prop = XListProperties(tempdisp, fl_get_canvas_id(
+                               prop = XListProperties(tempdisp,
+                                                      fl_get_canvas_id(
                                        figinset_canvas), &nprop);
                                if (!prop) break;
 
                                bool err = true;
                                for (i = 0; i < nprop; ++i) {
-                                       char *p = XGetAtomName(tempdisp, prop[i]);
+                                       char * p = XGetAtomName(tempdisp,
+                                                               prop[i]);
                                        if (strcmp(p, "GHOSTVIEW") == 0) {
                                                err = false;
                                                break;
                                        }
                                        XFree(p);
                                }
-                               XFree((char *)prop);    /* jc: */
+                               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
+                               // ok, property found, we must wait until
+                               // ghostscript deletes it
                                if (lyxerr.debugging()) {
-                                       lyxerr << "Releasing the server" << endl;
-                                       lyxerr << "["
+                                       lyxerr << "Releasing the server\n["
                                               << getpid()
                                               << "] GHOSTVIEW property"
                                                " found. Waiting." << endl;
@@ -645,7 +655,8 @@ static void runqueue()
 
                        XChangeProperty(tempdisp, 
                                        fl_get_canvas_id(figinset_canvas),
-                                       XInternAtom(tempdisp, "GHOSTVIEW_COLORS", false),
+                                       XInternAtom(tempdisp,
+                                                   "GHOSTVIEW_COLORS", false),
                                        XInternAtom(tempdisp, "STRING", false),
                                        8, PropModeReplace, 
                                        reinterpret_cast<unsigned char*>(tbuf),
@@ -659,9 +670,12 @@ static void runqueue()
 
                        // set up environment
                        while (environ[ne]) ++ne;
-                       env = (char **) malloc(sizeof(char*)*(ne+2));
+                       typedef char * char_p;
+                       env = new char_p[ne + 2];
+                       //env = static_cast<char **>
+                       //      (malloc(sizeof(char*) * (ne + 2)));
                        env[0] = tbuf2;
-                       memcpy(&env[1], environ, sizeof(char*)*(ne+1));
+                       memcpy(&env[1], environ, sizeof(char*) * (ne + 1));
                        environ = env;
 
                        // now make gs command
@@ -677,8 +691,9 @@ static void runqueue()
                        sprintf(tbuf, "%s/~lyxgs%d.ps", system_tempdir.c_str(),
                                int(getpid()));
                        if (lyxerr.debugging()) {
-                               printf("starting gs %s %s, pid: %d\n", tbuf,
-                                      p->data->fname.c_str(), int(getpid()));
+                               lyxerr << "starting gs " << tbuf << " "
+                                      << p->data->fname
+                                      << ", pid: " << getpid() << endl;
                        }
 
                        int err = execlp(lyxrc->ps_command.c_str(), 
@@ -703,33 +718,26 @@ static void runqueue()
                if (lyxerr.debugging()) {
                        lyxerr << "GS ["  << pid << "] started" << endl;
                }
-               gsqueue = gsqueue->next;
-               gsrunning++;
+
                p->data->gspid = pid;
-               delete p;
+               ++gsrunning;
+               gsqueue.pop();
        }
 }
 
 
-static void addwait(int psx, int psy, int pswid, int pshgh, figdata *data)
+static void addwait(int psx, int psy, int pswid, int pshgh, figdata * data)
 {
        // recompute the stuff and put in the queue
-       queue * p = new queue;
-       p->ofsx = psx;
-       p->ofsy = psy;
-       p->rx = ((float)data->raw_wid*72)/pswid;
-       p->ry = ((float)data->raw_hgh*72)/pshgh;
+       queue_element p;
+       p.ofsx = psx;
+       p.ofsy = psy;
+       p.rx = (float(data->raw_wid) * 72.0) / pswid;
+       p.ry = (float(data->raw_hgh) * 72.0) / pshgh;
 
-       p->data = data;
-       p->next = 0;
+       p.data = data;
 
-       // now put into queue
-       queue * p2 = gsqueue;
-       if (!gsqueue) gsqueue = p;
-       else {
-               while (p2->next) p2 = p2->next;
-               p2->next = p;
-       }
+       gsqueue.push(p);
 
        // if possible, run the queue
        runqueue();
@@ -759,9 +767,14 @@ static figdata * getfigdata(int wid, int hgh, string const & fname,
        if (bmpinsref > bmparrsize) {
                // allocate more space
                bmparrsize += figallocchunk;
-               figdata ** tmp = (figdata**) malloc(sizeof(figdata*)*bmparrsize);
-               memcpy(tmp, bitmaps, sizeof(figdata*)*(bmparrsize-figallocchunk));
-               free(bitmaps);
+               typedef figdata * figdata_p;
+               figdata ** tmp = new figdata_p[bmparrsize];
+               //figdata ** tmp = static_cast<figdata**>
+               //      (malloc(sizeof(figdata*) * bmparrsize));
+               memcpy(tmp, bitmaps,
+                      sizeof(figdata*) * (bmparrsize - figallocchunk));
+               delete[] bitmaps;
+               //free(bitmaps);
                bitmaps = tmp;
        }
        figdata * p = new figdata;
@@ -805,13 +818,13 @@ static figdata * getfigdata(int wid, int hgh, string const & fname,
 }
 
 
-static void getbitmap(figdata *p)
+static void getbitmap(figdata * p)
 {
        p->gspid = -1;
 }
 
 
-static void makeupdatelist(figdata *p)
+static void makeupdatelist(figdata * p)
 {
        for (int i = 0; i < figinsref; ++i)
                if (figures[i]->data == p) {
@@ -863,30 +876,22 @@ void sigchldchecker(pid_t pid, int * status)
                                p->broken = true;
                        }
                        makeupdatelist(bitmaps[i]);
-                       gsrunning--;
+                       --gsrunning;
                        runqueue();
                        pid_handled = true;
                }
        }
        if (!pid_handled) {
                lyxerr.debug() << "Checking pid in pidwait" << endl;
-               pidwait * p = pw, * prev = 0;
-               while (p) {
-                       if (pid == p->pid) {
-                               lyxerr.debug() << "Found pid in pidwait" << endl;
-                               lyxerr.debug() << "Caught child pid of recompute routine " << pid << endl;
-                               if (prev)
-                                       prev->next = p->next;
-                               else
-                                       pw = p->next;
-                               free(p);
-                               break;
-                       }
-                       prev = p;
-                       p = p->next;
+               list<int>::iterator it = find(pidwaitlist.begin(),
+                                             pidwaitlist.end(), pid);
+               if (it != pidwaitlist.end()) {
+                       lyxerr.debug() << "Found pid in pidwait\n"
+                                      << "Caught child pid of recompute "
+                               "routine" << pid << endl;
+                       pidwaitlist.erase(it);
                }
        }
-
        if (pid == -1) {
                lyxerr.debug() << "waitpid error" << endl;
                switch (errno) {
@@ -926,7 +931,7 @@ static void getbitmaps()
 }
 
 
-static void RegisterFigure(InsetFig *fi)
+static void RegisterFigure(InsetFig * fi)
 {
        if (figinsref == 0) InitFigures();
        fi->form = 0;
@@ -934,9 +939,14 @@ static void RegisterFigure(InsetFig *fi)
        if (figinsref > figarrsize) {
                // allocate more space
                figarrsize += figallocchunk;
-               Figref **tmp = (Figref**) malloc(sizeof(Figref*)*figarrsize);
-               memcpy(tmp, figures, sizeof(Figref*)*(figarrsize-figallocchunk));
-               free(figures);
+               typedef Figref * Figref_p;
+               Figref ** tmp = new Figref_p[figarrsize];
+               //Figref ** tmp = static_cast<Figref**>
+               //      (malloc(sizeof(Figref*)*figarrsize));
+               memcpy(tmp, figures,
+                      sizeof(Figref*)*(figarrsize-figallocchunk));
+               delete[] figures;
+               //free(figures);
                figures = tmp;
        }
        Figref * tmpfig = new Figref;
@@ -978,7 +988,7 @@ static void UnregisterFigure(InsetFig * fi)
 #warning Reactivate this free_form calls
 #else
                fl_free_form(tmpfig->inset->form->Figure);
-               free(tmpfig->inset->form);
+               free(tmpfig->inset->form); // Why free?
                tmpfig->inset->form = 0;
 #endif
        }
@@ -994,20 +1004,16 @@ static void UnregisterFigure(InsetFig * fi)
 }
 
 
-static char * NextToken(FILE * myfile)
+static string NextToken(istream & is)
 {
-       char * token = 0;
+       string token;
        char c;
-       int i = 0;
-   
-       if (!feof(myfile)) {
-               token = new char[256];
+       if (!is.eof()) {
                do {
-                       c = fgetc(myfile);
-                       token[i++]= c;
-               } while (!feof(myfile) && !isspace(c));
-      
-               token[i-1]= '\0';         /* just the end of a command  */
+                       is.get(c);
+                       token += c;
+               } while (!is.eof() && !isspace(c));
+               token.erase(token.length() - 1); // remove the isspace
        }
        return token;
 }
@@ -1025,6 +1031,7 @@ InsetFig::InsetFig(int tmpx, int tmpy, Buffer * o)
        pflags = flags = 9;
        psubfigure = subfigure = false;
        xwid = xhgh = angle = 0;
+       pswid = pshgh = 0;
        raw_wid = raw_hgh = 0;
        changedfname = false;
        RegisterFigure(this);
@@ -1080,15 +1087,15 @@ void InsetFig::Draw(LyXFont font, LyXScreen & scr, int baseline, float & x)
                char * msg = 0;
                // draw frame
                XDrawRectangle(fl_display, pm, getGC(gc_copy),
-                              (int) x,
+                              int(x),
                               baseline - hgh - 1, wid+1, hgh+1);
                if (figure && figure->data) {
-                 if (figure->data->broken)  msg = _("[render error]");
-                 else if (figure->data->reading) msg = _("[rendering ... ]");
+                       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]");
+                       if (fname.empty()) msg = _("[no file]");
+                       else if ((flags & 3) == 0) msg = _("[not displayed]");
+                       else if (lyxrc->ps_command.empty()) msg = _("[no ghostscript]");
 
                if (!msg) msg = _("[unknown error]");
                
@@ -1096,12 +1103,12 @@ void InsetFig::Draw(LyXFont font, LyXScreen & scr, int baseline, float & x)
                font.setSize (LyXFont::SIZE_FOOTNOTE);
                string justname = OnlyFilename (fname);
                font.drawString(justname, pm,
-                              baseline - font.maxAscent() - 4,
-                              (int) x + 8);
+                               baseline - font.maxAscent() - 4,
+                               int(x) + 8);
                font.setSize (LyXFont::SIZE_TINY);
                font.drawText (msg, strlen(msg), pm,
                               baseline - 4,
-                              (int) x + 8);
+                              int(x) + 8);
 
        }
        x += Width(font);    // ?
@@ -1113,14 +1120,14 @@ void InsetFig::Write(ostream & os)
        Regenerate();
        os << "Figure size " << wid << " " << hgh << "\n";
        if (!fname.empty()) {
-         string buf1 = OnlyPath(owner->getFileName());
-         string fname2 = MakeRelPath(fname, buf1);
-         os << "file " << fname2 << "\n";
+               string buf1 = OnlyPath(owner->fileName());
+               string fname2 = MakeRelPath(fname, buf1);
+               os << "file " << fname2 << "\n";
        }
        if (!subcaption.empty())
                os << "subcaption " << subcaption << "\n";
-       if (wtype) os << "width " << wtype << " " << xwid << "\n";
-       if (htype) os << "height " << htype << " " << xhgh << "\n";
+       if (wtype) os << "width " << static_cast<int>(wtype) << " " << xwid << "\n";
+       if (htype) os << "height " << static_cast<int>(htype) << " " << xhgh << "\n";
        if (angle != 0) os << "angle " << angle << "\n";
        os << "flags " << flags << "\n";
        if (subfigure) os << "subfigure\n";
@@ -1145,7 +1152,7 @@ void InsetFig::Read(LyXLex & lex)
                } else if (token == "file") {
                        if (lex.next()) {
                                buf = lex.GetString();
-                               string buf1 = OnlyPath(owner->getFileName());
+                               string buf1 = OnlyPath(owner->fileName());
                                fname = MakeAbsPath(buf, buf1);
                                changedfname = true;
                        }
@@ -1235,7 +1242,7 @@ int InsetFig::Linuxdoc(string &/*file*/)
 
 int InsetFig::DocBook(string & file)
 {
-       string figurename= fname;
+       string figurename = fname;
 
        if(suffixIs(figurename, ".eps"))
                figurename.erase(fname.length() - 5);
@@ -1273,7 +1280,7 @@ void InsetFig::Edit(int, int)
        // The actual prevention of altering a readonly doc
        // is done in CallbackFig()
        if(current_view->buffer()->isReadonly()) 
-               WarnReadonly();
+               WarnReadonly(current_view->buffer()->fileName());
 
        if (!form) {
                form = create_form_Figure();
@@ -1321,7 +1328,8 @@ Inset * InsetFig::Clone() const
        tmp->pshgh = pshgh;
        tmp->fname = fname;
        if (!fname.empty() && (flags & 3) && !lyxrc->ps_command.empty()) { 
-         // do not display if there is "do not display" chosen (Matthias 260696)
+               // do not display if there is
+               // "do not display" chosen (Matthias 260696)
                tmp->figure->data = getfigdata(wid, hgh, fname, psx, psy,
                                               pswid, pshgh, raw_wid, raw_hgh,
                                               angle, flags & (3|8));
@@ -1354,7 +1362,7 @@ void InsetFig::Regenerate()
                return;
        }
 
-       string buf1 = OnlyPath(owner->getFileName());
+       string buf1 = OnlyPath(owner->fileName());
        string fname2 = MakeRelPath(fname, buf1);
 
        string gcmd = "\\includegraphics{" + fname2 + '}';
@@ -1454,7 +1462,7 @@ void InsetFig::Regenerate()
        if (subfigure) {
                if (!subcaption.empty())
                        cmdbuf = "\\subfigure[" + subcaption +
-                         "]{" + cmdbuf + "}";
+                               "]{" + cmdbuf + "}";
                else
                        cmdbuf = "\\subfigure{" + cmdbuf + "}";
        }
@@ -1486,7 +1494,7 @@ void InsetFig::TempRegenerate()
                return;
        }
 
-       string buf1 = OnlyPath(owner->getFileName());
+       string buf1 = OnlyPath(owner->fileName());
        string fname2 = MakeRelPath(tfname, buf1);
        // \includegraphics*[<llx,lly>][<urx,ury>]{file}
        string gcmd = "\\includegraphics{" + fname2 + '}';
@@ -1585,7 +1593,7 @@ void InsetFig::TempRegenerate()
        if (!recmd.empty()) cmdbuf += '}';
        if (psubfigure && !tsubcap.empty()) {
                cmdbuf = string("\\subfigure{") + tsubcap
-                 + string("}{") + cmdbuf + "}";
+                       + string("}{") + cmdbuf + "}";
        }
 }
 
@@ -1599,8 +1607,8 @@ void InsetFig::Recompute()
 
        float sin_a = sin (angle / DEG2PI);  /* rotation; H. Zeller 021296 */
        float cos_a = cos (angle / DEG2PI);
-       int frame_wid = (int) ceil (fabs(cos_a * pswid) + fabs(sin_a * pshgh));
-       int frame_hgh= (int) ceil (fabs(cos_a * pshgh) + fabs(sin_a * pswid));
+       int frame_wid = int(ceil(fabs(cos_a * pswid) + fabs(sin_a * pshgh)));
+       int frame_hgh= int(ceil(fabs(cos_a * pshgh) + fabs(sin_a * pswid)));
 
        /* now recompute wid and hgh, and if that is changed, set changed */
        /* this depends on chosen size of the picture and its bbox */
@@ -1614,16 +1622,16 @@ void InsetFig::Recompute()
                case DEF:
                        break;
                case CM:        /* cm */
-                       newx = (int) (28.346*xwid);
+                       newx = int(28.346 * xwid);
                        break;
                case IN: /* in */
-                       newx = (int) (72*xwid);
+                       newx = int(72 * xwid);
                        break;
                case PER_PAGE:  /* % of page */
-                       newx = (int) (5.95*xwid);
+                       newx = int(5.95 * xwid);
                        break;
                case PER_COL:   /* % of col */
-                       newx = (int) (2.975*xwid);
+                       newx = int(2.975 * xwid);
                        break;
                }
                
@@ -1634,20 +1642,21 @@ void InsetFig::Recompute()
                        //lyxerr << "This should not happen!" << endl;
                        break;
                case CM:        /* cm */
-                       newy = (int) (28.346*xhgh);
+                       newy = int(28.346 * xhgh);
                        break;
                case IN: /* in */
-                       newy = (int) (72*xhgh);
+                       newy = int(72 * xhgh);
                        break;
                case PER_PAGE:  /* % of page */
-                       newy = (int) (8.42*xhgh);
+                       newy = int(8.42 * xhgh);
                        break;
                case PER_COL: 
                        // Doesn't occur; case exists to suppress
                        // compiler warnings.  
                         break;
                }
-               if (htype && !wtype && frame_hgh) newx = newy*frame_wid/frame_hgh;
+               if (htype && !wtype && frame_hgh)
+                       newx = newy*frame_wid/frame_hgh;
        } else {
                newx = wid;
                newy = hgh;
@@ -1656,12 +1665,12 @@ void InsetFig::Recompute()
        if (frame_wid == 0)
                nraw_x = 5;
        else
-               nraw_x = (int) ((1.0 * pswid * newx)/frame_wid);
+               nraw_x = int((1.0 * pswid * newx)/frame_wid);
 
        if (frame_hgh == 0)
                nraw_y = 5;
        else
-               nraw_y = (int) ((1.0 * pshgh * newy)/frame_hgh);
+               nraw_y = int((1.0 * pshgh * newy)/frame_hgh);
 
        // cannot be zero, actually, set it to some minimum, so its clickable
        if (newx < 5) newx = 5;
@@ -1683,7 +1692,8 @@ void InsetFig::Recompute()
                figdata * pf = figure->data;
 
                // get new data
-               if (!fname.empty() && (flags & 3) && !lyxrc->ps_command.empty()) {
+               if (!fname.empty() && (flags & 3)
+                   && !lyxrc->ps_command.empty()) {
                        // do not display if there is "do not display"
                        // chosen (Matthias 260696)
                        figure->data = getfigdata(wid, hgh, fname,
@@ -1702,11 +1712,7 @@ void InsetFig::Recompute()
 
 void InsetFig::GetPSSizes()
 {
-#ifdef WITH_WARNINGS
-#warning rewrite this method to use ifstream
-#endif
        /* get %%BoundingBox: from postscript file */
-       char * p = 0;
        
        /* defaults to associated size
         * ..just in case the PS-file is not readable (Henner, 24-Aug-97) 
@@ -1717,10 +1723,10 @@ void InsetFig::GetPSSizes()
        pshgh = hgh;
 
        if (fname.empty()) return;
+       string p;
+       ifstream ifs(fname.c_str());
 
-       FilePtr f(fname, FilePtr::read);
-
-       if (!f()) return;       // file not found !!!!
+       if (!ifs) return;       // file not found !!!!
 
        /* defaults to A4 page */
        psx = 0;
@@ -1728,30 +1734,29 @@ void InsetFig::GetPSSizes()
        pswid = 595;
        pshgh = 842;
 
-       int lastchar = fgetc(f);
+       char lastchar = 0; ifs.get(lastchar);
        for (;;) {
-               int c = fgetc(f);
-               if (c == EOF) {
+               char c = 0; ifs.get(c);
+               if (ifs.eof()) {
                        lyxerr.debug() << "End of (E)PS file reached and"
                                " no BoundingBox!" << endl;
                        break;
                }
                if (c == '%' && lastchar == '%') {
-                       p = NextToken(f);
-                       if (!p) break;
+                       p = NextToken(ifs);
+                       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;
-                       if (strcmp(p, "BoundingBox:") == 0) {
+                       lyxerr.debug() << "Token: `" << p << "'" << endl;
+                       if (p == "BoundingBox:") {
                                float fpsx, fpsy, fpswid, fpshgh;
-                               if (fscanf(f, "%f %f %f %f", &fpsx, &fpsy,
-                                          &fpswid, &fpshgh) == 4) {
-                                       psx = (int) fpsx;
-                                       psy = (int) fpsy;
-                                       pswid = (int) fpswid;
-                                       pshgh = (int) fpshgh;
-                               } 
-
+                               if (ifs >> fpsx >> fpsy >> fpswid >> fpshgh) {
+                                       psx = int(fpsx);
+                                       psy = int(fpsy);
+                                       pswid = int(fpswid);
+                                       pshgh = int(fpshgh);
+                               }
                                if (lyxerr.debugging()) {
                                        lyxerr << "%%%%BoundingBox:"
                                               << psx << ' '
@@ -1762,12 +1767,9 @@ void InsetFig::GetPSSizes()
                                break;
                        }
                        c = 0;
-                       delete[] p;
-                       p = 0;
                }
                lastchar = c;
        }
-       if (p) delete[] p;
        pswid -= psx;
        pshgh -= psy;
 
@@ -1780,7 +1782,7 @@ void InsetFig::CallbackFig(long arg)
        char const * p;
 
        if (lyxerr.debugging()) {
-               printf("Figure callback, arg %ld\n", arg);
+               lyxerr << "Figure callback, arg " << arg << endl;
        }
 
        switch (arg) {
@@ -1894,7 +1896,7 @@ void InsetFig::CallbackFig(long arg)
                        angle = atof(fl_get_input(form->Angle));
                        p = fl_get_input(form->EpsFile);
                        if (p && *p) {
-                               string buf1 = OnlyPath(owner->getFileName());
+                               string buf1 = OnlyPath(owner->fileName());
                                fname = MakeAbsPath(p, buf1);
                                changedfname = true;
                        } else {
@@ -1920,7 +1922,7 @@ void InsetFig::CallbackFig(long arg)
 #warning Reactivate this free_form calls
 #else
                                fl_free_form(form->Figure);
-                               free(form);
+                               free(form); // Why free?
                                form = 0;
 #endif
                        }
@@ -1935,9 +1937,10 @@ void InsetFig::CallbackFig(long arg)
                fl_hide_form(form->Figure);
 #if FL_REVISION == 89
 #warning Reactivate this free_form calls
+#warning Jug, is this still a problem?
 #else
                fl_free_form(form->Figure);
-               free(form);
+               free(form); // Why free?
                form = 0;
 #endif
                break;
@@ -2067,7 +2070,7 @@ void InsetFig::RestoreForm()
        sprintf(buf, "%g", angle);
        fl_set_input(form->Angle, buf);
        if (!fname.empty()){
-               string buf1 = OnlyPath(owner->getFileName());
+               string buf1 = OnlyPath(owner->fileName());
                string fname2 = MakeRelPath(fname, buf1);
                fl_set_input(form->EpsFile, fname2.c_str());
        }
@@ -2093,7 +2096,7 @@ void InsetFig::Preview(char const * p)
                return;         // parent process
        }
 
-       string buf1 = OnlyPath(owner->getFileName());
+       string buf1 = OnlyPath(owner->fileName());
        string buf2 = MakeAbsPath(p, buf1);
        
        lyxerr << "Error during rendering "
@@ -2113,25 +2116,25 @@ void InsetFig::BrowseFile()
 
        if (lyxerr.debugging()) {
                lyxerr << "Filename: "
-                      << owner->getFileName() << endl;
+                      << owner->fileName() << endl;
        }
        string p = fl_get_input(form->EpsFile);
 
-       string buf = MakeAbsPath(owner->getFileName());
+       string buf = MakeAbsPath(owner->fileName());
        string buf2 = OnlyPath(buf);
        if (!p.empty()) {
                buf = MakeAbsPath(p, buf2);
                buf = OnlyPath(buf);
        } else {
-         buf = OnlyPath(owner->getFileName().c_str());
+               buf = OnlyPath(owner->fileName().c_str());
        }
        
        // Does user clipart directory exist?
        string bufclip = AddName (user_lyxdir, "clipart");      
        FileInfo fileInfo(bufclip);
        if (!(fileInfo.isOK() && fileInfo.isDir()))
-         // No - bail out to system clipart directory
-         bufclip = AddName (system_lyxdir, "clipart"); 
+               // No - bail out to system clipart directory
+               bufclip = AddName (system_lyxdir, "clipart");   
 
 
        fileDlg.SetButton(0, _("Clipart"), bufclip); 
@@ -2141,7 +2144,8 @@ void InsetFig::BrowseFile()
        do {
                ProhibitInput();
                if (once) {
-                       p = fileDlg.Select(_("EPS Figure"), current_figure_path,
+                       p = fileDlg.Select(_("EPS Figure"),
+                                          current_figure_path,
                                           "*ps", string());
                } else {
                        p = fileDlg.Select(_("EPS Figure"), buf,
@@ -2157,11 +2161,13 @@ void InsetFig::BrowseFile()
                
                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;
-               }
+                       {
+                               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());