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