]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiView.h
Don't be so cute with VSpace: We just output it inline now for
[lyx.git] / src / frontends / qt4 / GuiView.h
1 // -*- C++ -*-
2 /**
3  * \file GuiView.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Lars Gullik Bjornes
8  * \author John Levon
9  * \author Abdelrazak Younes
10  * \author Peter Kümmel
11  *
12  * Full author contact details are available in file CREDITS.
13  */
14
15 #ifndef GUI_VIEW_H
16 #define GUI_VIEW_H
17
18 #include "frontends/Delegates.h"
19
20 #include "support/strfwd.h"
21
22 #include <QMainWindow>
23
24 class QCloseEvent;
25 class QDragEnterEvent;
26 class QDropEvent;
27 class QMenu;
28 class QShowEvent;
29
30
31 namespace lyx {
32
33 namespace support { class FileName; }
34
35 class Buffer;
36 class BufferView;
37 class Cursor;
38 class DispatchResult;
39 class FuncStatus;
40 class FuncRequest;
41 class Inset;
42
43 namespace frontend {
44
45 class Dialog;
46 class LayoutBox;
47 class GuiToolbar;
48 class GuiWorkArea;
49 class TabWorkArea;
50 class TocModels;
51 class ToolbarInfo;
52
53 /**
54  * GuiView - Qt main LyX window
55  *
56  * This class represents the main LyX window and provides
57  * accessor functions to its content.
58  *
59  * Note: a QObject emits a destroyed(QObject *) Qt signal when it
60  * is deleted. This might be useful for closing other dialogs
61  * depending on a given GuiView.
62  */
63 class GuiView : public QMainWindow, public GuiBufferViewDelegate,
64         public GuiBufferDelegate
65 {
66         Q_OBJECT
67
68 public:
69         /// create a main window of the given dimensions
70         GuiView(int id);
71
72         ~GuiView();
73
74         /// closes the view such that the view knows that is closed
75         /// programmatically and not by the user clicking the x.
76         bool closeScheduled();
77
78         /// Things that need to be done when the OSes session manager
79         /// requests a log out.
80         bool prepareAllBuffersForLogout();
81
82         int id() const { return id_; }
83
84         /// are we busy ?
85         bool busy() const;
86
87         /// Signal that the any "auto" minibuffer can be closed now.
88         void resetCommandExecute();
89
90         /// \name Generic accessor functions
91         //@{
92         /// The current BufferView refers to the BufferView that has the focus,
93         /// including for example the one that is created when you use the
94         /// advanced search and replace pane.
95         /// \return the currently selected buffer view.
96         BufferView * currentBufferView();
97         BufferView const * currentBufferView() const;
98
99         /// The document BufferView always refers to the view's main document
100         /// BufferView. So, even if the BufferView in e.g., the advanced
101         /// search and replace pane has the focus.
102         /// \return the current document buffer view.
103         BufferView * documentBufferView();
104         BufferView const * documentBufferView() const;
105
106         void newDocument(std::string const & filename,
107                 bool fromTemplate);
108
109         /// display a message in the view
110         /// could be called from any thread
111         void message(docstring const &);
112         
113         bool getStatus(FuncRequest const & cmd, FuncStatus & flag);
114         /// dispatch command.
115         /// \return true if the \c FuncRequest has been dispatched.
116         void dispatch(FuncRequest const & cmd, DispatchResult & dr);
117
118         void restartCursor();
119         /// Update the completion popup and the inline completion state.
120         /// If \c start is true, then a new completion might be started.
121         /// If \c keep is true, an active completion will be kept active
122         /// even though the cursor moved. The update flags of \c cur might
123         /// be changed.
124         void updateCompletion(Cursor & cur, bool start, bool keep);
125
126         ///
127         void setFocus();
128         bool hasFocus() const;
129
130         ///
131         void focusInEvent(QFocusEvent * e);
132         /// set a buffer to the current workarea.
133         void setBuffer(Buffer * b); ///< \c Buffer to set.
134
135         /// load a document into the current workarea.
136         Buffer * loadDocument(
137                 support::FileName const &  name, ///< File to load.
138                 bool tolastfiles = true  ///< append to the "Open recent" menu?
139                 );
140
141         /// add toolbar, if newline==true, add a toolbar break before the toolbar
142         GuiToolbar * makeToolbar(ToolbarInfo const & tbinfo, bool newline);
143         void updateStatusBar();
144
145         /// updates the possible layouts selectable
146         void updateLayoutList();
147         void updateToolbars();
148         QMenu * createPopupMenu();
149
150         ///
151         LayoutBox * getLayoutDialog() const;
152
153         /// hides the workarea and makes sure it is clean
154         bool hideWorkArea(GuiWorkArea * wa);
155         /// closes workarea; close buffer only if no other workareas point to it
156         bool closeWorkArea(GuiWorkArea * wa);
157         /// closes the buffer
158         bool closeBuffer(Buffer & buf);
159         ///
160         void openDocument(std::string const & filename);
161         ///
162         void importDocument(std::string const &);
163
164         /// \name GuiBufferDelegate.
165         //@{
166         void resetAutosaveTimers();
167         void errors(std::string const &, bool from_master = false);
168         void structureChanged();
169         void updateTocItem(std::string const &, DocIterator const &);
170         //@}
171
172         ///
173         TocModels & tocModels();
174         
175         /// called on timeout
176         void autoSave();
177
178         /// check for external change of any opened buffer, mainly for svn usage
179         void checkExternallyModifiedBuffers();
180
181         /** redraw \c inset in all the BufferViews in which it is currently
182          *  visible. If successful return a pointer to the owning Buffer.
183          */
184         Buffer const * updateInset(Inset const *);
185
186         /// \return the \c Workarea associated to \p  Buffer
187         /// \retval 0 if no \c WorkArea is found.
188         GuiWorkArea * workArea(Buffer & buffer);
189         /// \return the \c Workarea at index \c index
190         GuiWorkArea * workArea(int index);
191
192         /// Add a \c WorkArea 
193         /// \return the \c Workarea associated to \p  Buffer
194         /// \retval 0 if no \c WorkArea is found.
195         GuiWorkArea * addWorkArea(Buffer & buffer);
196         /// \param work_area The current \c WorkArea, or \c NULL
197         void setCurrentWorkArea(GuiWorkArea * work_area);
198         ///
199         void removeWorkArea(GuiWorkArea * work_area);
200         /// return the current WorkArea (the one that has the focus).
201         GuiWorkArea const * currentWorkArea() const;
202         /// return the current WorkArea (the one that has the focus).
203         GuiWorkArea * currentWorkArea();
204
205         /// return the current document WorkArea (it may not have the focus).
206         GuiWorkArea const * currentMainWorkArea() const;
207         /// return the current document WorkArea (it may not have the focus).
208         GuiWorkArea * currentMainWorkArea();
209         
210         /// Current ratio between physical pixels and device-independent pixels
211         double pixelRatio() const;
212
213 Q_SIGNALS:
214         void closing(int);
215         void triggerShowDialog(QString const & qname, QString const & qdata, Inset * inset);
216
217 public Q_SLOTS:
218         ///
219         void setBusy(bool);
220         /// idle timeout.
221         /// clear any temporary message and replace with current status.
222         void clearMessage();
223
224 private Q_SLOTS:
225         ///
226         void updateWindowTitle(GuiWorkArea * wa);
227         ///
228         void resetWindowTitleAndIconText();
229
230         ///
231         void on_currentWorkAreaChanged(GuiWorkArea *);
232         ///
233         void on_lastWorkAreaRemoved();
234
235         /// slots to change the icon size
236         void smallSizedIcons();
237         void normalSizedIcons();
238         void bigSizedIcons();
239         void hugeSizedIcons();
240         void giantSizedIcons();
241
242         /// For completion of autosave or export threads.
243         void processingThreadStarted();
244         void processingThreadFinished();
245         void autoSaveThreadFinished();
246
247         /// must be called in GUI thread
248         void doShowDialog(QString const & qname, QString const & qdata,
249         Inset * inset);
250
251         /// must be called from GUI thread
252         void updateStatusBarMessage(QString const & str);
253         void clearMessageText();
254
255 private:
256         /// Open given child document in current buffer directory.
257         void openChildDocument(std::string const & filename);
258         /// Close current document buffer.
259         bool closeBuffer();
260         /// Close all document buffers.
261         bool closeBufferAll();
262         ///
263         TabWorkArea * addTabWorkArea();
264
265         /// connect to signals in the given BufferView
266         void connectBufferView(BufferView & bv);
267         /// disconnect from signals in the given BufferView
268         void disconnectBufferView();
269         /// connect to signals in the given buffer
270         void connectBuffer(Buffer & buf);
271         /// disconnect from signals in the given buffer
272         void disconnectBuffer();
273         ///
274         void dragEnterEvent(QDragEnterEvent * ev);
275         ///
276         void dropEvent(QDropEvent * ev);
277         /// make sure we quit cleanly
278         void closeEvent(QCloseEvent * e);
279         ///
280         void showEvent(QShowEvent *);
281
282         /// in order to catch Tab key press.
283         bool event(QEvent * e);
284         bool focusNextPrevChild(bool);
285
286         ///
287         bool goToFileRow(std::string const & argument);
288
289         ///
290         class GuiViewPrivate;
291         GuiViewPrivate & d;
292
293 public:
294         ///
295         /// dialogs for this view
296         ///
297
298         ///
299         void resetDialogs();
300
301         /// Hide all visible dialogs
302         void hideAll() const;
303
304         /// Update all visible dialogs.
305         /** 
306          *  Check the status of all visible dialogs and disable or reenable
307          *  them as appropriate.
308          *
309          *  Disabling is needed for example when a dialog is open and the
310          *  cursor moves to a position where the corresponding inset is not
311          *  allowed.
312          */
313         void updateDialogs();
314
315         /** Show dialog could be called from arbitrary threads.
316             \param name == "bibtex", "citation" etc; an identifier used to
317             launch a particular dialog.
318             \param data is a string representation of the Inset contents.
319             It is often little more than the output from Inset::write.
320             It is passed to, and parsed by, the frontend dialog.
321             Several of these dialogs do not need any data,
322             so it defaults to string().
323             \param inset ownership is _not_ passed to the frontend dialog.
324             It is stored internally and used by the kernel to ascertain
325             what to do with the FuncRequest dispatched from the frontend
326             dialog on 'Apply'; should it be used to create a new inset at
327             the current cursor position or modify an existing, 'open' inset?
328         */
329         void showDialog(std::string const & name,
330                 std::string const & data, Inset * inset = 0);
331
332         /** \param name == "citation", "bibtex" etc; an identifier used
333             to reset the contents of a particular dialog with \param data.
334             See the comments to 'show', above.
335         */
336         void updateDialog(std::string const & name, std::string const & data);
337
338         /** All Dialogs of the given \param name will be closed if they are
339             connected to the given \param inset.
340         */
341         void hideDialog(std::string const & name, Inset * inset);
342         ///
343         void disconnectDialog(std::string const & name);
344
345 private:
346         /// Saves the layout and geometry of the window
347         void saveLayout() const;
348         /// Saves the settings of toolbars and all dialogs
349         void saveUISettings() const;
350         ///
351         bool restoreLayout();
352         ///
353         GuiToolbar * toolbar(std::string const & name);
354         ///
355         void constructToolbars();
356         ///
357         void initToolbars();
358         ///
359         void initToolbar(std::string const & name);
360         ///
361         bool lfunUiToggle(std::string const & ui_component);
362         ///
363         void toggleFullScreen();
364         ///
365         void insertLyXFile(docstring const & fname);
366         ///
367         /// Open Export As ... dialog. \p iformat is the format the
368         /// filter is initially set to.
369         bool exportBufferAs(Buffer & b, docstring const & iformat);
370
371         ///
372         enum RenameKind { LV_WRITE_AS, LV_VC_RENAME, LV_VC_COPY };
373         /// Save a buffer as a new file. 
374         /**
375         Write a buffer to a new file name and rename the buffer
376     according to the new file name.
377
378     This function is e.g. used by menu callbacks and
379     LFUN_BUFFER_WRITE_AS.
380
381     If 'newname' is empty, the user is asked via a
382     dialog for the buffer's new name and location.
383
384     If 'newname' is non-empty and has an absolute path, that is used.
385     Otherwise the base directory of the buffer is used as the base
386     for any relative path in 'newname'.
387
388          \p kind controls what is done besides the pure renaming:
389          * LV_WRITE_AS  => The buffer is written without version control actions.
390          * LV_VC_RENAME => The file is renamed in version control.
391          * LV_VC_COPY   => The file is copied in version control.
392          */
393         bool renameBuffer(Buffer & b, docstring const & newname,
394                           RenameKind kind = LV_WRITE_AS);
395         ///
396         bool saveBuffer(Buffer & b);
397         /// save and rename buffer to fn. If fn is empty, the buffer
398         /// is just saved as the filename it already has.
399         bool saveBuffer(Buffer & b, support::FileName const & fn);
400         /// closes a workarea, if close_buffer is true the buffer will
401         /// also be released, otherwise the buffer will be hidden.
402         bool closeWorkArea(GuiWorkArea * wa, bool close_buffer);
403         /// closes the tabworkarea and all tabs. If we are in a close event,
404         /// all buffers will be closed, otherwise they will be hidden.
405         bool closeTabWorkArea(TabWorkArea * twa);
406         /// gives the user the possibility to save his work 
407         /// or to discard the changes. If hiding is true, the
408         /// document will be reloaded.
409         bool saveBufferIfNeeded(Buffer & buf, bool hiding);
410         /// closes all workareas
411         bool closeWorkAreaAll();
412         /// write all open workareas into the session file
413         void writeSession() const;
414         /// is the buffer in this workarea also shown in another tab ?
415         /// This tab can either be in the same view or in another one.
416         bool inMultiTabs(GuiWorkArea * wa);
417         /// is the buffer shown in some other view ?
418         bool inOtherView(Buffer & buf);
419         ///
420         enum NextOrPrevious {
421                 NEXTBUFFER,
422                 PREVBUFFER
423         };
424         ///
425         void gotoNextOrPreviousBuffer(NextOrPrevious np, bool const move);
426
427         /// Is the dialog currently visible?
428         bool isDialogVisible(std::string const & name) const;
429         ///
430         Dialog * findOrBuild(std::string const & name, bool hide_it);
431         ///
432         Dialog * build(std::string const & name);
433         ///
434         bool reloadBuffer(Buffer & buffer);
435         ///
436         void dispatchVC(FuncRequest const & cmd, DispatchResult & dr);
437         ///
438         void dispatchToBufferView(FuncRequest const & cmd, DispatchResult & dr);
439         ///
440         void showMessage();
441
442         /// This view ID.
443         int id_;
444
445         /// flag to avoid two concurrent close events.
446         bool closing_;
447         /// if the view is busy the cursor shouldn't blink for instance.
448         /// This counts the number of times more often we called
449         /// setBusy(true) compared to setBusy(false), so we can nest
450         /// functions that call setBusy;
451         int busy_;
452
453         /// Request to open the command toolbar if it is "auto"
454         bool command_execute_;
455
456 };
457
458 } // namespace frontend
459 } // namespace lyx
460
461 #endif // GUIVIEW_H