4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
7 * \author Lars Gullik Bjornes
9 * \author Abdelrazak Younes
10 * \author Peter Kümmel
12 * Full author contact details are available in file CREDITS.
18 #include "frontends/Delegates.h"
20 #include "support/strfwd.h"
22 #include <QMainWindow>
27 class QDragEnterEvent;
37 namespace support { class FileName; }
58 * GuiView - Qt main LyX window
60 * This class represents the main LyX window and provides
61 * accessor functions to its content.
63 * Note: a QObject emits a destroyed(QObject *) Qt signal when it
64 * is deleted. This might be useful for closing other dialogs
65 * depending on a given GuiView.
67 class GuiView : public QMainWindow, public GuiBufferViewDelegate,
68 public GuiBufferDelegate
73 /// create a main window of the given dimensions
78 /// closes the view such that the view knows that is closed
79 /// programmatically and not by the user clicking the x.
80 bool closeScheduled();
82 /// Things that need to be done when the OSes session manager
83 /// requests a log out.
84 bool prepareAllBuffersForLogout();
86 int id() const { return id_; }
91 /// Signal that the any "auto" minibuffer can be closed now.
92 void resetCommandExecute();
94 /// \name Generic accessor functions
96 /// The current BufferView refers to the BufferView that has the focus,
97 /// including for example the one that is created when you use the
98 /// advanced search and replace pane.
99 /// \return the currently selected buffer view.
100 BufferView * currentBufferView();
101 BufferView const * currentBufferView() const;
103 /// The document BufferView always refers to the view's main document
104 /// BufferView. So, even if the BufferView in e.g., the advanced
105 /// search and replace pane has the focus.
106 /// \return the current document buffer view.
107 BufferView * documentBufferView();
108 BufferView const * documentBufferView() const;
110 void newDocument(std::string const & filename,
111 std::string templatefile = std::string(),
112 bool fromTemplate = false);
114 /// display a message in the view
115 /// could be called from any thread
116 void message(docstring const &) override;
118 bool getStatus(FuncRequest const & cmd, FuncStatus & flag);
119 /// dispatch command.
120 /// \return true if the \c FuncRequest has been dispatched.
121 void dispatch(FuncRequest const & cmd, DispatchResult & dr);
124 /// Update the completion popup and the inline completion state.
125 /// If \c start is true, then a new completion might be started.
126 /// If \c keep is true, an active completion will be kept active
127 /// even though the cursor moved. The update flags of \c cur might
129 void updateCompletion(Cursor & cur, bool start, bool keep);
133 bool hasFocus() const;
136 void focusInEvent(QFocusEvent * e) override;
137 /// Add a Buffer to the View
138 /// \param b Buffer to set.
139 /// \param switch_to Whether to set it to the current workarea.
140 void setBuffer(Buffer * b, bool switch_to = true);
142 /// load a document into the current workarea.
143 Buffer * loadDocument(
144 support::FileName const & name, ///< File to load.
145 bool tolastfiles = true ///< append to the "Open recent" menu?
148 /// add toolbar, if newline==true, add a toolbar break before the toolbar
149 GuiToolbar * makeToolbar(ToolbarInfo const & tbinfo, bool newline);
150 void updateStatusBar();
152 /// updates the possible layouts selectable
153 void updateLayoutList();
154 void updateToolbars();
157 LayoutBox * getLayoutDialog() const;
159 /// hides the workarea and makes sure it is clean
160 bool hideWorkArea(GuiWorkArea * wa);
161 /// closes workarea; close buffer only if no other workareas point to it
162 bool closeWorkArea(GuiWorkArea * wa);
163 /// closes the buffer
164 bool closeBuffer(Buffer & buf);
166 void openDocument(std::string const & filename);
168 void importDocument(std::string const &);
170 /// \name GuiBufferDelegate.
172 void resetAutosaveTimers() override;
173 // shows an error list
174 // if from_master is true, show master's error list
175 void errors(std::string const &, bool from_master = false) override;
176 void structureChanged() override;
177 void updateTocItem(std::string const &, DocIterator const &) override;
181 TocModels & tocModels();
183 /// called on timeout
186 /// check for external change of any opened buffer, mainly for svn usage
187 void checkExternallyModifiedBuffers();
189 /** redraw \c inset in all the BufferViews in which it is currently
190 * visible. If successful return a pointer to the owning Buffer.
192 Buffer const * updateInset(Inset const *);
194 /// \return the \c Workarea associated to \p Buffer
195 /// \retval 0 if no \c WorkArea is found.
196 GuiWorkArea * workArea(Buffer & buffer);
197 /// \return the \c Workarea at index \c index
198 GuiWorkArea * workArea(int index);
200 /// Add a \c WorkArea
201 /// \return the \c Workarea associated to \p Buffer
202 /// \retval 0 if no \c WorkArea is found.
203 GuiWorkArea * addWorkArea(Buffer & buffer);
204 /// \param work_area The current \c WorkArea, or \c NULL
205 void setCurrentWorkArea(GuiWorkArea * work_area);
207 void removeWorkArea(GuiWorkArea * work_area);
208 /// return the current WorkArea (the one that has the focus).
209 GuiWorkArea const * currentWorkArea() const;
210 /// return the current WorkArea (the one that has the focus).
211 GuiWorkArea * currentWorkArea();
213 /// return the current document WorkArea (it may not have the focus).
214 GuiWorkArea const * currentMainWorkArea() const;
215 /// return the current document WorkArea (it may not have the focus).
216 GuiWorkArea * currentMainWorkArea();
218 /// Current ratio between physical pixels and device-independent pixels
219 double pixelRatio() const;
223 void triggerShowDialog(QString const & qname, QString const & qdata, Inset * inset);
224 // emitted when the work area or its buffer view changed
225 void bufferViewChanged();
226 /// emitted when zoom is modified
227 void currentZoomChanged(int);
231 void setBusy(bool) override;
233 /// clear any temporary message and replace with current status.
236 void updateWindowTitle(GuiWorkArea * wa);
238 void disableShellEscape();
242 void resetWindowTitle();
244 void flatGroupBoxes(const QObject * object, bool flag);
247 void checkCancelBackground();
249 void zoomSliderMoved(int);
251 void zoomValueChanged(int);
253 void zoomInPressed();
255 void zoomOutPressed();
257 void showZoomContextMenu();
259 void on_currentWorkAreaChanged(GuiWorkArea *);
261 void onBufferViewChanged();
263 void on_lastWorkAreaRemoved();
265 /// For completion of autosave or export threads.
266 void processingThreadStarted();
267 void processingThreadFinished();
268 void autoSaveThreadFinished();
270 /// must be called in GUI thread
271 void doShowDialog(QString const & qname, QString const & qdata,
274 /// must be called from GUI thread
275 void updateStatusBarMessage(QString const & str);
276 void clearMessageText();
279 void toolBarPopup(const QPoint &pos);
282 /// Open given child document in current buffer directory.
283 void openChildDocument(std::string const & filename);
284 /// Close current document buffer.
286 /// Close all document buffers.
287 bool closeBufferAll();
289 TabWorkArea * addTabWorkArea();
292 void scheduleRedrawWorkAreas();
294 /// connect to signals in the given BufferView
295 void connectBufferView(BufferView & bv);
296 /// disconnect from signals in the given BufferView
297 void disconnectBufferView();
298 /// connect to signals in the given buffer
299 void connectBuffer(Buffer & buf);
300 /// disconnect from signals in the given buffer
301 void disconnectBuffer();
303 void dragEnterEvent(QDragEnterEvent * ev) override;
305 void dropEvent(QDropEvent * ev) override;
306 /// make sure we quit cleanly
307 void closeEvent(QCloseEvent * e) override;
309 void showEvent(QShowEvent *) override;
311 /// in order to catch Tab key press.
312 bool event(QEvent * e) override;
313 bool focusNextPrevChild(bool) override;
316 bool goToFileRow(std::string const & argument);
319 class GuiViewPrivate;
324 /// dialogs for this view
330 /// Hide all visible dialogs
331 void hideAll() const;
333 /// Update all visible dialogs.
335 * Check the status of all visible dialogs and disable or re-enable
336 * them as appropriate.
338 * Disabling is needed for example when a dialog is open and the
339 * cursor moves to a position where the corresponding inset is not
342 void updateDialogs();
344 /** Show dialog could be called from arbitrary threads.
345 \param name == "bibtex", "citation" etc; an identifier used to
346 launch a particular dialog.
347 \param data is a string representation of the Inset contents.
348 It is often little more than the output from Inset::write.
349 It is passed to, and parsed by, the frontend dialog.
350 Several of these dialogs do not need any data,
351 so it defaults to string().
352 \param inset ownership is _not_ passed to the frontend dialog.
353 It is stored internally and used by the kernel to ascertain
354 what to do with the FuncRequest dispatched from the frontend
355 dialog on 'Apply'; should it be used to create a new inset at
356 the current cursor position or modify an existing, 'open' inset?
358 void showDialog(std::string const & name,
359 std::string const & data, Inset * inset = 0) override;
361 /** \param name == "citation", "bibtex" etc; an identifier used
362 to reset the contents of a particular dialog with \param data.
363 See the comments to 'show', above.
365 void updateDialog(std::string const & name, std::string const & data) override;
367 /** All Dialogs of the given \param name will be closed if they are
368 connected to the given \param inset.
370 void hideDialog(std::string const & name, Inset * inset);
372 void disconnectDialog(std::string const & name);
374 bool develMode() const { return devel_mode_; }
376 void setCurrentZoom(int const v);
379 /// Saves the layout and geometry of the window
380 void saveLayout() const;
381 /// Saves the settings of toolbars and all dialogs
382 void saveUISettings() const;
384 bool restoreLayout();
386 GuiToolbar * toolbar(std::string const & name);
388 void constructToolbars();
392 void initToolbar(std::string const & name);
393 /// Update lock (all) toolbars position
394 void updateLockToolbars();
395 /// refill the toolbars (dark/light mode switch)
396 void refillToolbars();
398 bool lfunUiToggle(std::string const & ui_component);
400 void toggleFullScreen();
401 /// \return whether we did anything
402 bool insertLyXFile(docstring const & fname, bool ignorelang = false);
404 /// Open Export As ... dialog. \p iformat is the format the
405 /// filter is initially set to.
406 bool exportBufferAs(Buffer & b, docstring const & iformat);
411 LV_WRITE_AS_TEMPLATE,
415 /// Get a path for LFUN_BUFFER_WRITE_AS_TEMPLATE
416 std::string const getTemplatesPath(Buffer & buf);
417 /// Save a buffer as a new file.
419 * Write a buffer to a new file name and rename the buffer
420 * according to the new file name.
422 * This function is e.g. used by menu callbacks and
423 * LFUN_BUFFER_WRITE_AS.
425 * If 'newname' is empty, the user is asked via a
426 * dialog for the buffer's new name and location.
428 * If 'newname' is non-empty and has an absolute path, that is used.
429 * Otherwise the base directory of the buffer is used as the base
430 * for any relative path in 'newname'.
432 * \p kind controls what is done besides the pure renaming:
433 * LV_WRITE_AS => The buffer is written without version control actions.
434 * LV_VC_RENAME => The file is renamed in version control.
435 * LV_VC_COPY => The file is copied in version control.
437 bool renameBuffer(Buffer & b, docstring const & newname,
438 RenameKind kind = LV_WRITE_AS);
440 bool saveBuffer(Buffer & b);
441 /// save and rename buffer to fn. If fn is empty, the buffer
442 /// is just saved as the filename it already has.
443 bool saveBuffer(Buffer & b, support::FileName const & fn);
444 /// closes a workarea, if close_buffer is true the buffer will
445 /// also be released, otherwise the buffer will be hidden.
446 bool closeWorkArea(GuiWorkArea * wa, bool close_buffer);
447 /// closes the tabworkarea and all tabs. If we are in a close event,
448 /// all buffers will be closed, otherwise they will be hidden.
449 bool closeTabWorkArea(TabWorkArea * twa);
450 /// gives the user the possibility to save their work
451 /// or to discard the changes. If hiding is true, the
452 /// document will be reloaded.
453 bool saveBufferIfNeeded(Buffer & buf, bool hiding);
454 /// closes all workareas
455 bool closeWorkAreaAll();
456 /// write all open workareas into the session file
457 void writeSession() const;
458 /// is the buffer in this workarea also shown in another tab ?
459 /// This tab can either be in the same view or in another one.
460 bool inMultiTabs(GuiWorkArea * wa);
461 /// is the buffer shown in some other view ?
462 bool inOtherView(Buffer & buf);
464 enum NextOrPrevious {
469 void gotoNextOrPreviousBuffer(NextOrPrevious np, bool const move);
471 /// Is the dialog currently visible?
472 bool isDialogVisible(std::string const & name) const;
474 Dialog * findOrBuild(std::string const & name, bool hide_it);
476 Dialog * build(std::string const & name);
478 bool reloadBuffer(Buffer & buffer);
480 void dispatchVC(FuncRequest const & cmd, DispatchResult & dr);
482 void dispatchToBufferView(FuncRequest const & cmd, DispatchResult & dr);
489 /// flag to avoid two concurrent close events.
491 /// if the view is busy the cursor shouldn't blink for instance.
492 /// This counts the number of times more often we called
493 /// setBusy(true) compared to setBusy(false), so we can nest
494 /// functions that call setBusy;
497 /// Request to open the command toolbar if it is "auto"
498 bool command_execute_;
499 /// Request to give focus to minibuffer
500 bool minibuffer_focus_;
502 /// Statusbar widget that shows shell-escape status
503 QLabel * shell_escape_;
504 /// Statusbar widget that shows read-only status
506 /// Statusbar widget that shows version control status
507 QLabel * version_control_;
508 /// Statusbar widget that shows zoom value
509 QLabel * zoom_value_;
510 /// The zoom slider widget
511 QSlider * zoom_slider_;
512 /// Zoom in ("+") Button
513 QPushButton * zoom_in_;
514 /// Zoom out ("-") Button
515 QPushButton * zoom_out_;
517 /// The rate from which the actual zoom value is calculated
518 /// from the default zoom pref
519 double zoom_ratio_ = 1.0;
520 /// Minimum zoom percentage
521 static int const zoom_min_ = 10;
523 // movability flag of all toolbars
524 bool toolbarsMovable_;
531 class SEMenu : public QMenu
535 explicit SEMenu(QWidget * parent);
538 void showMenu(QPoint const &) { exec(QCursor::pos()); }
542 class PressableSvgWidget : public QSvgWidget
546 explicit PressableSvgWidget(const QString &file, QWidget * parent = nullptr)
547 : QSvgWidget(file, parent) {};
549 void mousePressEvent(QMouseEvent *event) override;
554 } // namespace frontend