]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/ControlInset.h
floatlist work + handle lyxfuncs with args.
[lyx.git] / src / frontends / controllers / ControlInset.h
1 /* This file is part of
2  * ====================================================== 
3  *
4  *           LyX, The Document Processor
5  *
6  *           Copyright 2001 The LyX Team.
7  *
8  * ======================================================
9  *
10  * \file ControlInsets.h
11  * \author Angus Leeming <a.leeming@ic.ac.uk>
12  *
13  * ControlInset is to be used as a parent class for dialogs that display and
14  * can perhaps modify the contents of an individual inset. An example being the
15  * ubiquitous Citation dialog.
16  */
17
18 #ifndef CONTROLINSET_H
19 #define CONTROLINSET_H
20
21 #include "support/LAssert.h"
22 #include "ControlConnections.h"
23
24 class Inset;
25
26 template <class Inset, class Params>
27 class ControlInset : public ControlConnectBD
28 {
29 public:
30         ///
31         ControlInset(LyXView &, Dialogs &);
32         /// Allow the View access to the local copy.
33         Params & params() const;
34
35 protected:
36         /// Slots connected in the daughter classes c-tor.
37         /// Slot launching dialog to (possibly) create a new inset.
38         void createInset(string const &);
39         /// Slot launching dialog to an existing inset.
40         void showInset(Inset *);
41         /// Allow the daughter methods to access the inset.
42         Inset * inset() const;
43
44 private:
45         /** These 6 methods are all that the individual daughter classes
46             should need to instantiate. */
47
48         /// if the inset exists then do this...
49         virtual void applyParamsToInset() = 0;
50         /// else this...
51         virtual void applyParamsNoInset() = 0;
52
53         /// get the parameters from the string passed to createInset.
54         virtual Params const getParams(string const &) = 0;
55         /// get the parameters from the inset passed to showInset.
56         virtual Params const getParams(Inset const &) = 0;
57
58         /** Most derived classes won't need these two, so they default to empty.
59          */
60
61         /// set any daughter class-particular data on show().
62         virtual void setDaughterParams() {}
63         /// clean-up any daughter class-particular data on hide().
64         virtual void clearDaughterParams() {}
65
66
67
68         
69         /// Instantiation of ControlButtons virtual methods.
70
71         /// Get changed parameters and Dispatch them to the kernel.
72         virtual void apply();
73         /// Disconnect signals and hide View.
74         virtual void hide();
75         /// Update the dialog.
76         virtual void update();
77
78         /** Instantiation of ControlConnectBD private virtual method.
79             Slot connected to update signal. */
80         virtual void updateSlot(bool);
81
82         /// Show the dialog.
83         void show(Params const &);
84         /// Connect signals
85         void connectInset(Inset * = 0);
86
87         /// pointer to the inset passed through connectInset
88         Inset * inset_; 
89         /// inset::hide connection.
90         SigC::Connection ih_;
91         /** A local copy of the inset's params.
92             Memory is allocated only whilst the dialog is visible.
93         */
94         Params * params_;
95 };
96
97
98 template <class Inset, class Params>
99 ControlInset<Inset, Params>::ControlInset(LyXView & lv, Dialogs & d)
100         : ControlConnectBD(lv, d),
101           inset_(0), ih_(0), params_(0)
102 {}
103
104
105 template <class Inset, class Params>
106 void ControlInset<Inset, Params>::showInset(Inset * inset)
107 {
108         if (inset == 0) return;  // maybe we should Assert this?
109
110         connectInset(inset);
111         show(getParams(*inset));
112 }
113
114
115 template <class Inset, class Params>
116 void ControlInset<Inset, Params>::createInset(string const & arg)
117 {
118         connectInset();
119
120         if ( !arg.empty() )
121                 bc().valid(); // so that the user can press Ok
122
123         show(getParams(arg));
124 }
125
126
127 template <class Inset, class Params>
128 void ControlInset<Inset, Params>::show(Params const & params)
129 {
130         if (params_) delete params_;
131         params_ = new Params(params);
132
133         setDaughterParams();
134
135         bc().readOnly(isReadonly());
136         view().show();
137 }
138
139
140 template <class Inset, class Params>
141 void ControlInset<Inset, Params>::hide()
142 {
143         if (params_) {
144                 delete params_;
145                 params_ = 0;
146         }
147         inset_ = 0;
148
149         clearDaughterParams();
150
151         ih_.disconnect();
152         disconnect();
153         view().hide();
154 }
155
156
157 template <class Inset, class Params>
158 void ControlInset<Inset, Params>::update()
159 {
160         if (params_) delete params_;
161
162         if (inset_)
163                 params_ = new Params(getParams(*inset_));
164         else
165                 params_ = new Params();
166
167         bc().readOnly(isReadonly());
168         view().update();
169 }
170
171
172 template <class Inset, class Params>
173 void ControlInset<Inset, Params>::apply()
174 {
175         if (lv_.buffer()->isReadonly())
176                 return;
177
178         view().apply();
179
180         if (inset_ && params() != getParams(*inset_))
181                 applyParamsToInset();
182         else
183                 applyParamsNoInset();
184 }
185
186
187 template <class Inset, class Params>
188 Params & ControlInset<Inset, Params>::params() const
189 {
190         lyx::Assert(params_);
191         return *params_;
192 }
193
194
195 template <class Inset, class Params>
196 Inset * ControlInset<Inset, Params>::inset() const
197 {
198         lyx::Assert(inset_);
199         return inset_;
200 }
201
202
203 template <class Inset, class Params>
204 void ControlInset<Inset, Params>::updateSlot(bool switched)
205 {
206         if (switched)
207                 hide();
208         else
209                 update();
210 }
211
212
213 template <class Inset, class Params>
214 void ControlInset<Inset, Params>::connectInset(Inset * inset)
215 {
216         // If connected to another inset, disconnect from it.
217         if (inset_) {
218                 ih_.disconnect();
219                 inset_ = 0;
220         }
221
222         if (inset) {
223                 inset_ = inset;
224                 ih_ = inset->hideDialog.connect(
225                         SigC::slot(this, &ControlInset::hide));
226         }
227         connect();
228 }
229 #endif // CONTROLINSET_H