]> git.lyx.org Git - lyx.git/blobdiff - src/insets/figinset.C
reformatting and remove using delc
[lyx.git] / src / insets / figinset.C
index c49d262203516bedf9beae594240d0c2a29de8b3..d80165688c7c6cfe32821a65b88c70f0dac623b1 100644 (file)
@@ -33,6 +33,7 @@
 #include <list>
 #include <algorithm>
 #include <vector>
+#include <utility>
 
 #include <unistd.h>
 #include <csignal>
 #include <cmath>
 
 #include "figinset.h"
-#include "lyx.h"
 #include "lyx_main.h"
 #include "buffer.h"
-#include "filedlg.h"
+#include "frontends/FileDialog.h"
 #include "support/filetools.h"
 #include "LyXView.h" // just because of form_main
 #include "debug.h"
@@ -61,6 +61,8 @@
 #include "font.h"
 #include "bufferview_funcs.h"
 #include "ColorHandler.h"
+#include "converter.h"
+#include "frontends/Dialogs.h" // redrawGUI
 
 using std::ostream;
 using std::istream;
@@ -73,13 +75,22 @@ using std::find;
 using std::flush;
 using std::endl;
 using std::ostringstream;
+using std::copy;
+using std::pair;
+using std::make_pair;
 
 extern BufferView * current_view;
 extern FL_OBJECT * figinset_canvas;
 
 extern char ** environ; // is this only redundtant on linux systems? Lgb.
 
-static float const DEG2PI = 57.295779513;
+// xforms doesn't define this (but it should be in <forms.h>).
+extern "C"
+FL_APPEVENT_CB fl_set_preemptive_callback(Window, FL_APPEVENT_CB, void *);
+
+namespace {
+
+float const DEG2PI = 57.295779513;
 
 struct queue_element {
        float rx, ry;          // resolution x and y
@@ -87,48 +98,45 @@ struct queue_element {
        figdata * data;        // we are doing it for this data
 };
 
-static int const MAXGS = 3;                    /* maximum 3 gs's at a time */
+int const MAXGS = 3;                   /* maximum 3 gs's at a time */
 
 typedef vector<Figref *> figures_type;
 typedef vector<figdata *> bitmaps_type;
-static figures_type figures; // all figures
-static bitmaps_type bitmaps; // all bitmaps
+figures_type figures; // all figures
+bitmaps_type bitmaps; // all bitmaps
 
-static queue<queue_element> gsqueue; // queue for ghostscripting
+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 */
+int gsrunning = 0;     /* currently so many gs's are running */
+bool bitmap_waiting = false; /* bitmaps are waiting finished */
 
-static bool gs_color;                  // do we allocate colors for gs?
-static bool color_visual;              // is the visual color?
-static bool gs_xcolor = false;         // allocated extended colors
-static unsigned long gs_pixels[128];   // allocated pixels
-static int gs_spc;                     // shades per color
-static int gs_allcolors;               // number of all colors
+bool gs_color;                 // do we allocate colors for gs?
+bool color_visual;                     // is the visual color?
+bool gs_xcolor = false;                // allocated extended colors
+unsigned long gs_pixels[128];  // allocated pixels
+int gs_spc;                    // shades per color
+int gs_allcolors;              // number of all colors
 
-static list<int> pidwaitlist; // pid wait list
+list<int> pidwaitlist; // pid wait list
 
-static
 GC createGC()
 {
        XGCValues val;
-       val.foreground = BlackPixel(fl_display
-                                   DefaultScreen(fl_display));
+       val.foreground = BlackPixel(fl_get_display()
+                                   DefaultScreen(fl_get_display()));
        
        val.function=GXcopy;
        val.graphics_exposures = false;
        val.line_style = LineSolid;
        val.line_width = 0;
-       return XCreateGC(fl_display, RootWindow(fl_display, 0), 
+       return XCreateGC(fl_get_display(), RootWindow(fl_get_display(), 0), 
                         GCForeground | GCFunction | GCGraphicsExposures
                         | GCLineWidth | GCLineStyle , &val);
 }
 
-static
 GC local_gc_copy;
 
 
-static
 void addpidwait(int pid)
 {
        // adds pid to pid wait list
@@ -136,23 +144,19 @@ void addpidwait(int pid)
 
        if (lyxerr.debugging()) {
                lyxerr << "Pids to wait for: \n";
-               for (list<int>::const_iterator cit = pidwaitlist.begin();
-                    cit != pidwaitlist.end(); ++cit) {
-                       lyxerr << (*cit) << '\n';
-               }
+               copy(pidwaitlist.begin(), pidwaitlist.end(),
+                    std::ostream_iterator<int>(lyxerr, "\n"));
                lyxerr << flush;
        }
 }
 
 
-static
 string make_tmp(int pid)
 {
        return system_tempdir + "/~lyxgs" + tostr(pid) + ".ps";
 }
 
 
-static
 void kill_gs(int pid, int sig)
 {
        if (lyxerr.debugging()) 
@@ -162,13 +166,16 @@ void kill_gs(int pid, int sig)
 }
 
 
-extern "C" // static
-int GhostscriptMsg(FL_OBJECT *, Window, int, int,
-                  XEvent * ev, void *)
+extern "C"
+int GhostscriptMsg(XEvent * ev, void *)
 {
+       // bin all events not of interest
+       if (ev->type != ClientMessage)
+               return FL_PREEMPT;
+
        XClientMessageEvent * e = reinterpret_cast<XClientMessageEvent*>(ev);
 
-       if(lyxerr.debugging()) {
+       if (lyxerr.debugging()) {
                lyxerr << "ClientMessage, win:[xx] gs:[" << e->data.l[0]
                       << "] pm:[" << e->data.l[1] << "]" << endl;
        }
@@ -199,11 +206,11 @@ int GhostscriptMsg(FL_OBJECT *, Window, int, int,
                                Display * tmpdisp;
                                GC gc = local_gc_copy;
 
-                               XGetWindowAttributes(fl_display,
+                               XGetWindowAttributes(fl_get_display(),
                                                     fl_get_canvas_id(
                                                             figinset_canvas),
                                                     &wa);
-                               XFlush(fl_display);
+                               XFlush(fl_get_display());
                                if (lyxerr.debugging()) {
                                        lyxerr << "Starting image translation "
                                               << p->bitmap << " "
@@ -218,7 +225,7 @@ int GhostscriptMsg(FL_OBJECT *, Window, int, int,
                                        lyxerr.debug()
                                                << "Cannot fork, using slow "
                                                "method for pixmap translation." << endl;
-                                       tmpdisp = fl_display;
+                                       tmpdisp = fl_get_display();
                                } else if (forkstat > 0) { // parent
                                        // register child
                                        if (lyxerr.debugging()) {
@@ -228,7 +235,7 @@ int GhostscriptMsg(FL_OBJECT *, Window, int, int,
                                        addpidwait(forkstat);
                                        break;
                                } else {  // child
-                                       tmpdisp = XOpenDisplay(XDisplayName(0));
+                                       tmpdisp = XOpenDisplay(DisplayString(fl_get_display()));
                                        XFlush(tmpdisp);
                                }
                                im = XGetImage(tmpdisp, p->bitmap, 0, 0,
@@ -292,11 +299,10 @@ int GhostscriptMsg(FL_OBJECT *, Window, int, int,
                        }
                        break;
                }
-       return 0;
+       return FL_PREEMPT;
 }
 
 
-static
 void AllocColors(int num)
 // allocate color cube numxnumxnum, if possible
 {
@@ -317,12 +323,12 @@ void AllocColors(int num)
                xcol.green = short(65535 * ((i / num) % num) / (num - 1));
                xcol.blue = short(65535 * (i % num) / (num - 1));
                xcol.flags = DoRed | DoGreen | DoBlue;
-               if (!XAllocColor(fl_display,
+               if (!XAllocColor(fl_get_display(),
                                 fl_state[fl_get_vclass()].colormap, &xcol)) {
-                       if (i) XFreeColors(fl_display,
+                       if (i) XFreeColors(fl_get_display(),
                                           fl_state[fl_get_vclass()].colormap,
                                           gs_pixels, i, 0);
-                       if(lyxerr.debugging()) {
+                       if (lyxerr.debugging()) {
                                lyxerr << "Cannot allocate color cube "
                                       << num << endl;;
                        }
@@ -337,7 +343,6 @@ void AllocColors(int num)
 
 
 // allocate grayscale ramp
-static
 void AllocGrays(int num)
 {
        if (lyxerr.debugging()) {
@@ -355,9 +360,9 @@ void AllocGrays(int num)
        for (int i = 0; i < num; ++i) {
                xcol.red = xcol.green = xcol.blue = short(65535 * i / (num - 1));
                xcol.flags = DoRed | DoGreen | DoBlue;
-               if (!XAllocColor(fl_display,
+               if (!XAllocColor(fl_get_display(),
                                 fl_state[fl_get_vclass()].colormap, &xcol)) {
-                       if (i) XFreeColors(fl_display,
+                       if (i) XFreeColors(fl_get_display(),
                                           fl_state[fl_get_vclass()].colormap,
                                           gs_pixels, i, 0);
                        if (lyxerr.debugging()) {
@@ -372,33 +377,25 @@ void AllocGrays(int num)
        gs_color = true;
 }
 
-
-static
 void InitFigures()
 {
        // if bitmaps and figures are not empty we will leak mem
        figures.clear();
        bitmaps.clear();
 
-       unsigned int k;
-       for (unsigned int i = 0; i < 256; ++i) {
-               k = 0;
-               for (unsigned int j = 0; j < 8; ++j)
-                       if (i & (1 << (7-j))) k |= 1 << j;
-       }
-
        // allocate color cube on pseudo-color display
        // first get visual
        gs_color = false;
        if (lyxrc.use_gui) {
-               fl_add_canvas_handler(figinset_canvas, ClientMessage,
-                                     GhostscriptMsg,
-                                     current_view->owner()->getForm());
+               /* we want to capture every event, in order to work around an
+                * xforms bug.
+                */
+               fl_set_preemptive_callback(fl_get_canvas_id(figinset_canvas), GhostscriptMsg, 0);
 
                local_gc_copy = createGC();
 
-               Visual * vi = DefaultVisual(fl_display,
-                                           DefaultScreen(fl_display));
+               Visual * vi = DefaultVisual(fl_get_display(),
+                                           DefaultScreen(fl_get_display()));
                if (lyxerr.debugging()) {
                        printf("Visual ID: %ld, class: %d, bprgb: %d, mapsz: %d\n", 
                               vi->visualid, vi->c_class, 
@@ -424,7 +421,6 @@ void InitFigures()
 }
 
 
-static
 void DoneFigures()
 {
        // if bitmaps and figures are not empty we will leak mem
@@ -432,13 +428,9 @@ void DoneFigures()
        figures.clear();
        
        lyxerr.debug() << "Unregistering figures..." << endl;
-
-       fl_remove_canvas_handler(figinset_canvas, ClientMessage,
-                                GhostscriptMsg);
 }
 
 
-static
 void freefigdata(figdata * tmpdata)
 {
        tmpdata->ref--;
@@ -451,13 +443,12 @@ void freefigdata(figdata * tmpdata)
                kill_gs(pid, SIGKILL);
        }
 
-       if (tmpdata->bitmap) XFreePixmap(fl_display, tmpdata->bitmap);
+       if (tmpdata->bitmap) XFreePixmap(fl_get_display(), tmpdata->bitmap);
        bitmaps.erase(find(bitmaps.begin(), bitmaps.end(), tmpdata));
        delete tmpdata;
 }
 
 
-static
 void runqueue()
 {
        // This _have_ to be set before the fork!
@@ -505,7 +496,7 @@ void runqueue()
                if (pid == 0) { // child
                        char ** env;
                        int ne = 0;
-                       Display * tempdisp = XOpenDisplay(XDisplayName(0));
+                       Display * tempdisp = XOpenDisplay(DisplayString(fl_get_display()));
 
                        // create translation file
                        ofstream ofs;
@@ -562,7 +553,7 @@ void runqueue()
                                for (i = 0; i < nprop; ++i) {
                                        char * p = XGetAtomName(tempdisp,
                                                                prop[i]);
-                                       if (strcmp(p, "GHOSTVIEW") == 0) {
+                                       if (compare(p, "GHOSTVIEW") == 0) {
                                                err = false;
                                                // We free it when we leave so we don't leak.
                                                XFree(p);
@@ -690,7 +681,6 @@ void runqueue()
 }
 
 
-static
 void addwait(int psx, int psy, int pswid, int pshgh, figdata * data)
 {
        // recompute the stuff and put in the queue
@@ -709,7 +699,6 @@ void addwait(int psx, int psy, int pswid, int pshgh, figdata * data)
 }
 
 
-static
 figdata * getfigdata(int wid, int hgh, string const & fname, 
                     int psx, int psy, int pswid, int pshgh, 
                     int raw_wid, int raw_hgh, float angle, char flags)
@@ -738,12 +727,12 @@ figdata * getfigdata(int wid, int hgh, string const & fname,
        p->flags = flags;
        bitmaps.push_back(p);
        XWindowAttributes wa;
-       XGetWindowAttributes(fl_display, fl_get_canvas_id(
+       XGetWindowAttributes(fl_get_display(), fl_get_canvas_id(
                figinset_canvas), &wa);
 
        if (lyxerr.debugging()) {
-               lyxerr << "Create pixmap disp:" << fl_display
-                      << " scr:" << DefaultScreen(fl_display)
+               lyxerr << "Create pixmap disp:" << fl_get_display()
+                      << " scr:" << DefaultScreen(fl_get_display())
                       << " w:" << wid
                       << " h:" << hgh
                       << " depth:" << wa.depth << endl;
@@ -754,7 +743,7 @@ figdata * getfigdata(int wid, int hgh, string const & fname,
        p->broken = false;
        p->gspid = -1;
        if (flags) {
-               p->bitmap = XCreatePixmap(fl_display, fl_get_canvas_id(
+               p->bitmap = XCreatePixmap(fl_get_display(), fl_get_canvas_id(
                        figinset_canvas), wid, hgh, wa.depth);
                p->gsdone = false;
                // initialize reading of .eps file with correct sizes and stuff
@@ -769,17 +758,15 @@ figdata * getfigdata(int wid, int hgh, string const & fname,
 }
 
 
-static
 void getbitmap(figdata * p)
 {
        p->gspid = -1;
 }
 
 
-static
 void makeupdatelist(figdata * p)
 {
-       for(figures_type::iterator it = figures.begin();
+       for (figures_type::iterator it = figures.begin();
            it != figures.end(); ++it)
                if ((*it)->data == p) {
                        if (lyxerr.debugging()) {
@@ -792,6 +779,8 @@ void makeupdatelist(figdata * p)
                }
 }
 
+} // namespace anon
+
 
 // this func is only "called" in spellchecker.C
 void sigchldchecker(pid_t pid, int * status)
@@ -873,7 +862,8 @@ void sigchldchecker(pid_t pid, int * status)
 }
 
 
-static
+namespace {
+
 void getbitmaps()
 {
        bitmap_waiting = false;
@@ -884,7 +874,6 @@ void getbitmaps()
 }
 
 
-static
 void RegisterFigure(InsetFig * fi)
 {
        if (figures.empty()) InitFigures();
@@ -902,7 +891,6 @@ void RegisterFigure(InsetFig * fi)
 }
 
 
-static
 void UnregisterFigure(InsetFig * fi)
 {
        if (!lyxrc.use_gui)
@@ -931,9 +919,11 @@ void UnregisterFigure(InsetFig * fi)
        if (figures.empty()) DoneFigures();
 }
 
+} // namespace anon
+
 
-InsetFig::InsetFig(int tmpx, int tmpy, Buffer * o)
-       : owner(o)
+InsetFig::InsetFig(int tmpx, int tmpy, Buffer const & o)
+       : owner(&o)
 {
        wid = tmpx;
        hgh = tmpy;
@@ -948,6 +938,7 @@ InsetFig::InsetFig(int tmpx, int tmpy, Buffer * o)
        raw_wid = raw_hgh = 0;
        changedfname = false;
        RegisterFigure(this);
+       r_ = Dialogs::redrawGUI.connect(SigC::slot(this, &InsetFig::redraw));
 }
 
 
@@ -957,6 +948,14 @@ InsetFig::~InsetFig()
                lyxerr << "Figure destructor called" << endl;
        }
        UnregisterFigure(this);
+       r_.disconnect();
+}
+
+
+void InsetFig::redraw()
+{
+       if (form && form->Figure->visible)
+               fl_redraw_form(form->Figure);
 }
 
 
@@ -1001,7 +1000,7 @@ void InsetFig::draw(BufferView * bv, LyXFont const & f,
                                       wid + 1, hgh + 1);
                
        } else {
-               char * msg = 0;
+               char const * msg = 0;
                string lfname = fname;
                if (!fname.empty() && GetExtension(fname).empty())
                    lfname += ".eps";
@@ -1165,7 +1164,7 @@ int InsetFig::DocBook(Buffer const *, ostream & os) const
        string buf1 = OnlyPath(owner->fileName());
        string figurename = MakeRelPath(fname, buf1);
 
-       if(suffixIs(figurename, ".eps"))
+       if (suffixIs(figurename, ".eps"))
                figurename.erase(figurename.length() - 4);
 
        os << "@<graphic fileref=\"" << figurename << "\"></graphic>";
@@ -1206,7 +1205,7 @@ void InsetFig::Edit(BufferView * bv, int, int, unsigned int)
        // We should have RO-versions of the form instead.
        // The actual prevention of altering a readonly doc
        // is done in CallbackFig()
-       if(bv->buffer()->isReadonly()) 
+       if (bv->buffer()->isReadonly()) 
                WarnReadonly(bv->buffer()->fileName());
 
        if (!form) {
@@ -1220,19 +1219,20 @@ void InsetFig::Edit(BufferView * bv, int, int, unsigned int)
        if (form->Figure->visible) {
                fl_raise_form(form->Figure);
        } else {
-               fl_show_form(form->Figure, FL_PLACE_MOUSE | FL_PLACE_SIZE,
-                            FL_FULLBORDER, _("Figure"));
+               fl_show_form(form->Figure,
+                            FL_PLACE_MOUSE | FL_FREE_SIZE, FL_TRANSIENT,
+                            _("Figure"));
        }
 }
 
 
-Inset * InsetFig::Clone() const
+Inset * InsetFig::Clone(Buffer const & buffer) const
 {
-       InsetFig * tmp = new InsetFig(100, 100, owner);
+       InsetFig * tmp = new InsetFig(100, 100, buffer);
 
        if (lyxerr.debugging()) {
                lyxerr << "Clone Figure: buffer:["
-                      << current_view->buffer()
+                      << &buffer
                       << "], cbuffer:[xx]" << endl;
        }
 
@@ -1254,12 +1254,15 @@ Inset * InsetFig::Clone() const
        tmp->pswid = pswid;
        tmp->pshgh = pshgh;
        tmp->fname = fname;
-       if (!fname.empty() && IsFileReadable(fname) 
+       string lfname = fname;
+       if (!fname.empty() && GetExtension(fname).empty())
+               lfname += ".eps";
+       if (!fname.empty() && IsFileReadable(lfname) 
            && (flags & 3) && !lyxrc.ps_command.empty()
            && lyxrc.use_gui) { 
                // do not display if there is
                // "do not display" chosen (Matthias 260696)
-               tmp->figure->data = getfigdata(wid, hgh, fname, psx, psy,
+               tmp->figure->data = getfigdata(wid, hgh, lfname, psx, psy,
                                               pswid, pshgh, raw_wid, raw_hgh,
                                               angle, flags & (3|8));
        } else tmp->figure->data = 0;
@@ -1277,8 +1280,9 @@ Inset::Code InsetFig::LyxCode() const
 }
 
 
-static
-string stringify(InsetFig::HWTYPE hw, float f, string suffix)
+namespace {
+
+string const stringify(InsetFig::HWTYPE hw, float f, string suffix)
 {
        string res;
        switch (hw) {
@@ -1301,6 +1305,8 @@ string stringify(InsetFig::HWTYPE hw, float f, string suffix)
        return res;
 }
 
+} // namespace anon
+
 
 void InsetFig::Regenerate() const
 {
@@ -1549,7 +1555,10 @@ void InsetFig::GetPSSizes()
 
        if (fname.empty()) return;
        string p;
-       ifstream ifs(fname.c_str());
+       string lfname = fname;
+       if (GetExtension(fname).empty())
+               lfname += ".eps";
+       ifstream ifs(lfname.c_str());
 
        if (!ifs) return;       // file not found !!!!
 
@@ -1710,7 +1719,7 @@ void InsetFig::CallbackFig(long arg)
                break;
        case 7:                         /* apply */
        case 8:                         /* ok (apply and close) */
-               if(!current_view->buffer()->isReadonly()) {
+               if (!current_view->buffer()->isReadonly()) {
                        wtype = twtype;
                        htype = thtype;
                        xwid = atof(fl_get_input(form->Width));
@@ -1751,7 +1760,7 @@ void InsetFig::CallbackFig(long arg)
                        break;
                } //if not readonly
                //  The user has already been informed about RO in ::Edit
-               if(arg == 7) // if 'Apply'
+               if (arg == 7) // if 'Apply'
                        break;
                // fall through
        case 9:                         /* cancel = restore and close */
@@ -1895,7 +1904,7 @@ void InsetFig::RestoreForm()
        }
        else fl_set_input(form->EpsFile, "");
        fl_set_input(form->Subcaption, subcaption.c_str());
-       if(current_view->buffer()->isReadonly()) 
+       if (current_view->buffer()->isReadonly()) 
                DisableFigurePanel(form);
 
        TempRegenerate();
@@ -1904,37 +1913,19 @@ void InsetFig::RestoreForm()
 
 void InsetFig::Preview(string const & p)
 {
-       int pid = fork();
-
-       if (pid == -1) {
-               lyxerr << "Cannot fork process!" << endl;
-               return;         // error
-       }
-       if (pid > 0) {
-               addpidwait(pid);
-               return;         // parent process
-       }
-
        string tfname = p;
        if (GetExtension(tfname).empty())
            tfname += ".eps";
        string buf1 = OnlyPath(owner->fileName());
        string buf2 = MakeAbsPath(tfname, buf1);
-       
-       lyxerr << "Error during rendering "
-              << execlp(lyxrc.view_pspic_command.c_str(),
-                        lyxrc.view_pspic_command.c_str(),
-                        buf2.c_str(), 0)
-              << endl;
-       _exit(0);
+       if (!formats.View(owner, buf2, "eps"))
+               lyxerr << "Can't view " << buf2 << endl;
 }
 
-
 void InsetFig::BrowseFile()
 {
        static string current_figure_path;
        static int once = 0;
-       LyXFileDlg fileDlg;
 
        if (lyxerr.debugging()) {
                lyxerr << "Filename: "
@@ -1959,23 +1950,21 @@ void InsetFig::BrowseFile()
                bufclip = AddName (system_lyxdir, "clipart");   
 
 
-       fileDlg.SetButton(0, _("Clipart"), bufclip); 
-       fileDlg.SetButton(1, _("Document"), buf); 
+       FileDialog fileDlg(current_view->owner(), _("Select an EPS figure"),
+               LFUN_SELECT_FILE_SYNC,
+               make_pair(string(_("Clip art")), string(bufclip)),
+               make_pair(string(_("Documents")), string(buf)));
 
        bool error = false;
        do {
-               ProhibitInput(current_view);
-               if (once) {
-                       p = fileDlg.Select(_("EPS Figure"),
-                                          current_figure_path,
-                                          "*ps", string());
-               } else {
-                       p = fileDlg.Select(_("EPS Figure"), buf,
-                                          "*ps", string());
-               }
-               AllowInput(current_view);
+               string const path = (once) ? current_figure_path : buf;
+
+               FileDialog::Result result = fileDlg.Select(path, _("*ps| PostScript documents"));
 
-               if (p.empty()) return;
+               string const p = result.second;
+
+               if (p.empty())
+                       return;
 
                buf = MakeRelPath(p, buf2);
                current_figure_path = OnlyPath(p);