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