1 /*  Copyright (C) 2008 e_k (e_k@users.sourceforge.net)
2 
3     This library is free software; you can redistribute it and/or
4     modify it under the terms of the GNU Library General Public
5     License as published by the Free Software Foundation; either
6     version 2 of the License, or (at your option) any later version.
7 
8     This library is distributed in the hope that it will be useful,
9     but WITHOUT ANY WARRANTY; without even the implied warranty of
10     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11     Library General Public License for more details.
12 
13     You should have received a copy of the GNU Library General Public License
14     along with this library; see the file COPYING.LIB.  If not, write to
15     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16     Boston, MA 02110-1301, USA.
17 */
18 
19 
20 #ifndef _Q_TERM_WIDGET
21 #define _Q_TERM_WIDGET
22 
23 #include <QTranslator>
24 #include <QWidget>
25 #include "Emulation.h"
26 #include "Filter.h"
27 #include "qtermwidget_export.h"
28 #include "qtermwidget_version.h"
29 
30 class QVBoxLayout;
31 class TermWidgetImpl;
32 class SearchBar;
33 class QUrl;
34 
35 class QTERMWIDGET_EXPORT QTermWidget : public QWidget {
36     Q_OBJECT
37 public:
38 
39     /**
40      * This enum describes the location where the scroll bar is positioned in the display widget.
41      */
42     enum ScrollBarPosition {
43         /** Do not show the scroll bar. */
44         NoScrollBar = 0,
45         /** Show the scroll bar on the left side of the display. */
46         ScrollBarLeft = 1,
47         /** Show the scroll bar on the right side of the display. */
48         ScrollBarRight = 2
49     };
50 
51     using KeyboardCursorShape = Konsole::Emulation::KeyboardCursorShape;
52 
53     //Creation of widget
54     QTermWidget(int startnow, // 1 = start shell programm immediatelly
55                 QWidget * parent = nullptr);
56     // A dummy constructor for Qt Designer. startnow is 1 by default
57     QTermWidget(QWidget *parent = nullptr);
58 
59     ~QTermWidget() override;
60 
61     //Initial size
62     QSize sizeHint() const override;
63 
64     // expose TerminalDisplay::TerminalSizeHint, setTerminalSizeHint
65     void setTerminalSizeHint(bool on);
66     bool terminalSizeHint();
67 
68     //start shell program if it was not started in constructor
69     void startShellProgram();
70 
71     /**
72      * Start terminal teletype as is
73      * and redirect data for external recipient.
74      * It can be used for display and control a remote terminal.
75      */
76     void startTerminalTeletype();
77 
78     int getShellPID();
79 
80     void changeDir(const QString & dir);
81 
82     //look-n-feel, if you don`t like defaults
83 
84     //  Terminal font
85     // Default is application font with family Monospace, size 10
86     // Beware of a performance penalty and display/alignment issues when using a proportional font.
87     void setTerminalFont(const QFont & font);
88     QFont getTerminalFont();
89     void setTerminalOpacity(qreal level);
90     void setTerminalBackgroundImage(const QString& backgroundImage);
91     void setTerminalBackgroundMode(int mode);
92 
93     //environment
94     void setEnvironment(const QStringList & environment);
95 
96     //  Shell program, default is /bin/bash
97     void setShellProgram(const QString & progname);
98 
99     //working directory
100     void setWorkingDirectory(const QString & dir);
101     QString workingDirectory();
102 
103     // Shell program args, default is none
104     void setArgs(const QStringList & args);
105 
106     //Text codec, default is UTF-8
107     void setTextCodec(QTextCodec * codec);
108 
109     /** @brief Sets the color scheme, default is white on black
110      *
111      * @param[in] name The name of the color scheme, either returned from
112      * availableColorSchemes() or a full path to a color scheme.
113      */
114     void setColorScheme(const QString & name);
115     static QStringList availableColorSchemes();
116     static void addCustomColorSchemeDir(const QString& custom_dir);
117 
118     // History size for scrolling
119     void setHistorySize(int lines); //infinite if lines < 0
120 
121     // Presence of scrollbar
122     void setScrollBarPosition(ScrollBarPosition);
123 
124     // Wrapped, scroll to end.
125     void scrollToEnd();
126 
127     // Send some text to terminal
128     void sendText(const QString & text);
129 
130     // Send key event to terminal
131     void sendKeyEvent(QKeyEvent* e);
132 
133     // Sets whether flow control is enabled
134     void setFlowControlEnabled(bool enabled);
135 
136     // Returns whether flow control is enabled
137     bool flowControlEnabled(void);
138 
139     /**
140      * Sets whether the flow control warning box should be shown
141      * when the flow control stop key (Ctrl+S) is pressed.
142      */
143     void setFlowControlWarningEnabled(bool enabled);
144 
145     /*! Get all available keyboard bindings
146      */
147     static QStringList availableKeyBindings();
148 
149     //! Return current key bindings
150     QString keyBindings();
151 
152     void setMotionAfterPasting(int);
153 
154     /** Return the number of lines in the history buffer. */
155     int historyLinesCount();
156 
157     int screenColumnsCount();
158     int screenLinesCount();
159 
160     void setSelectionStart(int row, int column);
161     void setSelectionEnd(int row, int column);
162     void getSelectionStart(int& row, int& column);
163     void getSelectionEnd(int& row, int& column);
164 
165     /**
166      * Returns the currently selected text.
167      * @param preserveLineBreaks Specifies whether new line characters should
168      * be inserted into the returned text at the end of each terminal line.
169      */
170     QString selectedText(bool preserveLineBreaks = true);
171 
172     void setMonitorActivity(bool);
173     void setMonitorSilence(bool);
174     void setSilenceTimeout(int seconds);
175 
176     /** Returns the available hotspot for the given point \em pos.
177      *
178      * This method may return a nullptr if no hotspot is available.
179      *
180      * @param[in] pos The point of interest in the QTermWidget coordinates.
181      * @return Hotspot for the given position, or nullptr if no hotspot.
182      */
183     Filter::HotSpot* getHotSpotAt(const QPoint& pos) const;
184 
185     /** Returns the available hotspots for the given row and column.
186      *
187      * @return Hotspot for the given position, or nullptr if no hotspot.
188      */
189     Filter::HotSpot* getHotSpotAt(int row, int column) const;
190 
191     /*
192      * Proxy for TerminalDisplay::filterActions
193      * */
194     QList<QAction*> filterActions(const QPoint& position);
195 
196     /**
197      * Returns a pty slave file descriptor.
198      * This can be used for display and control
199      * a remote terminal.
200      */
201     int getPtySlaveFd() const;
202 
203     /**
204      * Sets the shape of the keyboard cursor.  This is the cursor drawn
205      * at the position in the terminal where keyboard input will appear.
206      */
207     void setKeyboardCursorShape(KeyboardCursorShape shape);
208 
209     void setBlinkingCursor(bool blink);
210 
211     /** Enables or disables bidi text in the terminal. */
212     void setBidiEnabled(bool enabled);
213     bool isBidiEnabled();
214 
215     /**
216      * Automatically close the terminal session after the shell process exits or
217      * keep it running.
218      */
219     void setAutoClose(bool);
220 
221     QString title() const;
222     QString icon() const;
223 
224     /** True if the title() or icon() was (ever) changed by the session. */
225     bool isTitleChanged() const;
226 
227     /** change and wrap text corresponding to paste mode **/
228     void bracketText(QString& text);
229 
230     /** forcefully disable bracketed paste mode **/
231     void disableBracketedPasteMode(bool disable);
232     bool bracketedPasteModeIsDisabled() const;
233 
234     /** Set the empty space outside the terminal */
235     void setMargin(int);
236 
237     /** Get the empty space outside the terminal */
238     int getMargin() const;
239 
240     void setDrawLineChars(bool drawLineChars);
241 
242     void setBoldIntense(bool boldIntense);
243 
244     void setConfirmMultilinePaste(bool confirmMultilinePaste);
245     void setTrimPastedTrailingNewlines(bool trimPastedTrailingNewlines);
246 signals:
247     void finished();
248     void copyAvailable(bool);
249 
250     void termGetFocus();
251     void termLostFocus();
252 
253     void termKeyPressed(QKeyEvent *);
254 
255     void urlActivated(const QUrl&, bool fromContextMenu);
256 
257     void bell(const QString& message);
258 
259     void activity();
260     void silence();
261 
262     /**
263      * Emitted when emulator send data to the terminal process
264      * (redirected for external recipient). It can be used for
265      * control and display the remote terminal.
266      */
267     void sendData(const char *,int);
268 
269     void profileChanged(const QString & profile);
270 
271     void titleChanged();
272 
273     /**
274      * Signals that we received new data from the process running in the
275      * terminal emulator
276      */
277     void receivedData(const QString &text);
278 
279 public slots:
280     // Copy selection to clipboard
281     void copyClipboard();
282 
283     // Paste clipboard to terminal
284     void pasteClipboard();
285 
286     // Paste selection to terminal
287     void pasteSelection();
288 
289     // Set zoom
290     void zoomIn();
291     void zoomOut();
292 
293     // Set size
294     void setSize(const QSize &);
295 
296     /*! Set named key binding for given widget
297      */
298     void setKeyBindings(const QString & kb);
299 
300     /*! Clear the terminal content and move to home position
301      */
302     void clear();
303 
304     void toggleShowSearchBar();
305 
306     void saveHistory(QIODevice *device);
307 protected:
308     void resizeEvent(QResizeEvent *) override;
309 
310 protected slots:
311     void sessionFinished();
312     void selectionChanged(bool textSelected);
313 
314 private slots:
315     void find();
316     void findNext();
317     void findPrevious();
318     void matchFound(int startColumn, int startLine, int endColumn, int endLine);
319     void noMatchFound();
320     /**
321      * Emulation::cursorChanged() signal propogates to here and QTermWidget
322      * sends the specified cursor states to the terminal display
323      */
324     void cursorChanged(Konsole::Emulation::KeyboardCursorShape cursorShape, bool blinkingCursorEnabled);
325 
326 private:
327     void search(bool forwards, bool next);
328     void setZoom(int step);
329     void init(int startnow);
330     TermWidgetImpl * m_impl;
331     SearchBar* m_searchBar;
332     QVBoxLayout *m_layout;
333     QTranslator *m_translator;
334 };
335 
336 
337 //Maybe useful, maybe not
338 
339 #ifdef __cplusplus
340 extern "C"
341 #endif
342 void * createTermWidget(int startnow, void * parent);
343 
344 #endif
345 
346