1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 2011-2021 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING.  If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if defined (HAVE_CONFIG_H)
27 #  include "config.h"
28 #endif
29 
30 #include <QDesktopWidget>
31 
32 #include "gui-preferences-cs.h"
33 #include "gui-preferences-global.h"
34 #include "octave-qobject.h"
35 #include "terminal-dock-widget.h"
36 
37 #include "quit.h"
38 #include "signal-wrappers.h"
39 
40 #include "sighandlers.h"
41 
42 namespace octave
43 {
terminal_dock_widget(QWidget * p,base_qobject & oct_qobj)44   terminal_dock_widget::terminal_dock_widget (QWidget *p,
45                                               base_qobject& oct_qobj)
46     : octave_dock_widget ("TerminalDockWidget", p, oct_qobj),
47       m_terminal (QTerminal::create (oct_qobj, p))
48   {
49     m_terminal->setObjectName ("OctaveTerminal");
50     m_terminal->setFocusPolicy (Qt::StrongFocus);
51 
52     setWindowIcon (QIcon (":/actions/icons/logo.png"));
53     set_title (tr ("Command Window"));
54 
55     setWidget (m_terminal);
56     setFocusProxy (m_terminal);
57 
58     connect (m_terminal, SIGNAL (interrupt_signal (void)),
59              this, SLOT (terminal_interrupt (void)));
60 
61     // Connect the visibility signal to the terminal for dis-/enabling timers
62     connect (this, SIGNAL (visibilityChanged (bool)),
63              m_terminal, SLOT (handle_visibility_changed (bool)));
64 
65     // Chose a reasonable size at startup in order to avoid truncated
66     // startup messages
67     resource_manager& rmgr = m_octave_qobj.get_resource_manager ();
68     gui_settings *settings = rmgr.get_settings ();
69 
70     QFont font = QFont ();
71     font.setStyleHint (QFont::TypeWriter);
72     QString default_font = settings->value (global_mono_font).toString ();
73     font.setFamily
74       (settings->value (cs_font.key, default_font).toString ());
75     font.setPointSize
76       (settings->value (cs_font_size).toInt ());
77 
78     QFontMetrics metrics(font);
79 
80     int win_x =  metrics.maxWidth()*80;
81     int win_y =  metrics.height()*25;
82 
83     int max_x = QApplication::desktop ()->screenGeometry (this).width ();
84     int max_y = QApplication::desktop ()->screenGeometry (this).height ();
85 
86     if (win_x > max_x)
87       win_x = max_x;
88     if (win_y > max_y)
89       win_y = max_y;
90 
91     setGeometry (0, 0, win_x, win_y);
92   }
93 
~terminal_dock_widget(void)94   terminal_dock_widget::~terminal_dock_widget (void)
95   {
96     delete m_terminal;
97   }
98 
has_focus(void) const99   bool terminal_dock_widget::has_focus (void) const
100   {
101     QWidget *w = widget ();
102 
103     return w->hasFocus ();
104   }
105 
terminal_interrupt(void)106   void terminal_dock_widget::terminal_interrupt (void)
107   {
108     // FIXME: Protect with mutex?
109 
110     octave_signal_caught = 1;
111     octave_interrupt_state++;
112 
113     // Send SIGINT to all other processes in our process group.
114     // This is needed to interrupt calls to system (), for example.
115 
116     int sigint;
117     octave_get_sig_number ("SIGINT", &sigint);
118 
119     octave_kill_wrapper (0, sigint);
120   }
121 }
122