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>
26 class QDragEnterEvent;
34 namespace support { class FileName; }
55 * GuiView - Qt main LyX window
57 * This class represents the main LyX window and provides
58 * accessor functions to its content.
60 * Note: a QObject emits a destroyed(QObject *) Qt signal when it
61 * is deleted. This might be useful for closing other dialogs
62 * depending on a given GuiView.
64 class GuiView : public QMainWindow, public GuiBufferViewDelegate,
65 public GuiBufferDelegate
70 /// create a main window of the given dimensions
75 /// closes the view such that the view knows that is closed
76 /// programmatically and not by the user clicking the x.
77 bool closeScheduled();
79 /// Things that need to be done when the OSes session manager
80 /// requests a log out.
81 bool prepareAllBuffersForLogout();
83 int id() const { return id_; }
88 /// Signal that the any "auto" minibuffer can be closed now.
89 void resetCommandExecute();
91 /// \name Generic accessor functions
93 /// The current BufferView refers to the BufferView that has the focus,
94 /// including for example the one that is created when you use the
95 /// advanced search and replace pane.
96 /// \return the currently selected buffer view.
97 BufferView * currentBufferView();
98 BufferView const * currentBufferView() const;
100 /// The document BufferView always refers to the view's main document
101 /// BufferView. So, even if the BufferView in e.g., the advanced
102 /// search and replace pane has the focus.
103 /// \return the current document buffer view.
104 BufferView * documentBufferView();
105 BufferView const * documentBufferView() const;
107 void newDocument(std::string const & filename,
110 /// display a message in the view
111 /// could be called from any thread
112 void message(docstring const &);
114 bool getStatus(FuncRequest const & cmd, FuncStatus & flag);
115 /// dispatch command.
116 /// \return true if the \c FuncRequest has been dispatched.
117 void dispatch(FuncRequest const & cmd, DispatchResult & dr);
120 /// Update the completion popup and the inline completion state.
121 /// If \c start is true, then a new completion might be started.
122 /// If \c keep is true, an active completion will be kept active
123 /// even though the cursor moved. The update flags of \c cur might
125 void updateCompletion(Cursor & cur, bool start, bool keep);
129 bool hasFocus() const;
132 void focusInEvent(QFocusEvent * e);
133 /// Add a Buffer to the View
134 /// \param b Buffer to set.
135 /// \param switch_to Whether to set it to the current workarea.
136 void setBuffer(Buffer * b, bool switch_to = true);
138 /// load a document into the current workarea.
139 Buffer * loadDocument(
140 support::FileName const & name, ///< File to load.
141 bool tolastfiles = true ///< append to the "Open recent" menu?
144 /// add toolbar, if newline==true, add a toolbar break before the toolbar
145 GuiToolbar * makeToolbar(ToolbarInfo const & tbinfo, bool newline);
146 void updateStatusBar();
148 /// updates the possible layouts selectable
149 void updateLayoutList();
150 void updateToolbars();
153 LayoutBox * getLayoutDialog() const;
155 /// hides the workarea and makes sure it is clean
156 bool hideWorkArea(GuiWorkArea * wa);
157 /// closes workarea; close buffer only if no other workareas point to it
158 bool closeWorkArea(GuiWorkArea * wa);
159 /// closes the buffer
160 bool closeBuffer(Buffer & buf);
162 void openDocument(std::string const & filename);
164 void importDocument(std::string const &);
166 /// \name GuiBufferDelegate.
168 void resetAutosaveTimers();
169 void errors(std::string const &, bool from_master = false);
170 void structureChanged();
171 void updateTocItem(std::string const &, DocIterator const &);
175 TocModels & tocModels();
177 /// called on timeout
180 /// check for external change of any opened buffer, mainly for svn usage
181 void checkExternallyModifiedBuffers();
183 /** redraw \c inset in all the BufferViews in which it is currently
184 * visible. If successful return a pointer to the owning Buffer.
186 Buffer const * updateInset(Inset const *);
188 /// \return the \c Workarea associated to \p Buffer
189 /// \retval 0 if no \c WorkArea is found.
190 GuiWorkArea * workArea(Buffer & buffer);
191 /// \return the \c Workarea at index \c index
192 GuiWorkArea * workArea(int index);
194 /// Add a \c WorkArea
195 /// \return the \c Workarea associated to \p Buffer
196 /// \retval 0 if no \c WorkArea is found.
197 GuiWorkArea * addWorkArea(Buffer & buffer);
198 /// \param work_area The current \c WorkArea, or \c NULL
199 void setCurrentWorkArea(GuiWorkArea * work_area);
201 void removeWorkArea(GuiWorkArea * work_area);
202 /// return the current WorkArea (the one that has the focus).
203 GuiWorkArea const * currentWorkArea() const;
204 /// return the current WorkArea (the one that has the focus).
205 GuiWorkArea * currentWorkArea();
207 /// return the current document WorkArea (it may not have the focus).
208 GuiWorkArea const * currentMainWorkArea() const;
209 /// return the current document WorkArea (it may not have the focus).
210 GuiWorkArea * currentMainWorkArea();
212 /// Current ratio between physical pixels and device-independent pixels
213 double pixelRatio() const;
217 void triggerShowDialog(QString const & qname, QString const & qdata, Inset * inset);
218 // emitted when the work area or its buffer view changed
219 void bufferViewChanged();
225 /// clear any temporary message and replace with current status.
228 void updateWindowTitle(GuiWorkArea * wa);
230 void disableShellEscape();
234 void resetWindowTitle();
237 void on_currentWorkAreaChanged(GuiWorkArea *);
239 void onBufferViewChanged();
241 void on_lastWorkAreaRemoved();
243 /// For completion of autosave or export threads.
244 void processingThreadStarted();
245 void processingThreadFinished();
246 void autoSaveThreadFinished();
248 /// must be called in GUI thread
249 void doShowDialog(QString const & qname, QString const & qdata,
252 /// must be called from GUI thread
253 void updateStatusBarMessage(QString const & str);
254 void clearMessageText();
257 void toolBarPopup(const QPoint &pos);
260 /// Open given child document in current buffer directory.
261 void openChildDocument(std::string const & filename);
262 /// Close current document buffer.
264 /// Close all document buffers.
265 bool closeBufferAll();
267 TabWorkArea * addTabWorkArea();
269 /// connect to signals in the given BufferView
270 void connectBufferView(BufferView & bv);
271 /// disconnect from signals in the given BufferView
272 void disconnectBufferView();
273 /// connect to signals in the given buffer
274 void connectBuffer(Buffer & buf);
275 /// disconnect from signals in the given buffer
276 void disconnectBuffer();
278 void dragEnterEvent(QDragEnterEvent * ev);
280 void dropEvent(QDropEvent * ev);
281 /// make sure we quit cleanly
282 void closeEvent(QCloseEvent * e);
284 void showEvent(QShowEvent *);
286 /// in order to catch Tab key press.
287 bool event(QEvent * e);
288 bool focusNextPrevChild(bool);
291 bool goToFileRow(std::string const & argument);
294 class GuiViewPrivate;
299 /// dialogs for this view
305 /// Hide all visible dialogs
306 void hideAll() const;
308 /// Update all visible dialogs.
310 * Check the status of all visible dialogs and disable or reenable
311 * them as appropriate.
313 * Disabling is needed for example when a dialog is open and the
314 * cursor moves to a position where the corresponding inset is not
317 void updateDialogs();
319 /** Show dialog could be called from arbitrary threads.
320 \param name == "bibtex", "citation" etc; an identifier used to
321 launch a particular dialog.
322 \param data is a string representation of the Inset contents.
323 It is often little more than the output from Inset::write.
324 It is passed to, and parsed by, the frontend dialog.
325 Several of these dialogs do not need any data,
326 so it defaults to string().
327 \param inset ownership is _not_ passed to the frontend dialog.
328 It is stored internally and used by the kernel to ascertain
329 what to do with the FuncRequest dispatched from the frontend
330 dialog on 'Apply'; should it be used to create a new inset at
331 the current cursor position or modify an existing, 'open' inset?
333 void showDialog(std::string const & name,
334 std::string const & data, Inset * inset = 0);
336 /** \param name == "citation", "bibtex" etc; an identifier used
337 to reset the contents of a particular dialog with \param data.
338 See the comments to 'show', above.
340 void updateDialog(std::string const & name, std::string const & data);
342 /** All Dialogs of the given \param name will be closed if they are
343 connected to the given \param inset.
345 void hideDialog(std::string const & name, Inset * inset);
347 void disconnectDialog(std::string const & name);
349 bool develMode() const { return devel_mode_; }
352 /// Saves the layout and geometry of the window
353 void saveLayout() const;
354 /// Saves the settings of toolbars and all dialogs
355 void saveUISettings() const;
357 bool restoreLayout();
359 GuiToolbar * toolbar(std::string const & name);
361 void constructToolbars();
365 void initToolbar(std::string const & name);
366 /// Update lock (all) toolbars position
367 void updateLockToolbars();
369 bool lfunUiToggle(std::string const & ui_component);
371 void toggleFullScreen();
373 void insertLyXFile(docstring const & fname);
375 /// Open Export As ... dialog. \p iformat is the format the
376 /// filter is initially set to.
377 bool exportBufferAs(Buffer & b, docstring const & iformat);
380 enum RenameKind { LV_WRITE_AS, LV_VC_RENAME, LV_VC_COPY };
381 /// Save a buffer as a new file.
383 Write a buffer to a new file name and rename the buffer
384 according to the new file name.
386 This function is e.g. used by menu callbacks and
387 LFUN_BUFFER_WRITE_AS.
389 If 'newname' is empty, the user is asked via a
390 dialog for the buffer's new name and location.
392 If 'newname' is non-empty and has an absolute path, that is used.
393 Otherwise the base directory of the buffer is used as the base
394 for any relative path in 'newname'.
396 \p kind controls what is done besides the pure renaming:
397 * LV_WRITE_AS => The buffer is written without version control actions.
398 * LV_VC_RENAME => The file is renamed in version control.
399 * LV_VC_COPY => The file is copied in version control.
401 bool renameBuffer(Buffer & b, docstring const & newname,
402 RenameKind kind = LV_WRITE_AS);
404 bool saveBuffer(Buffer & b);
405 /// save and rename buffer to fn. If fn is empty, the buffer
406 /// is just saved as the filename it already has.
407 bool saveBuffer(Buffer & b, support::FileName const & fn);
408 /// closes a workarea, if close_buffer is true the buffer will
409 /// also be released, otherwise the buffer will be hidden.
410 bool closeWorkArea(GuiWorkArea * wa, bool close_buffer);
411 /// closes the tabworkarea and all tabs. If we are in a close event,
412 /// all buffers will be closed, otherwise they will be hidden.
413 bool closeTabWorkArea(TabWorkArea * twa);
414 /// gives the user the possibility to save his work
415 /// or to discard the changes. If hiding is true, the
416 /// document will be reloaded.
417 bool saveBufferIfNeeded(Buffer & buf, bool hiding);
418 /// closes all workareas
419 bool closeWorkAreaAll();
420 /// write all open workareas into the session file
421 void writeSession() const;
422 /// is the buffer in this workarea also shown in another tab ?
423 /// This tab can either be in the same view or in another one.
424 bool inMultiTabs(GuiWorkArea * wa);
425 /// is the buffer shown in some other view ?
426 bool inOtherView(Buffer & buf);
428 enum NextOrPrevious {
433 void gotoNextOrPreviousBuffer(NextOrPrevious np, bool const move);
435 /// Is the dialog currently visible?
436 bool isDialogVisible(std::string const & name) const;
438 Dialog * findOrBuild(std::string const & name, bool hide_it);
440 Dialog * build(std::string const & name);
442 bool reloadBuffer(Buffer & buffer);
444 void dispatchVC(FuncRequest const & cmd, DispatchResult & dr);
446 void dispatchToBufferView(FuncRequest const & cmd, DispatchResult & dr);
453 /// flag to avoid two concurrent close events.
455 /// if the view is busy the cursor shouldn't blink for instance.
456 /// This counts the number of times more often we called
457 /// setBusy(true) compared to setBusy(false), so we can nest
458 /// functions that call setBusy;
461 /// Request to open the command toolbar if it is "auto"
462 bool command_execute_;
463 /// Request to give focus to minibuffer
464 bool minibuffer_focus_;
466 /// Statusbar widget that shows shell-escape status
467 QLabel * shell_escape_;
468 /// Statusbar widget that shows read-only status
470 /// Statusbar widget that shows version control status
471 QLabel * version_control_;
473 /// The rate from which the actual zoom value is calculated
474 /// from the default zoom pref
475 double zoom_ratio_ = 1.0;
476 /// Minimum zoom percentage
477 static int const zoom_min_ = 10;
479 // movability flag of all toolbars
480 bool toolbarsMovable_;
487 class SEMenu : public QMenu
491 explicit SEMenu(QWidget * parent);
494 void showMenu(QPoint const &) { exec(QCursor::pos()); }
497 } // namespace frontend