diff libgui/src/octave-gui.cc @ 22089:c2c668b3051b

use classes for octave application and interpreter * main-cli.cc (main): Simplify using class objects. * main-gui.cc (main): Likeiwse. * octave.h, octave.cc (octave::cmdline_options, octave::application, octave::cli_application, octave::embedded_application, octave::interpreter): New classes. Replace ordinary functions and static/global data with objects. Access global application data through application object. * octave-gui.h, octave-gui.cc (octave::gui_application): New class. Replace ordinary functions and static/global data with objects. * main-window.cc (main_window::m_app_context): New data member. (main_window::main_window): Pass application object instead of start_gui flag. (main_window::construct_octave_qt_link): Pass m_app_context to octave_qt_link constructor. * octave-interpreter.h, octave-interpreter.cc (octave_interpreter::m_app_context, octave_interpreter::m_exit_status): New data members. (octave_interpreter::execute): Use class object to start interpreter. Save exit status. * octave-qt-link.h, octave-qt-link.cc (octave_qt_link::m_app_context): New data member. (octave_qt_link::do_set_default_prompts): Make prompt settings conditional on whether the GUI is running. * input.h, input.cc (interactive, forced_interactive): Delete global data. * dirfns.cc, error.cc, input.cc, oct-stream.cc, pager.cc, sighandlers.cc, sysdep.cc, toplev.cc, lex.ll: Access interactive and forced_interactive through global application object.
author John W. Eaton <jwe@octave.org>
date Sun, 10 Jul 2016 07:15:21 -0400
parents c3823cb0ea02
children bbaeaed0c584
line wrap: on
line diff
--- a/libgui/src/octave-gui.cc	Mon Jul 11 13:45:12 2016 -0400
+++ b/libgui/src/octave-gui.cc	Sun Jul 10 07:15:21 2016 -0400
@@ -29,6 +29,8 @@
 #include <QThread>
 #include <QTranslator>
 
+#include <cstdio>
+
 #include <iostream>
 
 #if defined (HAVE_SYS_IOCTL_H)
@@ -40,10 +42,12 @@
 #include "oct-syscalls.h"
 
 #include "builtin-defun-decls.h"
+#include "display.h"
 #if defined (HAVE_QT_GRAPHICS)
 #  include "__init_qt__.h"
 #endif
 #include "octave.h"
+#include "unistd-wrappers.h"
 
 #include "main-window.h"
 #include "octave-gui.h"
@@ -59,101 +63,164 @@
 {
 }
 
-// If START_GUI is false, we still set up the QApplication so that we
-// can use Qt widgets for plot windows.
-
-int
-octave_start_gui (int argc, char *argv[], bool start_gui)
+namespace octave
 {
-  octave_thread_manager::block_interrupt_signal ();
+  bool gui_application::start_gui_p (void) const
+  {
+    if (m_options.no_window_system ())
+      return false;
+
+    std::string err_msg;
+    if (! display_info::display_available (err_msg))
+      {
+        if (! (m_options.inhibit_startup_message () || err_msg.empty ()))
+          warning (err_msg.c_str ());
+
+        return false;
+      }
 
-  std::string show_gui_msgs = octave::sys::env::getenv ("OCTAVE_SHOW_GUI_MESSAGES");
+    if (! m_options.line_editing ())
+      {
+        if (! (m_options.inhibit_startup_message ()
+               || m_options.no_gui ()))
+          warning ("--no-line-editing option given, disabling GUI");
+
+        return false;
+      }
+
+    if (m_options.force_gui ())
+      return true;
+
+    if (m_options.no_gui ())
+      return false;
 
-  // Installing our handler suppresses the messages.
-  if (show_gui_msgs.empty ())
-    qInstallMsgHandler (message_handler);
+    if (m_options.persist ())
+      return true;
+
+    // If stdin is not a tty, then assume we are reading commands from a
+    // pipe or a redirected file and the GUI should not start.  If this
+    // is not the case (for example, starting from a desktop "launcher"
+    // with no terminal) and you want to start the GUI, you may use the
+    // --force-gui option to start the GUI.
+
+    if (! octave_isatty_wrapper (fileno (stdin)))
+      return false;
+
+    // If we have code to eval or execute from a file, and we are going
+    // to exit immediately after executing it, don't start the gui.
+
+    std::string code_to_eval = m_options.code_to_eval ();
+    if (! code_to_eval.empty () || m_have_script_file)
+      return false;
+
+    return true;
+  }
+
+  int gui_application::execute (void)
+  {
+    octave_thread_manager::block_interrupt_signal ();
+
+    std::string show_gui_msgs = octave::sys::env::getenv ("OCTAVE_SHOW_GUI_MESSAGES");
+
+    // Installing our handler suppresses the messages.
+
+    if (show_gui_msgs.empty ())
+      qInstallMsgHandler (message_handler);
 
 #if defined (HAVE_QT_GRAPHICS)
-  install___init_qt___functions ();
+    install___init_qt___functions ();
 
-  Fregister_graphics_toolkit (ovl ("qt"));
+    Fregister_graphics_toolkit (ovl ("qt"));
 #endif
 
-  QApplication application (argc, argv);
-  QTranslator gui_tr, qt_tr, qsci_tr;
+    // If START_GUI is false, we still set up the QApplication so that
+    // we can use Qt widgets for plot windows.
 
-  // Set the codec for all strings (before wizard)
+    QApplication qt_app (m_argc, m_argv);
+    QTranslator gui_tr, qt_tr, qsci_tr;
+
+    // Set the codec for all strings (before wizard or any GUI object)
 #if ! defined (Q_OS_WIN32)
-  QTextCodec::setCodecForCStrings (QTextCodec::codecForName ("UTF-8"));
+    QTextCodec::setCodecForCStrings (QTextCodec::codecForName ("UTF-8"));
 #endif
 
-  // show wizard if this is the first run
-  if (resource_manager::is_first_run () && start_gui)
-    {
-      // before wizard
-      resource_manager::config_translators (&qt_tr, &qsci_tr, &gui_tr);
-      application.installTranslator (&qt_tr);
-      application.installTranslator (&gui_tr);
-      application.installTranslator (&qsci_tr);
+    bool start_gui = start_gui_p ();
+
+    // Show welcome wizard if this is the first run.
+
+    if (resource_manager::is_first_run () && start_gui)
+      {
+        // Before wizard.
+        resource_manager::config_translators (&qt_tr, &qsci_tr, &gui_tr);
 
-      welcome_wizard welcomeWizard;
-
-      if (welcomeWizard.exec () == QDialog::Rejected)
-        exit (1);
+        qt_app.installTranslator (&qt_tr);
+        qt_app.installTranslator (&gui_tr);
+        qt_app.installTranslator (&qsci_tr);
 
-      resource_manager::reload_settings ();  // install settings file
-    }
-  else
-    {
-      resource_manager::reload_settings ();  // get settings file
+        welcome_wizard welcomeWizard;
+
+        if (welcomeWizard.exec () == QDialog::Rejected)
+          exit (1);
 
-      // after settings
-      resource_manager::config_translators (&qt_tr, &qsci_tr, &gui_tr);
-      application.installTranslator (&qt_tr);
-      application.installTranslator (&gui_tr);
-      if (start_gui)
-        application.installTranslator (&qsci_tr);
-    }
+        // Install settings file.
+        resource_manager::reload_settings ();
+      }
+    else
+      {
+        // Get settings file.
+        resource_manager::reload_settings ();
+
+        // After settings.
+        resource_manager::config_translators (&qt_tr, &qsci_tr, &gui_tr);
+
+        qt_app.installTranslator (&qt_tr);
+        qt_app.installTranslator (&gui_tr);
 
-  if (start_gui)
-    {
-      // update network-settings
-      resource_manager::update_network_settings ();
+        if (start_gui)
+          qt_app.installTranslator (&qsci_tr);
+      }
+
+    if (start_gui)
+      {
+        resource_manager::update_network_settings ();
 
-      // We provide specific terminal capabilities, so ensure that TERM is
-      // always set appropriately
+        // We provide specific terminal capabilities, so ensure that
+        // TERM is always set appropriately.
+
 #if defined (OCTAVE_USE_WINDOWS_API)
-      octave::sys::env::putenv ("TERM", "cygwin");
+        octave::sys::env::putenv ("TERM", "cygwin");
 #else
-      octave::sys::env::putenv ("TERM", "xterm");
+        octave::sys::env::putenv ("TERM", "xterm");
 #endif
 
-      // shortcut manager
-      shortcut_manager::init_data ();
-    }
+        shortcut_manager::init_data ();
+      }
 
-  // Force left-to-right alignment (see bug #46204)
-  application.setLayoutDirection (Qt::LeftToRight);
+    // Force left-to-right alignment (see bug #46204)
+    qt_app.setLayoutDirection (Qt::LeftToRight);
+
+    // Create and show main window.
 
-  // Create and show main window.
-
-  main_window w (0, start_gui);
+    main_window w (0, this);
 
-  if (start_gui)
-    {
-      w.read_settings ();
+    if (start_gui)
+      {
+        w.read_settings ();
+
+        w.init_terminal_size ();
 
-      w.init_terminal_size ();
+        // Connect signals for changes in visibility not before w
+        // is shown.
 
-      // Connect signals for changes in visibility not before w
-      // is shown.
+        w.connect_visibility_changed ();
 
-      w.connect_visibility_changed ();
+        w.focus_command_window ();
 
-      w.focus_command_window ();
-    }
-  else
-    application.setQuitOnLastWindowClosed (false);
+        gui_running (true);
+      }
+    else
+      qt_app.setQuitOnLastWindowClosed (false);
 
-  return application.exec ();
+    return qt_app.exec ();
+  }
 }