]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt2/lyx_gui.C
better selection and scrolling behaviour
[lyx.git] / src / frontends / qt2 / lyx_gui.C
index 0991a98181d57d7020c618a03a1e522c469f779a..c64afc720fded4539e31c216dcbcb76a349c8883 100644 (file)
@@ -1,28 +1,32 @@
 /**
- * \file lyx_gui.C
- * Copyright 2002 the LyX Team
- * Read the file COPYING
+ * \file qt2/lyx_gui.C
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
  *
  * \author unknown
- * \author John Levon <moz@compsoc.man.ac.uk>
+ * \author John Levon
+ *
+ * Full author contact details are available in file CREDITS
  */
 
 #include <config.h>
+
+#ifdef __GNUG__
+#pragma implementation
+#endif
+
 #include "support/lyxlib.h"
 #include "support/os.h"
 #include "support/filetools.h"
+#include "support/lstrings.h"
 #include "debug.h"
 #include "gettext.h"
-#include <fcntl.h>
-#include <boost/bind.hpp>
+
 #include "lyx_gui.h"
 #include "lyx_main.h"
 #include "lyxrc.h"
 #include "lyxfont.h"
+
 // FIXME: move this stuff out again
 #include "bufferlist.h"
 #include "lyxfunc.h"
 #include <boost/shared_ptr.hpp>
 #include <boost/function/function0.hpp>
 #include <boost/signals/signal1.hpp>
+#include <boost/bind.hpp>
+
 #include "QtView.h"
 #include "QLImage.h"
 #include "qfont_loader.h"
-#include "io_callback.h" 
+#include "io_callback.h"
+
 #include <qapplication.h>
+#include <qwidget.h>
+#include <qpaintdevicemetrics.h>
+
+#include <fcntl.h>
+
 #ifndef CXX_GLOBAL_CSTD
 using std::exit;
 #endif
 
 using std::vector;
-using std::hex;
+using std::map;
 using std::endl;
-extern bool finished;
+
 extern BufferList bufferlist;
+
+namespace {
+
+float getDPI()
+{
+       QWidget w;
+       QPaintDeviceMetrics pdm(&w);
+       return 0.5 * (pdm.logicalDpiX() + pdm.logicalDpiY());
+}
+
+map<int, io_callback *> io_callbacks;
+
+} // namespace anon
+
+
 // FIXME: wrong place !
 LyXServer * lyxserver;
+
+#ifdef Q_WS_X11
+extern bool lyxX11EventFilter(XEvent * xev);
+#endif
+
+class LQApplication : public QApplication
+{
+public:
+       LQApplication(int &argc, char **argv);
+       ~LQApplication();
+#ifdef Q_WS_X11
+       bool x11EventFilter (XEvent * ev) { return lyxX11EventFilter(ev); }
+#endif
+};
+
+LQApplication::LQApplication(int &argc, char **argv)
+       : QApplication( argc, argv )
+{}
+
+LQApplication::~LQApplication()
+{}
+
 void lyx_gui::parse_init(int & argc, char * argv[])
 {
-       static QApplication a(argc, argv);
+       static LQApplication a(argc, argv);
+
        using namespace grfx;
 
        Image::newImage = boost::bind(&QLImage::newImage);
        Image::loadableFormats = boost::bind(&QLImage::loadableFormats);
+
+       // needs to be done before reading lyxrc
+       lyxrc.dpi = getDPI();
 }
+
 
 void lyx_gui::parse_lyxrc()
 {
-       // FIXME !!!! 
-       lyxrc.dpi = 95;
 }
 
-void lyx_gui::start(string const & batch, vector<string> files)
+
+void lyx_gui::start(string const & batch, vector<string> const & files)
 {
        // initial geometry
        int xpos = -1;
        int ypos = -1;
        unsigned int width = 690;
        unsigned int height = 510;
+
        QtView view(width, height);
        view.show(xpos, ypos, "LyX");
        view.init();
 
        Buffer * last = 0;
+
        // FIXME: some code below needs moving
 
-       lyxserver = new LyXServer(view.getLyXFunc(), lyxrc.lyxpipes);
+       lyxserver = new LyXServer(&view.getLyXFunc(), lyxrc.lyxpipes);
+
        vector<string>::const_iterator cit = files.begin();
        vector<string>::const_iterator end = files.end();
        for (; cit != end; ++cit) {
@@ -108,23 +152,26 @@ void lyx_gui::start(string const & batch, vector<string> files)
 
        // handle the batch commands the user asked for
        if (!batch.empty()) {
-               view.getLyXFunc()->dispatch(batch);
+               view.getLyXFunc().dispatch(batch);
        }
 
-       // FIXME: something somewhere is EATING CPU
-       while (!finished) {
-               qApp->processEvents();
-       }
-                
-       // FIXME 
+       qApp->exec();
+
+       // FIXME
        delete lyxserver;
 }
+
+
+void lyx_gui::exit()
+{
+       qApp->exit(0);
+}
+
+
 string const lyx_gui::hexname(LColor::color col)
 {
        QColor color(lcolor.getX11Name(col).c_str());
-       return color.name().latin1();
+       return ltrim(color.name().latin1(), "#");
 }
 
 
@@ -148,6 +195,15 @@ bool lyx_gui::font_available(LyXFont const & font)
 
 void lyx_gui::set_read_callback(int fd, LyXComm * comm)
 {
-       // FIXME: "leak"
-       new io_callback(fd, comm);
+       io_callbacks[fd] = new io_callback(fd, comm);
+}
+
+
+void lyx_gui::remove_read_callback(int fd)
+{
+       map<int, io_callback *>::iterator it = io_callbacks.find(fd);
+       if (it != io_callbacks.end()) {
+               delete it->second;
+               io_callbacks.erase(it);
+       }
 }