]> git.lyx.org Git - lyx.git/blobdiff - src/insets/figinset.C
reformatting and remove using delc
[lyx.git] / src / insets / figinset.C
index 297a4de03e88814d2880895b86e37c2bb24d9b5f..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"
@@ -62,6 +62,7 @@
 #include "bufferview_funcs.h"
 #include "ColorHandler.h"
 #include "converter.h"
+#include "frontends/Dialogs.h" // redrawGUI
 
 using std::ostream;
 using std::istream;
@@ -75,13 +76,21 @@ 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
@@ -89,28 +98,27 @@ 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;
@@ -126,11 +134,9 @@ GC createGC()
                         | GCLineWidth | GCLineStyle , &val);
 }
 
-static
 GC local_gc_copy;
 
 
-static
 void addpidwait(int pid)
 {
        // adds pid to pid wait list
@@ -139,20 +145,18 @@ void addpidwait(int pid)
        if (lyxerr.debugging()) {
                lyxerr << "Pids to wait for: \n";
                copy(pidwaitlist.begin(), pidwaitlist.end(),
-                    ostream_iterator<int>(lyxerr, "\n"));
+                    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;
        }
@@ -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
 {
@@ -322,7 +328,7 @@ void AllocColors(int num)
                        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()) {
@@ -372,8 +377,6 @@ void AllocGrays(int num)
        gs_color = true;
 }
 
-
-static
 void InitFigures()
 {
        // if bitmaps and figures are not empty we will leak mem
@@ -384,9 +387,10 @@ void InitFigures()
        // 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();
 
@@ -417,7 +421,6 @@ void InitFigures()
 }
 
 
-static
 void DoneFigures()
 {
        // if bitmaps and figures are not empty we will leak mem
@@ -425,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--;
@@ -450,7 +449,6 @@ void freefigdata(figdata * tmpdata)
 }
 
 
-static
 void runqueue()
 {
        // This _have_ to be set before the fork!
@@ -498,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;
@@ -555,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);
@@ -683,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
@@ -702,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)
@@ -762,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()) {
@@ -785,6 +779,8 @@ void makeupdatelist(figdata * p)
                }
 }
 
+} // namespace anon
+
 
 // this func is only "called" in spellchecker.C
 void sigchldchecker(pid_t pid, int * status)
@@ -866,7 +862,8 @@ void sigchldchecker(pid_t pid, int * status)
 }
 
 
-static
+namespace {
+
 void getbitmaps()
 {
        bitmap_waiting = false;
@@ -877,7 +874,6 @@ void getbitmaps()
 }
 
 
-static
 void RegisterFigure(InsetFig * fi)
 {
        if (figures.empty()) InitFigures();
@@ -895,7 +891,6 @@ void RegisterFigure(InsetFig * fi)
 }
 
 
-static
 void UnregisterFigure(InsetFig * fi)
 {
        if (!lyxrc.use_gui)
@@ -924,6 +919,8 @@ void UnregisterFigure(InsetFig * fi)
        if (figures.empty()) DoneFigures();
 }
 
+} // namespace anon
+
 
 InsetFig::InsetFig(int tmpx, int tmpy, Buffer const & o)
        : owner(&o)
@@ -941,6 +938,7 @@ InsetFig::InsetFig(int tmpx, int tmpy, Buffer const & o)
        raw_wid = raw_hgh = 0;
        changedfname = false;
        RegisterFigure(this);
+       r_ = Dialogs::redrawGUI.connect(SigC::slot(this, &InsetFig::redraw));
 }
 
 
@@ -950,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);
 }
 
 
@@ -1158,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>";
@@ -1199,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) {
@@ -1213,8 +1219,9 @@ 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"));
        }
 }
 
@@ -1247,12 +1254,15 @@ Inset * InsetFig::Clone(Buffer const & buffer) 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;
@@ -1270,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) {
@@ -1294,6 +1305,8 @@ string stringify(InsetFig::HWTYPE hw, float f, string suffix)
        return res;
 }
 
+} // namespace anon
+
 
 void InsetFig::Regenerate() const
 {
@@ -1542,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 !!!!
 
@@ -1703,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));
@@ -1744,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 */
@@ -1888,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();
@@ -1902,7 +1918,7 @@ void InsetFig::Preview(string const & p)
            tfname += ".eps";
        string buf1 = OnlyPath(owner->fileName());
        string buf2 = MakeAbsPath(tfname, buf1);
-       if (!Formats::View(owner, buf2))
+       if (!formats.View(owner, buf2, "eps"))
                lyxerr << "Can't view " << buf2 << endl;
 }
 
@@ -1910,7 +1926,6 @@ void InsetFig::BrowseFile()
 {
        static string current_figure_path;
        static int once = 0;
-       LyXFileDlg fileDlg;
 
        if (lyxerr.debugging()) {
                lyxerr << "Filename: "
@@ -1935,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;
 
-               if (p.empty()) return;
+               FileDialog::Result result = fileDlg.Select(path, _("*ps| PostScript documents"));
+
+               string const p = result.second;
+
+               if (p.empty())
+                       return;
 
                buf = MakeRelPath(p, buf2);
                current_figure_path = OnlyPath(p);