]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiView.h
Pass the Buffer to GuiView::reloadBuffer. We always know which buffer we want to...
[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 - Qt4 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         int id() const { return id_; }
79
80         ///
81         void setBusy(bool);
82         /// are we busy ?
83         bool busy() const;
84
85         void saveCursorShapes();
86         void restoreCursorShapes();
87         void setCursorShapes(Qt::CursorShape shape);
88
89         /// \name Generic accessor functions
90         //@{
91         /// The current BufferView refers to the BufferView that has the focus,
92         /// including for example the one that is created when you use the
93         /// advanced search and replace pane.
94         /// \return the currently selected buffer view.
95         BufferView * currentBufferView();
96         BufferView const * currentBufferView() const;
97
98         /// The document BufferView always refers to the view's main document
99         /// BufferView. So, even if the BufferView in e.g., the advanced
100         /// search and replace pane has the focus.
101         /// \return the current document buffer view.
102         BufferView * documentBufferView();
103         BufferView const * documentBufferView() const;
104
105         void newDocument(std::string const & filename,
106                 bool fromTemplate);
107
108         /// display a message in the view
109         /// could be called from any thread
110         void message(docstring const &);
111         
112         bool getStatus(FuncRequest const & cmd, FuncStatus & flag);
113         /// dispatch command.
114         /// \return true if the \c FuncRequest has been dispatched.
115         void dispatch(FuncRequest const & cmd, DispatchResult & dr);
116
117         void restartCursor();
118         /// Update the completion popup and the inline completion state.
119         /// If \c start is true, then a new completion might be started.
120         /// If \c keep is true, an active completion will be kept active
121         /// even though the cursor moved. The update flags of \c cur might
122         /// be changed.
123         void updateCompletion(Cursor & cur, bool start, bool keep);
124
125         ///
126         void setFocus();
127
128         ///
129         void focusInEvent(QFocusEvent * e);
130         /// set a buffer to the current workarea.
131         void setBuffer(Buffer * b); ///< \c Buffer to set.
132
133         /// load a document into the current workarea.
134         Buffer * loadDocument(
135                 support::FileName const &  name, ///< File to load.
136                 bool tolastfiles = true  ///< append to the "Open recent" menu?
137                 );
138
139         /// add toolbar, if newline==true, add a toolbar break before the toolbar
140         GuiToolbar * makeToolbar(ToolbarInfo const & tbinfo, bool newline);
141         void updateStatusBar();
142
143         /// updates the possible layouts selectable
144         void updateLayoutList();
145         void updateToolbars();
146         QMenu * createPopupMenu();
147
148         ///
149         LayoutBox * getLayoutDialog() const;
150
151         /// hides the workarea and makes sure it is clean
152         bool hideWorkArea(GuiWorkArea * wa);
153         /// closes the workarea
154         bool closeWorkArea(GuiWorkArea * wa);
155         /// closes the buffer
156         bool closeBuffer(Buffer & buf);
157         ///
158         void openDocument(std::string const & filename);
159         ///
160         void importDocument(std::string const &);
161
162         /// \name GuiBufferDelegate.
163         //@{
164         void resetAutosaveTimers();
165         void errors(std::string const &, bool from_master = false);
166         void structureChanged();
167         void updateTocItem(std::string const &, DocIterator const &);
168         //@}
169
170         ///
171         TocModels & tocModels();
172         
173         /// called on timeout
174         void autoSave();
175
176         /// check for external change of any opened buffer, mainly for svn usage
177         void checkExternallyModifiedBuffers();
178
179         /** redraw \c inset in all the BufferViews in which it is currently
180          *  visible. If successful return a pointer to the owning Buffer.
181          */
182         Buffer const * updateInset(Inset const *);
183
184         /// \return the \c Workarea associated to \p  Buffer
185         /// \retval 0 if no \c WorkArea is found.
186         GuiWorkArea * workArea(Buffer & buffer);
187
188         /// Add a \c WorkArea 
189         /// \return the \c Workarea associated to \p  Buffer
190         /// \retval 0 if no \c WorkArea is found.
191         GuiWorkArea * addWorkArea(Buffer & buffer);
192         /// \param work_area The current \c WorkArea, or \c NULL
193         void setCurrentWorkArea(GuiWorkArea * work_area);
194         ///
195         void removeWorkArea(GuiWorkArea * work_area);
196         /// return the current WorkArea (the one that has the focus).
197         GuiWorkArea const * currentWorkArea() const;
198         /// return the current WorkArea (the one that has the focus).
199         GuiWorkArea * currentWorkArea();
200
201         /// return the current document WorkArea (it may not have the focus).
202         GuiWorkArea const * currentMainWorkArea() const;
203         /// return the current document WorkArea (it may not have the focus).
204         GuiWorkArea * currentMainWorkArea();
205
206 Q_SIGNALS:
207         void closing(int);
208         void triggerShowDialog(QString const & qname, QString const & qdata, Inset * inset);
209
210 public Q_SLOTS:
211         /// idle timeout.
212         /// clear any temporary message and replace with current status.
213         void clearMessage();
214
215 private Q_SLOTS:
216         ///
217         void updateWindowTitle(GuiWorkArea * wa);
218         ///
219         void resetWindowTitleAndIconText();
220
221         ///
222         void on_currentWorkAreaChanged(GuiWorkArea *);
223         ///
224         void on_lastWorkAreaRemoved();
225
226         /// slots to change the icon size
227         void smallSizedIcons();
228         void normalSizedIcons();
229         void bigSizedIcons();
230
231         /// For completion of autosave or export threads.
232         void processingThreadStarted();
233         void processingThreadFinished();
234         void indicateProcessing();
235
236         /// must be called in GUI thread
237         void doShowDialog(QString const & qname, QString const & qdata,
238         Inset * inset);
239
240         /// must be called from GUI thread
241         void updateStatusBarMessage(QString const & str);
242         void clearMessageText();
243
244 private:
245         /// Open given child document in current buffer directory.
246         void openChildDocument(std::string const & filename);
247         /// Close current document buffer.
248         bool closeBuffer();
249         /// Close all document buffers.
250         bool closeBufferAll();
251         ///
252         TabWorkArea * addTabWorkArea();
253
254         /// connect to signals in the given BufferView
255         void connectBufferView(BufferView & bv);
256         /// disconnect from signals in the given BufferView
257         void disconnectBufferView();
258         /// connect to signals in the given buffer
259         void connectBuffer(Buffer & buf);
260         /// disconnect from signals in the given buffer
261         void disconnectBuffer();
262         ///
263         void dragEnterEvent(QDragEnterEvent * ev);
264         ///
265         void dropEvent(QDropEvent * ev);
266         /// make sure we quit cleanly
267         void closeEvent(QCloseEvent * e);
268         ///
269         void showEvent(QShowEvent *);
270
271         /// in order to catch Tab key press.
272         bool event(QEvent * e);
273         bool focusNextPrevChild(bool);
274
275         ///
276         bool goToFileRow(std::string const & argument);
277
278         ///
279         struct GuiViewPrivate;
280         GuiViewPrivate & d;
281
282 public:
283         ///
284         /// dialogs for this view
285         ///
286
287         ///
288         void resetDialogs();
289
290         /// Hide all visible dialogs
291         void hideAll() const;
292
293         /// Update all visible dialogs.
294         /** 
295          *  Check the status of all visible dialogs and disable or reenable
296          *  them as appropriate.
297          *
298          *  Disabling is needed for example when a dialog is open and the
299          *  cursor moves to a position where the corresponding inset is not
300          *  allowed.
301          */
302         void updateDialogs();
303
304         /** Show dialog could be called from arbitrary threads.
305             \param name == "bibtex", "citation" etc; an identifier used to
306             launch a particular dialog.
307             \param data is a string representation of the Inset contents.
308             It is often little more than the output from Inset::write.
309             It is passed to, and parsed by, the frontend dialog.
310             Several of these dialogs do not need any data,
311             so it defaults to string().
312             \param inset ownership is _not_ passed to the frontend dialog.
313             It is stored internally and used by the kernel to ascertain
314             what to do with the FuncRequest dispatched from the frontend
315             dialog on 'Apply'; should it be used to create a new inset at
316             the current cursor position or modify an existing, 'open' inset?
317         */
318         void showDialog(std::string const & name,
319                 std::string const & data, Inset * inset = 0);
320
321         /** \param name == "citation", "bibtex" etc; an identifier used
322             to reset the contents of a particular dialog with \param data.
323             See the comments to 'show', above.
324         */
325         void updateDialog(std::string const & name, std::string const & data);
326
327         /** All Dialogs of the given \param name will be closed if they are
328             connected to the given \param inset.
329         */
330         void hideDialog(std::string const & name, Inset * inset);
331         ///
332         void disconnectDialog(std::string const & name);
333
334 private:
335         ///
336         void saveLayout() const;
337         ///
338         bool restoreLayout();
339         ///
340         GuiToolbar * toolbar(std::string const & name);
341         ///
342         void constructToolbars();
343         ///
344         void initToolbars();
345         ///
346         bool lfunUiToggle(std::string const & ui_component);
347         ///
348         void toggleFullScreen();
349         ///
350         void insertLyXFile(docstring const & fname);
351         ///
352         void insertPlaintextFile(docstring const & fname,
353                 bool asParagraph);
354
355         /// Save a buffer as a new file. 
356         /**
357         Write a buffer to a new file name and rename the buffer
358     according to the new file name.
359
360     This function is e.g. used by menu callbacks and
361     LFUN_BUFFER_WRITE_AS.
362
363     If 'newname' is empty, the user is asked via a
364     dialog for the buffer's new name and location.
365
366     If 'newname' is non-empty and has an absolute path, that is used.
367     Otherwise the base directory of the buffer is used as the base
368     for any relative path in 'newname'.
369         */
370         bool renameBuffer(Buffer & b, docstring const & newname);
371         ///
372         bool saveBuffer(Buffer & b);
373         /// closes a workarea, if close_buffer is true the buffer will
374         /// also be released, otherwise the buffer will be hidden.
375         bool closeWorkArea(GuiWorkArea * wa, bool close_buffer);
376         /// closes the tabworkarea and all tabs. If we are in a close event,
377         /// all buffers will be closed, otherwise they will be hidden.
378         bool closeTabWorkArea(TabWorkArea * twa);
379         /// gives the user the possibility to save his work 
380         /// or to discard the changes. If hiding is true, the
381         /// document will be reloaded.
382         bool saveBufferIfNeeded(Buffer & buf, bool hiding);
383         /// closes all workareas
384         bool closeWorkAreaAll();
385         /// write all open workareas into the session file
386         void writeSession() const;
387         /// is the buffer in this workarea also shown in another tab ?
388         /// This tab can either be in the same view or in another one.
389         bool inMultiTabs(GuiWorkArea * wa);
390         /// is the buffer in this workarea also shown in another view ?
391         bool inMultiViews(GuiWorkArea * wa);
392         ///
393         enum NextOrPrevious {
394                 NEXTBUFFER,
395                 PREVBUFFER
396         };
397         ///
398         void gotoNextOrPreviousBuffer(NextOrPrevious np);
399
400         /// Is the dialog currently visible?
401         bool isDialogVisible(std::string const & name) const;
402         ///
403         Dialog * findOrBuild(std::string const & name, bool hide_it);
404         ///
405         Dialog * build(std::string const & name);
406         ///
407         bool reloadBuffer(Buffer & buffer);
408         ///
409         void dispatchVC(FuncRequest const & cmd, DispatchResult & dr);
410         ///
411         void showMessage();
412
413         /// This view ID.
414         int id_;
415
416         /// flag to avoid two concurrent close events.
417         bool closing_;
418         /// if the view is busy the cursor shouldn't blink for instance.
419         bool busy_;
420
421 };
422
423 } // namespace frontend
424 } // namespace lyx
425
426 #endif // GUIVIEW_H