]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/lyx_gui.C
* frontends/qt3/QtView.C: use QRect ctor, coding style
[lyx.git] / src / frontends / qt4 / lyx_gui.C
index fe523c964cddf179c6eb7dca5583c23cc837dbc4..32ef21f94fa98dccd5eaca2c23b0990d06a3d72e 100644 (file)
@@ -5,10 +5,13 @@
  *
  * \author unknown
  * \author John Levon
+ * \author Abdelrazak Younes
  *
  * Full author contact details are available in file CREDITS.
  */
 
+#undef QT3_SUPPORT
+
 #include <config.h>
 
 #include "lyx_gui.h"
 #include "support/package.h"
 #include "debug.h"
 
-// Dear Lord, deliver us from Evil, aka the Qt headers
-// Qt defines a macro 'signals' that clashes with a boost namespace.
-// All is well if the namespace is visible first.
-#include <boost/signal.hpp> // FIXME: Is this needed? (Lgb)
 #include <boost/bind.hpp>
 #include <boost/shared_ptr.hpp>
 
-#include "QtView.h"
-#include "lcolorcache.h"
-#include "qfont_loader.h"
+#include "GuiView.h"
+#include "ColorCache.h"
+#include "FontLoader.h"
 #include "QLImage.h"
 #include "qt_helpers.h"
 #include "socket_callback.h"
-
-#ifdef Q_WS_MACX
-#include <Carbon/Carbon.h>
-#endif
+#include "Application.h"
 
 #include <QApplication>
 #include <QEventLoop>
 #include <QTranslator>
 #include <QTextCodec>
+#include <QLocale>
+#include <QLibraryInfo>
+
+
 
 using lyx::support::ltrim;
 using lyx::support::package;
 
-using lyx::frontend::QtView;
+using lyx::frontend::GuiView;
+using lyx::frontend::Application;
 
 namespace os = lyx::support::os;
 
@@ -73,8 +74,9 @@ using std::map;
 using std::vector;
 using std::string;
 
-
-extern BufferList bufferlist;
+// FIXME: wrong place !
+LyXServer * lyxserver;
+LyXServerSocket * lyxsocket;
 
 namespace {
 
@@ -86,90 +88,35 @@ int getDPI()
 
 map<int, shared_ptr<socket_callback> > socket_callbacks;
 
-} // namespace anon
-
-// FIXME: wrong place !
-LyXServer * lyxserver;
-LyXServerSocket * lyxsocket;
-
-// in QLyXKeySym.C
-extern void initEncodings();
-
-#ifdef Q_WS_X11
-extern bool lyxX11EventFilter(XEvent * xev);
-#endif
-
-#ifdef Q_WS_MACX
-extern bool macEventFilter(EventRef event);
-extern pascal OSErr
-handleOpenDocuments(const AppleEvent* inEvent, AppleEvent* /*reply*/,
-                   long /*refCon*/);
-#endif
-
-class LQApplication : public QApplication
-{
-public:
-       LQApplication(int & argc, char ** argv);
-#ifdef Q_WS_X11
-       bool x11EventFilter (XEvent * ev) { return lyxX11EventFilter(ev); }
-#endif
-#ifdef Q_WS_MACX
-       bool macEventFilter(EventRef event);
-#endif
-};
-
-
-LQApplication::LQApplication(int & argc, char ** argv)
-       : QApplication(argc, argv)
-{
-#ifdef Q_WS_MACX
-       AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
-                             NewAEEventHandlerUPP(handleOpenDocuments),
-                             0, false);
-#endif
-}
-
-
-#ifdef Q_WS_MACX
-bool LQApplication::macEventFilter(EventRef event)
+void cleanup()
 {
-       if (GetEventClass(event) == kEventClassAppleEvent) {
-               EventRecord eventrec;
-               ConvertEventRefToEventRecord(event, &eventrec);
-               AEProcessAppleEvent(&eventrec);
-
-               return false;
-       }
-       return false;
+       delete lyxsocket;
+       lyxsocket = 0;
+       delete lyxserver;
+       lyxserver = 0;
 }
-#endif
-
 
-namespace {
-
-LQApplication * app = 0;
-
-}
+} // namespace anon
 
+// in QLyXKeySym.C
+extern void initEncodings();
 
 namespace lyx_gui {
 
 bool use_gui = true;
 
-void parse_init(int & argc, char * argv[])
+void exec(int & argc, char * argv[])
 {      
        /*
        FIXME : Abdel 29/05/2006 (younes.a@free.fr)
        reorganize this code. In particular make sure that this
-       advise from Qt documentation is respected:
+       advice from Qt documentation is respected:
        
                Since the QApplication object does so much initialization, it
                must be created before any other objects related to the user
                interface are created.
        
-       Right now this is not the case. For example, the call to
-       "FontLoader::initFontPath()" below is doned before the QApplication
-       creation. Moreover, I suspect that a number of global variables
+       Right now this is not the case, I suspect that a number of global variables
        contains Qt object that are initialized before the passage through
        parse_init(). This might also explain the message displayed by Qt
        that caused the hanging:
@@ -181,32 +128,36 @@ void parse_init(int & argc, char * argv[])
        FontLoader::initFontPath();
 
 #ifdef Q_WS_WIN
-       static QApplication win_app(argc, argv);
+       static Application app(argc, argv);
 #else
-       app = new LQApplication(argc, argv);
+       Application app(argc, argv);
 #endif
 
+
        // install translation file for Qt built-in dialogs
        // These are only installed since Qt 3.2.x
-       static QTranslator qt_trans(0);
-       if (qt_trans.load(QString("qt_") + QTextCodec::locale(),
-                         qInstallPathTranslations())) {
+       QTranslator qt_trans;
+       QString language_name = QString("qt_") + QLocale::system().name();
+       language_name.truncate(5);
+       if (qt_trans.load(language_name,
+               QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
+       {
                qApp->installTranslator(&qt_trans);
                // even if the language calls for RtL, don't do that
-               qApp->setReverseLayout(false);
+               qApp->setLayoutDirection(Qt::LeftToRight);
                lyxerr[Debug::GUI]
                        << "Successfully installed Qt translations for locale "
-                       << QTextCodec::locale() << std::endl;
+                       << fromqstr(language_name) << std::endl;
        } else
                lyxerr[Debug::GUI]
                        << "Could not find  Qt translations for locale "
-                       << QTextCodec::locale() << std::endl;
+                       << fromqstr(language_name) << std::endl;
 
 /*#ifdef Q_WS_MACX
        // These translations are meant to break Qt/Mac menu merging
        // algorithm on some entries. It lists the menu names that
        // should not be moved to the LyX menu
-       static QTranslator aqua_trans(0);
+       QTranslator aqua_trans(0);
        aqua_trans.insert(QTranslatorMessage("QMenuBar", "Setting", 0,
                                             "do_not_merge_me"));
        aqua_trans.insert(QTranslatorMessage("QMenuBar", "Config", 0,
@@ -216,7 +167,7 @@ void parse_init(int & argc, char * argv[])
        aqua_trans.insert(QTranslatorMessage("QMenuBar", "Setup", 0,
                                             "do_not_merge_me"));
 
-       app.installTranslator(&aqua_trans);
+       qApp->installTranslator(&aqua_trans);
 #endif
 */
        using namespace lyx::graphics;
@@ -228,6 +179,8 @@ void parse_init(int & argc, char * argv[])
        lyxrc.dpi = getDPI();
 
        LoaderQueue::setPriority(10,100);
+
+       LyX::ref().exec2(argc, argv);
 }
 
 
@@ -236,21 +189,39 @@ void parse_lyxrc()
 
 
 void start(string const & batch, vector<string> const & files,
-          unsigned int width, unsigned int height, int posx, int posy)
+          unsigned int width, unsigned int height, int posx, int posy, bool maximize)
 {
        // this can't be done before because it needs the Languages object
        initEncodings();
 
-       boost::shared_ptr<QtView> view_ptr(new QtView(width, height));
+       boost::shared_ptr<GuiView> view_ptr(new GuiView);
+
        LyX::ref().addLyXView(view_ptr);
 
-       QtView & view = *view_ptr.get();
+       GuiView & view = *view_ptr.get();
+
+       view.init();
+
+       // only true when the -geometry option was NOT used
+       if (width != -1 && height != -1) {
+               if (posx != -1 && posy != -1) {
+#ifdef Q_OS_WIN32
+                       // FIXME: use only setGeoemtry when Trolltech has
+                       // fixed the qt4/X11 bug
+                       view.setGeometry(posx, posy,width, height);
+#else
+                       view.resize(width, height);
+                       view.move(posx, posy);
+#endif
+               } else {
+                       view.resize(width, height);
+               }
 
-       if (posx != -1 && posy != -1)
-               view.move(QPoint(posx, posy));
+               if (maximize)
+                       view.setWindowState(Qt::WindowMaximized);
+       }
 
        view.show();
-       view.init();
 
        // FIXME: some code below needs moving
 
@@ -269,10 +240,7 @@ void start(string const & batch, vector<string> const & files,
        qApp->exec();
 
        // FIXME
-       delete lyxsocket;
-       delete lyxserver;
-       lyxserver = 0;
-       delete app;
+       cleanup();
 }
 
 
@@ -282,24 +250,20 @@ void sync_events()
        // During screen update/ redraw, this method is disabled to
        // prevent keyboard events being handed to the LyX core, where
        // they could cause re-entrant calls to screen update.
-#if QT_VERSION >= 0x030100
-       qApp->processEvents(QEventLoop::ExcludeUserInput);
-#endif
+       qApp->processEvents(QEventLoop::ExcludeUserInputEvents);
 }
 
 
-void exit()
+void exit(int status)
 {
-       delete lyxsocket;
-       delete lyxserver;
-       lyxserver = 0;
+       cleanup();
 
-       // we cannot call qApp->exit(0) - that could return us
+       // we cannot call QApplication::exit(status) - that could return us
        // into a static dialog return in the lyx code (for example,
        // load autosave file QMessageBox. We have to just get the hell
        // out.
 
-       ::exit(0);
+       ::exit(status);
 }