]> git.lyx.org Git - lyx.git/blob - src/insets/figinset.C
Add missing const to some insettoc methods
[lyx.git] / src / insets / figinset.C
1 /*
2  *      figinset.C - part of LyX project
3  */
4
5 extern long int background_pixels;
6
7 /*  Rework of path-handling (Matthias 04.07.1996 )
8  * ------------------------------------------------
9  *   figinsets keep an absolute path to the eps-file.
10  *   For the user alway a relative path appears
11  *   (in lyx-file, latex-file and edit-popup).
12  *   To calculate this relative path the path to the
13  *   document where the figinset is in is needed. 
14  *   This is done by a reference to the buffer, called
15  *   figinset::cbuffer. To be up to date the cbuffer
16  *   is sometimes set to the current buffer 
17  *   bufferlist.current(), when it can be assumed that
18  *   this operation happens in the current buffer. 
19  *   This is true for InsetFig::Edit(...), 
20  *   InsetFig::InsetFig(...), InsetFig::Read(...),
21  *   InsetFig::Write and InsetFig::Latex(...).
22  *   Therefore the bufferlist has to make sure that
23  *   during these operations bufferlist.current() 
24  *   returns the buffer where the figinsets are in.
25  *   This made few changes in buffer.C necessary.
26  *
27  * The above is not totally valid anymore. (Lgb)
28  */
29
30
31 #include <config.h>
32
33 #include <unistd.h>
34 #include <csignal>
35 #include <sys/wait.h>
36
37 #include FORMS_H_LOCATION
38 #include <cstdlib>
39 #include <cctype>
40 #include <cmath>
41 #include <fstream>
42 #include <queue>
43 #include <list>
44 #include <algorithm>
45 using std::ofstream;
46 using std::ifstream;
47 using std::queue;
48 using std::list;
49 using std::find;
50 using std::flush;
51
52 #include "figinset.h"
53 #include "lyx.h"
54 #include "lyx_main.h"
55 #include "buffer.h"
56 #include "filedlg.h"
57 #include "support/filetools.h"
58 #include "LyXView.h" // just because of form_main
59 #include "debug.h"
60 #include "LaTeXFeatures.h"
61 #include "lyxrc.h"
62 #include "gettext.h"
63 #include "lyx_gui_misc.h" // CancelCloseBoxCB
64 #include "support/FileInfo.h"
65 #include "support/lyxlib.h"
66 #include "Painter.h"
67
68 extern BufferView * current_view;
69 extern FL_OBJECT * figinset_canvas;
70
71 extern char ** environ; // is this only redundtant on linux systems? Lgb.
72 extern void UpdateInset(BufferView *, Inset * inset, bool mark_dirty = true);
73 // better for asyncron updating:
74 void PutInsetIntoInsetUpdateList(Inset * inset);
75 extern void ProhibitInput();
76 extern void AllowInput();
77
78 static float const DEG2PI = 57.295779513;
79 static int const figallocchunk = 32;
80
81 static int figinsref = 0;       /* number of figures */
82 static int figarrsize = 0;      /* current max number of figures */
83 static int bmpinsref = 0;       /* number of bitmaps */
84 static int bmparrsize = 0;      /* current max number of bitmaps */
85
86 struct queue_element {
87         float rx, ry;          // resolution x and y
88         int ofsx, ofsy;        // x and y translation
89         figdata * data;        // we are doing it for this data
90 };
91
92 static int const MAXGS = 3;                     /* maximum 3 gs's at a time */
93
94 static Figref ** figures;       /* all the figures */
95 static figdata ** bitmaps;      /* all the bitmaps */
96
97 static queue<queue_element> gsqueue; // queue for ghostscripting
98
99 static int gsrunning = 0;       /* currently so many gs's are running */
100 static bool bitmap_waiting = false; /* bitmaps are waiting finished */
101 static char bittable[256];      /* bit reversion table */
102
103 static bool gs_color;                   // do we allocate colors for gs?
104 static bool color_visual;               // is the visual color?
105 static bool gs_xcolor = false;          // allocated extended colors
106 static unsigned long gs_pixels[128];    // allocated pixels
107 static int gs_num_pixels;               // number of pixels allocated
108 static int gs_spc;                      // shades per color
109 static bool gs_gray;                    // is grayscale?
110 static int gs_allcolors;                // number of all colors
111
112 static list<int> pidwaitlist; // pid wait list
113
114 //extern Colormap color_map;
115
116
117
118 static
119 GC createGC()
120 {
121         XGCValues val;
122         val.foreground = BlackPixel(fl_display, 
123                                     DefaultScreen(fl_display));
124         
125         val.function=GXcopy;
126         val.graphics_exposures = false;
127         val.line_style = LineSolid;
128         val.line_width = 0;
129         return XCreateGC(fl_display, RootWindow(fl_display, 0), 
130                          GCForeground | GCFunction | GCGraphicsExposures
131                          | GCLineWidth | GCLineStyle , &val);
132 }
133
134 static
135 GC local_gc_copy;
136
137
138 void addpidwait(int pid)
139 {
140         // adds pid to pid wait list
141         pidwaitlist.push_back(pid);
142
143         if (lyxerr.debugging()) {
144                 lyxerr << "Pids to wait for: \n";
145                 for (list<int>::const_iterator cit = pidwaitlist.begin();
146                      cit != pidwaitlist.end(); ++cit) {
147                         lyxerr << (*cit) << '\n';
148                 }
149                 lyxerr << flush;
150         }
151 }
152
153
154 extern "C" int GhostscriptMsg(FL_OBJECT *, Window, int, int,
155                               XEvent * ev, void *)
156 {
157         char tmp[128];
158
159         XClientMessageEvent * e = reinterpret_cast<XClientMessageEvent*>(ev);
160
161         if(lyxerr.debugging()) {
162                 lyxerr << "ClientMessage, win:[xx] gs:[" << e->data.l[0]
163                        << "] pm:[" << e->data.l[1] << "]" << endl;
164         }
165
166         // just kill gs, that way it will work for sure
167         // This loop looks like S**T so it probably is...
168         for (int i = 0; i < bmpinsref; ++i)
169                 if ((long)bitmaps[i]->bitmap == (long)e->data.l[1]) {
170                         // found the one
171                         figdata * p = bitmaps[i];
172                         p->gsdone = true;
173
174                         // first update p->bitmap, if necessary
175                         if (p->bitmap != None
176                             && p->flags > (1|8) && gs_color && p->wid) {
177                                 // query current colormap and re-render
178                                 // the pixmap with proper colors
179                                 //XColor * cmap;
180                                 XWindowAttributes wa;
181                                 register XImage * im;
182                                 int i;
183                                 int y;
184                                 int wid1;
185                                 int spc1 = gs_spc-1;
186                                 int spc2 = gs_spc * gs_spc;
187                                 int wid = p->wid;
188                                 int forkstat;
189                                 Display * tmpdisp;
190                                 GC gc = local_gc_copy;
191
192                                 XGetWindowAttributes(fl_display,
193                                                      fl_get_canvas_id(
194                                                              figinset_canvas),
195                                                      &wa);
196                                 XFlush(fl_display);
197                                 if (lyxerr.debugging()) {
198                                         lyxerr << "Starting image translation "
199                                                << p->bitmap << " "
200                                                << p->flags << " "
201                                                << p->wid << "x" << p->hgh
202                                                << " " << wa.depth
203                                                << " " << XYPixmap << endl;
204                                 }
205                                 // now fork rendering process
206                                 forkstat = fork();
207                                 if (forkstat == -1) {
208                                         lyxerr.debug()
209                                                 << "Cannot fork, using slow "
210                                                 "method for pixmap translation." << endl;
211                                         tmpdisp = fl_display;
212                                 } else if (forkstat > 0) { // parent
213                                         // register child
214                                         if (lyxerr.debugging()) {
215                                                 lyxerr << "Spawned child "
216                                                        << forkstat << endl;
217                                         }
218                                         addpidwait(forkstat);
219                                         break;
220                                 } else {  // child
221                                         tmpdisp = XOpenDisplay(XDisplayName(0));
222                                         XFlush(tmpdisp);
223                                 }
224                                 im = XGetImage(tmpdisp, p->bitmap, 0, 0,
225                                                p->wid, p->hgh, (1<<wa.depth)-1, XYPixmap);
226                                 XFlush(tmpdisp);
227                                 if (lyxerr.debugging()) {
228                                         lyxerr << "Got the image" << endl;
229                                 }
230                                 if (!im) {
231                                         if (lyxerr.debugging()) {
232                                                 lyxerr << "Error getting the image" << endl;
233                                         }
234                                         goto noim;
235                                 }
236                                 {
237                                 // query current colormap
238                                         XColor * cmap = new XColor[gs_allcolors];
239                                         for (i = 0; i < gs_allcolors; ++i) cmap[i].pixel = i;
240 #if 1
241                                         XQueryColors(tmpdisp, fl_state[fl_get_vclass()].colormap, cmap, gs_allcolors);
242 #else
243                                         XQueryColors(tmpdisp, color_map, cmap, gs_allcolors);
244 #endif
245                                         XFlush(tmpdisp);
246                                         wid1 = p->wid - 1;
247                                 // now we process all the image
248                                         for (y = 0; y < p->hgh; ++y) {
249                                                 for (int x = 0; x < wid; ++x) {
250                                                         XColor * pc = cmap +
251                                                                 XGetPixel(im, x, y);
252                                                         XFlush(tmpdisp);
253                                                         XPutPixel(im, x, y,
254                                                                   gs_pixels[((pc->red+6553)*
255                                                                              spc1/65535)*spc2+((pc->green+6553)*
256                                                                                                spc1/65535)*gs_spc+((pc->blue+6553)*
257                                                                                                                    spc1/65535)]);
258                                                         XFlush(tmpdisp);
259                                                 }
260                                         }
261                                 // This must be correct.
262                                         delete [] cmap;
263                                         if (lyxerr.debugging()) {
264                                                 lyxerr << "Putting image back"
265                                                        << endl;
266                                         }
267                                         XPutImage(tmpdisp, p->bitmap,
268                                                   gc, im, 0, 0,
269                                                   0, 0, p->wid, p->hgh);
270                                         XDestroyImage(im);
271                                         if (lyxerr.debugging()) {
272                                                 lyxerr << "Done translation"
273                                                        << endl;
274                                         }
275                                 }
276                           noim:
277                                 if (lyxerr.debugging()) {
278                                         lyxerr << "Killing gs " 
279                                                << p->gspid << endl;
280                                 }
281                                 lyx::kill(p->gspid, SIGHUP);
282
283                                 sprintf(tmp, "%s/~lyxgs%d.ps",
284                                         system_tempdir.c_str(), 
285                                         p->gspid);
286                                 unlink(tmp);
287                                 if (forkstat == 0) {
288                                         XCloseDisplay(tmpdisp);
289                                         _exit(0);
290                                 }
291                         } else {
292                                 if (lyxerr.debugging()) {
293                                         lyxerr << "Killing gs " 
294                                                << p->gspid << endl;
295                                 }
296                                 lyx::kill(p->gspid, SIGHUP);
297
298                                 sprintf(tmp, "%s/~lyxgs%d.ps", 
299                                         system_tempdir.c_str(),
300                                         p->gspid);
301                                 unlink(tmp);
302                         }
303                         break;
304                 }
305         return 0;
306 }
307
308
309 static void AllocColors(int num)
310 // allocate color cube numxnumxnum, if possible
311 {
312         if (lyxerr.debugging()) {
313                 lyxerr << "Allocating color cube " << num
314                        << 'x' << num << 'x' << num << endl;
315         }
316
317         if (num <= 1) {
318                 lyxerr << "Error allocating color colormap." << endl;
319                 gs_color = false;
320                 return;
321         }
322         if (num > 5) num = 5;
323         XColor xcol;
324         for (int i = 0; i < num * num * num; ++i) {
325                 xcol.red = 65535 * (i / (num * num)) / (num - 1);
326                 xcol.green = 65535 * ((i / num) % num) / (num - 1);
327                 xcol.blue = 65535 * (i % num) / (num - 1);
328                 xcol.flags = DoRed | DoGreen | DoBlue;
329                 if (!XAllocColor(fl_display,
330                                  fl_state[fl_get_vclass()].colormap, &xcol)) {
331                         if (i) XFreeColors(fl_display,
332                                            fl_state[fl_get_vclass()].colormap,
333                                            gs_pixels, i, 0);
334                         if(lyxerr.debugging()) {
335                                 lyxerr << "Cannot allocate color cube "
336                                        << num << endl;;
337                         }
338                         AllocColors(num - 1);
339                         return;
340                 }
341                 gs_pixels[i] = xcol.pixel;
342         }
343         gs_color = true;
344         gs_gray = false;
345         gs_spc = num;
346         gs_num_pixels = num * num * num;
347 }
348
349
350 // allocate grayscale ramp
351 static
352 void AllocGrays(int num)
353 {
354         if (lyxerr.debugging()) {
355                 lyxerr << "Allocating grayscale colormap "
356                        << num << endl;
357         }
358
359         if (num < 4) {
360                 lyxerr << "Error allocating grayscale colormap." << endl;
361                 gs_color = false;
362                 return;
363         }
364         if (num > 128) num = 128;
365         XColor xcol;
366         for (int i = 0; i < num; ++i) {
367                 xcol.red = xcol.green = xcol.blue = 65535 * i / (num - 1);
368                 xcol.flags = DoRed | DoGreen | DoBlue;
369                 if (!XAllocColor(fl_display,
370                                  fl_state[fl_get_vclass()].colormap, &xcol)) {
371                         if (i) XFreeColors(fl_display,
372                                            fl_state[fl_get_vclass()].colormap,
373                                            gs_pixels, i, 0);
374                         if (lyxerr.debugging()) {
375                                 lyxerr << "Cannot allocate grayscale " 
376                                        << num << endl;
377                         }
378                         AllocGrays(num / 2);
379                         return;
380                 }
381                 gs_pixels[i] = xcol.pixel;
382         }
383         gs_color = true;
384         gs_gray = false;
385         gs_num_pixels = num;
386 }
387
388
389 void InitFigures()
390 {
391         bmparrsize = figarrsize = figallocchunk;
392         typedef Figref * Figref_p;
393         figures = new Figref_p[figallocchunk];
394         typedef figdata * figdata_p;
395         bitmaps = new figdata_p[figallocchunk];
396
397         unsigned int k;
398         for (unsigned int i = 0; i < 256; ++i) {
399                 k = 0;
400                 for (unsigned int j = 0; j < 8; ++j)
401                         if (i & (1 << (7-j))) k |= 1 << j;
402                 bittable[i] = char(~k);
403         }
404
405         fl_add_canvas_handler(figinset_canvas, ClientMessage,
406                               GhostscriptMsg, current_view->owner()->getMainForm());
407
408 #if 0
409         // now we have to init color_map
410         if (!color_map) color_map = DefaultColormap(fl_display,
411                                                     DefaultScreen(fl_display));
412 #endif
413         // allocate color cube on pseudo-color display
414         // first get visual
415         gs_color = false;
416         local_gc_copy = createGC();
417
418         Visual * vi = DefaultVisual(fl_display, DefaultScreen(fl_display));
419         if (lyxerr.debugging()) {
420                 printf("Visual ID: %ld, class: %d, bprgb: %d, mapsz: %d\n", 
421                        vi->visualid, vi->c_class, 
422                        vi->bits_per_rgb, vi->map_entries);
423         }
424         color_visual = ( (vi->c_class == StaticColor) ||
425                          (vi->c_class == PseudoColor) ||
426                          (vi->c_class == TrueColor) ||
427                          (vi->c_class == DirectColor) );
428         if ((vi->c_class & 1) == 0) return;
429         // now allocate colors
430         if (vi->c_class == GrayScale) {
431                 // allocate grayscale
432                 AllocGrays(vi->map_entries/2);
433         } else {
434                 // allocate normal color
435                 int i = 5;
436                 while (i * i * i * 2 > vi->map_entries) --i;
437                 AllocColors(i);
438         }
439         gs_allcolors = vi->map_entries;
440 }
441
442
443 void DoneFigures()
444 {
445         delete[] figures;
446         delete[] bitmaps;
447         figarrsize = 0;
448         bmparrsize = 0;
449
450         lyxerr.debug() << "Unregistering figures..." << endl;
451
452         fl_remove_canvas_handler(figinset_canvas, ClientMessage,
453                                  GhostscriptMsg);
454
455 #if 0
456         if (gs_color) {
457                 lyxerr.debug() << "Freeing up the colors..." << endl;
458                 XFreeColors(fl_display, color_map, gs_pixels,
459                             gs_num_pixels, 0);
460                 /******????????????????? what's planes in this case ??????***/
461         }
462 #endif
463 }
464
465
466 int FindBmpIndex(figdata * tmpdata)
467 {
468         int i = 0;
469         while (i < bmpinsref) {
470                 if (bitmaps[i] == tmpdata) return i;
471                 ++i;
472         }
473         return i;
474 }
475
476
477 static void freefigdata(figdata * tmpdata)
478 {
479         tmpdata->ref--;
480         if (tmpdata->ref) return;
481
482         if (tmpdata->gspid > 0) {
483                 int pid = tmpdata->gspid;
484                 char buf[128];
485                 // kill ghostscript and unlink it's files
486                 tmpdata->gspid = -1;
487                 lyx::kill(pid, SIGKILL);
488                 sprintf(buf, "%s/~lyxgs%d.ps", system_tempdir.c_str(), pid);
489                 unlink(buf);
490         }
491
492         if (tmpdata->bitmap) XFreePixmap(fl_display, tmpdata->bitmap);
493         delete tmpdata;
494         int i = FindBmpIndex(tmpdata);
495         --bmpinsref;
496         while (i < bmpinsref) {
497                 bitmaps[i] = bitmaps[i + 1];
498                 ++i;
499         }
500 }
501
502
503 static void runqueue()
504 {
505         // run queued requests for ghostscript, if any
506         if (!gsrunning && gs_color && !gs_xcolor) {
507                 // here alloc all colors, so that gs will use only
508                 // those we allocated for it
509                 // *****
510                 gs_xcolor = true;
511         }
512         
513         while (gsrunning < MAXGS) {
514                 char tbuf[384], tbuf2[80];
515                 Atom * prop;
516                 int nprop, i;
517
518                 if (gsqueue.empty()) {
519                         if (!gsrunning && gs_xcolor) {
520                                 // de-allocate rest of colors
521                                 // *****
522                                 gs_xcolor = false;
523                         }
524                         return;
525                 }
526                 queue_element * p = &gsqueue.front();
527                 if (!p->data) {
528                         gsqueue.pop();
529                         continue;
530                 }
531
532                 int pid = fork();
533                 
534                 if (pid == -1) {
535                         if (lyxerr.debugging()) {
536                                 lyxerr << "GS start error! Cannot fork."
537                                        << endl;
538                         }
539                         p->data->broken = true;
540                         p->data->reading = false;
541                         return;
542                 }
543                 if (pid == 0) { // child
544                         char ** env, rbuf[80], gbuf[40];
545                         int ne = 0;
546                         Display * tempdisp = XOpenDisplay(XDisplayName(0));
547
548                         // create translation file
549                         sprintf(tbuf, "%s/~lyxgs%d.ps", system_tempdir.c_str(),
550                                 int(getpid()));
551                         
552                         ofstream ofs(tbuf);
553                         ofs << "gsave clippath pathbbox grestore\n"
554                             << "4 dict begin\n"
555                             << "/ury exch def /urx exch def /lly exch def "
556                                 "/llx exch def\n"
557                             << p->data->wid / 2.0 << " "
558                             << p->data->hgh / 2.0 << " translate\n"
559                             << p->data->angle << " rotate\n"
560                             << -(p->data->raw_wid / 2.0) << " "
561                             << -(p->data->raw_hgh / 2.0) << " translate\n"
562                             << p->rx / 72.0 << " " << p->ry / 72.0
563                             << " scale\n"
564                             << -p->ofsx << " " << -p->ofsy << " translate\n"
565                             << "end" << endl;
566                         ofs.close(); // Don't remove this.
567
568                         // gs process - set ghostview environment first
569                         sprintf(tbuf2, "GHOSTVIEW=%ld %ld", fl_get_canvas_id(
570                                 figinset_canvas), p->data->bitmap);
571
572                         // now set up ghostview property on a window
573                         sprintf(tbuf, "0 0 0 0 %d %d 72 72 0 0 0 0",
574                                 p->data->wid, p->data->hgh);
575                         // #warning BUG seems that the only bug here
576                         // might be the hardcoded dpi.. Bummer!
577                         
578                         if (lyxerr.debugging()) {
579                                 lyxerr << "Will set GHOSTVIEW property to ["
580                                        << tbuf << "]" << endl;
581                         }
582                         // wait until property is deleted if executing multiple
583                         // ghostscripts
584                         for (;;) {
585                                 // grab server to prevent other child
586                                 // interfering with setting GHOSTVIEW property
587                                 if (lyxerr.debugging()) {
588                                         lyxerr << "Grabbing the server"
589                                                << endl;
590                                 }
591                                 XGrabServer(tempdisp);
592                                 prop = XListProperties(tempdisp,
593                                                        fl_get_canvas_id(
594                                         figinset_canvas), &nprop);
595                                 if (!prop) break;
596
597                                 bool err = true;
598                                 for (i = 0; i < nprop; ++i) {
599                                         char * p = XGetAtomName(tempdisp,
600                                                                 prop[i]);
601                                         if (strcmp(p, "GHOSTVIEW") == 0) {
602                                                 err = false;
603                                                 break;
604                                         }
605                                         XFree(p);
606                                 }
607                                 XFree(reinterpret_cast<char *>(prop)); // jc:
608                                 if (err) break;
609                                 // release the server
610                                 XUngrabServer(tempdisp);
611                                 XFlush(tempdisp);
612                                 // ok, property found, we must wait until
613                                 // ghostscript deletes it
614                                 if (lyxerr.debugging()) {
615                                         lyxerr << "Releasing the server\n["
616                                                << getpid()
617                                                << "] GHOSTVIEW property"
618                                                 " found. Waiting." << endl;
619                                 }
620
621                                 sleep(1);
622                         }
623
624                         XChangeProperty(tempdisp, 
625                                         fl_get_canvas_id(figinset_canvas),
626                                         XInternAtom(tempdisp, "GHOSTVIEW", false),
627                                         XInternAtom(tempdisp, "STRING", false),
628                                         8, PropModeAppend, 
629                                         reinterpret_cast<unsigned char*>(tbuf),
630                                         strlen(tbuf));
631                         
632                         switch (p->data->flags & 3) {
633                         case 0: tbuf[0] = 'H'; break; // Hidden
634                         case 1: tbuf[0] = 'M'; break; // Mono
635                         case 2: tbuf[0] = 'G'; break; // Gray
636                         case 3:
637                                 if (color_visual) 
638                                         tbuf[0] = 'C'; // Color
639                                 else 
640                                         tbuf[0] = 'G'; // Gray
641                                 break;
642                         }
643
644                         sprintf(tbuf+1, " %ld %ld", BlackPixelOfScreen(
645                                 DefaultScreenOfDisplay(fl_display)),
646                                 fl_get_pixel(FL_WHITE));
647
648                         XChangeProperty(tempdisp, 
649                                         fl_get_canvas_id(figinset_canvas),
650                                         XInternAtom(tempdisp,
651                                                     "GHOSTVIEW_COLORS", false),
652                                         XInternAtom(tempdisp, "STRING", false),
653                                         8, PropModeReplace, 
654                                         reinterpret_cast<unsigned char*>(tbuf),
655                                         strlen(tbuf));
656                         XUngrabServer(tempdisp);
657                         XFlush(tempdisp);
658                         if (lyxerr.debugging()) {
659                                 lyxerr << "Releasing the server" << endl;
660                         }
661                         XCloseDisplay(tempdisp);
662
663                         // set up environment
664                         while (environ[ne]) ++ne;
665                         typedef char * char_p;
666                         env = new char_p[ne + 2];
667                         env[0] = tbuf2;
668                         memcpy(&env[1], environ, sizeof(char*) * (ne + 1));
669                         environ = env;
670
671                         // now make gs command
672                         // close(0);
673                         // close(1); do NOT close. If GS writes out
674                         // errors it would hang. (Matthias 290596) 
675                         sprintf(rbuf, "-r%gx%g", p->rx, p->ry);
676                         sprintf(gbuf, "-g%dx%d", p->data->wid, p->data->hgh);
677                         // now chdir into dir with .eps file, to be on the safe
678                         // side
679                         chdir(OnlyPath(p->data->fname).c_str());
680                         // make temp file name
681                         sprintf(tbuf, "%s/~lyxgs%d.ps", system_tempdir.c_str(),
682                                 int(getpid()));
683                         if (lyxerr.debugging()) {
684                                 lyxerr << "starting gs " << tbuf << " "
685                                        << p->data->fname
686                                        << ", pid: " << getpid() << endl;
687                         }
688
689                         int err = execlp(lyxrc->ps_command.c_str(), 
690                                          lyxrc->ps_command.c_str(), 
691                                          "-sDEVICE=x11",
692                                          "-dNOPAUSE", "-dQUIET",
693                                          "-dSAFER", 
694                                          rbuf, gbuf, tbuf, 
695                                          p->data->fname.c_str(), 
696                                          "showpage.ps", "quit.ps", "-", 0);
697                         // if we are still there, an error occurred.
698                         lyxerr << "Error executing ghostscript. "
699                                << "Code: " << err << endl;
700                         lyxerr.debug() << "Cmd: " 
701                                        << lyxrc->ps_command
702                                        << " -sDEVICE=x11 "
703                                        << tbuf << ' '
704                                        << p->data->fname << endl;
705                         _exit(0);       // no gs?
706                 }
707                 // normal process (parent)
708                 if (lyxerr.debugging()) {
709                         lyxerr << "GS ["  << pid << "] started" << endl;
710                 }
711
712                 p->data->gspid = pid;
713                 ++gsrunning;
714                 gsqueue.pop();
715         }
716 }
717
718
719 static void addwait(int psx, int psy, int pswid, int pshgh, figdata * data)
720 {
721         // recompute the stuff and put in the queue
722         queue_element p;
723         p.ofsx = psx;
724         p.ofsy = psy;
725         p.rx = (float(data->raw_wid) * 72.0) / pswid;
726         p.ry = (float(data->raw_hgh) * 72.0) / pshgh;
727
728         p.data = data;
729
730         gsqueue.push(p);
731
732         // if possible, run the queue
733         runqueue();
734 }
735
736
737 static figdata * getfigdata(int wid, int hgh, string const & fname, 
738                             int psx, int psy, int pswid, int pshgh, 
739                             int raw_wid, int raw_hgh, float angle, char flags)
740 {
741         /* first search for an exact match with fname and width/height */
742
743         if (fname.empty()) return 0;
744
745         int i = 0;
746         while (i < bmpinsref) {
747                 if (bitmaps[i]->wid == wid && bitmaps[i]->hgh == hgh &&
748                     bitmaps[i]->flags == flags && bitmaps[i]->fname == fname &&
749                     bitmaps[i]->angle == angle) {
750                         bitmaps[i]->ref++;
751                         return bitmaps[i];
752                 }
753                 ++i;
754         }
755         /* not found -> create new record or return 0 if no record */
756         ++bmpinsref;
757         if (bmpinsref > bmparrsize) {
758                 // allocate more space
759                 bmparrsize += figallocchunk;
760                 typedef figdata * figdata_p;
761                 figdata ** tmp = new figdata_p[bmparrsize];
762                 memcpy(tmp, bitmaps,
763                        sizeof(figdata*) * (bmparrsize - figallocchunk));
764                 delete[] bitmaps;
765                 bitmaps = tmp;
766         }
767         figdata * p = new figdata;
768         bitmaps[bmpinsref-1] = p;
769         p->wid = wid;
770         p->hgh = hgh;
771         p->raw_wid = raw_wid;
772         p->raw_hgh = raw_hgh;
773         p->angle = angle;
774         p->fname = fname;
775         p->flags = flags;
776         XWindowAttributes wa;
777         XGetWindowAttributes(fl_display, fl_get_canvas_id(
778                 figinset_canvas), &wa);
779
780         if (lyxerr.debugging()) {
781                 lyxerr << "Create pixmap disp:" << fl_display
782                        << " scr:" << DefaultScreen(fl_display)
783                        << " w:" << wid
784                        << " h:" << hgh
785                        << " depth:" << wa.depth << endl;
786         }
787         
788         p->ref = 1;
789         p->reading = false;
790         p->broken = false;
791         p->gspid = -1;
792         if (flags) {
793                 p->bitmap = XCreatePixmap(fl_display, fl_get_canvas_id(
794                         figinset_canvas), wid, hgh, wa.depth);
795                 p->gsdone = false;
796                 // initialize reading of .eps file with correct sizes and stuff
797                 addwait(psx, psy, pswid, pshgh, p);
798                 p->reading = true;
799         } else {
800                 p->bitmap = None;
801                 p->gsdone = true;
802         }
803
804         return p;
805 }
806
807
808 static void getbitmap(figdata * p)
809 {
810         p->gspid = -1;
811 }
812
813
814 static void makeupdatelist(figdata * p)
815 {
816         for (int i = 0; i < figinsref; ++i)
817                 if (figures[i]->data == p) {
818                         if (lyxerr.debugging()) {
819                                 lyxerr << "Updating inset "
820                                        << figures[i]->inset
821                                        << endl;
822                         }
823                         // add inset figures[i]->inset into to_update list
824                         PutInsetIntoInsetUpdateList(figures[i]->inset);
825                 }
826 }
827
828
829 void sigchldchecker(pid_t pid, int * status)
830 {
831         lyxerr.debug() << "Got pid = " << pid << endl;
832         bool pid_handled = false;
833         for (int i = bmpinsref - 1; i >= 0; --i) {
834                 if (bitmaps[i]->reading && pid == bitmaps[i]->gspid) {
835                         lyxerr.debug() << "Found pid in bitmaps" << endl;
836                         // now read the file and remove it from disk
837                         figdata * p = bitmaps[i];
838                         p->reading = false;
839                         if (bitmaps[i]->gsdone) *status = 0;
840                         if (*status == 0) {
841                                 lyxerr.debug() << "GS [" << pid
842                                                << "] exit OK." << endl;
843                         } else {
844                                 lyxerr << "GS [" << pid  << "] error "
845                                        << *status << " E:"
846                                        << WIFEXITED(*status)
847                                        << " " << WEXITSTATUS(*status)
848                                        << " S:" << WIFSIGNALED(*status)
849                                        << " " << WTERMSIG(*status) << endl;
850                         }
851                         if (*status == 0) {
852                                 bitmap_waiting = true;
853                                 p->broken = false;
854                         } else {
855                                 // remove temporary files
856                                 char tmp[128];
857                                 sprintf(tmp, "%s/~lyxgs%d.ps", 
858                                         system_tempdir.c_str(),
859                                         p->gspid);
860                                 unlink(tmp);
861                                 p->gspid = -1;
862                                 p->broken = true;
863                         }
864                         makeupdatelist(bitmaps[i]);
865                         --gsrunning;
866                         runqueue();
867                         pid_handled = true;
868                 }
869         }
870         if (!pid_handled) {
871                 lyxerr.debug() << "Checking pid in pidwait" << endl;
872                 list<int>::iterator it = find(pidwaitlist.begin(),
873                                               pidwaitlist.end(), pid);
874                 if (it != pidwaitlist.end()) {
875                         lyxerr.debug() << "Found pid in pidwait\n"
876                                        << "Caught child pid of recompute "
877                                 "routine" << pid << endl;
878                         pidwaitlist.erase(it);
879                 }
880         }
881         if (pid == -1) {
882                 lyxerr.debug() << "waitpid error" << endl;
883                 switch (errno) {
884                 case ECHILD:
885                         lyxerr << "The process or process group specified by "
886                                 "pid does  not exist or is not a child of "
887                                 "the calling process or can never be in the "
888                                 "states specified by options." << endl;
889                         break;
890                 case EINTR:
891                         lyxerr << "waitpid() was interrupted due to the "
892                                 "receipt of a signal sent by the calling "
893                                 "process." << endl;
894                         break;
895                 case EINVAL:
896                         lyxerr << "An invalid value was specified for "
897                                 "options." << endl;
898                         break;
899                 default:
900                         lyxerr << "Unknown error from waitpid" << endl;
901                         break;
902                 }
903         } else if (pid == 0) {
904                 lyxerr << "waitpid nohang" << endl;;
905         } else {
906                 lyxerr.debug() << "normal exit from childhandler" << endl;
907         }
908 }
909
910
911 static void getbitmaps()
912 {
913         bitmap_waiting = false;
914         for (int i = 0; i < bmpinsref; ++i)
915                 if (bitmaps[i]->gspid > 0 && !bitmaps[i]->reading)
916                         getbitmap(bitmaps[i]);
917 }
918
919
920 static void RegisterFigure(InsetFig * fi)
921 {
922         if (figinsref == 0) InitFigures();
923         fi->form = 0;
924         ++figinsref;
925         if (figinsref > figarrsize) {
926                 // allocate more space
927                 figarrsize += figallocchunk;
928                 typedef Figref * Figref_p;
929                 Figref ** tmp = new Figref_p[figarrsize];
930                 memcpy(tmp, figures,
931                        sizeof(Figref*)*(figarrsize-figallocchunk));
932                 delete[] figures;
933                 figures = tmp;
934         }
935         Figref * tmpfig = new Figref;
936         tmpfig->data = 0;
937         tmpfig->inset = fi;
938         figures[figinsref-1] = tmpfig;
939         fi->figure = tmpfig;
940
941         if (lyxerr.debugging()) {
942                 lyxerr << "Register Figure: buffer:["
943                        << current_view->buffer() << "]" << endl;
944         }
945 }
946
947
948 int FindFigIndex(Figref * tmpfig)
949 {
950         int i = 0;
951         while (i < figinsref) {
952                 if (figures[i] == tmpfig) return i;
953                 ++i;
954         }
955         return i;
956 }
957
958
959 static void UnregisterFigure(InsetFig * fi)
960 {
961         Figref * tmpfig = fi->figure;
962
963         if (tmpfig->data) freefigdata(tmpfig->data);
964         if (tmpfig->inset->form) {
965                 if (tmpfig->inset->form->Figure->visible) {
966                         fl_set_focus_object(tmpfig->inset->form->Figure,
967                                             tmpfig->inset->form->OkBtn);
968                         fl_hide_form(tmpfig->inset->form->Figure);
969                 }
970 #if FL_REVISION == 89
971 #warning Reactivate this free_form calls
972 #else
973                 fl_free_form(tmpfig->inset->form->Figure);
974                 free(tmpfig->inset->form); // Why free?
975                 tmpfig->inset->form = 0;
976 #endif
977         }
978         int i = FindFigIndex(tmpfig);
979         --figinsref;
980         while (i < figinsref) {
981                 figures[i] = figures[i+1];
982                 ++i;
983         }
984         delete tmpfig;
985
986         if (figinsref == 0) DoneFigures();
987 }
988
989
990 static string NextToken(istream & is)
991 {
992         string token;
993         char c;
994         if (!is.eof()) {
995                 do {
996                         is.get(c);
997                         token += c;
998                 } while (!is.eof() && !isspace(c));
999                 token.erase(token.length() - 1); // remove the isspace
1000         }
1001         return token;
1002 }
1003
1004
1005 InsetFig::InsetFig(int tmpx, int tmpy, Buffer * o)
1006         : owner(o)
1007 {
1008         wid = tmpx;
1009         hgh = tmpy;
1010         wtype = DEF;
1011         htype = DEF;
1012         twtype = DEF;
1013         thtype = DEF;
1014         pflags = flags = 9;
1015         psubfigure = subfigure = false;
1016         xwid = xhgh = angle = 0;
1017         pswid = pshgh = 0;
1018         raw_wid = raw_hgh = 0;
1019         changedfname = false;
1020         RegisterFigure(this);
1021 }
1022
1023
1024 InsetFig::~InsetFig()
1025 {
1026         if (lyxerr.debugging()) {
1027                 lyxerr << "Figure destructor called" << endl;
1028         }
1029         UnregisterFigure(this);
1030 }
1031
1032
1033 int InsetFig::ascent(Painter &, LyXFont const &) const
1034 {
1035         return hgh + 3;
1036 }
1037
1038
1039 int InsetFig::descent(Painter &, LyXFont const &) const
1040 {
1041         return 1;
1042 }
1043
1044
1045 int InsetFig::width(Painter &, LyXFont const &) const
1046 {
1047         return wid + 2;
1048 }
1049
1050
1051 void InsetFig::draw(Painter & pain, LyXFont const & f,
1052                     int baseline, float & x) const
1053 {
1054         LyXFont font(f);
1055         
1056         if (bitmap_waiting) getbitmaps();
1057         
1058         // I wish that I didn't have to use this
1059         // but the figinset code is so complicated so
1060         // I don't want to fiddle with it now.
1061
1062         if (figure && figure->data && figure->data->bitmap &&
1063             !figure->data->reading && !figure->data->broken) {
1064                 // draw the bitmap
1065                 pain.pixmap(int(x + 1), baseline - hgh,
1066                             wid, hgh, figure->data->bitmap);
1067
1068                 if (flags & 4)
1069                         pain.rectangle(int(x), baseline - hgh - 1,
1070                                        wid + 1, hgh + 1);
1071                 
1072         } else {
1073                 char * msg = 0;
1074                 // draw frame
1075                 pain.rectangle(x, baseline - hgh - 1, wid + 1, hgh + 1);
1076
1077                 if (figure && figure->data) {
1078                         if (figure->data->broken)  msg = _("[render error]");
1079                         else if (figure->data->reading) msg = _("[rendering ... ]");
1080                 } else 
1081                         if (fname.empty()) msg = _("[no file]");
1082                         else if ((flags & 3) == 0) msg = _("[not displayed]");
1083                         else if (lyxrc->ps_command.empty()) msg = _("[no ghostscript]");
1084                 
1085                 if (!msg) msg = _("[unknown error]");
1086                 
1087                 font.setFamily(LyXFont::SANS_FAMILY);
1088                 font.setSize(LyXFont::SIZE_FOOTNOTE);
1089                 string justname = OnlyFilename (fname);
1090                 pain.text(int(x + 8), baseline - font.maxAscent() - 4,
1091                           justname, font);
1092                 
1093                 font.setSize(LyXFont::SIZE_TINY);
1094                 pain.text(int(x + 8), baseline - 4, msg, strlen(msg), font);
1095         }
1096         x += width(pain, font);    // ?
1097 }
1098
1099
1100 void InsetFig::Write(ostream & os) const
1101 {
1102         Regenerate();
1103         os << "Figure size " << wid << " " << hgh << "\n";
1104         if (!fname.empty()) {
1105                 string buf1 = OnlyPath(owner->fileName());
1106                 string fname2 = MakeRelPath(fname, buf1);
1107                 os << "file " << fname2 << "\n";
1108         }
1109         if (!subcaption.empty())
1110                 os << "subcaption " << subcaption << "\n";
1111         if (wtype) os << "width " << static_cast<int>(wtype) << " " << xwid << "\n";
1112         if (htype) os << "height " << static_cast<int>(htype) << " " << xhgh << "\n";
1113         if (angle != 0) os << "angle " << angle << "\n";
1114         os << "flags " << flags << "\n";
1115         if (subfigure) os << "subfigure\n";
1116 }
1117
1118
1119 void InsetFig::Read(LyXLex & lex)
1120 {
1121         string buf;
1122         bool finished = false;
1123         
1124         while (lex.IsOK() && !finished) {
1125                 lex.next();
1126
1127                 string const token = lex.GetString();
1128                 lyxerr.debug() << "Token: " << token << endl;
1129                 
1130                 if (token.empty())
1131                         continue;
1132                 else if (token == "\\end_inset") {
1133                         finished = true;
1134                 } else if (token == "file") {
1135                         if (lex.next()) {
1136                                 buf = lex.GetString();
1137                                 string buf1 = OnlyPath(owner->fileName());
1138                                 fname = MakeAbsPath(buf, buf1);
1139                                 changedfname = true;
1140                         }
1141                 } else if (token == "extra") {
1142                         if (lex.next());
1143                         // kept for backwards compability. Delete in 0.13.x
1144                 } else if (token == "subcaption") {
1145                         if (lex.EatLine())
1146                                 subcaption = lex.GetString();
1147                 } else if (token == "label") {
1148                         if (lex.next());
1149                         // kept for backwards compability. Delete in 0.13.x
1150                 } else if (token == "angle") {
1151                         if (lex.next())
1152                                 angle = lex.GetFloat();
1153                 } else if (token == "size") {
1154                         if (lex.next())
1155                                 wid = lex.GetInteger();
1156                         if (lex.next())
1157                                 hgh = lex.GetInteger();
1158                 } else if (token == "flags") {
1159                         if (lex.next())
1160                                 flags = pflags = lex.GetInteger();
1161                 } else if (token == "subfigure") {
1162                         subfigure = psubfigure = true;
1163                 } else if (token == "width") {
1164                         int typ = 0;
1165                         if (lex.next())
1166                                 typ = lex.GetInteger();
1167                         if (lex.next())
1168                                 xwid = lex.GetFloat();
1169                         switch (typ) {
1170                         case DEF: wtype = DEF; break;
1171                         case CM: wtype = CM; break;
1172                         case IN: wtype = IN; break;
1173                         case PER_PAGE: wtype = PER_PAGE; break;
1174                         case PER_COL: wtype = PER_COL; break;
1175                         default:
1176                                 lyxerr.debug() << "Unknown type!" << endl;
1177                                 break;
1178                         }
1179                         twtype = wtype;
1180                 } else if (token == "height") {
1181                         int typ = 0;
1182                         if (lex.next())
1183                                 typ = lex.GetInteger();
1184                         if (lex.next())
1185                                 xhgh = lex.GetFloat();
1186                         switch (typ) {
1187                         case DEF: htype = DEF; break;
1188                         case CM: htype = CM; break;
1189                         case IN: htype = IN; break;
1190                         case PER_PAGE: htype = PER_PAGE; break;
1191                         default:
1192                                 lyxerr.debug() << "Unknown type!" << endl;
1193                                 break;
1194                         }
1195                         thtype = htype;
1196                 }
1197         }
1198         Regenerate();
1199         Recompute();
1200 }
1201
1202
1203 int InsetFig::Latex(ostream & os, signed char /* fragile*/ ) const
1204 {
1205         Regenerate();
1206         if (!cmd.empty()) os << cmd << " ";
1207         return 0;
1208 }
1209
1210
1211 int InsetFig::Latex(string & file, signed char /* fragile*/ ) const
1212 {
1213         Regenerate();
1214         file += cmd + ' ';
1215         return 0;
1216 }
1217
1218
1219 int InsetFig::Linuxdoc(string &/*file*/) const
1220 {
1221         return 0;
1222 }
1223
1224
1225 int InsetFig::DocBook(string & file) const
1226 {
1227         string figurename = fname;
1228
1229         if(suffixIs(figurename, ".eps"))
1230                 figurename.erase(fname.length() - 5);
1231
1232         file += "@<graphic fileref=\"" + figurename + "\"></graphic>";
1233         return 0;
1234 }
1235
1236
1237 void InsetFig::Validate(LaTeXFeatures & features) const
1238 {
1239         features.graphics = true;
1240         if (subfigure) features.subfigure = true;
1241 }
1242
1243
1244 unsigned char InsetFig::Editable() const
1245 {
1246         return 1;
1247 }
1248
1249
1250 bool InsetFig::Deletable() const
1251 {
1252         return false;
1253 }
1254
1255
1256 void InsetFig::Edit(int, int)
1257 {
1258         lyxerr.debug() << "Editing InsetFig." << endl;
1259         Regenerate();
1260
1261         // We should have RO-versions of the form instead.
1262         // The actual prevention of altering a readonly doc
1263         // is done in CallbackFig()
1264         if(current_view->buffer()->isReadonly()) 
1265                 WarnReadonly(current_view->buffer()->fileName());
1266
1267         if (!form) {
1268                 form = create_form_Figure();
1269                 fl_set_form_atclose(form->Figure, CancelCloseBoxCB, 0);
1270                 fl_set_object_return(form->Angle, FL_RETURN_ALWAYS);
1271                 fl_set_object_return(form->Width, FL_RETURN_ALWAYS);
1272                 fl_set_object_return(form->Height, FL_RETURN_ALWAYS);
1273         }
1274         RestoreForm();
1275         if (form->Figure->visible) {
1276                 fl_raise_form(form->Figure);
1277         } else {
1278                 fl_show_form(form->Figure, FL_PLACE_MOUSE | FL_PLACE_SIZE,
1279                              FL_FULLBORDER, _("Figure"));
1280         }
1281 }
1282
1283
1284 Inset * InsetFig::Clone() const
1285 {
1286         InsetFig * tmp = new InsetFig(100, 100, owner);
1287
1288         if (lyxerr.debugging()) {
1289                 lyxerr << "Clone Figure: buffer:["
1290                        << current_view->buffer()
1291                        << "], cbuffer:[xx]" << endl;
1292         }
1293
1294         tmp->wid = wid;
1295         tmp->hgh = hgh;
1296         tmp->raw_wid = raw_wid;
1297         tmp->raw_hgh = raw_hgh;
1298         tmp->angle = angle;
1299         tmp->xwid = xwid;
1300         tmp->xhgh = xhgh;
1301         tmp->flags = flags;
1302         tmp->pflags = pflags;
1303         tmp->subfigure = subfigure;
1304         tmp->psubfigure = psubfigure;
1305         tmp->wtype = wtype;
1306         tmp->htype = htype;
1307         tmp->psx = psx;
1308         tmp->psy = psy;
1309         tmp->pswid = pswid;
1310         tmp->pshgh = pshgh;
1311         tmp->fname = fname;
1312         if (!fname.empty() && (flags & 3) && !lyxrc->ps_command.empty()) { 
1313                 // do not display if there is
1314                 // "do not display" chosen (Matthias 260696)
1315                 tmp->figure->data = getfigdata(wid, hgh, fname, psx, psy,
1316                                                pswid, pshgh, raw_wid, raw_hgh,
1317                                                angle, flags & (3|8));
1318         } else tmp->figure->data = 0;
1319         tmp->subcaption = subcaption;
1320         tmp->changedfname = false;
1321         tmp->owner = owner;
1322         tmp->Regenerate();
1323         return tmp;
1324 }
1325
1326
1327 Inset::Code InsetFig::LyxCode() const
1328 {
1329         return Inset::GRAPHICS_CODE;
1330 }
1331
1332
1333 void InsetFig::Regenerate() const
1334 {
1335         string cmdbuf;
1336         string resizeW, resizeH;
1337         string rotate, recmd;
1338
1339         if (fname.empty()) {
1340                 cmd = "\\fbox{\\rule[-0.5in]{0pt}{1in}";
1341                 cmd += _("empty figure path");
1342                 cmd += '}';
1343                 return;
1344         }
1345
1346         string buf1 = OnlyPath(owner->fileName());
1347         string fname2 = MakeRelPath(fname, buf1);
1348
1349         string gcmd = "\\includegraphics{" + fname2 + '}';
1350         
1351         switch (wtype) {
1352         case DEF:
1353                 break;
1354         case CM:{// \resizebox*{h-length}{v-length}{text}
1355                 char buf[10];
1356                 sprintf(buf, "%g", xwid); // should find better
1357                 resizeW = buf;
1358                 resizeW += "cm";
1359                 break;
1360         }
1361         case IN: {
1362                 char buf[10];
1363                 sprintf(buf, "%g", xwid);
1364                 resizeW = buf;
1365                 resizeW += "in";
1366                 break;
1367         }
1368         case PER_PAGE:{
1369                 char buf[10];
1370                 sprintf(buf, "%g", xwid/100);
1371                 resizeW = buf;
1372                 resizeW += "\\textwidth";
1373                 break;
1374         }
1375         case PER_COL:{
1376                 char buf[10];
1377                 sprintf(buf, "%g", xwid/100);
1378                 resizeW = buf;
1379                 resizeW += "\\columnwidth";
1380                 break;
1381         }
1382         }
1383
1384         switch (htype) {
1385         case DEF:
1386                 break;
1387         case CM: {
1388                 char buf[10];
1389                 sprintf(buf, "%g", xhgh);
1390                 resizeH = buf;
1391                 resizeH += "cm";
1392                 break;
1393         }
1394         case IN:{
1395                 char buf[10];
1396                 sprintf(buf, "%g", xhgh);
1397                 resizeH = buf;
1398                 resizeH += "in";
1399                 break;
1400         }
1401         case PER_PAGE: {
1402                 char buf[10];
1403                 sprintf(buf, "%g", xhgh/100);
1404                 resizeH = buf;
1405                 resizeH += "\\textheight";
1406                 break;
1407         }
1408         case PER_COL: {
1409                 // Doesn't occur; case exists to suppress compiler warnings.
1410                 break;
1411         }
1412         }
1413
1414         if (!resizeW.empty() || !resizeH.empty()) {
1415                 recmd = "\\resizebox*{";
1416                 if (!resizeW.empty())
1417                         recmd += resizeW;
1418                 else
1419                         recmd += '!';
1420                 recmd += "}{";
1421                 if (!resizeH.empty())
1422                         recmd += resizeH;
1423                 else
1424                         recmd += '!';
1425                 recmd += "}{";
1426         }
1427         
1428         
1429         if (angle != 0) {
1430                 char buf[10];
1431                 sprintf(buf, "%g", angle);
1432                 // \rotatebox{angle}{text}
1433                 rotate = "\\rotatebox{";
1434                 rotate += buf;
1435                 rotate += "}{";
1436         }
1437
1438         cmdbuf = recmd;
1439         cmdbuf += rotate;
1440         cmdbuf += gcmd;
1441         if (!rotate.empty()) cmdbuf += '}';
1442         if (!recmd.empty()) cmdbuf += '}';
1443         if (subfigure) {
1444                 if (!subcaption.empty())
1445                         cmdbuf = "\\subfigure[" + subcaption +
1446                                 "]{" + cmdbuf + "}";
1447                 else
1448                         cmdbuf = "\\subfigure{" + cmdbuf + "}";
1449         }
1450         
1451         cmd = cmdbuf;
1452 }
1453
1454
1455 void InsetFig::TempRegenerate()
1456 {
1457         string cmdbuf;
1458         string resizeW, resizeH;
1459         string rotate, recmd;
1460         
1461         char const * tfname = fl_get_input(form->EpsFile);
1462         string tsubcap = fl_get_input(form->Subcaption);
1463         float tangle = atof(fl_get_input(form->Angle));
1464         float txwid = atof(fl_get_input(form->Width));
1465         float txhgh = atof(fl_get_input(form->Height));
1466
1467         if (!tfname || !*tfname) {
1468                 cmd = "\\fbox{\\rule[-0.5in]{0pt}{1in}";
1469                 cmd += _("empty figure path");
1470                 cmd += '}';
1471                 return;
1472         }
1473
1474         string buf1 = OnlyPath(owner->fileName());
1475         string fname2 = MakeRelPath(tfname, buf1);
1476         // \includegraphics*[<llx,lly>][<urx,ury>]{file}
1477         string gcmd = "\\includegraphics{" + fname2 + '}';
1478         
1479         switch (twtype) {
1480         case DEF:
1481                 break;
1482         case CM: {// \resizebox*{h-length}{v-length}{text}
1483                 char buf[10];
1484                 sprintf(buf, "%g", txwid); // should find better
1485                 resizeW = buf;
1486                 resizeW += "cm";
1487                 break;
1488         }
1489         case IN: {
1490                 char buf[10];
1491                 sprintf(buf, "%g", txwid);
1492                 resizeW = buf;
1493                 resizeW += "in";
1494                 break;
1495         }
1496         case PER_PAGE: {
1497                 char buf[10];
1498                 sprintf(buf, "%g", txwid/100);
1499                 resizeW = buf;
1500                 resizeW += "\\textwidth";
1501                 break;
1502         }
1503         case PER_COL: {
1504                 char buf[10];
1505                 sprintf(buf, "%g", txwid/100);
1506                 resizeW = buf;
1507                 resizeW += "\\columnwidth";
1508                 break;
1509         }
1510         }
1511
1512         switch (thtype) {
1513         case DEF:
1514                 break;
1515         case CM: {
1516                 char buf[10];
1517                 sprintf(buf, "%g", txhgh);
1518                 resizeH = buf;
1519                 resizeH += "cm";
1520                 break;
1521         }
1522         case IN: {
1523                 char buf[10];
1524                 sprintf(buf, "%g", txhgh);
1525                 resizeH = buf;
1526                 resizeH += "in";
1527                 break;
1528         }
1529         case PER_PAGE: {
1530                 char buf[10];
1531                 sprintf(buf, "%g", txhgh/100);
1532                 resizeH = buf;
1533                 resizeH += "\\textheight";
1534                 break;
1535         }
1536         case PER_COL: {
1537                 // Doesn't occur; case exists to suppress compiler warnings.
1538                 break;
1539         }
1540         }
1541
1542         // \resizebox*{h-length}{v-length}{text}
1543         if (!resizeW.empty() || !resizeH.empty()) {
1544                 recmd = "\\resizebox*{";
1545                 if (!resizeW.empty())
1546                         recmd += resizeW;
1547                 else
1548                         recmd += '!';
1549                 recmd += "}{";
1550                 if (!resizeH.empty())
1551                         recmd += resizeH;
1552                 else
1553                         recmd += '!';
1554                 recmd += "}{";
1555         }
1556         
1557         if (tangle != 0) {
1558                 char buf[10];
1559                 sprintf(buf, "%g", tangle);
1560                 // \rotatebox{angle}{text}
1561                 rotate = "\\rotatebox{";
1562                 rotate += buf;
1563                 rotate += "}{";
1564         }
1565
1566         cmdbuf = recmd;
1567         cmdbuf += rotate;
1568         cmdbuf += gcmd;
1569         if (!rotate.empty()) cmdbuf += '}';
1570         if (!recmd.empty()) cmdbuf += '}';
1571         if (psubfigure && !tsubcap.empty()) {
1572                 cmdbuf = string("\\subfigure{") + tsubcap
1573                         + string("}{") + cmdbuf + "}";
1574         }
1575 }
1576
1577
1578 void InsetFig::Recompute()
1579 {
1580         bool changed = changedfname;
1581         int newx, newy, nraw_x, nraw_y;
1582
1583         if (changed) GetPSSizes();
1584
1585         float sin_a = sin (angle / DEG2PI);  /* rotation; H. Zeller 021296 */
1586         float cos_a = cos (angle / DEG2PI);
1587         int frame_wid = int(ceil(fabs(cos_a * pswid) + fabs(sin_a * pshgh)));
1588         int frame_hgh= int(ceil(fabs(cos_a * pshgh) + fabs(sin_a * pswid)));
1589
1590         /* now recompute wid and hgh, and if that is changed, set changed */
1591         /* this depends on chosen size of the picture and its bbox */
1592         // This will be redone in 0.13 ... (hen)
1593         if (!fname.empty()) {
1594                 // say, total width is 595 pts, as A4 in TeX, thats in 1/72" */
1595
1596                 newx = frame_wid;
1597                 newy = frame_hgh;
1598                 switch (wtype) {
1599                 case DEF:
1600                         break;
1601                 case CM:        /* cm */
1602                         newx = int(28.346 * xwid);
1603                         break;
1604                 case IN: /* in */
1605                         newx = int(72 * xwid);
1606                         break;
1607                 case PER_PAGE:  /* % of page */
1608                         newx = int(5.95 * xwid);
1609                         break;
1610                 case PER_COL:   /* % of col */
1611                         newx = int(2.975 * xwid);
1612                         break;
1613                 }
1614                 
1615                 if (wtype && frame_wid) newy = newx*frame_hgh/frame_wid;
1616                 
1617                 switch (htype) {
1618                 case DEF:
1619                         //lyxerr << "This should not happen!" << endl;
1620                         break;
1621                 case CM:        /* cm */
1622                         newy = int(28.346 * xhgh);
1623                         break;
1624                 case IN: /* in */
1625                         newy = int(72 * xhgh);
1626                         break;
1627                 case PER_PAGE:  /* % of page */
1628                         newy = int(8.42 * xhgh);
1629                         break;
1630                 case PER_COL: 
1631                         // Doesn't occur; case exists to suppress
1632                         // compiler warnings.  
1633                         break;
1634                 }
1635                 if (htype && !wtype && frame_hgh)
1636                         newx = newy*frame_wid/frame_hgh;
1637         } else {
1638                 newx = wid;
1639                 newy = hgh;
1640         }
1641
1642         if (frame_wid == 0)
1643                 nraw_x = 5;
1644         else
1645                 nraw_x = int((1.0 * pswid * newx)/frame_wid);
1646
1647         if (frame_hgh == 0)
1648                 nraw_y = 5;
1649         else
1650                 nraw_y = int((1.0 * pshgh * newy)/frame_hgh);
1651
1652         // cannot be zero, actually, set it to some minimum, so its clickable
1653         if (newx < 5) newx = 5;
1654         if (newy < 5) newy = 5;
1655
1656         if (newx   != wid     || newy   != hgh     || 
1657             nraw_x != raw_wid || nraw_y != raw_hgh ||
1658             flags  != pflags  || subfigure != psubfigure) 
1659                 changed = true;
1660        
1661         raw_wid = nraw_x;
1662         raw_hgh = nraw_y;
1663         wid = newx;
1664         hgh = newy;
1665         flags = pflags;
1666         subfigure = psubfigure;
1667
1668         if (changed) {
1669                 figdata * pf = figure->data;
1670
1671                 // get new data
1672                 if (!fname.empty() && (flags & 3)
1673                     && !lyxrc->ps_command.empty()) {
1674                         // do not display if there is "do not display"
1675                         // chosen (Matthias 260696)
1676                         figure->data = getfigdata(wid, hgh, fname,
1677                                                   psx, psy, pswid, pshgh,
1678                                                   raw_wid, raw_hgh,
1679                                                   angle, flags & (3|8));
1680                 } else figure->data = 0;
1681
1682                 // free the old data
1683                 if (pf) freefigdata(pf);
1684         }
1685
1686         changedfname = false;
1687 }
1688
1689
1690 void InsetFig::GetPSSizes()
1691 {
1692         /* get %%BoundingBox: from postscript file */
1693         
1694         /* defaults to associated size
1695          * ..just in case the PS-file is not readable (Henner, 24-Aug-97) 
1696          */
1697         psx = 0;
1698         psy = 0;
1699         pswid = wid;
1700         pshgh = hgh;
1701
1702         if (fname.empty()) return;
1703         string p;
1704         ifstream ifs(fname.c_str());
1705
1706         if (!ifs) return;       // file not found !!!!
1707
1708         /* defaults to A4 page */
1709         psx = 0;
1710         psy = 0;
1711         pswid = 595;
1712         pshgh = 842;
1713
1714         char lastchar = 0; ifs.get(lastchar);
1715         for (;;) {
1716                 char c = 0; ifs.get(c);
1717                 if (ifs.eof()) {
1718                         lyxerr.debug() << "End of (E)PS file reached and"
1719                                 " no BoundingBox!" << endl;
1720                         break;
1721                 }
1722                 if (c == '%' && lastchar == '%') {
1723                         p = NextToken(ifs);
1724                         if (p.empty()) break;
1725                         // we should not use this, with it we cannot
1726                         // discover bounding box and end of file.
1727                         //if (strcmp(p, "EndComments") == 0) break;
1728                         lyxerr.debug() << "Token: `" << p << "'" << endl;
1729                         if (p == "BoundingBox:") {
1730                                 float fpsx, fpsy, fpswid, fpshgh;
1731                                 if (ifs >> fpsx >> fpsy >> fpswid >> fpshgh) {
1732                                         psx = int(fpsx);
1733                                         psy = int(fpsy);
1734                                         pswid = int(fpswid);
1735                                         pshgh = int(fpshgh);
1736                                 }
1737                                 if (lyxerr.debugging()) {
1738                                         lyxerr << "%%%%BoundingBox:"
1739                                                << psx << ' '
1740                                                << psy << ' '
1741                                                << pswid << ' '
1742                                                << pshgh << endl;
1743                                 }
1744                                 break;
1745                         }
1746                         c = 0;
1747                 }
1748                 lastchar = c;
1749         }
1750         pswid -= psx;
1751         pshgh -= psy;
1752
1753 }
1754
1755
1756 void InsetFig::CallbackFig(long arg)
1757 {
1758         bool regen = false;
1759         char const * p;
1760
1761         if (lyxerr.debugging()) {
1762                 lyxerr << "Figure callback, arg " << arg << endl;
1763         }
1764
1765         switch (arg) {
1766         case 10:
1767         case 11:
1768         case 12:        /* width type */
1769         case 13:
1770         case 14:
1771                 switch (arg - 10) {
1772                 case DEF:
1773                         twtype = DEF;
1774                         // put disable here
1775                         fl_deactivate_object(form->Width);
1776                         break;
1777                 case CM:
1778                         twtype = CM;
1779                         // put enable here
1780                         fl_activate_object(form->Width);
1781                         break;
1782                 case IN:
1783                         twtype = IN;
1784                         // put enable here
1785                         fl_activate_object(form->Width);
1786                         break;
1787                 case PER_PAGE:
1788                         twtype = PER_PAGE;
1789                         // put enable here
1790                         fl_activate_object(form->Width);
1791                         break;
1792                 case PER_COL:
1793                         twtype = PER_COL;
1794                         // put enable here
1795                         fl_activate_object(form->Width);
1796                         break;
1797                 default:
1798                         lyxerr.debug() << "Unknown type!" << endl;
1799                         break;
1800                 }
1801                 regen = true;
1802                 break;
1803         case 20:
1804         case 21:
1805         case 22:        /* height type */
1806         case 23:
1807                 switch (arg - 20) {
1808                 case DEF:
1809                         thtype = DEF;
1810                         // put disable here
1811                         fl_deactivate_object(form->Height);
1812                         break;
1813                 case CM:
1814                         thtype = CM;
1815                         // put enable here
1816                         fl_activate_object(form->Height);
1817                         break;
1818                 case IN:
1819                         thtype = IN;
1820                         // put enable here
1821                         fl_activate_object(form->Height);
1822                         break;
1823                 case PER_PAGE:
1824                         thtype = PER_PAGE;
1825                         // put enable here
1826                         fl_activate_object(form->Height);
1827                         break;
1828                 default:
1829                         lyxerr.debug() << "Unknown type!" << endl;
1830                         break;
1831                 }
1832                 regen = true;
1833                 break;
1834         case 3:
1835                 pflags = pflags & ~3;           /* wysiwyg0 */
1836                 break;
1837         case 33:
1838                 pflags = (pflags & ~3) | 1;     /* wysiwyg1 */
1839                 break;
1840         case 43:
1841                 pflags = (pflags & ~3) | 2;     /* wysiwyg2 */
1842                 break;
1843         case 63:
1844                 pflags = (pflags & ~3) | 3;     /* wysiwyg3 */
1845                 break;
1846         case 53:
1847                 pflags ^= 4;    /* frame */
1848                 break;
1849         case 54:
1850                 pflags ^= 8;    /* do translations */
1851                 break;
1852         case 70:
1853                 psubfigure = !psubfigure;       /* This is a subfigure */
1854                 break;
1855         case 2:
1856                 regen = true;           /* regenerate command */
1857                 break;
1858         case 0:                         /* browse file */
1859                 BrowseFile();
1860                 regen = true;
1861                 break;
1862         case 1:                         /* preview */
1863                 p = fl_get_input(form->EpsFile);
1864                 Preview(p);
1865                 break;
1866         case 7:                         /* apply */
1867         case 8:                         /* ok (apply and close) */
1868                 if(!current_view->buffer()->isReadonly()) {
1869                         wtype = twtype;
1870                         htype = thtype;
1871                         xwid = atof(fl_get_input(form->Width));
1872                         xhgh = atof(fl_get_input(form->Height));
1873                         angle = atof(fl_get_input(form->Angle));
1874                         p = fl_get_input(form->EpsFile);
1875                         if (p && *p) {
1876                                 string buf1 = OnlyPath(owner->fileName());
1877                                 fname = MakeAbsPath(p, buf1);
1878                                 changedfname = true;
1879                         } else {
1880                                 if (!fname.empty()) {
1881                                         changedfname = true;
1882                                         fname.clear();
1883                                 }
1884                         }
1885                         subcaption = fl_get_input(form->Subcaption);
1886         
1887                         Regenerate();
1888                         Recompute();
1889                         /* now update inset */
1890                         if (lyxerr.debugging()) {
1891                                 lyxerr << "Update: ["
1892                                        << wid << 'x' << hgh << ']' << endl;
1893                         }
1894                         UpdateInset(current_view, this);
1895                         if (arg == 8) {
1896                                 fl_set_focus_object(form->Figure, form->OkBtn);
1897                                 fl_hide_form(form->Figure);
1898 #if FL_REVISION == 89
1899 #warning Reactivate this free_form calls
1900 #else
1901                                 fl_free_form(form->Figure);
1902                                 free(form); // Why free?
1903                                 form = 0;
1904 #endif
1905                         }
1906                         break;
1907                 } //if not readonly
1908                 //  The user has already been informed about RO in ::Edit
1909                 if(arg == 7) // if 'Apply'
1910                         break;
1911                 // fall through
1912         case 9:                         /* cancel = restore and close */
1913                 fl_set_focus_object(form->Figure, form->OkBtn);
1914                 fl_hide_form(form->Figure);
1915 #if FL_REVISION == 89
1916 #warning Reactivate this free_form calls
1917 #warning Jug, is this still a problem?
1918 #else
1919                 fl_free_form(form->Figure);
1920                 free(form); // Why free?
1921                 form = 0;
1922 #endif
1923                 break;
1924         }
1925
1926         if (regen) TempRegenerate();
1927 }
1928
1929
1930 inline void DisableFigurePanel(FD_Figure * const form)
1931 {
1932         fl_deactivate_object(form->EpsFile);
1933         fl_deactivate_object(form->Browse);
1934         fl_deactivate_object(form->Width);
1935         fl_deactivate_object(form->Height);
1936         fl_deactivate_object(form->Frame);
1937         fl_deactivate_object(form->Translations);
1938         fl_deactivate_object(form->Angle);
1939         fl_deactivate_object(form->HeightGrp);
1940         fl_deactivate_object(form->page2);
1941         fl_deactivate_object(form->Default2);
1942         fl_deactivate_object(form->cm2);
1943         fl_deactivate_object(form->in2);
1944         fl_deactivate_object(form->HeightLabel);
1945         fl_deactivate_object(form->WidthLabel);
1946         fl_deactivate_object(form->DisplayGrp);
1947         fl_deactivate_object(form->Wysiwyg3);
1948         fl_deactivate_object(form->Wysiwyg0);
1949         fl_deactivate_object(form->Wysiwyg2);
1950         fl_deactivate_object(form->Wysiwyg1);
1951         fl_deactivate_object(form->WidthGrp);
1952         fl_deactivate_object(form->Default1);
1953         fl_deactivate_object(form->cm1);
1954         fl_deactivate_object(form->in1);
1955         fl_deactivate_object(form->page1);
1956         fl_deactivate_object(form->column1);
1957         fl_deactivate_object(form->Subcaption);
1958         fl_deactivate_object(form->Subfigure);
1959         fl_deactivate_object (form->OkBtn);
1960         fl_deactivate_object (form->ApplyBtn);
1961         fl_set_object_lcol (form->OkBtn, FL_INACTIVE);
1962         fl_set_object_lcol (form->ApplyBtn, FL_INACTIVE);
1963 }
1964
1965
1966 inline void EnableFigurePanel(FD_Figure * const form)
1967 {
1968         fl_activate_object(form->EpsFile);
1969         fl_activate_object(form->Browse);
1970         fl_activate_object(form->Width);
1971         fl_activate_object(form->Height);
1972         fl_activate_object(form->Frame);
1973         fl_activate_object(form->Translations);
1974         fl_activate_object(form->Angle);
1975         fl_activate_object(form->HeightGrp);
1976         fl_activate_object(form->page2);
1977         fl_activate_object(form->Default2);
1978         fl_activate_object(form->cm2);
1979         fl_activate_object(form->in2);
1980         fl_activate_object(form->HeightLabel);
1981         fl_activate_object(form->WidthLabel);
1982         fl_activate_object(form->DisplayGrp);
1983         fl_activate_object(form->Wysiwyg3);
1984         fl_activate_object(form->Wysiwyg0);
1985         fl_activate_object(form->Wysiwyg2);
1986         fl_activate_object(form->Wysiwyg1);
1987         fl_activate_object(form->WidthGrp);
1988         fl_activate_object(form->Default1);
1989         fl_activate_object(form->cm1);
1990         fl_activate_object(form->in1);
1991         fl_activate_object(form->page1);
1992         fl_activate_object(form->column1);
1993         fl_activate_object(form->Subcaption);
1994         fl_activate_object(form->Subfigure);
1995         fl_activate_object (form->OkBtn);
1996         fl_activate_object (form->ApplyBtn);
1997         fl_set_object_lcol (form->OkBtn, FL_BLACK);
1998         fl_set_object_lcol (form->ApplyBtn, FL_BLACK);
1999 }
2000
2001
2002 void InsetFig::RestoreForm()
2003 {
2004         char buf[32];
2005
2006         EnableFigurePanel(form);
2007
2008         twtype = wtype;
2009         fl_set_button(form->Default1, (wtype == 0));
2010         fl_set_button(form->cm1, (wtype == 1));
2011         fl_set_button(form->in1, (wtype == 2));
2012         fl_set_button(form->page1, (wtype == 3));
2013         fl_set_button(form->column1, (wtype == 4));
2014         if (wtype == 0) {
2015                 fl_deactivate_object(form->Width);
2016         } else {
2017                 fl_activate_object(form->Width);
2018         }
2019                 
2020         // enable and disable should be put here.
2021         thtype = htype;
2022         fl_set_button(form->Default2, (htype == 0));
2023         fl_set_button(form->cm2, (htype == 1));
2024         fl_set_button(form->in2, (htype == 2));
2025         fl_set_button(form->page2, (htype == 3));
2026         // enable and disable should be put here.
2027         if (htype == 0) {
2028                 fl_deactivate_object(form->Height);
2029         } else {
2030                 fl_activate_object(form->Height);
2031         }
2032
2033         int pflags = flags & 3;
2034         fl_set_button(form->Wysiwyg0, (pflags == 0));
2035         fl_set_button(form->Wysiwyg1, (pflags == 1));
2036         fl_set_button(form->Wysiwyg2, (pflags == 2));
2037         fl_set_button(form->Wysiwyg3, (pflags == 3));
2038         fl_set_button(form->Frame, ((flags & 4) != 0));
2039         fl_set_button(form->Translations, ((flags & 8) != 0));
2040         fl_set_button(form->Subfigure, (subfigure != 0));
2041         pflags = flags;
2042         psubfigure = subfigure;
2043         sprintf(buf, "%g", xwid);
2044         fl_set_input(form->Width, buf);
2045         sprintf(buf, "%g", xhgh);
2046         fl_set_input(form->Height, buf);
2047         sprintf(buf, "%g", angle);
2048         fl_set_input(form->Angle, buf);
2049         if (!fname.empty()){
2050                 string buf1 = OnlyPath(owner->fileName());
2051                 string fname2 = MakeRelPath(fname, buf1);
2052                 fl_set_input(form->EpsFile, fname2.c_str());
2053         }
2054         else fl_set_input(form->EpsFile, "");
2055         fl_set_input(form->Subcaption, subcaption.c_str());
2056         if(current_view->buffer()->isReadonly()) 
2057                 DisableFigurePanel(form);
2058
2059         TempRegenerate();
2060 }
2061
2062
2063 void InsetFig::Preview(char const * p)
2064 {
2065         int pid = fork();
2066
2067         if (pid == -1) {
2068                 lyxerr << "Cannot fork process!" << endl;
2069                 return;         // error
2070         }
2071         if (pid > 0) {
2072                 addpidwait(pid);
2073                 return;         // parent process
2074         }
2075
2076         string buf1 = OnlyPath(owner->fileName());
2077         string buf2 = MakeAbsPath(p, buf1);
2078         
2079         lyxerr << "Error during rendering "
2080                << execlp(lyxrc->view_pspic_command.c_str(),
2081                          lyxrc->view_pspic_command.c_str(),
2082                          buf2.c_str(), 0)
2083                << endl;
2084         _exit(0);
2085 }
2086
2087
2088 void InsetFig::BrowseFile()
2089 {
2090         static string current_figure_path;
2091         static int once = 0;
2092         LyXFileDlg fileDlg;
2093
2094         if (lyxerr.debugging()) {
2095                 lyxerr << "Filename: "
2096                        << owner->fileName() << endl;
2097         }
2098         string p = fl_get_input(form->EpsFile);
2099
2100         string buf = MakeAbsPath(owner->fileName());
2101         string buf2 = OnlyPath(buf);
2102         if (!p.empty()) {
2103                 buf = MakeAbsPath(p, buf2);
2104                 buf = OnlyPath(buf);
2105         } else {
2106                 buf = OnlyPath(owner->fileName().c_str());
2107         }
2108         
2109         // Does user clipart directory exist?
2110         string bufclip = AddName (user_lyxdir, "clipart");      
2111         FileInfo fileInfo(bufclip);
2112         if (!(fileInfo.isOK() && fileInfo.isDir()))
2113                 // No - bail out to system clipart directory
2114                 bufclip = AddName (system_lyxdir, "clipart");   
2115
2116
2117         fileDlg.SetButton(0, _("Clipart"), bufclip); 
2118         fileDlg.SetButton(1, _("Document"), buf); 
2119
2120         bool error = false;
2121         do {
2122                 ProhibitInput();
2123                 if (once) {
2124                         p = fileDlg.Select(_("EPS Figure"),
2125                                            current_figure_path,
2126                                            "*ps", string());
2127                 } else {
2128                         p = fileDlg.Select(_("EPS Figure"), buf,
2129                                            "*ps", string());
2130                 }
2131                 AllowInput();
2132
2133                 if (p.empty()) return;
2134
2135                 buf = MakeRelPath(p, buf2);
2136                 current_figure_path = OnlyPath(p);
2137                 once = 1;
2138                 
2139                 if (contains(p, "#") || contains(p, "~") || contains(p, "$")
2140                     || contains(p, "%") || contains(p, " ")) 
2141                         {
2142                                 WriteAlert(_("Filename can't contain any "
2143                                              "of these characters:"),
2144                                            // xgettext:no-c-format
2145                                            _("space, '#', '~', '$' or '%'.")); 
2146                                 error = true;
2147                         }
2148         } while (error);
2149
2150         if (form) fl_set_input(form->EpsFile, buf.c_str());
2151 }
2152
2153
2154 void GraphicsCB(FL_OBJECT * obj, long arg)
2155 {
2156         /* obj->form contains the form */
2157
2158         if (lyxerr.debugging()) {
2159                 lyxerr << "GraphicsCB callback: " << arg << endl;
2160         }
2161
2162         /* find inset we were reacting to */
2163         for (int i = 0; i < figinsref; ++i)
2164                 if (figures[i]->inset->form && figures[i]->inset->form->Figure
2165                     == obj->form) {
2166             
2167                         if (lyxerr.debugging()) {
2168                                 lyxerr << "Calling back figure " << i << endl;
2169                         }
2170                         figures[i]->inset->CallbackFig(arg);
2171                         return;
2172                 }
2173 }
2174
2175
2176 void HideFiguresPopups()
2177 {
2178         for (int i = 0; i < figinsref; ++i)
2179                 if (figures[i]->inset->form 
2180                     && figures[i]->inset->form->Figure->visible) {
2181                         if (lyxerr.debugging()) {
2182                                 lyxerr << "Hiding figure " << i << endl;
2183                         }
2184                         // hide and free the form
2185                         figures[i]->inset->CallbackFig(9);
2186                 }
2187 }