]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/Dialog.h
fix memory leaks
[lyx.git] / src / frontends / qt4 / Dialog.h
1 // -*- C++ -*-
2 /**
3  * \file Dialog.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Angus Leeming
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #ifndef DIALOG_H
13 #define DIALOG_H
14
15 #include "lfuns.h"
16
17 #include "support/strfwd.h"
18
19 namespace lyx {
20
21 class Buffer;
22 class BufferView;
23 class FuncRequest;
24
25 namespace frontend {
26
27 class GuiView;
28
29 /** \enum KernelDocType used to flag the different kinds of buffer
30  *  without making the kernel header files available to the
31  *  dialog's Controller or View.
32  */
33 enum KernelDocType
34 {
35         LATEX,
36         LITERATE,
37         DOCBOOK
38 };
39
40
41 /** \c Dialog collects the different parts of a Model-Controller-View
42  *  split of a generic dialog together.
43  */
44 class Dialog 
45 {
46 public:
47         /// \param lv is the access point for the dialog to the LyX kernel.
48         /// \param name is the identifier given to the dialog by its parent
49         /// container.
50         Dialog(GuiView & lv, std::string const & name);
51
52         virtual ~Dialog();
53
54         /** \name Container Access
55          *  These methods are publicly accessible because they are invoked
56          *  by the parent container acting on commands from the LyX kernel.
57          */
58         //@{
59         /// \param data is a string encoding of the data to be displayed.
60         /// It is passed to the Controller to be translated into a useable form.
61         virtual void showData(std::string const & /*data*/) {}
62         virtual void updateData(std::string const & /*data*/) {}
63
64         virtual void hide() {}
65
66         //@}
67
68         /** Check whether we may apply our data.
69          *
70          *  The buttons are disabled if not and (re-)enabled if yes.
71          */
72         virtual void checkStatus() {}
73
74         /** When applying, it's useful to know whether the dialog is about
75          *  to close or not (no point refreshing the display for example).
76          */
77         virtual bool isClosing() const { return false; }
78
79
80         /** \c Button controller part
81          */
82         virtual void setButtonsValid(bool /*valid*/) {}
83
84
85         /** \c View part
86          *  of a Model-Controller-View split of a generic dialog.
87          *  These few methods are all that a generic dialog needs of a
88          *  view.
89          */
90         //@{
91         /** A request to modify the data structures stored by the
92          *  accompanying Controller in preparation for their dispatch to
93          *  the LyX kernel.
94          */
95         virtual void applyView() = 0;
96
97         /// Hide the dialog from sight
98         virtual void hideView() = 0;
99
100         /// Create the dialog if necessary, update it and display it.
101         virtual void showView() = 0;
102
103         /// Update the display of the dialog whilst it is still visible.
104         virtual void updateView() = 0;
105
106         /// \return true if the dialog is visible.
107         virtual bool isVisibleView() const = 0;
108         //@}
109
110         ///
111         std::string name() const;
112
113         //@{
114         /** Enable the controller to initialise its data structures.
115          *  \param data is a string encoding of the parameters to be displayed.
116          *  \return true if the translation was successful.
117          */
118         virtual bool initialiseParams(std::string const & data) = 0;
119
120         /// Enable the controller to clean up its data structures.
121         virtual void clearParams() = 0;
122
123         /// Enable the Controller to dispatch its data back to the LyX kernel.
124         virtual void dispatchParams() = 0;
125
126         /** \return true if the dialog should be shown only when
127          *  a buffer is open.
128          */
129         virtual bool isBufferDependent() const = 0;
130
131         /** \return true if the dialog can apply data also
132          *  for ReadOnly buffers.
133          *  This has to be distinguished from isBufferDependent()
134          */
135         virtual bool canApplyToReadOnly() const { return false; }
136
137         /** The lfun that is sent for applying the data.
138          *
139          * This method is used by the default implementation of canApply()
140          * for buffer dependent dialogs that send one lfun when applying the
141          * data.
142          * It should be used in dispatchParams(), too for consistency reasons.
143          *  \returns the lfun that is sent for applying the data.
144          */
145         virtual kb_action getLfun() const { return LFUN_INSET_APPLY; }
146
147         /** Check whether we may apply our data.
148          *
149          * The default implementation works for all dialogs that send one
150          * lfun when applying the data. Dialogs that send none or more than
151          * one lfun need to reimplement it.
152          *  \returns whether the data can be applied or not.
153          */
154         virtual bool canApply() const;
155
156         /** \return true if the kernel should disconnect the dialog from
157          *  a particular inset after the data has been applied to it.
158          *  Clearly this makes sense only for dialogs modifying the contents
159          *  of an inset :-)
160          *  In practise, only a very few dialogs (e.g. the citation dialog)
161          *  return true.
162          */
163         virtual bool disconnectOnApply() const { return false; }
164
165         /** \return true if Dialog::View::show() should not display the dialog
166          *   after running update. Currently, only ControlSpellchecker
167          *   makes use of that.
168         */
169         virtual bool exitEarly() const { return false; }
170         //@}
171
172         /** \c Kernel part: a wrapper making the LyX kernel available to the dialog.
173          * (Ie, it provides an interface to the Model part of the Model-Controller-
174          *  View split.
175          *  In an ideal world, it will shrink as more info is passed to the
176          *  Dialog::show() and Dialog::update() methods.
177          */
178
179
180         /** This method is the primary purpose of the class. It provides
181          *  the "gateway" by which the dialog can send a request (of a
182          *  change in the data, for more information) to the kernel.
183          *  \param fr is the encoding of the request.
184          */
185         void dispatch(FuncRequest const & fr) const;
186
187         /** The dialog has received a request from the user
188          *  (who pressed the "Restore" button) to update contents.
189          *  It must, therefore, ask the kernel to provide this information.
190          *  \param name is used to identify the dialog to the kernel.
191          */
192         void updateDialog() const;
193
194         /** A request from the Controller that future changes to the data
195          *  stored by the dialog are not applied to the inset currently
196          *  connected to the dialog. Instead, they will be used to generate
197          *  a new inset at the cursor position.
198          *  \param name is used to identify the dialog to the kernel.
199          */
200         void disconnect() const;
201
202         /** \name Kernel Wrappers
203          *  Simple wrapper functions to Buffer methods.
204          */
205         //@{
206         bool isBufferAvailable() const;
207         bool isBufferReadonly() const;
208         std::string const bufferFilepath() const;
209         //@}
210
211         /// The type of the current buffer.
212         KernelDocType docType() const;
213
214         /** \name Kernel Nasties
215          *  Unpleasantly public internals of the LyX kernel.
216          *  We should aim to reduce/remove these from the interface.
217          */
218         //@{
219         GuiView & lyxview() { return *lyxview_; }
220         GuiView const & lyxview() const { return *lyxview_; }
221
222         Buffer & buffer();
223         Buffer const & buffer() const;
224
225         BufferView * bufferview();
226         BufferView const * bufferview() const;
227         //@}
228
229 protected:
230         virtual void apply() {}
231
232 private:
233         /** The Dialog's name is the means by which a dialog identifies
234          *  itself to the LyXView.
235          */
236         char const * const name_;
237         ///
238         GuiView * lyxview_;
239
240         /// intentionally unimplemented, therefore uncopiable
241         Dialog(Dialog const &);
242         void operator=(Dialog const &);
243
244 };
245
246
247 } // namespace frontend
248 } // namespace lyx
249
250 #endif // DIALOG_H