]> git.lyx.org Git - lyx.git/blobdiff - src/lyx_main.C
ensure that Application is properly destroyed after at the end of LyX::priv_exec()
[lyx.git] / src / lyx_main.C
index 958f2fdd39a9bc4fe48183b28ad16976cfc56bbc..f1eb44260610411e3c0ffa3378c882c42cc751e5 100644 (file)
@@ -42,7 +42,6 @@
 
 #include "frontends/Alert.h"
 #include "frontends/Application.h"
-#include "frontends/lyx_gui.h"
 #include "frontends/LyXView.h"
 
 #include "support/environment.h"
@@ -77,6 +76,7 @@ using lyx::support::Systemcall;
 
 using lyx::docstring;
 
+namespace Alert = lyx::frontend::Alert;
 namespace os = lyx::support::os;
 namespace fs = boost::filesystem;
 
@@ -92,9 +92,22 @@ using std::system;
 #endif
 
 
-// convenient to have it here.
+/// convenient to have it here.
 boost::scoped_ptr<kb_keymap> toplevel_keymap;
 
+///
+lyx::frontend::Application * theApp;
+
+namespace lyx {
+
+/// are we using the GUI at all?
+/** 
+* We default to true and this is changed to false when the export feature is used.
+*/
+bool use_gui = true;
+
+}
+
 namespace {
 
 // Filled with the command line arguments "foo" of "-sysdir foo" or
@@ -108,7 +121,7 @@ void lyx_exit(int status)
        // FIXME: We should not directly call exit(), since it only
        // guarantees a return to the system, no application cleanup.
        // This may cause troubles with not executed destructors.
-       if (lyx_gui::use_gui) {
+       if (lyx::use_gui) {
                theApp->exit(status);
                // Restore original font resources after Application is destroyed.
                lyx::support::restoreFontResources();
@@ -168,9 +181,29 @@ LyX const & LyX::cref()
 }
 
 
+BufferList & theBufferList()
+{
+       return LyX::ref().bufferList();
+}
+
+
 LyX::LyX()
        : first_start(false), geometryOption_(false)
-{}
+{
+       buffer_list_.reset(new BufferList);
+}
+
+
+BufferList & LyX::bufferList()
+{
+       return *buffer_list_.get();
+}
+
+
+BufferList const & LyX::bufferList() const
+{
+       return *buffer_list_.get();
+}
 
 
 lyx::Session & LyX::session()
@@ -214,19 +247,29 @@ int LyX::priv_exec(int & argc, char * argv[])
 {
        // Here we need to parse the command line. At least
        // we need to parse for "-dbg" and "-help"
-       lyx_gui::use_gui = easyParse(argc, argv);
+       easyParse(argc, argv);
 
        lyx::support::init_package(argv[0], cl_system_support, cl_user_support,
                                   lyx::support::top_build_dir_is_one_level_up);
 
        // Start the real execution loop.
-       if (lyx_gui::use_gui) {
+       if (lyx::use_gui) {
                // Force adding of font path _before_ Application is initialized
                lyx::support::addFontResources();
-               return lyx_gui::exec(argc, argv);
+               application_.reset(lyx::createApplication(argc, argv));
+               theApp = application_.get();
+       }
+       else {
+               // FIXME: create a ConsoleApplication
+               theApp = 0;
        }
-       else
-               return exec2(argc, argv);
+
+       int exit_status = exec2(argc, argv);
+
+       if (lyx::use_gui)
+               application_.reset();
+
+       return exit_status;
 }
 
 
@@ -278,7 +321,7 @@ int LyX::exec2(int & argc, char * argv[])
                                if (b)
                                        last_loaded = b;
                        } else {
-                               Buffer * buf = theApp->bufferList().newBuffer(s, false);
+                               Buffer * buf = theBufferList().newBuffer(s, false);
                                if (loadLyXFile(buf, s)) {
                                        last_loaded = buf;
                                        ErrorList const & el = buf->errorList("Parse");
@@ -287,7 +330,7 @@ int LyX::exec2(int & argc, char * argv[])
                                                        boost::bind(&LyX::printError, this, _1));
                                }
                                else
-                                       theApp->bufferList().release(buf);
+                                       theBufferList().release(buf);
                        }
                }
 
@@ -302,7 +345,7 @@ int LyX::exec2(int & argc, char * argv[])
                files.clear(); // the files are already loaded
        }
 
-       if (lyx_gui::use_gui) {
+       if (lyx::use_gui) {
                // determine windows size and position, from lyxrc and/or session
                // initial geometry
                unsigned int width = 690;
@@ -341,7 +384,7 @@ int LyX::exec2(int & argc, char * argv[])
                        height = 0;
                }
                // create the main window
-               LyXView * view = &theApp->createView(width, height, posx, posy, maximize);
+               LyXView * view = &application_->createView(width, height, posx, posy, maximize);
                ref().addLyXView(view);
 
                // load files
@@ -360,7 +403,7 @@ int LyX::exec2(int & argc, char * argv[])
                // clear this list to save a few bytes of RAM
                session_->clearLastOpenedFiles();
 
-               return theApp->start(batch_command);
+               return application_->start(batch_command);
        } else {
                // Something went wrong above
                quitLyX(false);
@@ -497,15 +540,15 @@ bool LyX::init()
 
        if (lyxrc.roman_font_name.empty())
                lyxrc.roman_font_name = 
-                       lyx_gui::use_gui? theApp->romanFontName(): "serif";
+                       lyx::use_gui? application_->romanFontName(): "serif";
 
        if (lyxrc.sans_font_name.empty())
                lyxrc.sans_font_name =
-                       lyx_gui::use_gui? theApp->sansFontName(): "sans";
+                       lyx::use_gui? application_->sansFontName(): "sans";
 
        if (lyxrc.typewriter_font_name.empty())
                lyxrc.typewriter_font_name =
-                       lyx_gui::use_gui? theApp->typewriterFontName(): "monospace";
+                       lyx::use_gui? application_->typewriterFontName(): "monospace";
 
        //
        // Read configuration files
@@ -530,7 +573,7 @@ bool LyX::init()
                reconfigureUserLyXDir();
 
        // no need for a splash when there is no GUI
-       if (!lyx_gui::use_gui) {
+       if (!lyx::use_gui) {
                first_start = false;
        }
 
@@ -561,7 +604,7 @@ bool LyX::init()
        if (!LyXSetStyle())
                return false;
 
-       if (lyx_gui::use_gui) {
+       if (lyx::use_gui) {
                // Set up bindings
                toplevel_keymap.reset(new kb_keymap);
                defaultKeyBindings(toplevel_keymap.get());
@@ -667,8 +710,8 @@ void LyX::emergencyCleanup() const
        // contain documents etc. which might be helpful on
        // a crash
 
-       theApp->bufferList().emergencyWriteAll();
-       theApp->server().emergencyCleanup();
+       theBufferList().emergencyWriteAll();
+       application_->server().emergencyCleanup();
 }
 
 
@@ -899,7 +942,6 @@ bool LyX::readEncodingsFile(string const & name)
 
 namespace {
 
-bool is_gui = true;
 string batch;
 
 /// return the the number of arguments consumed
@@ -994,7 +1036,7 @@ int parse_export(string const & type, string const &)
                exit(1);
        }
        batch = "buffer-export " + type;
-       is_gui = false;
+       lyx::use_gui = true;
        return 1;
 }
 
@@ -1017,7 +1059,7 @@ int parse_import(string const & type, string const & file)
 } // namespace anon
 
 
-bool LyX::easyParse(int & argc, char * argv[])
+void LyX::easyParse(int & argc, char * argv[])
 {
        std::map<string, cmd_helper> cmdmap;
 
@@ -1061,6 +1103,4 @@ bool LyX::easyParse(int & argc, char * argv[])
        }
 
        batch_command = batch;
-
-       return is_gui;
 }