Hello,

This patch introduces frontends/Application.[Ch] and makes qt4/GuiApplication (renamed from qt4/Application) derive from it.

Most of the code in qt4/lyx_gui.C has been transferred either to Application or to qt4/GuiApplication.

Application handles unique instances of LyXFunc, LyXServer and LyXServerSocket.

Most of qt3 and gtk should stay compilable except for LyXView.h because the LyXFunc instance has been transferred to Application.

I need volunteers for gtk and qt3. If nobody steps up, there are two solutions:

1) I put an "#ifdef QT4" or something in LyXView.h so that I can commit. I will have then to wait for some potential volunteer to do the other frontend cleanups before doing some more (see below).

2) I just commit and continue my cleanup work. This includes:
- replace all instances of lyxserver with theApp->server()
- replace all instances of lyxsocket with theApp->socket()
- replace all instances of bv->owner()->getLyXFunc() with theApp->lyXFunc()
- transfer the Clipboard and Selection interfaces from Gui to Application and replace all instances of bv->owner()->gui()->[clipboard,selection]() with theApp->[clipboard,selection](). - remove lyx_gui.[Ch] and replace that with direct calls to theApp global pointer. This will probably involves some cleanup of lyx_main.[Ch].

When I've done all this. I think we will be mostly ready for multiple LyXVviews.

Comments? Opinions? Objections?

Abdel.
Index: frontends/Application.C
===================================================================
--- frontends/Application.C     (revision 0)
+++ frontends/Application.C     (revision 0)
@@ -0,0 +1,104 @@
+/**
+ * \file frontend/Application.C
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
+ *
+ * \author Abdelrazak Younes
+ *
+ * Full author contact details are available in file CREDITS.
+ */
+
+#include <config.h>
+
+#include "Application.h"
+
+#include "funcrequest.h"
+#include "LyXAction.h"
+#include "lyxrc.h"
+#include "LyXView.h"
+
+#include "support/lstrings.h"
+#include "support/os.h"
+#include "support/package.h"
+
+#include <boost/scoped_ptr.hpp>
+
+using lyx::support::package;
+
+// FIXME: replace all occurence of lyxserver with theApp->server().
+LyXServer * lyxserver;
+// FIXME: replace all occurence of lyxsocket with theApp->socket().
+LyXServerSocket * lyxsocket;
+
+namespace lyx {
+namespace frontend {
+
+
+Application::Application(int & argc, char ** argv)
+{
+}
+
+
+LyXFunc & Application::lyXFunc()
+{
+       return *lyxfunc_.get(); 
+}
+
+
+LyXFunc const & Application::lyXFunc() const
+{
+       return *lyxfunc_.get(); 
+}
+
+
+LyXServer & Application::server()
+{
+       return *lyx_server_.get(); 
+}
+
+
+LyXServer const & Application::server() const 
+{
+       return *lyx_server_.get(); 
+}
+
+
+LyXServerSocket & Application::socket()
+{
+       return *lyx_socket_.get();
+}
+
+
+LyXServerSocket const & Application::socket() const
+{
+       return *lyx_socket_.get();
+}
+
+
+void Application::setBufferView(BufferView * buffer_view)
+{
+       buffer_view_ = buffer_view_;
+}
+
+
+int Application::start(std::string const & batch)
+{
+       lyx_server_.reset(new LyXServer(lyxfunc_.get(), lyxrc.lyxpipes));
+       lyx_socket_.reset(new LyXServerSocket(lyxfunc_.get(), 
+               lyx::support::os::internal_path(package().temp_dir() + 
"/lyxsocket")));
+
+       // FIXME: these two lines should disappear soon (Abdel 20/09/71)
+       lyxserver = lyx_server_.get();
+       lyxsocket = lyx_socket_.get();
+
+       // handle the batch commands the user asked for
+       if (!batch.empty()) {
+               lyxfunc_->dispatch(lyxaction.lookupFunc(batch));
+       }
+
+       return exec();
+}
+
+
+} // namespace frontend
+} // namespace lyx
Index: frontends/Application.h
===================================================================
--- frontends/Application.h     (revision 0)
+++ frontends/Application.h     (revision 0)
@@ -0,0 +1,89 @@
+/**
+ * \file frontend/Application.h
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
+ *
+ * \author Abdelrazak Younes
+ *
+ * Full author contact details are available in file CREDITS.
+ */
+
+#ifndef LYX_APPLICATION_H
+#define LYX_APPLICATION_H
+
+#include "lyxfunc.h"
+#include "lyxserver.h"
+#include "lyxsocket.h"
+
+#include <boost/scoped_ptr.hpp>
+
+#include <string>
+
+class BufferView;
+class LyXView;
+
+namespace lyx {
+namespace frontend {
+
+//class GuiWorkArea;
+class Gui;
+
+
+/// The main application class
+/**
+There should be only one instance of this class. No Qt object
+initialisation should be done before the instanciation of this class.
+
+\todo The work areas handling could be moved to a base virtual class
+comon to all frontends.
+*/
+class Application
+{
+public:
+       Application(int & argc, char ** argv);
+
+       int start(std::string const & batch);
+       ///
+       virtual Gui & gui() = 0;
+       ///
+       virtual int const exec() = 0;
+       ///
+       virtual void exit(int status) = 0;
+
+       ///
+       LyXFunc & lyXFunc();
+       LyXFunc const & lyXFunc() const;
+       ///
+       LyXServer & server();
+       LyXServer const & server() const;
+       ///
+       LyXServerSocket & socket();
+       LyXServerSocket const & socket() const;
+       ///
+       void setBufferView(BufferView * buffer_view);
+
+protected:
+       ///
+       BufferView * buffer_view_;
+
+       // FIXME: lyxfunc_ should be private. But the actual construction is 
done in
+       // GuiApplication for now.
+
+       /// our function handler
+       boost::scoped_ptr<LyXFunc> lyxfunc_;
+
+private:
+       ///
+       boost::scoped_ptr<LyXServer> lyx_server_;
+       ///
+       boost::scoped_ptr<LyXServerSocket> lyx_socket_;
+
+}; // Application
+
+} // namespace frontend
+} // namespace lyx
+
+extern lyx::frontend::Application * theApp;
+
+
+#endif // LYX_APPLICATION_H
Index: frontends/LyXView.h
===================================================================
--- frontends/LyXView.h (revision 15068)
+++ frontends/LyXView.h (working copy)
@@ -13,6 +13,7 @@
 #ifndef LYXVIEW_H
 #define LYXVIEW_H
 
+#include "frontends/Application.h"
 #include "frontends/Toolbars.h"
 
 #include <boost/scoped_ptr.hpp>
@@ -87,9 +88,9 @@
        Buffer * buffer() const;
 
        /// return the LyX function handler for this view
-       LyXFunc & getLyXFunc() { return *lyxfunc_.get(); }
+       LyXFunc & getLyXFunc() { return theApp->lyXFunc(); }
        ///
-       LyXFunc const & getLyXFunc() const { return *lyxfunc_.get(); }
+       LyXFunc const & getLyXFunc() const { return theApp->lyXFunc(); }
 
        /// return the toolbar for this view
        Toolbars & getToolbars() { return *toolbars_.get(); }
Index: frontends/qt4/Application.C
===================================================================
--- frontends/qt4/Application.C (revision 15068)
+++ frontends/qt4/Application.C (working copy)
@@ -1,183 +0,0 @@
-/**
- * \file qt4/Application.C
- * This file is part of LyX, the document processor.
- * Licence details can be found in the file COPYING.
- *
- * \author unknown
- * \author John Levon
- * \author Abdelrazak Younes
- *
- * Full author contact details are available in file CREDITS.
- */
-
-#include <config.h>
-
-#include "Application.h"
-
-#include "GuiWorkArea.h"
-
-#include "qt_helpers.h"
-
-#include "BufferView.h"
-#include "debug.h"
-
-#include "support/lstrings.h"
-
-#include <QApplication>
-#include <QEventLoop>
-#include <QTranslator>
-#include <QTextCodec>
-#include <QClipboard>
-
-#ifdef Q_WS_X11
-#include <X11/Xlib.h>
-#endif
-
-using lyx::support::subst;
-
-using std::string;
-using std::endl;
-
-///////////////////////////////////////////////////////////////
-// You can find other X11 and MACX specific stuff
-// at the end of this file...
-///////////////////////////////////////////////////////////////
-
-namespace lyx {
-namespace frontend {
-
-Application::Application(int & argc, char ** argv)
-       : QApplication(argc, argv), buffer_view_(0)
-{
-#ifdef Q_WS_X11
-       // doubleClickInterval() is 400 ms on X11 witch is just too long.
-       // On Windows and Mac OS X, the operating system's value is used.
-       // On Microsoft Windows, calling this function sets the double
-       // click interval for all applications. So we don't!
-       QApplication::setDoubleClickInterval(300);
-#endif
-
-#ifdef Q_WS_MACX
-       AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
-                             NewAEEventHandlerUPP(handleOpenDocuments),
-                             0, false);
-#endif
-}
-
-
-void Application::setBufferView(BufferView * buffer_view)
-{
-       buffer_view_ = buffer_view;
-}
-
-
-////////////////////////////////////////////////////////////////////////
-// X11 specific stuff goes here...
-#ifdef Q_WS_X11
-bool Application::x11EventFilter(XEvent * xev)
-{
-       switch (xev->type) {
-       case SelectionRequest:
-               lyxerr[Debug::GUI] << "X requested selection." << endl;
-               if (buffer_view_) {
-                       lyx::docstring const sel = 
buffer_view_->requestSelection();
-                       if (!sel.empty())
-                               gui_.selection().put(sel);
-               }
-               break;
-       case SelectionClear:
-               lyxerr[Debug::GUI] << "Lost selection." << endl;
-               if (buffer_view_)
-                       buffer_view_->clearSelection();
-               break;
-       }
-       return false;
-}
-#endif
-
-
-////////////////////////////////////////////////////////////////////////
-// Mac OSX specific stuff goes here...
-
-#ifdef Q_WS_MACX
-namespace{
-OSErr checkAppleEventForMissingParams(const AppleEvent& theAppleEvent)
- {
-       DescType returnedType;
-       Size actualSize;
-       OSErr err = AEGetAttributePtr(&theAppleEvent, keyMissedKeywordAttr,
-                                     typeWildCard, &returnedType, nil, 0,
-                                     &actualSize);
-       switch (err) {
-       case errAEDescNotFound:
-               return noErr;
-       case noErr:
-               return errAEEventNotHandled;
-       default:
-               return err;
-       }
- }
-} // namespace
-
-OSErr Application::handleOpenDocuments(const AppleEvent* inEvent,
-                                      AppleEvent* /*reply*/, long /*refCon*/)
-{
-       QString s_arg;
-       AEDescList documentList;
-       OSErr err = AEGetParamDesc(inEvent, keyDirectObject, typeAEList,
-                                  &documentList);
-       if (err != noErr)
-               return err;
-
-       err = checkAppleEventForMissingParams(*inEvent);
-       if (err == noErr) {
-               long documentCount;
-               err = AECountItems(&documentList, &documentCount);
-               for (long documentIndex = 1;
-                    err == noErr && documentIndex <= documentCount;
-                    documentIndex++) {
-                       DescType returnedType;
-                       Size actualSize;
-                       AEKeyword keyword;
-                       FSRef ref;
-                       char qstr_buf[1024];
-                       err = AESizeOfNthItem(&documentList, documentIndex,
-                                             &returnedType, &actualSize);
-                       if (err == noErr) {
-                               err = AEGetNthPtr(&documentList, documentIndex,
-                                                 typeFSRef, &keyword,
-                                                 &returnedType, (Ptr)&ref,
-                                                 sizeof(FSRef), &actualSize);
-                               if (err == noErr) {
-                                       FSRefMakePath(&ref, (UInt8*)qstr_buf,
-                                                     1024);
-                                       s_arg=QString::fromUtf8(qstr_buf);
-//                                     buffer_view_->workAreaDispatch(
-//                                             FuncRequest(LFUN_FILE_OPEN,
-//                                                         fromqstr(s_arg)));
-                                       break;
-                               }
-                       }
-               } // for ...
-       }
-       AEDisposeDesc(&documentList);
-
-       return err;
-}
-
-bool Application::macEventFilter(EventRef event)
-{
-       if (GetEventClass(event) == kEventClassAppleEvent) {
-               EventRecord eventrec;
-               ConvertEventRefToEventRecord(event, &eventrec);
-               AEProcessAppleEvent(&eventrec);
-
-               return false;
-       }
-       return false;
-}
-
-#endif  // Q_WS_MACX
-
-} // namespace frontend
-} // namespace lyx
Index: frontends/qt4/Application.h
===================================================================
--- frontends/qt4/Application.h (revision 15068)
+++ frontends/qt4/Application.h (working copy)
@@ -1,87 +0,0 @@
-/**
- * \file qt4/Application.h
- * This file is part of LyX, the document processor.
- * Licence details can be found in the file COPYING.
- *
- * \author unknown
- * \author John Levon
- * \author Abdelrazak Younes
- *
- * Full author contact details are available in file CREDITS.
- */
-
-#ifndef LYX_APPLICATION_H
-#define LYX_APPLICATION_H
-
-#include "GuiImplementation.h"
-#include "FontLoader.h"
-
-#include <QApplication>
-
-///////////////////////////////////////////////////////////////
-// Specific stuff
-
-#ifdef Q_WS_MACX
-#include <Carbon/Carbon.h>
-#endif
-///////////////////////////////////////////////////////////////
-
-class BufferView;
-
-namespace lyx {
-namespace frontend {
-
-class GuiWorkArea;
-
-/// The Qt main application class
-/**
-There should be only one instance of this class. No Qt object
-initialisation should be done before the instanciation of this class.
-
-\todo The work areas handling could be moved to a base virtual class
-comon to all frontends.
-*/
-class Application : public QApplication
-{
-public:
-       Application(int & argc, char ** argv);
-
-       //
-       Gui & gui() { return gui_; }
-       ///
-       FontLoader & fontLoader() { return font_loader_; }
-       ///
-       void setBufferView(BufferView * buffer_view);
-
-private:
-       ///
-       BufferView * buffer_view_;
-
-       ///
-       GuiImplementation gui_;
-
-       ///
-       FontLoader font_loader_;
-
-#ifdef Q_WS_X11
-public:
-       bool x11EventFilter (XEvent * ev);
-#endif
-
-#ifdef Q_WS_MACX
-public:
-       bool macEventFilter(EventRef event);
-private:
-//     static OSStatus handleOpenDocuments(
-       static pascal OSErr     handleOpenDocuments(
-               const AppleEvent* inEvent, AppleEvent*, long);
-#endif
-}; // Application
-
-} // namespace frontend
-} // namespace lyx
-
-extern lyx::frontend::Application * theApp;
-
-
-#endif // LYX_APPLICATION_H
Index: frontends/qt4/GuiApplication.C
===================================================================
--- frontends/qt4/GuiApplication.C      (revision 15068)
+++ frontends/qt4/GuiApplication.C      (working copy)
@@ -1,5 +1,5 @@
 /**
- * \file qt4/Application.C
+ * \file qt4/GuiApplication.C
  * This file is part of LyX, the document processor.
  * Licence details can be found in the file COPYING.
  *
@@ -12,42 +12,68 @@
 
 #include <config.h>
 
-#include "Application.h"
+#include "GuiApplication.h"
 
+#include "GuiView.h"
 #include "GuiWorkArea.h"
-
 #include "qt_helpers.h"
+#include "QLImage.h"
 
 #include "BufferView.h"
-#include "debug.h"
 
+#include "graphics/LoaderQueue.h"
+
 #include "support/lstrings.h"
+#include "support/os.h"
+#include "support/package.h"
 
+#include "lyx_main.h"
+#include "lyxrc.h"
+#include "debug.h"
+
 #include <QApplication>
+#include <QClipboard>
 #include <QEventLoop>
+#include <QLocale>
+#include <QLibraryInfo>
+#include <QTextCodec>
 #include <QTranslator>
-#include <QTextCodec>
-#include <QClipboard>
 
 #ifdef Q_WS_X11
 #include <X11/Xlib.h>
 #endif
 
+#include <boost/bind.hpp>
+
 using lyx::support::subst;
 
 using std::string;
 using std::endl;
 
+// in QLyXKeySym.C
+extern void initEncodings();
+
 ///////////////////////////////////////////////////////////////
 // You can find other X11 and MACX specific stuff
 // at the end of this file...
 ///////////////////////////////////////////////////////////////
 
+namespace {
+
+int getDPI()
+{
+       QWidget w;
+       return int(0.5 * (w.logicalDpiX() + w.logicalDpiY()));
+}
+
+} // namespace anon
+
+
 namespace lyx {
 namespace frontend {
 
-Application::Application(int & argc, char ** argv)
-       : QApplication(argc, argv), buffer_view_(0)
+GuiApplication::GuiApplication(int & argc, char ** argv)
+       : QApplication(argc, argv), Application(argc, argv)
 {
 #ifdef Q_WS_X11
        // doubleClickInterval() is 400 ms on X11 witch is just too long.
@@ -62,19 +88,121 @@
                              NewAEEventHandlerUPP(handleOpenDocuments),
                              0, false);
 #endif
+
+       // install translation file for Qt built-in dialogs
+       // These are only installed since Qt 3.2.x
+       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->setLayoutDirection(Qt::LeftToRight);
+               lyxerr[Debug::GUI]
+                       << "Successfully installed Qt translations for locale "
+                       << fromqstr(language_name) << std::endl;
+       } else
+               lyxerr[Debug::GUI]
+                       << "Could not find  Qt translations for locale "
+                       << 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
+       QTranslator aqua_trans(0);
+       aqua_trans.insert(QTranslatorMessage("QMenuBar", "Setting", 0,
+                                            "do_not_merge_me"));
+       aqua_trans.insert(QTranslatorMessage("QMenuBar", "Config", 0,
+                                            "do_not_merge_me"));
+       aqua_trans.insert(QTranslatorMessage("QMenuBar", "Options", 0,
+                                            "do_not_merge_me"));
+       aqua_trans.insert(QTranslatorMessage("QMenuBar", "Setup", 0,
+                                            "do_not_merge_me"));
+
+       qApp->installTranslator(&aqua_trans);
+#endif
+*/
+       using namespace lyx::graphics;
+
+       Image::newImage = boost::bind(&QLImage::newImage);
+       Image::loadableFormats = boost::bind(&QLImage::loadableFormats);
+
+       // needs to be done before reading lyxrc
+       lyxrc.dpi = getDPI();
+
+       LoaderQueue::setPriority(10,100);
 }
 
 
-void Application::setBufferView(BufferView * buffer_view)
+int const GuiApplication::exec()
 {
-       buffer_view_ = buffer_view;
+       return QApplication::exec();
 }
 
 
+void GuiApplication::exit(int status)
+{
+       QApplication::exit(status);
+}
+
+
+// FIXME: this whole method needs to be moved to Application.
+LyXView & GuiApplication::createView(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();
+
+       int view_id = gui().newView(width, height);
+       GuiView & view = static_cast<GuiView &> (gui().view(view_id));
+
+       lyxfunc_.reset(new LyXFunc(&view));
+
+       // FIXME: for now we assume that there is only one LyXView with id = 0.
+       /*int workArea_id_ =*/ gui().newWorkArea(width, height, 0);
+       //WorkArea * workArea_ = & theApp->gui().workArea(workArea_id_);
+
+       LyX::ref().addLyXView(&view);
+
+       view.init();
+
+       // FIXME: put this initialisation code in GuiView accessible via
+       // a pure virtual method in LyXView.
+
+       // only true when the -geometry option was NOT used
+       if (width != 0 && height != 0) {
+               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 (maximize)
+                       view.setWindowState(Qt::WindowMaximized);
+       }
+
+       view.show();
+
+       return view;
+}
+
+
 ////////////////////////////////////////////////////////////////////////
 // X11 specific stuff goes here...
 #ifdef Q_WS_X11
-bool Application::x11EventFilter(XEvent * xev)
+bool GuiApplication::x11EventFilter(XEvent * xev)
 {
        switch (xev->type) {
        case SelectionRequest:
@@ -101,6 +229,7 @@
 
 #ifdef Q_WS_MACX
 namespace{
+
 OSErr checkAppleEventForMissingParams(const AppleEvent& theAppleEvent)
  {
        DescType returnedType;
@@ -117,9 +246,10 @@
                return err;
        }
  }
+
 } // namespace
 
-OSErr Application::handleOpenDocuments(const AppleEvent* inEvent,
+OSErr GuiApplication::handleOpenDocuments(const AppleEvent* inEvent,
                                       AppleEvent* /*reply*/, long /*refCon*/)
 {
        QString s_arg;
@@ -165,7 +295,7 @@
        return err;
 }
 
-bool Application::macEventFilter(EventRef event)
+bool GuiApplication::macEventFilter(EventRef event)
 {
        if (GetEventClass(event) == kEventClassAppleEvent) {
                EventRecord eventrec;
Index: frontends/qt4/GuiApplication.h
===================================================================
--- frontends/qt4/GuiApplication.h      (revision 15068)
+++ frontends/qt4/GuiApplication.h      (working copy)
@@ -1,5 +1,5 @@
 /**
- * \file qt4/Application.h
+ * \file qt4/GuiApplication.h
  * This file is part of LyX, the document processor.
  * Licence details can be found in the file COPYING.
  *
@@ -10,12 +10,14 @@
  * Full author contact details are available in file CREDITS.
  */
 
-#ifndef LYX_APPLICATION_H
-#define LYX_APPLICATION_H
+#ifndef QT4_APPLICATION_H
+#define QT4_APPLICATION_H
 
 #include "GuiImplementation.h"
 #include "FontLoader.h"
 
+#include "frontends/Application.h"
+
 #include <QApplication>
 
 ///////////////////////////////////////////////////////////////
@@ -41,23 +43,27 @@
 \todo The work areas handling could be moved to a base virtual class
 comon to all frontends.
 */
-class Application : public QApplication
+class GuiApplication : public QApplication, public Application
 {
 public:
-       Application(int & argc, char ** argv);
+       GuiApplication(int & argc, char ** argv);
 
-       //
-       Gui & gui() { return gui_; }
+       /// Method inherited from \c Application class
+       //@{
+       virtual int const exec();
+       virtual Gui & gui() { return gui_; }
+       virtual void exit(int status);
+       //@}
+
        ///
        FontLoader & fontLoader() { return font_loader_; }
+
        ///
-       void setBufferView(BufferView * buffer_view);
+       LyXView & createView(unsigned int width, unsigned int height,
+               int posx, int posy, bool maximize);
 
 private:
        ///
-       BufferView * buffer_view_;
-
-       ///
        GuiImplementation gui_;
 
        ///
@@ -76,12 +82,12 @@
        static pascal OSErr     handleOpenDocuments(
                const AppleEvent* inEvent, AppleEvent*, long);
 #endif
-}; // Application
+}; // GuiApplication
 
 } // namespace frontend
 } // namespace lyx
 
-extern lyx::frontend::Application * theApp;
+extern lyx::frontend::GuiApplication * guiApp;
 
 
-#endif // LYX_APPLICATION_H
+#endif // QT4_APPLICATION_H
Index: frontends/qt4/GuiWorkArea.C
===================================================================
--- frontends/qt4/GuiWorkArea.C (revision 15068)
+++ frontends/qt4/GuiWorkArea.C (working copy)
@@ -13,7 +13,7 @@
 
 #include "GuiWorkArea.h"
 
-#include "Application.h"
+#include "GuiApplication.h"
 #include "ColorCache.h"
 #include "QLPainter.h"
 #include "QLyXKeySym.h"
Index: frontends/qt4/lyx_gui.C
===================================================================
--- frontends/qt4/lyx_gui.C     (revision 15068)
+++ frontends/qt4/lyx_gui.C     (working copy)
@@ -28,12 +28,7 @@
 #include "lyxsocket.h"
 
 
-#include "graphics/LoaderQueue.h"
-
 #include "support/lstrings.h"
-#include "support/os.h"
-#include "support/package.h"
-#include "debug.h"
 
 
 #include "GuiView.h"
@@ -42,7 +37,7 @@
 #include "QLImage.h"
 #include "qt_helpers.h"
 #include "socket_callback.h"
-#include "Application.h"
+#include "GuiApplication.h"
 
 #include <QApplication>
 #include <QEventLoop>
@@ -56,14 +51,11 @@
 
 
 using lyx::support::ltrim;
-using lyx::support::package;
 
 using lyx::frontend::GuiImplementation;
 using lyx::frontend::GuiView;
-using lyx::frontend::Application;
+using lyx::frontend::GuiApplication;
 
-namespace os = lyx::support::os;
-
 using boost::shared_ptr;
 
 #ifndef CXX_GLOBAL_CSTD
@@ -74,35 +66,16 @@
 using std::vector;
 using std::string;
 
-// FIXME: wrong place !
-LyXServer * lyxserver;
-LyXServerSocket * lyxsocket;
-
+lyx::frontend::GuiApplication * guiApp;
 lyx::frontend::Application * theApp;
 
+
 namespace {
 
-int getDPI()
-{
-       QWidget w;
-       return int(0.5 * (w.logicalDpiX() + w.logicalDpiY()));
-}
-
 map<int, shared_ptr<socket_callback> > socket_callbacks;
 
-void cleanup()
-{
-       delete lyxsocket;
-       lyxsocket = 0;
-       delete lyxserver;
-       lyxserver = 0;
-}
-
 } // namespace anon
 
-// in QLyXKeySym.C
-extern void initEncodings();
-
 namespace lyx_gui {
 
 bool use_gui = true;
@@ -124,66 +97,24 @@
        that caused the hanging:
 
        QObject::killTimer: timers cannot be stopped from another thread
+
+       I hope that the problem will disappear automagically when we get rid of
+       lyx_gui entirely, thus using theApp directly throughout the code for 
LyXFunc,
+       Clipboard and Selection access.
        */
 
        // Force adding of font path _before_ QApplication is initialized
        FontLoader::initFontPath();
 
 #if defined(Q_WS_WIN) && !defined(Q_CYGWIN_WIN)
-       static Application app(argc, argv);
+       static GuiApplication app(argc, argv);
 #else
-       Application app(argc, argv);
+       GuiApplication app(argc, argv);
 #endif
 
-       theApp = &app;
+       guiApp = &app;
+       theApp = guiApp;
 
-
-       // install translation file for Qt built-in dialogs
-       // These are only installed since Qt 3.2.x
-       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->setLayoutDirection(Qt::LeftToRight);
-               lyxerr[Debug::GUI]
-                       << "Successfully installed Qt translations for locale "
-                       << fromqstr(language_name) << std::endl;
-       } else
-               lyxerr[Debug::GUI]
-                       << "Could not find  Qt translations for locale "
-                       << 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
-       QTranslator aqua_trans(0);
-       aqua_trans.insert(QTranslatorMessage("QMenuBar", "Setting", 0,
-                                            "do_not_merge_me"));
-       aqua_trans.insert(QTranslatorMessage("QMenuBar", "Config", 0,
-                                            "do_not_merge_me"));
-       aqua_trans.insert(QTranslatorMessage("QMenuBar", "Options", 0,
-                                            "do_not_merge_me"));
-       aqua_trans.insert(QTranslatorMessage("QMenuBar", "Setup", 0,
-                                            "do_not_merge_me"));
-
-       qApp->installTranslator(&aqua_trans);
-#endif
-*/
-       using namespace lyx::graphics;
-
-       Image::newImage = boost::bind(&QLImage::newImage);
-       Image::loadableFormats = boost::bind(&QLImage::loadableFormats);
-
-       // needs to be done before reading lyxrc
-       lyxrc.dpi = getDPI();
-
-       LoaderQueue::setPriority(10,100);
-
        return LyX::ref().exec2(argc, argv);
 }
 
@@ -195,63 +126,13 @@
 LyXView * create_view(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();
-
-       int view_id = theApp->gui().newView(width, height);
-       GuiView & view = static_cast<GuiView &> (theApp->gui().view(view_id));
-
-       // FIXME: for now we assume that there is only one LyXView with id = 0.
-       /*int workArea_id_ =*/ theApp->gui().newWorkArea(width, height, 0);
-       //WorkArea * workArea_ = & theApp->gui().workArea(workArea_id_);
-
-       LyX::ref().addLyXView(&view);
-
-       view.init();
-
-       // only true when the -geometry option was NOT used
-       if (width != 0 && height != 0) {
-               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 (maximize)
-                       view.setWindowState(Qt::WindowMaximized);
-       }
-
-       view.show();
-
-       return &view;
+       return &guiApp->createView(width, height, posx, posy, maximize);
 }
 
 
 int start(LyXView * view, string const & batch)
 {
-       // FIXME: some code below needs moving
-
-       lyxserver = new LyXServer(&view->getLyXFunc(), lyxrc.lyxpipes);
-       lyxsocket = new LyXServerSocket(&view->getLyXFunc(),
-                         os::internal_path(package().temp_dir() + 
"/lyxsocket"));
-
-       // handle the batch commands the user asked for
-       if (!batch.empty()) {
-               view->getLyXFunc().dispatch(lyxaction.lookupFunc(batch));
-       }
-
-       int const status = qApp->exec();
-
-       // FIXME
-       cleanup();
-       return status;
+       return theApp->start(batch);
 }
 
 
@@ -261,14 +142,13 @@
        // 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.
-       qApp->processEvents(QEventLoop::ExcludeUserInputEvents);
+       guiApp->processEvents(QEventLoop::ExcludeUserInputEvents);
 }
 
 
 void exit(int status)
 {
-       cleanup();
-       QApplication::exit(status);
+       guiApp->exit(status);
 }
 
 
@@ -318,13 +198,13 @@
 
 void update_fonts()
 {
-       theApp->fontLoader().update();
+       guiApp->fontLoader().update();
 }
 
 
 bool font_available(LyXFont const & font)
 {
-       return theApp->fontLoader().available(font);
+       return guiApp->fontLoader().available(font);
 }
 
 
Index: frontends/qt4/qfont_metrics.C
===================================================================
--- frontends/qt4/qfont_metrics.C       (revision 15068)
+++ frontends/qt4/qfont_metrics.C       (working copy)
@@ -14,7 +14,7 @@
 #include "frontends/font_metrics.h"
 #include "frontends/lyx_gui.h"
 
-#include "Application.h"
+#include "GuiApplication.h"
 #include "FontLoader.h"
 #include "qt_helpers.h"
 
@@ -39,8 +39,8 @@
        LyXFont smallfont = f;
        smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
 
-       QFontMetrics const & qm = theApp->fontLoader().metrics(f);
-       QFontMetrics const & qsmallm = theApp->fontLoader().metrics(smallfont);
+       QFontMetrics const & qm = guiApp->fontLoader().metrics(f);
+       QFontMetrics const & qsmallm = guiApp->fontLoader().metrics(smallfont);
 
        int w = 0;
 
@@ -65,7 +65,7 @@
 {
        if (!lyx_gui::use_gui)
                return 1;
-       return theApp->fontLoader().metrics(f).ascent();
+       return guiApp->fontLoader().metrics(f).ascent();
 }
 
 
@@ -75,7 +75,7 @@
                return 1;
        // We add 1 as the value returned by QT is different than X
        // See http://doc.trolltech.com/2.3/qfontmetrics.html#200b74
-       return theApp->fontLoader().metrics(f).descent() + 1;
+       return guiApp->fontLoader().metrics(f).descent() + 1;
 }
 
 
@@ -83,7 +83,7 @@
 {
        if (!lyx_gui::use_gui)
                return 1;
-       QRect const & r = 
theApp->fontLoader().metrics(f).boundingRect(ucs4_to_qchar(c));
+       QRect const & r = 
guiApp->fontLoader().metrics(f).boundingRect(ucs4_to_qchar(c));
        // Qt/Win 3.2.1nc (at least) corrects the GetGlyphOutlineA|W y
        // value by the height: (x, -y-height, width, height).
        // Other versions return: (x, -y, width, height)
@@ -99,7 +99,7 @@
 {
        if (!lyx_gui::use_gui)
                return 1;
-       QRect const & r = 
theApp->fontLoader().metrics(f).boundingRect(ucs4_to_qchar(c));
+       QRect const & r = 
guiApp->fontLoader().metrics(f).boundingRect(ucs4_to_qchar(c));
        // Qt/Win 3.2.1nc (at least) corrects the GetGlyphOutlineA|W y
        // value by the height: (x, -y-height, width, height).
        // Other versions return: (x, -y, width, height)
@@ -115,7 +115,7 @@
 {
        if (!lyx_gui::use_gui)
                return 1;
-       return theApp->fontLoader().metrics(f).leftBearing(ucs4_to_qchar(c));
+       return guiApp->fontLoader().metrics(f).leftBearing(ucs4_to_qchar(c));
 }
 
 
@@ -123,7 +123,7 @@
 {
        if (!lyx_gui::use_gui)
                return 1;
-       QFontMetrics const & m = theApp->fontLoader().metrics(f);
+       QFontMetrics const & m = guiApp->fontLoader().metrics(f);
 
        // Qt rbearing is from the right edge of the char's width().
        QChar sc = ucs4_to_qchar(c);
@@ -141,7 +141,7 @@
        if (f.realShape() == LyXFont::SMALLCAPS_SHAPE)
                return smallcapswidth(ucs2, f);
 
-       QLFontInfo & fi = theApp->fontLoader().fontinfo(f);
+       QLFontInfo & fi = guiApp->fontLoader().fontinfo(f);
 
        if (ls == 1)
                return fi.width(ucs2[0].unicode());
@@ -166,7 +166,7 @@
 void font_metrics::rectText(docstring const & str, LyXFont const & f,
        int & w, int & ascent, int & descent)
 {
-       QFontMetrics const & m = theApp->fontLoader().metrics(f);
+       QFontMetrics const & m = guiApp->fontLoader().metrics(f);
        static int const d = 2;
        w = width(str, f) + d * 2 + 2;
        ascent = m.ascent() + d;
@@ -178,7 +178,7 @@
 void font_metrics::buttonText(docstring const & str, LyXFont const & f,
        int & w, int & ascent, int & descent)
 {
-       QFontMetrics const & m = theApp->fontLoader().metrics(f);
+       QFontMetrics const & m = guiApp->fontLoader().metrics(f);
        static int const d = 3;
        w = width(str, f) + d * 2 + 2;
        ascent = m.ascent() + d;
Index: frontends/qt4/QLPainter.C
===================================================================
--- frontends/qt4/QLPainter.C   (revision 15068)
+++ frontends/qt4/QLPainter.C   (working copy)
@@ -18,7 +18,7 @@
 #include "ColorCache.h"
 #include "FontLoader.h"
 
-#include "Application.h"
+#include "GuiApplication.h"
 #include "qt_helpers.h"
 
 #include "debug.h"
@@ -202,8 +202,8 @@
        LyXFont smallfont(f);
        smallfont.decSize().decSize().setShape(LyXFont::UP_SHAPE);
 
-       QFont const & qfont = theApp->fontLoader().get(f);
-       QFont const & qsmallfont = theApp->fontLoader().get(smallfont);
+       QFont const & qfont = guiApp->fontLoader().get(f);
+       QFont const & qsmallfont = guiApp->fontLoader().get(smallfont);
        QFontMetrics const & qfontm = QFontMetrics(qfont);
        QFontMetrics const & qsmallfontm = QFontMetrics(qsmallfont);
 
@@ -252,7 +252,7 @@
 
        if (f.realShape() != LyXFont::SMALLCAPS_SHAPE) {
                setQPainterPen(f.realColor());
-               qp_->setFont(theApp->fontLoader().get(f));
+               qp_->setFont(guiApp->fontLoader().get(f));
                // We need to draw the text as LTR as we use our own bidi code.
                qp_->setLayoutDirection(Qt::LeftToRight);
                qp_->drawText(x, y, str);

Reply via email to