]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/Dialog.h
rename getExtFromContents() to getFormatFromContents()
[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         /** 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         /** Different dialogs will have different Controllers and Views.
85          *  deriving from these base classes.
86          */
87         //@{
88         class Controller;
89         class View;
90         //@}
91
92         /** \name Dialog Specialization
93          *  Methods to set the Controller and View and so specialise
94          *  to a particular dialog.
95          */
96         //@{
97         /// \param ptr is stored and destroyed by \c Dialog.
98         void setController(Controller * ptr);
99         /// \param ptr is stored and destroyed by \c Dialog.
100         void setView(View * ptr);
101         //@}
102
103         /** \name Dialog Components
104          *  Methods to access the various components making up a dialog.
105          */
106         //@{
107         Controller & controller() const;
108         ButtonController & bc() const;
109         View & view() const;
110         //@}
111
112 private:
113         void apply();
114
115         bool is_closing_;
116         Kernel kernel_;
117         std::string name_;
118         boost::scoped_ptr<ButtonController> bc_ptr_;
119         boost::scoped_ptr<Controller> controller_ptr_;
120         boost::scoped_ptr<View> view_ptr_;
121 };
122
123
124 /** \c Dialog::Controller is an abstract base class for the Controller
125  *  of a Model-Controller-View split of a generic dialog.
126  */
127 class Dialog::Controller : boost::noncopyable {
128 public:
129         /// \param parent Dialog owning this Controller.
130         Controller(Dialog & parent);
131         virtual ~Controller() {}
132
133         /** \name Generic Controller
134          *  These few methods are all that a generic dialog needs of a
135          *  controller.
136          */
137         //@{
138         /** Enable the controller to initialise its data structures.
139          *  \param data is a string encoding of the parameters to be displayed.
140          *  \return true if the translation was successful.
141          */
142         virtual bool initialiseParams(std::string const & data) = 0;
143
144         /// Enable the controller to clean up its data structures.
145         virtual void clearParams() = 0;
146
147         /// Enable the Controller to dispatch its data back to the LyX kernel.
148         virtual void dispatchParams() = 0;
149
150         /** \return true if the dialog should be shown only when
151          *  a buffer is open.
152          */
153         virtual bool isBufferDependent() const = 0;
154
155         /** \return true if the kernel should disconnect the dialog from
156          *  a particular inset after the data has been applied to it.
157          *  Clearly this makes sense only for dialogs modifying the contents
158          *  of an inset :-)
159          *  In practise, only a very few dialogs (e.g. the citation dialog)
160          *  return true.
161          */
162         virtual bool disconnectOnApply() const { return false; }
163         //@}
164
165 protected:
166         /** \name Controller Access
167          *  Enable the derived classes to access the other parts of the whole.
168          */
169         //@{
170         Dialog & dialog() { return parent_; }
171         Dialog const & dialog() const { return parent_; }
172
173         Kernel & kernel() { return parent_.kernel(); }
174         Kernel const & kernel() const { return parent_.kernel(); }
175         //@}
176
177 private:
178         Dialog & parent_;
179 };
180
181
182 /** \c Dialog::View is an abstract base class to the View
183  *  of a Model-Controller-View split of a generic dialog.
184  */
185 class Dialog::View : boost::noncopyable {
186 public:
187         /** \param parent Dialog owning this Controller.
188          *  \param title  is the dialog title displayed by the WM.
189          */
190         View(Dialog & parent, std::string title);
191         virtual ~View() {}
192
193         /** \name Generic View
194          *  These few methods are all that a generic dialog needs of a
195          *  view.
196          */
197         //@{
198         /** A request to modify the data structures stored by the
199          *  accompanying Controller in preparation for their dispatch to
200          *  the LyX kernel.
201          */
202         virtual void apply() = 0;
203
204         /// Hide the dialog from sight
205         virtual void hide() = 0;
206
207         /// Redraw the dialog (e.g. if the colors have been remapped).
208         virtual void redraw() {}
209
210         /// Create the dialog if necessary, update it and display it.
211         virtual void show() = 0;
212
213         /// Update the display of the dialog whilst it is still visible.
214         virtual void update() = 0;
215
216         /// \return true if the dialog is visible.
217         virtual bool isVisible() const = 0;
218         //@}
219
220         /** Defaults to nothing. Can be used by the Controller, however, to
221          *  indicate to the View that something has changed and that the
222          *  dialog therefore needs updating.
223          *  \param id identifies what should be updated.
224          */
225         virtual void partialUpdate(int id);
226
227         /// sets the title of the dialog (window caption)
228         void setTitle(std::string const &);
229         /// gets the title of the dialog (window caption)
230         std::string const & getTitle() const;
231
232         /** \name View Access
233          *  Enable the derived classes to access the other parts of the whole.
234          */
235         //@{
236         Dialog & dialog() { return p_; }
237         Dialog const & dialog() const { return p_; }
238
239 protected:
240         Kernel & kernel() { return p_.kernel(); }
241         Kernel const & kernel() const { return p_.kernel(); }
242
243         Controller & getController() { return p_.controller(); }
244         Controller const & getController() const { return p_.controller(); }
245
246         ButtonController & bc() { return p_.bc(); }
247         ButtonController const & bc() const { return p_.bc(); }
248         //@}
249
250 private:
251         Dialog & p_;
252         std::string title_;
253 };
254
255 } // namespace frontend
256 } // namespace lyx
257
258 #endif // DIALOG_H