GNU Octave  8.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
QWinTerminalImpl.cpp
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2011-2019 Michael Goffioul
4 
5 This file is part of QConsole.
6 
7 This program is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with this program. If not,
19 see <https://www.gnu.org/licenses/>.
20 
21 */
22 
23 #include <algorithm>
24 #include <csignal>
25 #include <cstdio>
26 #include <cstdarg>
27 #include <cstring>
28 
29 #define WIN32_LEAN_AND_MEAN
30 #if ! defined (_WIN32_WINNT) && ! defined (NTDDI_VERSION)
31 #define _WIN32_WINNT 0x0500
32 #endif
33 #include <windows.h>
34 #include <fcntl.h>
35 #include <io.h>
36 #include <versionhelpers.h>
37 
38 #include <QApplication>
39 #include <QClipboard>
40 #include <QColor>
41 #include <QFont>
42 #include <QGridLayout>
43 #include <QPaintEvent>
44 #include <QPainter>
45 #include <QResizeEvent>
46 #include <QScrollBar>
47 #include <QtDebug>
48 #include <QThread>
49 #include <QTimer>
50 #include <QToolTip>
51 #include <QCursor>
52 #include <QMessageBox>
53 #include <QDragEnterEvent>
54 #include <QDropEvent>
55 #include <QUrl>
56 #include <QMimeData>
57 
58 #include "QWinTerminalImpl.h"
59 #include "QTerminalColors.h"
60 
61 // Uncomment to log activity to LOGFILENAME
62 // #define DEBUG_QCONSOLE
63 #define LOGFILENAME "QConsole.log"
64 // Uncomment to create hidden console window
65 #define HIDDEN_CONSOLE
66 
67 #ifdef _MSC_VER
68 # pragma warning(disable : 4996)
69 #endif
70 
71 //////////////////////////////////////////////////////////////////////////////
72 
73 class QConsoleView : public QWidget
74 {
75 public:
76  QConsoleView (QWinTerminalImpl* parent = 0) : QWidget (parent), q (parent) { }
77  ~QConsoleView (void) { }
78 
79 protected:
80  void paintEvent (QPaintEvent* event) { q->viewPaintEvent (this, event); }
81  void resizeEvent (QResizeEvent* event) { q->viewResizeEvent (this, event); }
82 
83 private:
85 };
86 
87 //////////////////////////////////////////////////////////////////////////////
88 
89 class QConsoleThread : public QThread
90 {
91 public:
93 
94 protected:
95  void run (void)
96  { q->start (); }
97 
98 private:
100 };
101 
102 //////////////////////////////////////////////////////////////////////////////
103 
104 static QString translateKey (QKeyEvent *ev)
105 {
106  QString esc = "\x1b";
107  QString s;
108 
109  if (ev->key () == Qt::Key_Delete)
110  s = esc + "[C\b";
111  else if (!ev->text ().isEmpty ())
112  s = ev->text ();
113  else
114  {
115 
116  switch (ev->key ())
117  {
118  case Qt::Key_Up:
119  s = esc + "[A";
120  break;
121 
122  case Qt::Key_Down:
123  s = esc + "[B";
124  break;
125 
126  case Qt::Key_Right:
127  s = esc + "[C";
128  break;
129 
130  case Qt::Key_Left:
131  s = esc + "[D";
132  break;
133 
134  case Qt::Key_Home:
135  s = esc + "[H";
136  break;
137 
138  case Qt::Key_End:
139  s = esc + "[F";
140  break;
141 
142  case Qt::Key_Insert:
143  s = esc + "[2~";
144  break;
145 
146  case Qt::Key_PageUp:
147  s = esc + "[5~";
148  break;
149 
150  case Qt::Key_PageDown:
151  s = esc + "[6~";
152  break;
153 
154  case Qt::Key_Escape:
155  s = esc;
156  break;
157 
158  default:
159  break;
160  }
161  }
162 
163  return s;
164 }
165 
167 {
168  friend class QWinTerminalImpl;
169 
170 public:
171 
173  {
177  };
178 
179  QConsolePrivate (QWinTerminalImpl* parent, const QString& cmd = QString ());
180  ~QConsolePrivate (void);
181 
182  void updateConsoleSize (bool sync = false, bool allow_smaller_width = false);
183  void syncConsoleParameters (void);
184  void grabConsoleBuffer (CHAR_INFO* buf = 0);
185  void updateHorizontalScrollBar (void);
186  void updateVerticalScrollBar (void);
187  void setHorizontalScrollValue (int value);
188  void setVerticalScrollValue (int value);
189  void updateConsoleView (bool grab = true);
190  void monitorConsole (void);
191  void startCommand (void);
192  void sendConsoleText (const QString& s);
193  QRect cursorRect (void);
194  void selectAll();
195  void selectWord(const QPoint& cellPos);
196  void selectLine(const QPoint& cellPos);
197 
198  void log (const char* fmt, ...);
199 
200  void closeStandardIO (int fd, DWORD stdHandleId, const char* name);
201  void setupStandardIO (DWORD stdHandleId, int fd, const char* name,
202  const char* devName);
203 
204  QPoint posToCell (const QPoint& pt);
205  QString getSelection (void);
206  void updateSelection (void);
207  void clearSelection (void);
208 
209  QColor backgroundColor (void) const;
210  QColor foregroundColor (void) const;
211  QColor selectionColor (void) const;
212  QColor cursorColor (void) const;
213 
214  void setBackgroundColor (const QColor& color);
215  void setForegroundColor (const QColor& color);
216  void setSelectionColor (const QColor& color);
217  void setCursorColor (bool useForegroundColor, const QColor& color);
218  void setScrollBufferSize (int value);
219 
220  void drawTextBackground (QPainter& p, int cx1, int cy1, int cx2, int cy2,
221  int cw, int ch);
222 
223  void drawSelection (QPainter& p, int cx1, int cy1, int cx2, int cy2,
224  int cw, int ch);
225 
226  void drawCursor (QPainter& p);
227 
228  void drawText (QPainter& p, int cx1, int cy1, int cx2, int cy2,
229  int cw, int ch);
230 
231 private:
233 
234 private:
235  QFont m_font;
236  QString m_command;
239  QString m_title;
240 
241  QSize m_charSize;
245  QPoint m_cursorPos;
250 
254 
257 
258  HANDLE m_stdOut;
260  CHAR_INFO* m_buffer;
261  CHAR_INFO* m_tmpBuffer;
262  HANDLE m_process;
263 
266  QScrollBar* m_verticalScrollBar;
269 
270  // The delay in milliseconds between redrawing blinking text.
271  static const int BLINK_DELAY = 500;
272 };
273 
274 static void maybeSwapPoints (QPoint& begin, QPoint& end)
275 {
276  if (end.y () < begin.y ()
277  || (end.y () == begin.y () && end.x () < begin.x ()))
278  std::swap (begin, end);
279 }
280 
281 //////////////////////////////////////////////////////////////////////////////
282 
284  : q (parent), m_command (cmd), m_auto_scroll (true), m_cursorBlinking (false),
285  m_hasBlinkingCursor (true), m_cursorType (BlockCursor),
286  m_beginSelection (0, 0), m_endSelection (0, 0), m_settingSelection (false),
287  m_process (nullptr), m_inWheelEvent (false)
288 {
289  log (nullptr);
290 
291  // Possibly detach from any existing console
292  log ("Detaching from existing console (if any)...\n");
293  FreeConsole ();
294  log ("Closing standard IO...\n");
295  closeStandardIO (0, STD_INPUT_HANDLE, "STDIN");
296  closeStandardIO (1, STD_OUTPUT_HANDLE, "STDOUT");
297  closeStandardIO (2, STD_ERROR_HANDLE, "STDERR");
298 
299 #ifdef HIDDEN_CONSOLE
300  HWINSTA hOrigSta, hNewSta;
301 
302  // Create new (hidden) console
303  hOrigSta = GetProcessWindowStation ();
304  hNewSta = CreateWindowStation (nullptr, 0, GENERIC_ALL, nullptr);
305  log ("Current Windows station: %p.\nNew Windows station: %p.\n", hOrigSta,
306  hNewSta);
307  if (! SetProcessWindowStation (hNewSta))
308  log ("Failed to switch to new Windows station.\n");
309 #endif
310  if (! AllocConsole ())
311  log ("Failed to create new console.\n");
312 #ifdef HIDDEN_CONSOLE
313  if (! SetProcessWindowStation (hOrigSta))
314  log ("Failed to restore original Windows station.\n");
315  if (! CloseWindowStation (hNewSta))
316  log ("Failed to close new Windows station.\n");
317 #endif
318 
319  log ("New (hidden) console created.\n");
320 
321  setupStandardIO (STD_INPUT_HANDLE, 0, "STDIN", "CONIN$");
322  setupStandardIO (STD_OUTPUT_HANDLE, 1, "STDOUT", "CONOUT$");
323  setupStandardIO (STD_ERROR_HANDLE, 2, "STDERR", "CONOUT$");
324 
325  log ("Standard input/output/error set up.\n");
326 
327  *stdin = *(fdopen (0, "rb"));
328  *stdout = *(fdopen (1, "wb"));
329  *stderr = *(fdopen (2, "wb"));
330 
331  log ("POSIX standard streams created.\n");
332 
333  setvbuf (stdin, nullptr, _IONBF, 0);
334  setvbuf (stdout, nullptr, _IONBF, 0);
335  setvbuf (stderr, nullptr, _IONBF, 0);
336 
337  log ("POSIX standard stream buffers adjusted.\n");
338 
339  HANDLE hStdOut = GetStdHandle (STD_OUTPUT_HANDLE);
340 
341  log ("Console allocated: hStdOut: %p\n", hStdOut);
342 
343  m_stdOut = hStdOut;
344  m_consoleWindow = GetConsoleWindow ();
345 
346  // In case the console window hasn't been created hidden...
347 #ifdef HIDDEN_CONSOLE
348  ShowWindow (m_consoleWindow, SW_HIDE);
349 #endif
350 
351  CONSOLE_SCREEN_BUFFER_INFO sbi;
352 
353  GetConsoleScreenBufferInfo (hStdOut, &sbi);
354  m_bufferSize = QSize (sbi.dwSize.X, qMax (sbi.dwSize.Y, (SHORT)500));
355  m_consoleRect = QRect (sbi.srWindow.Left, sbi.srWindow.Top,
356  sbi.srWindow.Right - sbi.srWindow.Left + 1,
357  sbi.srWindow.Bottom - sbi.srWindow.Top + 1);
358  m_cursorPos = QPoint (sbi.dwCursorPosition.X, sbi.dwCursorPosition.Y);
359 
360  log ("Initial console parameters:\n");
361  log (" buffer size: %d x %d\n", m_bufferSize.width (),
362  m_bufferSize.height ());
363  log (" window: (%d, %d) -> (%d, %d) [%d x %d]\n",
364  m_consoleRect.left (), m_consoleRect.top (),
365  m_consoleRect.right (), m_consoleRect.bottom (),
366  m_consoleRect.width (), m_consoleRect.height ());
367 
368  wchar_t titleBuf[260];
369  GetConsoleTitleW (titleBuf, sizeof (titleBuf));
370  q->setWindowTitle (QString::fromWCharArray (titleBuf));
371 
372  m_font.setFamily ("Lucida Console");
373  m_font.setPointSize (9);
374  m_font.setStyleHint (QFont::TypeWriter);
375 
376  m_buffer = nullptr;
377  m_tmpBuffer = nullptr;
378 
379  m_consoleView = new QConsoleView (parent);
380  m_horizontalScrollBar = new QScrollBar (Qt::Horizontal, parent);
381  m_verticalScrollBar = new QScrollBar (Qt::Vertical, parent);
382 
383  QGridLayout* l = new QGridLayout (parent);
384  l->setContentsMargins (0, 0, 0, 0);
385  l->setSpacing (0);
386  l->addWidget (m_consoleView, 0, 0);
387  l->addWidget (m_horizontalScrollBar, 1, 0);
388  l->addWidget (m_verticalScrollBar, 0, 1);
389 
390  if (IsWindows7OrGreater ())
391  {
392  SetConsoleCP (65001);
393  SetConsoleOutputCP (65001);
394  }
395 
396  // Choose 0 (0x0) as index into the Windows console color map for the
397  // background and 7 (0x7) as the index for the foreground. This
398  // selection corresponds to the indices used in the foregroundColor,
399  // setForegroundColor, backgroundColor, and SetBackgroundColor
400  // functions.
401 
402  SetConsoleTextAttribute (m_stdOut, 0x07);
403 
404  // Defaults.
405  setBackgroundColor (Qt::white);
406  setForegroundColor (Qt::black);
407  setSelectionColor (Qt::lightGray);
408  setCursorColor (false, Qt::darkGray);
409 
410  // FIXME -- should we set the palette?
411  QPalette palette (backgroundColor ());
412  m_consoleView->setPalette (palette);
413 
414  m_consoleView->setFont (m_font);
415  parent->setFocusPolicy (Qt::StrongFocus);
416  parent->winId ();
417 
420 
421  m_consoleWatcher = new QTimer (parent);
422  m_consoleWatcher->setInterval (10);
423  m_consoleWatcher->setSingleShot (false);
424 
425  m_blinkCursorTimer = new QTimer (parent);
426  QObject::connect (m_blinkCursorTimer, SIGNAL (timeout()),
427  q, SLOT (blinkCursorEvent ()));
428 
429  QObject::connect (m_horizontalScrollBar, SIGNAL (valueChanged (int)),
430  q, SLOT (horizontalScrollValueChanged (int)));
431 
432  QObject::connect (m_verticalScrollBar, SIGNAL (valueChanged (int)),
433  q, SLOT (verticalScrollValueChanged (int)));
434 
435  QObject::connect (m_consoleWatcher, SIGNAL (timeout (void)),
436  q, SLOT (monitorConsole (void)));
437 
438  m_consoleWatcher->start ();
439 
440  if (m_command.isEmpty ())
441  m_consoleThread = 0;
442  else
443  {
445  QObject::connect (m_consoleThread, SIGNAL (finished (void)),
446  q, SIGNAL (terminated (void)));
447  m_consoleThread->start ();
448  }
449 }
450 
451 //////////////////////////////////////////////////////////////////////////////
452 
454 {
455  if (m_consoleThread && m_consoleThread->isRunning () && m_process)
456  {
457  TerminateProcess (m_process, (UINT)-1);
458  m_consoleThread->wait ();
459  }
460 
461  delete [] m_buffer;
462  delete [] m_tmpBuffer;
463 }
464 
465 //////////////////////////////////////////////////////////////////////////////
466 
467 void QConsolePrivate::setupStandardIO (DWORD stdHandleId, int targetFd,
468  const char* name, const char* devName)
469 {
470  log ("Opening %s...\n", devName);
471 
472  int fd = open (devName, _O_RDWR | _O_BINARY);
473 
474  if (fd != -1)
475  {
476  if (fd != targetFd)
477  {
478  log ("Opened %s is not at target file descriptor %d, "
479  "duplicating...\n", name, targetFd);
480  if (dup2 (fd, targetFd) == -1)
481  log ("Failed to duplicate file descriptor: errno=%d.\n", errno);
482  if (close (fd) == -1)
483  log ("Failed to close original file descriptor: errno=%d.\n",
484  errno);
485  }
486  else
487  log ("%s opened and assigned to file descriptor %d.\n", devName, fd);
488  if (! SetStdHandle (stdHandleId, (HANDLE) _get_osfhandle (targetFd)))
489  log ("Failed to re-assign %s: error=%08x.\n", name, GetLastError ());
490  }
491  else
492  log ("Failed to open %s: errno=%d.\n", devName, errno);
493 }
494 
495 QPoint QConsolePrivate::posToCell (const QPoint& p)
496 {
497  return QPoint (m_consoleRect.left () + p.x () / m_charSize.width (),
498  m_consoleRect.top () + p.y () / m_charSize.height ());
499 }
500 
502 {
503  QString selection;
504 
505  QPoint begin = m_beginSelection;
506  QPoint end = m_endSelection;
507 
508  maybeSwapPoints (begin, end);
509 
510  if (begin != end)
511  {
512  CHAR_INFO* buf;
513  COORD bufSize, bufCoord;
514  SMALL_RECT bufRect;
515  int nr;
516 
517  nr = end.y () - begin.y () + 1;
518  buf = new CHAR_INFO[m_bufferSize.width () * nr];
519  bufSize.X = m_bufferSize.width ();
520  bufSize.Y = nr;
521  bufCoord.X = 0;
522  bufCoord.Y = 0;
523 
524  bufRect.Left = 0;
525  bufRect.Right = m_bufferSize.width ();
526  bufRect.Top = begin.y ();
527  bufRect.Bottom = end.y ();
528 
529  if (ReadConsoleOutput (m_stdOut, buf, bufSize, bufCoord, &bufRect))
530  {
531  int start_pos = begin.x ();
532  int end_pos = (nr - 1) * m_bufferSize.width () + end.x ();
533  int lastNonSpace = -1;
534 
535  for (int i = start_pos; i <= end_pos; i++)
536  {
537  if (i && (i % m_bufferSize.width ()) == 0)
538  {
539  if (lastNonSpace >= 0)
540  selection.truncate (lastNonSpace);
541  selection.append ('\n');
542  lastNonSpace = selection.length ();
543  }
544 
545  QChar c (buf[i].Char.UnicodeChar);
546  if (c.isNull ())
547  c = QChar (' ');
548 
549  selection.append (c);
550  if (! c.isSpace ())
551  lastNonSpace = selection.length ();
552  }
553 
554  if (lastNonSpace >= 0)
555  selection.truncate (lastNonSpace);
556  }
557  }
558 
559  return selection;
560 }
561 
563 {
564  QPoint begin = m_beginSelection;
565  QPoint end = m_endSelection;
566 
567  maybeSwapPoints (begin, end);
568 
569  begin.rx () = 0;
570  end.rx () = m_consoleRect.width ();
571 
572  m_consoleView->update ();
573 }
574 
576 {
577  m_beginSelection = m_endSelection = QPoint ();
578 
579  m_consoleView->update ();
580 }
581 
583 {
584  return m_colors[0];
585 }
586 
588 {
589  return m_colors[7];
590 }
591 
593 {
594  return m_selectionColor;
595 }
596 
597 QColor QConsolePrivate::cursorColor (void) const
598 {
599  return m_cursorColor.isValid () ? m_cursorColor : foregroundColor ();
600 }
601 
602 void QConsolePrivate::setBackgroundColor (const QColor& color)
603 {
604  m_colors[0] = color;
605 
606  QPalette palette (color);
607  palette.setColor(QPalette::Base, color);
608  m_consoleView->setPalette (palette);
609 }
610 
611 void QConsolePrivate::setForegroundColor (const QColor& color)
612 {
613  m_colors[7] = color;
614 }
615 
616 void QConsolePrivate::setSelectionColor (const QColor& color)
617 {
618  m_selectionColor = color;
619 }
620 
621 void QConsolePrivate::setCursorColor (bool useForegroundColor,
622  const QColor& color)
623 {
624  m_cursorColor = useForegroundColor ? QColor () : color;
625 }
626 
628 {
629  CONSOLE_SCREEN_BUFFER_INFO sbi;
630  GetConsoleScreenBufferInfo (m_stdOut, &sbi);
631 
632  m_bufferSize = QSize (sbi.dwSize.X, (SHORT)value);
633 
634  updateConsoleSize (true);
635 }
636 
638  int cx2, int cy2, int cw, int ch)
639 {
640  p.save ();
641 
642  int ascent = p.fontMetrics ().ascent ();
643  int stride = m_consoleRect.width ();
644  int y = ascent + cy1 * ch;;
645 
646  for (int j = cy1; j <= cy2; j++, y += ch)
647  {
648  int len = 0;
649  bool hasChar = false;
650  int x = cx1 * cw;
651  WORD attr = 0;
652 
653  for (int i = cx1; i <= cx2; i++)
654  {
655  CHAR_INFO* ci = &(m_buffer[stride*j+i]);
656 
657  if ((ci->Attributes & 0x00ff) != attr)
658  {
659  // Character attributes changed
660  if (len != 0)
661  {
662  // String buffer not empty -> draw it
663  if (hasChar || (attr & 0x00f0))
664  {
665  if (attr & 0x00f0)
666  p.fillRect (x, y-ascent, len * cw, ch, p.brush ());
667  }
668 
669  x += (len * cw);
670  len = 0;
671  hasChar = false;
672  }
673  // Update current brush and store current attributes
674  attr = (ci->Attributes & 0x00ff);
675  p.setBrush (m_colors[(attr >> 4) & 0x000f]);
676  }
677 
678  // Append current character to the string buffer
679  len++;
680  if (ci->Char.UnicodeChar != L' ')
681  hasChar = true;
682  }
683 
684  if (len != 0 && (hasChar || (attr & 0x00f0)))
685  {
686  // Line end reached, but string buffer not empty -> draw it
687  // No need to update s or x, they will be reset on the next
688  // for-loop iteration
689 
690  if (attr & 0x00f0)
691  p.fillRect (x, y-ascent, len * cw, ch, p.brush ());
692  }
693  }
694 
695  p.restore ();
696 }
697 
699 {
700  m_beginSelection = QPoint (0,0);
701  m_endSelection = QPoint(m_bufferSize.width (),
702  m_cursorPos.y());
703  updateSelection();
704 }
705 
706 void QConsolePrivate::selectWord (const QPoint & cellpos)
707 {
708  QPoint begin = cellpos;
709  QPoint end = cellpos;
710 
711  int stride = m_consoleRect.width ();
712 
713  int verticalScrollOffset = m_consoleRect.top ();
714  int horizontalScrollOffset = m_consoleRect.left ();
715 
716  // get begin, end in buffer offsets
717  begin.ry () -= verticalScrollOffset;
718  end.ry () -= verticalScrollOffset;
719 
720  begin.rx () -= horizontalScrollOffset;
721  end.rx () -= horizontalScrollOffset;
722 
723  // loog at current clicked on char to determinate ig getting space chunk or nonspace chunk
724  if (QChar(m_buffer[begin.y ()*stride + begin.x ()].Char.UnicodeChar).isSpace () == false)
725  {
726  // from current char, go back and fwd to find start and end of block
727  while(begin.x () > 0 &&
728  QChar(m_buffer[begin.y ()*stride + begin.x () -1].Char.UnicodeChar).isSpace() == false)
729  {
730  begin.rx () --;
731  }
732 
733  while(end.x () < m_consoleRect.width () &&
734  QChar(m_buffer[end.y ()*stride + end.x () +1].Char.UnicodeChar).isSpace() == false)
735  {
736  end.rx () ++;
737  }
738  }
739  else
740  {
741  while(begin.x () > 0 &&
742  QChar(m_buffer[begin.y ()*stride + begin.x () -1].Char.UnicodeChar).isSpace())
743  {
744  begin.rx () --;
745  }
746 
747  while(end.x () < m_consoleRect.width () &&
748  QChar(m_buffer[end.y ()*stride + end.x () +1].Char.UnicodeChar).isSpace ())
749  {
750  end.rx () ++;
751  }
752  }
753 
754  // convert console offsets to absolute cell positions
755  begin.ry () += verticalScrollOffset;
756  end.ry () += verticalScrollOffset;
757 
758  begin.rx () += horizontalScrollOffset;
759  end.rx () += horizontalScrollOffset;
760 
761  m_beginSelection = begin;
762  m_endSelection = end;
763 
764  updateSelection ();
765 }
766 
767 void QConsolePrivate::selectLine (const QPoint & cellpos)
768 {
769  m_beginSelection = QPoint (0, cellpos.y ());
770  m_endSelection = QPoint (m_bufferSize.width ()-1, cellpos.y ());
771  updateSelection ();
772 }
773 
774 
775 void QConsolePrivate::drawSelection (QPainter& p, int cx1, int cy1,
776  int cx2, int cy2, int cw, int ch)
777 {
778  p.save ();
779 
780  QPoint begin = m_beginSelection;
781  QPoint end = m_endSelection;
782 
783  bool haveSelection = (begin != end);
784 
785  if (haveSelection)
786  maybeSwapPoints (begin, end);
787 
788  int verticalScrollOffset = m_consoleRect.top ();
789  int horizontalScrollOffset = m_consoleRect.left ();
790 
791  begin.ry () -= verticalScrollOffset;
792  end.ry () -= verticalScrollOffset;
793 
794  begin.rx () -= horizontalScrollOffset;
795  end.rx () -= horizontalScrollOffset;
796 
797  int ascent = p.fontMetrics ().ascent ();
798  int stride = m_consoleRect.width ();
799 
800  int y = ascent + cy1 * ch;;
801  for (int j = cy1; j <= cy2; j++, y += ch)
802  {
803  int charsThisLine = 0;
804  int len = 0;
805  bool hasChar = false;
806  WORD attr = 0;
807 
808  for (int i = cx1; i <= cx2; i++)
809  {
810  CHAR_INFO* ci = &(m_buffer[stride*j+i]);
811 
812  if ((ci->Attributes & 0x00ff) != attr)
813  {
814  // Character attributes changed
815  if (len != 0)
816  {
817  charsThisLine += len;
818  len = 0;
819  hasChar = false;
820  }
821 
822  // Store current attributes
823  attr = (ci->Attributes & 0x00ff);
824  }
825 
826  // Append current character to the string buffer
827  len++;
828  if (ci->Char.UnicodeChar != L' ')
829  hasChar = true;
830  }
831 
832  if (len != 0 && (hasChar || (attr & 0x00f0)))
833  charsThisLine += len;
834 
835  if (haveSelection && j >= begin.y () && j <= end.y ())
836  {
837  int selectionBegin = j == begin.y () ? begin.x (): 0;
838 
839  int len = ((j == end.y () && end.x () < charsThisLine)
840  ? end.x () - selectionBegin + 1
841  : stride - selectionBegin);
842 
843  p.fillRect (selectionBegin * cw, y-ascent, len * cw, ch,
844  selectionColor ());
845  }
846  }
847 
848  p.restore ();
849 }
850 
852 {
853  if (! m_cursorBlinking)
854  {
855  p.save ();
856 
857  QRect rect = cursorRect ();
858  QColor color = cursorColor ();
859 
860  p.setPen (color);
861 
863  {
864  if (q->hasFocus ())
865  p.fillRect (rect, color);
866  else
867  {
868  // draw the cursor outline, adjusting the area so that
869  // it is draw entirely inside 'rect'
870 
871  int penWidth = qMax (1, p.pen().width());
872 
873  p.drawRect (rect.adjusted (penWidth/2, penWidth/2,
874  - penWidth/2 - penWidth%2,
875  - penWidth/2 - penWidth%2));
876  }
877  }
879  {
880  p.drawLine (rect.left (), rect.bottom (),
881  rect.right (), rect.bottom ());
882  }
884  {
885  p.drawLine (rect.left (), rect.top (),
886  rect.left (), rect.bottom ());
887  }
888 
889  p.restore ();
890  }
891 }
892 
893 void QConsolePrivate::drawText (QPainter& p, int cx1, int cy1,
894  int cx2, int cy2, int cw, int ch)
895 {
896  p.save ();
897 
898  p.setFont (m_font);
899  p.setPen (foregroundColor ());
900 
901  QString s;
902  s.reserve (cx2 - cx1 + 1);
903 
904  int ascent = p.fontMetrics ().ascent ();
905  int stride = m_consoleRect.width ();
906 
907  int y = ascent + cy1 * ch;;
908  for (int j = cy1; j <= cy2; j++, y += ch)
909  {
910  // Reset string buffer and starting X coordinate
911  s.clear ();
912  bool hasChar = false;
913  int x = cx1 * cw;
914  WORD attr = 0;
915 
916  for (int i = cx1; i <= cx2; i++)
917  {
918  CHAR_INFO* ci = &(m_buffer[stride*j+i]);
919 
920  if ((ci->Attributes & 0x00ff) != attr)
921  {
922  // Character attributes changed
923  if (! s.isEmpty ())
924  {
925  // String buffer not empty -> draw it
926  if (hasChar || (attr & 0x00f0))
927  p.drawText (x, y, s);
928 
929  x += (s.length () * cw);
930  s.clear ();
931  hasChar = false;
932  }
933  // Update current pen and store current attributes
934  attr = (ci->Attributes & 0x00ff);
935  p.setPen (m_colors[attr & 0x000f]);
936  }
937 
938  // Append current character to the string buffer
939  s.append (ci->Char.UnicodeChar);
940  if (ci->Char.UnicodeChar != L' ')
941  hasChar = true;
942  }
943 
944  if (! s.isEmpty () && (hasChar || (attr & 0x00f0)))
945  {
946  // Line end reached, but string buffer not empty -> draw it
947  // No need to update s or x, they will be reset on the next
948  // for-loop iteration
949 
950  p.drawText (x, y, s);
951  }
952  }
953 
954  p.restore ();
955 }
956 
957 /////////////////////////////////////////////////////////////////////////////
958 
959 void QConsolePrivate::closeStandardIO (int fd, DWORD stdHandleId,
960  const char* name)
961 {
962  if (close (fd) == -1)
963  log ("Failed to close file descriptor %d: errno=%d.\n", fd, errno);
964  if (! CloseHandle (GetStdHandle (stdHandleId)))
965  log ("Failed to close Win32 %s: error=%08x.\n", name, GetLastError ());
966 }
967 
968 //////////////////////////////////////////////////////////////////////////////
969 
970 void QConsolePrivate::log (const char* fmt, ...)
971 {
972 #ifdef DEBUG_QCONSOLE
973  if (fmt)
974  {
975  va_list l;
976  FILE* flog = fopen (LOGFILENAME, "ab");
977 
978  va_start (l, fmt);
979  vfprintf (flog, fmt, l);
980  va_end (l);
981  fclose (flog);
982  }
983  else
984  {
985  // Special case to re-initialize the log file
986  FILE* flog = fopen (LOGFILENAME, "w");
987  fclose (flog);
988  }
989 #else
990  Q_UNUSED (fmt);
991 #endif
992 }
993 
994 //////////////////////////////////////////////////////////////////////////////
995 
996 void QConsolePrivate::updateConsoleSize (bool sync, bool allow_smaller_width)
997 {
998  QFontMetrics fm = m_consoleView->fontMetrics ();
999  QSize winSize = m_consoleView->size ();
1000 
1001  m_charSize.rwidth () = fm.averageCharWidth ();
1002  m_charSize.rheight () = fm.lineSpacing ();
1003 
1004  m_consoleRect.setWidth (winSize.width () / fm.averageCharWidth ());
1005  m_consoleRect.setHeight (winSize.height () / fm.lineSpacing ());
1006 
1007  // Don't shrink the size of the buffer. That way wide lines won't be
1008  // truncated and will reappear if the window is enlarged again later.
1009 
1010  if (allow_smaller_width || m_consoleRect.width () > m_bufferSize.width ())
1011  m_bufferSize.rwidth () = m_consoleRect.width ();
1012 
1013  if (qMax (m_bufferSize.height (), m_consoleRect.height ())
1014  > m_bufferSize.height ())
1015  m_bufferSize.rheight () = qMax (m_bufferSize.height (),
1016  m_consoleRect.height ());
1017 
1018  // Store the terminal size in the environment. When Octave is
1019  // initialized, we ask the command editor (usually readline) to prefer
1020  // using these values rather than querying the terminal so that the
1021  // buffer size can be larger than the size of the window that the
1022  // command editor will actually use.
1023 
1024  qputenv ("LINES", QByteArray::number (m_consoleRect.height ()));
1025  qputenv ("COLUMNS", QByteArray::number (m_consoleRect.width ()));
1026 
1027  // Force the command line editor (usually readline) to notice the
1028  // change in screen size as soon as possible.
1029 
1030  q->setSize (m_consoleRect.height (), m_consoleRect.width ());
1031 
1032  m_consoleRect.moveLeft (0);
1033  if (m_consoleRect.bottom () >= m_bufferSize.height ())
1034  m_consoleRect.moveTop (m_bufferSize.height () - m_consoleRect.height ());
1035 
1036  log ("Console resized:\n");
1037  log (" widget size: %d x %d\n", winSize.width (), winSize.height ());
1038  log (" buffer size: %d x %d\n", m_bufferSize.width (),
1039  m_bufferSize.height ());
1040  log (" window: (%d, %d) -> (%d, %d) [%d x %d]\n",
1041  m_consoleRect.left (), m_consoleRect.top (),
1042  m_consoleRect.right (), m_consoleRect.bottom (),
1043  m_consoleRect.width (), m_consoleRect.height ());
1044 
1045  if (sync)
1047 
1050 }
1051 
1052 //////////////////////////////////////////////////////////////////////////////
1053 
1055 {
1056  CONSOLE_SCREEN_BUFFER_INFO sbi;
1057  HANDLE hStdOut = m_stdOut;
1058 
1059  GetConsoleScreenBufferInfo (hStdOut, &sbi);
1060 
1061  COORD bs;
1062  SMALL_RECT sr;
1063 
1064  bs.X = m_bufferSize.width ();
1065  bs.Y = m_bufferSize.height ();
1066  sr.Left = m_consoleRect.left ();
1067  sr.Right = m_consoleRect.right ();
1068  sr.Top = m_consoleRect.top ();
1069  sr.Bottom = m_consoleRect.bottom ();
1070 
1071  if (bs.X * bs.Y > sbi.dwSize.X * sbi.dwSize.Y)
1072  {
1073  SetConsoleScreenBufferSize (hStdOut, bs);
1074  SetConsoleWindowInfo (hStdOut, TRUE, &sr);
1075  }
1076  else
1077  {
1078  SetConsoleWindowInfo (hStdOut, TRUE, &sr);
1079  SetConsoleScreenBufferSize (hStdOut, bs);
1080  }
1081 
1082  log ("Sync'ing console parameters:\n");
1083  log (" buffer size: %d x %d\n", bs.X, bs.Y);
1084  log (" window: (%d, %d) -> (%d, %d)\n",
1085  sr.Left, sr.Top, sr.Right, sr.Bottom);
1086 
1087  delete [] m_buffer;
1088  delete [] m_tmpBuffer;
1089 
1090  std::size_t bufSize = m_consoleRect.width () * m_consoleRect.height ();
1091 
1092  m_buffer = new CHAR_INFO[bufSize];
1093  m_tmpBuffer = new CHAR_INFO[bufSize];
1094 }
1095 
1096 //////////////////////////////////////////////////////////////////////////////
1097 
1099 {
1100  COORD bs, bc;
1101  SMALL_RECT r;
1102 
1103  bs.X = m_consoleRect.width ();
1104  bs.Y = m_consoleRect.height ();
1105  bc.X = 0;
1106  bc.Y = 0;
1107 
1108  r.Left = m_consoleRect.left ();
1109  r.Top = m_consoleRect.top ();
1110  r.Right = m_consoleRect.right ();
1111  r.Bottom = m_consoleRect.bottom ();
1112 
1113  log ("ReadConsoleOutput (%d,%d) -> (%d,%d)\n", r.Left, r.Top, r.Right, r.Bottom);
1114  if (! ReadConsoleOutput (m_stdOut, (buf ? buf : m_buffer), bs, bc, &r))
1115  qCritical ("cannot read console output");
1116 }
1117 
1118 //////////////////////////////////////////////////////////////////////////////
1119 
1121 {
1122  m_horizontalScrollBar->setMinimum (0);
1123  if (m_bufferSize.width () > m_consoleRect.width ())
1124  m_horizontalScrollBar->setMaximum (m_bufferSize.width () - m_consoleRect.width ());
1125  else
1126  m_horizontalScrollBar->setMaximum (0);
1127  m_horizontalScrollBar->setSingleStep (1);
1128  m_horizontalScrollBar->setPageStep (m_consoleRect.width ());
1129  m_horizontalScrollBar->setValue (m_consoleRect.left ());
1130 
1131  log ("Horizontal scrollbar parameters updated: %d/%d/%d/%d\n",
1132  m_horizontalScrollBar->minimum (),
1133  m_horizontalScrollBar->maximum (),
1134  m_horizontalScrollBar->singleStep (),
1135  m_horizontalScrollBar->pageStep ());
1136 }
1137 
1139 {
1140  m_verticalScrollBar->setMinimum (0);
1141  if (m_bufferSize.height () > m_consoleRect.height ())
1142  m_verticalScrollBar->setMaximum (m_bufferSize.height () - m_consoleRect.height ());
1143  else
1144  m_verticalScrollBar->setMaximum (0);
1145  m_verticalScrollBar->setSingleStep (1);
1146  m_verticalScrollBar->setPageStep (m_consoleRect.height ());
1147  m_verticalScrollBar->setValue (m_consoleRect.top ());
1148 
1149  log ("Vertical scrollbar parameters updated: %d/%d/%d/%d\n",
1150  m_verticalScrollBar->minimum (), m_verticalScrollBar->maximum (),
1151  m_verticalScrollBar->singleStep (), m_verticalScrollBar->pageStep ());
1152 }
1153 
1154 //////////////////////////////////////////////////////////////////////////////
1155 
1157 {
1158  if (value == m_consoleRect.left ())
1159  return;
1160 
1161  SMALL_RECT r;
1162  HANDLE hStdOut = m_stdOut;
1163 
1164  if (value + m_consoleRect.width () > m_bufferSize.width ())
1165  value = m_bufferSize.width () - m_consoleRect.width ();
1166 
1167  r.Left = value;
1168  r.Top = m_consoleRect.top ();
1169  r.Right = value + m_consoleRect.width () - 1;
1170  r.Bottom = m_consoleRect.bottom ();
1171 
1172  log ("Scrolling window horizontally: (%d, %d) -> (%d, %d) [%d x %d]\n",
1173  r.Left, r.Top, r.Right, r.Bottom,
1174  r.Right - r.Left + 1, r.Bottom - r.Top + 1);
1175 
1176  if (SetConsoleWindowInfo (hStdOut, TRUE, &r))
1177  {
1178  m_consoleRect.moveLeft (value);
1179  updateConsoleView ();
1180  }
1181 }
1182 
1184 {
1185  if (value == m_consoleRect.top ())
1186  return;
1187 
1188  SMALL_RECT r;
1189  HANDLE hStdOut = m_stdOut;
1190 
1191  if (value + m_consoleRect.height () > m_bufferSize.height ())
1192  value = m_bufferSize.height () - m_consoleRect.height ();
1193 
1194  r.Left = m_consoleRect.left ();
1195  r.Top = value;
1196  r.Right = m_consoleRect.right ();
1197  r.Bottom = value + m_consoleRect.height () - 1;
1198 
1199  log ("Scrolling window vertically: (%d, %d) -> (%d, %d) [%d x %d]\n",
1200  r.Left, r.Top, r.Right, r.Bottom,
1201  r.Right - r.Left + 1, r.Bottom - r.Top + 1);
1202 
1203  if (SetConsoleWindowInfo (hStdOut, TRUE, &r))
1204  {
1205  m_consoleRect.moveTop (value);
1206 
1207  CONSOLE_SCREEN_BUFFER_INFO sbi;
1208  if (GetConsoleScreenBufferInfo (hStdOut, &sbi))
1209  {
1210  if (sbi.dwCursorPosition.Y > m_consoleRect.bottom ())
1211  m_auto_scroll = false;
1212  else
1213  m_auto_scroll = true;
1214  }
1215 
1216  updateConsoleView ();
1217  }
1218 }
1219 
1220 //////////////////////////////////////////////////////////////////////////////
1221 
1223 {
1224  if (grab)
1225  grabConsoleBuffer ();
1226  m_consoleView->update ();
1227  m_consoleWatcher->start ();
1228 }
1229 
1230 //////////////////////////////////////////////////////////////////////////////
1231 
1233 {
1234  CONSOLE_SCREEN_BUFFER_INFO sbi;
1235  HANDLE hStdOut = GetStdHandle (STD_OUTPUT_HANDLE);
1236 
1237  static wchar_t titleBuf[260];
1238 
1239  GetConsoleTitleW (titleBuf, sizeof (titleBuf));
1240  QString title = QString::fromWCharArray (titleBuf);
1241 
1242  if (title != m_title)
1243  {
1244  q->setWindowTitle (title);
1245  emit q->titleChanged (title);
1246  }
1247 
1248  if (GetConsoleScreenBufferInfo (hStdOut, &sbi))
1249  {
1250  bool need_console_sync = false;
1251 
1252  if (m_bufferSize.width () != sbi.dwSize.X
1253  || m_bufferSize.height () != sbi.dwSize.Y)
1254  {
1255  // Buffer size changed
1256  m_bufferSize.rwidth () = sbi.dwSize.X;
1257  m_bufferSize.rheight () = sbi.dwSize.Y;
1260  need_console_sync = true;
1261  }
1262 
1263  if (m_cursorPos.x () != sbi.dwCursorPosition.X
1264  || m_cursorPos.y () != sbi.dwCursorPosition.Y)
1265  {
1266  // Cursor position changed
1267  m_consoleView->update
1268  ((m_cursorPos.x () - sbi.srWindow.Left) * m_charSize.width (),
1269  (m_cursorPos.y () - sbi.srWindow.Top) * m_charSize.height (),
1270  m_charSize.width (), m_charSize.height ());
1271  m_cursorPos.rx () = sbi.dwCursorPosition.X;
1272  m_cursorPos.ry () = sbi.dwCursorPosition.Y;
1273  m_consoleView->update
1274  ((m_cursorPos.x () - sbi.srWindow.Left) * m_charSize.width (),
1275  (m_cursorPos.y () - sbi.srWindow.Top) * m_charSize.height (),
1276  m_charSize.width (), m_charSize.height ());
1277  }
1278 
1279  if (m_consoleRect.left () != sbi.srWindow.Left
1280  || m_consoleRect.right () != sbi.srWindow.Right
1281  || (m_auto_scroll &&
1282  (m_consoleRect.top () != sbi.srWindow.Top
1283  || m_consoleRect.bottom () != sbi.srWindow.Bottom)))
1284  {
1285  // Console window changed
1286  log ("--> Console window changed\n");
1287  m_consoleRect = QRect (sbi.srWindow.Left, sbi.srWindow.Top,
1288  sbi.srWindow.Right - sbi.srWindow.Left + 1,
1289  sbi.srWindow.Bottom - sbi.srWindow.Top + 1);
1293  updateConsoleView ();
1294  return;
1295  }
1296 
1297  if (need_console_sync)
1298  {
1300  updateConsoleView ();
1301  return;
1302  }
1303 
1304  if (m_tmpBuffer && m_buffer)
1305  {
1307  if (memcmp (m_tmpBuffer, m_buffer,
1308  sizeof (CHAR_INFO) * m_consoleRect.width ()
1309  * m_consoleRect.height ()))
1310  {
1311  // FIXME: compute the area to update based on the
1312  // difference between the 2 buffers.
1313  std::swap (m_buffer, m_tmpBuffer);
1314  updateConsoleView (false);
1315  }
1316  }
1317  }
1318 }
1319 
1320 //////////////////////////////////////////////////////////////////////////////
1321 
1323 {
1324  QString cmd = m_command;
1325 
1326  if (cmd.isEmpty ())
1327  cmd = qgetenv ("COMSPEC").constData ();
1328 
1329  if (! cmd.isEmpty ())
1330  {
1331  STARTUPINFOW si;
1332  PROCESS_INFORMATION pi;
1333 
1334  ZeroMemory (&si, sizeof (si));
1335  si.cb = sizeof (si);
1336  ZeroMemory (&pi, sizeof (pi));
1337 
1338  if (CreateProcessW (nullptr,
1339  (LPWSTR)cmd.unicode (),
1340  nullptr,
1341  nullptr,
1342  TRUE,
1343  0,
1344  nullptr,
1345  nullptr,
1346  &si,
1347  &pi))
1348  {
1349  CloseHandle (pi.hThread);
1350  m_process = pi.hProcess;
1351  WaitForSingleObject (m_process, INFINITE);
1352  CloseHandle (m_process);
1353  m_process = nullptr;
1354  }
1355  }
1356 }
1357 
1358 //////////////////////////////////////////////////////////////////////////////
1359 
1360 void QConsolePrivate::sendConsoleText (const QString& s)
1361 {
1362  // Send the string in chunks of 512 characters. Each character is
1363  // translated into an equivalent keypress event.
1364 
1365 #define TEXT_CHUNK_SIZE 512
1366 
1367  // clear any selection on inserting text
1368  clearSelection();
1369  // enable auto-scrolling
1370  m_auto_scroll = true;
1371 
1372  int len = s.length ();
1373  INPUT_RECORD events[TEXT_CHUNK_SIZE];
1374  DWORD nEvents = 0, written;
1375  HANDLE hStdIn = GetStdHandle (STD_INPUT_HANDLE);
1376 
1377  ZeroMemory (events, sizeof (events));
1378 
1379  for (int i = 0; i < len; i++)
1380  {
1381  QChar c = s.at (i);
1382 
1383  if (c == L'\r' || c == L'\n')
1384  {
1385  if (c == L'\r' && i < (len - 1) && s.at (i+1) == L'\n')
1386  i++;
1387 
1388  // add new line
1389  events[nEvents].EventType = KEY_EVENT;
1390  events[nEvents].Event.KeyEvent.bKeyDown = TRUE;
1391  events[nEvents].Event.KeyEvent.wRepeatCount = 1;
1392  events[nEvents].Event.KeyEvent.wVirtualKeyCode =
1393  VK_RETURN;
1394  events[nEvents].Event.KeyEvent.wVirtualScanCode = 0;
1395  events[nEvents].Event.KeyEvent.uChar.UnicodeChar = c.unicode ();
1396  events[nEvents].Event.KeyEvent.dwControlKeyState = 0;
1397  nEvents++;
1398 
1399  WriteConsoleInput (hStdIn, events, nEvents, &written);
1400  nEvents = 0;
1401  ZeroMemory (events, sizeof (events));
1402 
1403  }
1404  else
1405  {
1406  events[nEvents].EventType = KEY_EVENT;
1407  events[nEvents].Event.KeyEvent.bKeyDown = TRUE;
1408  events[nEvents].Event.KeyEvent.wRepeatCount = 1;
1409  events[nEvents].Event.KeyEvent.wVirtualKeyCode =
1410  LOBYTE (VkKeyScan (c.unicode ()));
1411  events[nEvents].Event.KeyEvent.wVirtualScanCode = 0;
1412  events[nEvents].Event.KeyEvent.uChar.UnicodeChar = c.unicode ();
1413  events[nEvents].Event.KeyEvent.dwControlKeyState = 0;
1414  nEvents++;
1415  }
1416 
1417  if (nEvents == TEXT_CHUNK_SIZE
1418  || (nEvents > 0 && i == (len - 1)))
1419  {
1420  WriteConsoleInput (hStdIn, events, nEvents, &written);
1421  nEvents = 0;
1422  ZeroMemory (events, sizeof (events));
1423  }
1424  }
1425 }
1426 
1427 QRect
1429 {
1430  int cw = m_charSize.width ();
1431  int ch = m_charSize.height ();
1432 
1433  return QRect ((m_cursorPos.x () - m_consoleRect.x ()) * cw,
1434  (m_cursorPos.y () - m_consoleRect.y ()) * ch,
1435  cw, ch);
1436 }
1437 
1438 //////////////////////////////////////////////////////////////////////////////
1439 
1440 QWinTerminalImpl::QWinTerminalImpl (octave::base_qobject& oct_qobj,
1441  QWidget* parent)
1442  : QTerminal (oct_qobj, parent), d (new QConsolePrivate (this)),
1443  allowTripleClick (false)
1444 {
1445  installEventFilter (this);
1446 
1447  setAcceptDrops (true);
1448 }
1449 
1450 //////////////////////////////////////////////////////////////////////////////
1451 
1453 {
1454  delete d;
1455 }
1456 
1457 void QWinTerminalImpl::mouseMoveEvent (QMouseEvent *event)
1458 {
1459  if (d->m_settingSelection)
1460  {
1461  d->m_endSelection = d->posToCell (event->pos ());
1462 
1463  updateSelection ();
1464  }
1465 }
1466 
1467 void QWinTerminalImpl::mousePressEvent (QMouseEvent *event)
1468 {
1469  if (allowTripleClick)
1470  {
1471  mouseTripleClickEvent (event);
1472  }
1473  else if (event->button () == Qt::LeftButton)
1474  {
1475  d->m_settingSelection = true;
1476 
1477  d->m_beginSelection = d->posToCell (event->pos ());
1478  }
1479 }
1480 
1481 void QWinTerminalImpl::mouseReleaseEvent (QMouseEvent *event)
1482 {
1483  if (event->button () == Qt::LeftButton && d->m_settingSelection)
1484  {
1485  d->m_endSelection = d->posToCell (event->pos ());
1486 
1487  updateSelection ();
1488 
1489  d->m_settingSelection = false;
1490  }
1491 }
1492 
1494 {
1495  if (event->button () == Qt::LeftButton)
1496  {
1497  // doubleclick - select word
1498  d->m_settingSelection = false;
1499 
1500  d->selectWord (d->posToCell (event->pos ()));
1501 
1502  allowTripleClick = true;
1503 
1504  QTimer::singleShot (QApplication::doubleClickInterval (),this,
1505  SLOT (tripleClickTimeout ()));
1506 
1507  }
1508 }
1509 
1511 {
1512  if (event->button () == Qt::LeftButton)
1513  {
1514  d->selectLine (d->posToCell (event->pos ()));
1515  }
1516 }
1517 
1519 {
1520  allowTripleClick = false;
1521 }
1522 
1523 //////////////////////////////////////////////////////////////////////////////
1524 
1526 {
1527  d->updateConsoleSize (true);
1528  d->grabConsoleBuffer ();
1529 }
1530 
1531 //////////////////////////////////////////////////////////////////////////////
1532 
1534 {
1535  QPainter p (w);
1536 
1537  int cw = d->m_charSize.width ();
1538  int ch = d->m_charSize.height ();
1539 
1540  QRect updateRect = event->rect ();
1541  p.fillRect(updateRect, QBrush(d->backgroundColor()));
1542 
1543  int cx1 = updateRect.left () / cw;
1544  int cy1 = updateRect.top () / ch;
1545  int cx2 = qMin (d->m_consoleRect.width () - 1, updateRect.right () / cw);
1546  int cy2 = qMin (d->m_consoleRect.height () - 1, updateRect.bottom () / ch);
1547 
1548  if (cx1 > d->m_consoleRect.width () - 1
1549  || cy1 > d->m_consoleRect.height () - 1)
1550  return;
1551 
1552  d->drawTextBackground (p, cx1, cy1, cx2, cy2, cw, ch);
1553  d->drawSelection (p, cx1, cy1, cx2, cy2, cw, ch);
1554  d->drawCursor (p);
1555  d->drawText (p, cx1, cy1, cx2, cy2, cw, ch);
1556 }
1557 
1559 {
1560  if (d->m_hasBlinkingCursor)
1562  else
1563  d->m_cursorBlinking = false;
1564 
1565  d->m_consoleView->update (d->cursorRect ());
1566 }
1567 
1569 {
1570  d->m_hasBlinkingCursor = blink;
1571 
1572  setBlinkingCursorState (blink);
1573 }
1574 
1576 {
1577  if (blink && ! d->m_blinkCursorTimer->isActive ())
1578  d->m_blinkCursorTimer->start (d->BLINK_DELAY);
1579 
1580  if (! blink && d->m_blinkCursorTimer->isActive ())
1581  {
1582  d->m_blinkCursorTimer->stop ();
1583 
1584  if (d->m_cursorBlinking)
1585  blinkCursorEvent ();
1586  }
1587 }
1588 
1589 // Reset width of console buffer and terminal window to be the same.
1590 
1592 {
1593  d->updateConsoleSize (true, true);
1594 }
1595 
1596 //////////////////////////////////////////////////////////////////////////////
1597 
1598 void QWinTerminalImpl::wheelEvent (QWheelEvent* event)
1599 {
1600  if (! d->m_inWheelEvent)
1601  {
1602  // Forward to the scrollbar (avoid recursion)
1603  d->m_inWheelEvent = true;
1604  QApplication::sendEvent (d->m_verticalScrollBar, event);
1605  d->m_inWheelEvent = false;
1606  }
1607 }
1608 
1609 //////////////////////////////////////////////////////////////////////////////
1610 
1612 {
1613  d->setHorizontalScrollValue (value);
1614 }
1615 
1617 {
1618  d->setVerticalScrollValue (value);
1619 }
1620 
1621 //////////////////////////////////////////////////////////////////////////////
1622 
1624 {
1625  d->monitorConsole ();
1626 }
1627 
1629 {
1630  d->updateSelection ();
1631 }
1632 
1633 //////////////////////////////////////////////////////////////////////////////
1634 
1635 void QWinTerminalImpl::focusInEvent (QFocusEvent* event)
1636 {
1637  setBlinkingCursorState (true);
1638 
1639  QWidget::focusInEvent (event);
1640 }
1641 
1642 void QWinTerminalImpl::focusOutEvent (QFocusEvent* event)
1643 {
1644  // Force the cursor to be redrawn.
1645  d->m_cursorBlinking = true;
1646 
1647  setBlinkingCursorState (false);
1648 
1649  QWidget::focusOutEvent (event);
1650 }
1651 
1652 bool QWinTerminalImpl::eventFilter (QObject *obj, QEvent * event)
1653 {
1654  // if a keypress, filter out tab keys so that the next/prev tabbing is
1655  // disabled - but we still need to pass along to the console .
1656  if (event->type () == QEvent::KeyPress)
1657  {
1658  QKeyEvent* k = static_cast<QKeyEvent*>(event);
1659  if (k->key () == Qt::Key_Tab)
1660  {
1661  sendText ("\t");
1662  return true;
1663  }
1664  }
1665  return false;
1666 }
1667 
1668 void QWinTerminalImpl::keyPressEvent (QKeyEvent* event)
1669 {
1670  QString s = translateKey (event);
1671  if (!s.isEmpty ())
1672  sendText (s);
1673 
1674  if (d->m_hasBlinkingCursor)
1675  {
1676  d->m_blinkCursorTimer->start (d->BLINK_DELAY);
1677 
1678  if (d->m_cursorBlinking)
1679  blinkCursorEvent ();
1680  }
1681 
1682  QWidget::keyPressEvent (event);
1683 }
1684 
1685 //////////////////////////////////////////////////////////////////////////////
1686 
1688 {
1689  d->startCommand ();
1690 }
1691 
1692 //////////////////////////////////////////////////////////////////////////////
1693 
1694 void QWinTerminalImpl::sendText (const QString& s)
1695 {
1696  d->sendConsoleText (s);
1697 }
1698 
1700 {
1701  switch (type)
1702  {
1703  case UnderlineCursor:
1705  break;
1706 
1707  case BlockCursor:
1709  break;
1710 
1711  case IBeamCursor:
1713  break;
1714  }
1715 
1716  setBlinkingCursor (blinking);
1717 }
1718 
1719 void QWinTerminalImpl::setBackgroundColor (const QColor& color)
1720 {
1721  d->setBackgroundColor (color);
1722 }
1723 
1724 void QWinTerminalImpl::setForegroundColor (const QColor& color)
1725 {
1726  d->setForegroundColor (color);
1727 }
1728 
1729 void QWinTerminalImpl::setSelectionColor (const QColor& color)
1730 {
1731  d->setSelectionColor (color);
1732 }
1733 
1734 void QWinTerminalImpl::setCursorColor (bool useForegroundColor,
1735  const QColor& color)
1736 {
1737  d->setCursorColor (useForegroundColor, color);
1738 }
1739 
1741 {
1742  d->setScrollBufferSize (value);
1743 }
1744 
1745 
1746 //////////////////////////////////////////////////////////////////////////////
1747 
1749 {
1750  d->m_font = f;
1751  d->m_consoleView->setFont (f);
1752  d->updateConsoleSize (true);
1753 }
1754 
1755 //////////////////////////////////////////////////////////////////////////////
1756 
1757 void QWinTerminalImpl::setSize (int columns, int lines)
1758 {
1759  d->log ("emit set_screen_size_signal (%d, %d)\n", columns, lines);
1760 
1761  emit set_screen_size_signal (columns, lines);
1762 }
1763 
1764 //////////////////////////////////////////////////////////////////////////////
1765 
1767 {
1768  if(!hasFocus()) return;
1769 
1770  QClipboard *clipboard = QApplication::clipboard ();
1771 
1772  QString selection = d->getSelection ();
1773 
1774  if (selection.isEmpty ())
1775  {
1776  if (! _extra_interrupt)
1777  terminal_interrupt ();
1778  }
1779  else
1780  {
1781  clipboard->setText (selection);
1782  emit report_status_message (tr ("copied selection to clipboard"));
1783  }
1784 }
1785 
1786 //////////////////////////////////////////////////////////////////////////////
1787 
1789 {
1790  if(!hasFocus()) return;
1791 
1792  QString text = QApplication::clipboard()->text (QClipboard::Clipboard);
1793 
1794  if (! text.isEmpty ())
1795  {
1796  if (text.contains ("\t"))
1797  {
1798  qWarning ("Tabs replaced with spaces in pasted text before processing");
1799  text.replace ("\t", " ");
1800  }
1801 
1802  sendText (text);
1803  }
1804 }
1805 
1806 //////////////////////////////////////////////////////////////////////////////
1807 
1809 {
1810  if(!hasFocus()) return;
1811 
1812  d->selectAll();
1813 }
1814 
1815 
1816 
1817 //////////////////////////////////////////////////////////////////////////////
1818 
1820 {
1821  QString selection = d->getSelection ();
1822  return selection;
1823 }
1824 
1825 //////////////////////////////////////////////////////////////////////////////
1826 
1827 void QWinTerminalImpl::dragEnterEvent (QDragEnterEvent *event)
1828 {
1829  if (event->mimeData ()->hasUrls ())
1830  {
1831  event->acceptProposedAction();
1832  }
1833 }
1834 
1835 //////////////////////////////////////////////////////////////////////////////
1836 
1837 void QWinTerminalImpl::dropEvent (QDropEvent *event)
1838 {
1839  QString dropText;
1840 
1841  if (event->mimeData ()->hasUrls ())
1842  {
1843  foreach (QUrl url, event->mimeData ()->urls ())
1844  {
1845  if(dropText.length () > 0)
1846  dropText += '\n';
1847  dropText += url.toLocalFile ();
1848  }
1849  sendText (dropText);
1850  }
1851 }
1852 
1853 //////////////////////////////////////////////////////////////////////////////
1854 
1856 {
1857  _extra_interrupt = extra;
1858 }
static QString translateKey(QKeyEvent *ev)
#define LOGFILENAME
#define TEXT_CHUNK_SIZE
static void maybeSwapPoints(QPoint &begin, QPoint &end)
QColor backgroundColor(void) const
void log(const char *fmt,...)
void updateConsoleView(bool grab=true)
void drawText(QPainter &p, int cx1, int cy1, int cx2, int cy2, int cw, int ch)
CHAR_INFO * m_tmpBuffer
void updateSelection(void)
QScrollBar * m_horizontalScrollBar
QScrollBar * m_verticalScrollBar
QWinTerminalImpl * q
void setCursorColor(bool useForegroundColor, const QColor &color)
void setupStandardIO(DWORD stdHandleId, int fd, const char *name, const char *devName)
void clearSelection(void)
QConsoleThread * m_consoleThread
QColor foregroundColor(void) const
QColor cursorColor(void) const
KeyboardCursorType m_cursorType
void setHorizontalScrollValue(int value)
void syncConsoleParameters(void)
QPoint posToCell(const QPoint &pt)
QConsoleView * m_consoleView
static const int BLINK_DELAY
void drawSelection(QPainter &p, int cx1, int cy1, int cx2, int cy2, int cw, int ch)
void drawTextBackground(QPainter &p, int cx1, int cy1, int cx2, int cy2, int cw, int ch)
void setForegroundColor(const QColor &color)
QConsolePrivate(QWinTerminalImpl *parent, const QString &cmd=QString())
void updateHorizontalScrollBar(void)
void setScrollBufferSize(int value)
void selectWord(const QPoint &cellPos)
void selectLine(const QPoint &cellPos)
void closeStandardIO(int fd, DWORD stdHandleId, const char *name)
QConsoleColors m_colors
void setVerticalScrollValue(int value)
void setBackgroundColor(const QColor &color)
void grabConsoleBuffer(CHAR_INFO *buf=0)
QColor selectionColor(void) const
void updateVerticalScrollBar(void)
void setSelectionColor(const QColor &color)
void drawCursor(QPainter &p)
void updateConsoleSize(bool sync=false, bool allow_smaller_width=false)
QString getSelection(void)
void sendConsoleText(const QString &s)
QConsoleThread(QWinTerminalImpl *console)
QWinTerminalImpl * q
void resizeEvent(QResizeEvent *event)
void paintEvent(QPaintEvent *event)
QWinTerminalImpl * q
QConsoleView(QWinTerminalImpl *parent=0)
void report_status_message(const QString &)
void set_screen_size_signal(int, int)
@ IBeamCursor
Definition: QTerminal.h:74
@ UnderlineCursor
Definition: QTerminal.h:76
@ BlockCursor
Definition: QTerminal.h:75
void terminal_interrupt(void)
Definition: QTerminal.h:133
void setCursorColor(bool useForegoundColor, const QColor &color)
void dropEvent(QDropEvent *event)
void setForegroundColor(const QColor &color)
friend class QConsoleView
void mouseReleaseEvent(QMouseEvent *event)
void init_terminal_size(void)
void setSize(int columns, int lines)
void terminated(void)
void sendText(const QString &s)
void dragEnterEvent(QDragEnterEvent *event)
void setTerminalFont(const QFont &font)
void horizontalScrollValueChanged(int value)
void tripleClickTimeout(void)
QWinTerminalImpl(octave::base_qobject &, QWidget *parent)
void setBlinkingCursorState(bool blink)
void setScrollBufferSize(int value)
void mouseDoubleClickEvent(QMouseEvent *event)
void mousePressEvent(QMouseEvent *event)
void has_extra_interrupt(bool)
virtual void start(void)
void setCursorType(CursorType type, bool blinking)
void focusInEvent(QFocusEvent *)
void wheelEvent(QWheelEvent *)
void verticalScrollValueChanged(int value)
friend class QConsoleThread
bool eventFilter(QObject *obj, QEvent *ev)
void viewResizeEvent(QConsoleView *, QResizeEvent *)
void viewPaintEvent(QConsoleView *, QPaintEvent *)
void setBackgroundColor(const QColor &color)
void setSelectionColor(const QColor &color)
void setBlinkingCursor(bool blink)
void mouseMoveEvent(QMouseEvent *event)
void titleChanged(const QString &)
QConsolePrivate * d
void keyPressEvent(QKeyEvent *)
void focusOutEvent(QFocusEvent *)
void mouseTripleClickEvent(QMouseEvent *event)
F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
F77_RET_T const F77_DBLE * x
F77_RET_T const F77_DBLE const F77_DBLE * f
static const double pi
Definition: lo-specfun.cc:1944
std::FILE * fopen(const std::string &filename, const std::string &mode)
Definition: lo-sysdep.cc:314
T * r
Definition: mx-inlines.cc:773
std::complex< double > w(std::complex< double > z, double relerr=0)
int dup2(int old_fd, int new_fd)
Definition: oct-syscalls.cc:52
F77_RET_T len
Definition: xerbla.cc:61