]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/Dialog.h
* src/frontend/controllers/frontend_helpers.cpp: safety fix suggested by Andre
[lyx.git] / src / frontends / controllers / 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 "Kernel.h"
16 #include "lfuns.h"
17
18 #include "support/docstring.h"
19
20 #include <boost/utility.hpp>
21 #include <boost/scoped_ptr.hpp>
22
23 namespace lyx {
24
25 class LyXView;
26
27 namespace frontend {
28
29 class ButtonController;
30
31 /** \c Dialog collects the different parts of a Model-Controller-View
32  *  split of a generic dialog together.
33  */
34 class Dialog /*: boost::noncopyable*/ {
35 public:
36         /// \param lv is the access point for the dialog to the LyX kernel.
37         /// \param name is the identifier given to the dialog by its parent
38         /// container.
39         Dialog(LyXView & lv, std::string const & name);
40
41         /** The Dialog's name is the means by which a dialog identifies
42          *  itself to the kernel.
43          */
44         std::string const & name() const { return name_; }
45
46         /** \name Buttons
47          *  These methods are publicly accessible because they are invoked
48          *  by the View when the user presses... guess what ;-)
49          */
50         //@{
51         void ApplyButton();
52         void OKButton();
53         void CancelButton();
54         void RestoreButton();
55         //@}
56
57         /** \name Container Access
58          *  These methods are publicly accessible because they are invoked
59          *  by the parent container acting on commands from the LyX kernel.
60          */
61         //@{
62         /// \param data is a string encoding of the data to be displayed.
63         /// It is passed to the Controller to be translated into a useable form.
64         void show(std::string const & data);
65         void update(std::string const & data);
66
67         void hide();
68         bool isVisible() const;
69
70         /** This function is called, for example, if the GUI colours
71          *  have been changed.
72          */
73         void redraw();
74         //@}
75
76         /** Check whether we may apply our data.
77          *
78          *  The buttons are disabled if not and (re-)enabled if yes.
79          */
80         void checkStatus();
81
82         /** When applying, it's useful to know whether the dialog is about
83          *  to close or not (no point refreshing the display for example).
84          */
85         bool isClosing() const { return is_closing_; }
86
87         /** The LyX kernel is made available through this wrapper class.
88          *  In an ideal world, it will shrink as more info is passed to the
89          *  show() and update() methods.
90          */
91         Kernel & kernel() { return kernel_; }
92
93         /** Different dialogs will have different Controllers and Views.
94          *  deriving from these base classes.
95          */
96         //@{
97         class Controller;
98         class View;
99         //@}
100
101         /** \name Dialog Specialization
102          *  Methods to set the Controller and View and so specialise
103          *  to a particular dialog.
104          */
105         //@{
106         /// \param ptr is stored and destroyed by \c Dialog.
107         void setController(Controller * ptr);
108         /// \param ptr is stored and destroyed by \c Dialog.
109         void setView(View * ptr);
110         //@}
111
112         /** \name Dialog Components
113          *  Methods to access the various components making up a dialog.
114          */
115         //@{
116         Controller & controller() const;
117         ButtonController & bc() const;
118         View & view() const;
119         //@}
120
121 private:
122         void apply();
123
124         bool is_closing_;
125         Kernel kernel_;
126         std::string name_;
127         boost::scoped_ptr<ButtonController> bc_ptr_;
128         boost::scoped_ptr<Controller> controller_ptr_;
129         boost::scoped_ptr<View> view_ptr_;
130 };
131
132
133 /** \c Dialog::Controller is an abstract base class for the Controller
134  *  of a Model-Controller-View split of a generic dialog.
135  */
136 class Dialog::Controller : boost::noncopyable {
137 public:
138         /// \param parent Dialog owning this Controller.
139         Controller(Dialog & parent);
140         virtual ~Controller() {}
141
142         /** \name Generic Controller
143          *  These few methods are all that a generic dialog needs of a
144          *  controller.
145          */
146         //@{
147         /** Enable the controller to initialise its data structures.
148          *  \param data is a string encoding of the parameters to be displayed.
149          *  \return true if the translation was successful.
150          */
151         virtual bool initialiseParams(std::string const & data) = 0;
152
153         /// Enable the controller to clean up its data structures.
154         virtual void clearParams() = 0;
155
156         /// Enable the Controller to dispatch its data back to the LyX kernel.
157         virtual void dispatchParams() = 0;
158
159         /** \return true if the dialog should be shown only when
160          *  a buffer is open.
161          */
162         virtual bool isBufferDependent() const = 0;
163
164         /** \return true if the dialog can apply data also
165          *  for ReadOnly buffers.
166          *  This has to be distinguished from isBufferDependent()
167          */
168         virtual bool canApplyToReadOnly() const { return false; }
169
170         /** The lfun that is sent for applying the data.
171          *
172          * This method is used by the default implementation of canApply()
173          * for buffer dependent dialogs that send one lfun when applying the
174          * data.
175          * It should be used in dispatchParams(), too for consistency reasons.
176          *  \returns the lfun that is sent for applying the data.
177          */
178         virtual kb_action getLfun() const { return LFUN_INSET_APPLY; }
179
180         /** Check whether we may apply our data.
181          *
182          * The default implementation works for all dialogs that send one
183          * lfun when applying the data. Dialogs that send none or more than
184          * one lfun need to reimplement it.
185          *  \returns whether the data can be applied or not.
186          */
187         virtual bool canApply() const;
188
189         /** \return true if the kernel should disconnect the dialog from
190          *  a particular inset after the data has been applied to it.
191          *  Clearly this makes sense only for dialogs modifying the contents
192          *  of an inset :-)
193          *  In practise, only a very few dialogs (e.g. the citation dialog)
194          *  return true.
195          */
196         virtual bool disconnectOnApply() const { return false; }
197
198         /** \return true if Dialog::View::show() should not display the dialog
199          *   after running update. Currently, only ControlSpellchecker
200          *   makes use of that.
201         */
202         virtual bool exitEarly() const { return false; }
203         //@}
204
205         /// Main Window access.
206         /// This is unfortunately needed for the qt4 frontend and the \c
207         /// QDialogView framework. This permits to give a parent to the
208         /// constructed \c QDialog via a cast to \c GuiView.
209         LyXView * view() { return &parent_.kernel().lyxview(); }
210
211 protected:
212         /** \name Controller Access
213          *  Enable the derived classes to access the other parts of the whole.
214          */
215         //@{
216         Dialog & dialog() { return parent_; }
217         Dialog const & dialog() const { return parent_; }
218
219         Kernel & kernel() { return parent_.kernel(); }
220         Kernel const & kernel() const { return parent_.kernel(); }
221         //@}
222
223 private:
224         Dialog & parent_;
225 };
226
227
228 /** \c Dialog::View is an abstract base class to the View
229  *  of a Model-Controller-View split of a generic dialog.
230  */
231 class Dialog::View : boost::noncopyable {
232 public:
233         /** \param parent Dialog owning this Controller.
234          *  \param title  is the dialog title displayed by the WM.
235          */
236         View(Dialog & parent, docstring title);
237         virtual ~View() {}
238
239         /** \name Generic View
240          *  These few methods are all that a generic dialog needs of a
241          *  view.
242          */
243         //@{
244         /** A request to modify the data structures stored by the
245          *  accompanying Controller in preparation for their dispatch to
246          *  the LyX kernel.
247          */
248         virtual void apply() = 0;
249
250         /// Hide the dialog from sight
251         virtual void hide() = 0;
252
253         /// Redraw the dialog (e.g. if the colors have been remapped).
254         virtual void redraw() {}
255
256         /// Create the dialog if necessary, update it and display it.
257         virtual void show() = 0;
258
259         /// Update the display of the dialog whilst it is still visible.
260         virtual void update() = 0;
261
262         /// \return true if the dialog is visible.
263         virtual bool isVisible() const = 0;
264         //@}
265
266         /** Defaults to nothing. Can be used by the Controller, however, to
267          *  indicate to the View that something has changed and that the
268          *  dialog therefore needs updating.
269          *  \param id identifies what should be updated.
270          */
271         virtual void partialUpdate(int id);
272
273         /// sets the title of the dialog (window caption)
274         void setTitle(docstring const &);
275         /// gets the title of the dialog (window caption)
276         docstring const & getTitle() const;
277
278         /** \name View Access
279          *  Enable the derived classes to access the other parts of the whole.
280          */
281         //@{
282         Dialog & dialog() { return p_; }
283         Dialog const & dialog() const { return p_; }
284
285 protected:
286         Kernel & kernel() { return p_.kernel(); }
287         Kernel const & kernel() const { return p_.kernel(); }
288
289         Controller & getController() { return p_.controller(); }
290         Controller const & getController() const { return p_.controller(); }
291
292         ButtonController & bc() { return p_.bc(); }
293         ButtonController const & bc() const { return p_.bc(); }
294         //@}
295
296 private:
297         Dialog & p_;
298         docstring title_;
299 };
300
301 } // namespace frontend
302 } // namespace lyx
303
304 #endif // DIALOG_H