]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/Dialog.h
Pass the main window (GuiView) to the delimiter dialog so that it is properly handled...
[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         /// Main Window access.
201         /// This is unfortunately needed for the qt4 frontend and the \c
202         /// QDialogView framework. This permits to give a parent to the
203         /// constructed \c QDialog via a cast to \c GuiView.
204         LyXView * view() { return &parent_.kernel().lyxview(); }
205
206 protected:
207         /** \name Controller Access
208          *  Enable the derived classes to access the other parts of the whole.
209          */
210         //@{
211         Dialog & dialog() { return parent_; }
212         Dialog const & dialog() const { return parent_; }
213
214         Kernel & kernel() { return parent_.kernel(); }
215         Kernel const & kernel() const { return parent_.kernel(); }
216         //@}
217
218 private:
219         Dialog & parent_;
220 };
221
222
223 /** \c Dialog::View is an abstract base class to the View
224  *  of a Model-Controller-View split of a generic dialog.
225  */
226 class Dialog::View : boost::noncopyable {
227 public:
228         /** \param parent Dialog owning this Controller.
229          *  \param title  is the dialog title displayed by the WM.
230          */
231         View(Dialog & parent, lyx::docstring title);
232         virtual ~View() {}
233
234         /** \name Generic View
235          *  These few methods are all that a generic dialog needs of a
236          *  view.
237          */
238         //@{
239         /** A request to modify the data structures stored by the
240          *  accompanying Controller in preparation for their dispatch to
241          *  the LyX kernel.
242          */
243         virtual void apply() = 0;
244
245         /// Hide the dialog from sight
246         virtual void hide() = 0;
247
248         /// Redraw the dialog (e.g. if the colors have been remapped).
249         virtual void redraw() {}
250
251         /// Create the dialog if necessary, update it and display it.
252         virtual void show() = 0;
253
254         /// Update the display of the dialog whilst it is still visible.
255         virtual void update() = 0;
256
257         /// \return true if the dialog is visible.
258         virtual bool isVisible() const = 0;
259         //@}
260
261         /** Defaults to nothing. Can be used by the Controller, however, to
262          *  indicate to the View that something has changed and that the
263          *  dialog therefore needs updating.
264          *  \param id identifies what should be updated.
265          */
266         virtual void partialUpdate(int id);
267
268         /// sets the title of the dialog (window caption)
269         void setTitle(lyx::docstring const &);
270         /// gets the title of the dialog (window caption)
271         lyx::docstring const & getTitle() const;
272
273         /** \name View Access
274          *  Enable the derived classes to access the other parts of the whole.
275          */
276         //@{
277         Dialog & dialog() { return p_; }
278         Dialog const & dialog() const { return p_; }
279
280 protected:
281         Kernel & kernel() { return p_.kernel(); }
282         Kernel const & kernel() const { return p_.kernel(); }
283
284         Controller & getController() { return p_.controller(); }
285         Controller const & getController() const { return p_.controller(); }
286
287         ButtonController & bc() { return p_.bc(); }
288         ButtonController const & bc() const { return p_.bc(); }
289         //@}
290
291 private:
292         Dialog & p_;
293         lyx::docstring title_;
294 };
295
296 } // namespace frontend
297 } // namespace lyx
298
299 #endif // DIALOG_H