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