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