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