]> git.lyx.org Git - lyx.git/blobdiff - src/lyx_main.C
hopefully fix tex2lyx linking.
[lyx.git] / src / lyx_main.C
index a36bc40c0657b8ede8d62fb4b1c37ca56ec847e6..aab365784227cdb7de034661181b7bdbde76b7d9 100644 (file)
@@ -17,6 +17,7 @@
 
 #include "lyx_main.h"
 
+#include "ConverterCache.h"
 #include "buffer.h"
 #include "buffer_funcs.h"
 #include "bufferlist.h"
@@ -44,6 +45,7 @@
 
 #include "frontends/Alert.h"
 #include "frontends/Application.h"
+#include "frontends/Gui.h"
 #include "frontends/LyXView.h"
 
 #include "support/environment.h"
@@ -55,6 +57,7 @@
 #include "support/package.h"
 #include "support/path.h"
 #include "support/systemcall.h"
+#include "support/unicode.h"
 
 #include <boost/bind.hpp>
 #include <boost/filesystem/operations.hpp>
@@ -112,6 +115,7 @@ namespace {
 string cl_system_support;
 string cl_user_support;
 
+LyX * singleton_ = 0;
 
 void showFileError(string const & error)
 {
@@ -138,6 +142,9 @@ void reconfigureUserLyXDir()
 /// The main application class private implementation.
 struct LyX::Singletons 
 {
+       Singletons(): iconv(ucs4_codeset, "UTF-8")
+       {
+       }
        /// our function handler
        LyXFunc lyxfunc_;
        ///
@@ -152,41 +159,42 @@ struct LyX::Singletons
        boost::scoped_ptr<frontend::Application> application_;
        /// lyx session, containing lastfiles, lastfilepos, and lastopened
        boost::scoped_ptr<Session> session_;
-};
 
-
-boost::scoped_ptr<LyX> LyX::singleton_;
+       ///
+       IconvProcessor iconv;
+};
 
 
-int LyX::exec(int & argc, char * argv[])
+LyX::~LyX()
 {
-       BOOST_ASSERT(!singleton_.get());
-       // We must return from this before launching the gui so that
-       // other parts of the code can access singleton_ through
-       // LyX::ref and LyX::cref.
-       singleton_.reset(new LyX);
-       // Start the real execution loop.
-       return singleton_->priv_exec(argc, argv);
+       // Static data are not treated in the same way at all on the Mac (and
+       // the LyX singleton has static methods). This is the reason why the
+       // exit command on the Mac bypasses our dispatch machinery altogether.
+       // On Linux and Windows we won't pass a second time through quit()
+       // because quitting will already be set to true.
+       if (!quitting)
+               quit();
 }
 
 
 LyX & LyX::ref()
 {
-       BOOST_ASSERT(singleton_.get());
-       return *singleton_.get();
+       BOOST_ASSERT(singleton_);
+       return *singleton_;
 }
 
 
 LyX const & LyX::cref()
 {
-       BOOST_ASSERT(singleton_.get());
-       return *singleton_.get();
+       BOOST_ASSERT(singleton_);
+       return *singleton_;
 }
 
 
 LyX::LyX()
        : first_start(false), geometryOption_(false)
 {
+       singleton_ = this;
        pimpl_.reset(new Singletons);
 }
 
@@ -278,15 +286,16 @@ kb_keymap & LyX::topLevelKeymap()
 }
 
 
-kb_keymap const & LyX::topLevelKeymap() const
+IconvProcessor & LyX::iconvProcessor()
 {
-       BOOST_ASSERT(pimpl_->toplevel_keymap_.get());
-       return *pimpl_->toplevel_keymap_.get();
+       return pimpl_->iconv;
 }
 
-void LyX::addLyXView(LyXView * lyxview)
+
+kb_keymap const & LyX::topLevelKeymap() const
 {
-       views_.push_back(lyxview);
+       BOOST_ASSERT(pimpl_->toplevel_keymap_.get());
+       return *pimpl_->toplevel_keymap_.get();
 }
 
 
@@ -296,10 +305,12 @@ Buffer const * const LyX::updateInset(InsetBase const * inset) const
                return 0;
 
        Buffer const * buffer_ptr = 0;
-       ViewList::const_iterator it = views_.begin();
-       ViewList::const_iterator const end = views_.end();
+       vector<int> const & view_ids = pimpl_->application_->gui().viewIds();
+       vector<int>::const_iterator it = view_ids.begin();
+       vector<int>::const_iterator const end = view_ids.end();
        for (; it != end; ++it) {
-               Buffer const * ptr = (*it)->updateInset(inset);
+               Buffer const * ptr =
+                       pimpl_->application_->gui().view(*it).updateInset(inset);
                if (ptr)
                        buffer_ptr = ptr;
        }
@@ -307,7 +318,7 @@ Buffer const * const LyX::updateInset(InsetBase const * inset) const
 }
 
 
-int LyX::priv_exec(int & argc, char * argv[])
+int LyX::exec(int & argc, char * argv[])
 {
        // Here we need to parse the command line. At least
        // we need to parse for "-dbg" and "-help"
@@ -333,9 +344,10 @@ int LyX::priv_exec(int & argc, char * argv[])
                // Start the real execution loop.
 
                // FIXME
-               /* Create a CoreApplication class that will provide the main event loop and 
-                * the socket callback registering. With Qt4, only QtCore library would be needed.
-                * When this done, a server_mode could be created and the following two
+               /* Create a CoreApplication class that will provide the main event loop
+                * and the socket callback registering. With Qt4, only QtCore
+                * library would be needed.
+                * When this is done, a server_mode could be created and the following two
                 * line would be moved out from here.
                 */
                pimpl_->lyx_server_.reset(new LyXServer(&pimpl_->lyxfunc_, lyxrc.lyxpipes));
@@ -394,20 +406,14 @@ void LyX::earlyExit(int status)
 }
 
 
-void LyX::quit(bool noask)
+void LyX::quit()
 {
        lyxerr[Debug::INFO] << "Running QuitLyX." << endl;
 
-       if (use_gui) {
-               if (!noask && !pimpl_->buffer_list_.quitWriteAll())
-                       return;
-
-               pimpl_->session_->writeFile();
-       }
-
        prepareExit();
 
        if (use_gui) {
+               pimpl_->session_->writeFile();
                pimpl_->lyx_server_.reset();
                pimpl_->lyx_socket_.reset();
                pimpl_->application_->exit(0);
@@ -500,7 +506,7 @@ void LyX::restoreGuiSession(vector<string> const & files)
 
        // if a file is specified, I assume that user wants to edit *that* file
        if (files.empty() && lyxrc.load_session) {
-               vector<string> const & lastopened = pimpl_->session_->lastOpenedFiles();
+               vector<string> const & lastopened = pimpl_->session_->lastOpened().getfiles();
                // do not add to the lastfile list since these files are restored from
                // last seesion, and should be already there (regular files), or should
                // not be added at all (help files).
@@ -508,12 +514,15 @@ void LyX::restoreGuiSession(vector<string> const & files)
                        bind(&LyXView::loadLyXFile, view, _1, false));
        }
        // clear this list to save a few bytes of RAM
-       pimpl_->session_->clearLastOpenedFiles();
+       pimpl_->session_->lastOpened().clear();
 }
 
 
 LyXView * LyX::newLyXView()
 {
+       if (!lyx::use_gui)
+               return 0;
+
        // determine windows size and position, from lyxrc and/or session
        // initial geometry
        unsigned int width = 690;
@@ -526,13 +535,13 @@ LyXView * LyX::newLyXView()
        }
        // if lyxrc returns (0,0), then use session info
        else {
-               string val = session().loadSessionInfo("WindowWidth");
+               string val = session().sessionInfo().load("WindowWidth");
                if (!val.empty())
                        width = convert<unsigned int>(val);
-               val = session().loadSessionInfo("WindowHeight");
+               val = session().sessionInfo().load("WindowHeight");
                if (!val.empty())
                        height = convert<unsigned int>(val);
-               if (session().loadSessionInfo("WindowIsMaximized") == "yes")
+               if (session().sessionInfo().load("WindowIsMaximized") == "yes")
                        maximize = true;
        }
 
@@ -540,10 +549,10 @@ LyXView * LyX::newLyXView()
        int posx = -1;
        int posy = -1;
        if (lyxrc.geometry_xysaved) {
-               string val = session().loadSessionInfo("WindowPosX");
+               string val = session().sessionInfo().load("WindowPosX");
                if (!val.empty())
                        posx = convert<int>(val);
-               val = session().loadSessionInfo("WindowPosY");
+               val = session().sessionInfo().load("WindowPosY");
                if (!val.empty())
                        posy = convert<int>(val);
        }
@@ -554,7 +563,6 @@ LyXView * LyX::newLyXView()
        }
        // create the main window
        LyXView * view = &pimpl_->application_->createView(width, height, posx, posy, maximize);
-       ref().addLyXView(view);
 
        return view;
 }
@@ -733,6 +741,9 @@ bool LyX::init()
        // Query the OS to know what formats are viewed natively
        formats.setAutoOpen();
 
+       // Read lyxrc.dist again to be able to override viewer auto-detection.
+       readRcFile("lyxrc.dist");
+
        system_lyxrc = lyxrc;
        system_formats = formats;
        system_converters = converters;
@@ -798,6 +809,11 @@ bool LyX::init()
 
        lyxerr[Debug::INIT] << "Reading session information '.lyx/session'..." << endl;
        pimpl_->session_.reset(new Session(lyxrc.num_lastfiles));
+
+       // This must happen after package initialization and after lyxrc is
+       // read, therefore it can't be done by a static object.
+       ConverterCache::init();
+
        return true;
 }
 
@@ -1307,16 +1323,10 @@ kb_keymap & theTopLevelKeymap()
        return LyX::ref().topLevelKeymap();
 }
 
-} // namespace lyx
-
 
-namespace boost {
-
-void assertion_failed(char const* a, char const* b, char const* c, long d)
+IconvProcessor & utf8ToUcs4()
 {
-       lyx::lyxerr << "Assertion failed: " << a << ' ' << b << ' ' << c << ' '
-               << d << '\n';
+       return LyX::ref().iconvProcessor();
 }
 
-} // boost
-
+} // namespace lyx