]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/ControlInset.h
John's character.C patch (bug fix).
[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 "debug.h" 
24 #include "ControlConnections.h"
25
26 class Inset;
27
28 template <class Inset, class Params>
29 class ControlInset : public ControlConnectBD
30 {
31 public:
32         ///
33         ControlInset(LyXView &, Dialogs &);
34         /// Allow the View access to the local copy.
35         Params & params();
36         ///
37         Params const & params() const;
38
39 protected:
40         /// Slots connected in the daughter classes c-tor.
41         /// Slot launching dialog to (possibly) create a new inset.
42         void createInset(string const &);
43         /// Slot launching dialog to an existing inset.
44         void showInset(Inset *);
45         /// Allow the daughter methods to access the inset.
46         Inset * inset() const;
47
48 private:
49         /** These 6 methods are all that the individual daughter classes
50             should need to instantiate. */
51
52         /// if the inset exists then do this...
53         virtual void applyParamsToInset() = 0;
54         /// else this...
55         virtual void applyParamsNoInset() = 0;
56
57         /// get the parameters from the string passed to createInset.
58         virtual Params const getParams(string const &) = 0;
59         /// get the parameters from the inset passed to showInset.
60         virtual Params const getParams(Inset const &) = 0;
61
62         /** Most derived classes won't need these two, so they default to empty.
63          */
64
65         /// set any daughter class-particular data on show().
66         virtual void setDaughterParams() {}
67         /// clean-up any daughter class-particular data on hide().
68         virtual void clearDaughterParams() {}
69
70
71
72         
73         /// Instantiation of ControlButtons virtual methods.
74
75         /// Get changed parameters and Dispatch them to the kernel.
76         virtual void apply();
77         /// Disconnect signals and hide View.
78         virtual void hide();
79         /// Update the dialog.
80         virtual void update();
81
82         /** Instantiation of ControlConnectBD private virtual method.
83             Slot connected to update signal. */
84         virtual void updateSlot(bool);
85
86         /// Show the dialog.
87         void show(Params const &);
88         /// Connect signals
89         void connectInset(Inset * = 0);
90
91         /// pointer to the inset passed through connectInset
92         Inset * inset_; 
93         /// inset::hide connection.
94         SigC::Connection ih_;
95         /** A local copy of the inset's params.
96             Memory is allocated only whilst the dialog is visible.
97         */
98         Params * params_;
99 };
100
101
102 template <class Inset, class Params>
103 ControlInset<Inset, Params>::ControlInset(LyXView & lv, Dialogs & d)
104         : ControlConnectBD(lv, d),
105           inset_(0), ih_(0), params_(0)
106 {}
107
108
109 template <class Inset, class Params>
110 void ControlInset<Inset, Params>::showInset(Inset * inset)
111 {
112         if (inset == 0) return;  // maybe we should Assert this?
113
114         connectInset(inset);
115         show(getParams(*inset));
116 }
117
118
119 template <class Inset, class Params>
120 void ControlInset<Inset, Params>::createInset(string const & arg)
121 {
122         connectInset();
123
124         if ( !arg.empty() )
125                 bc().valid(); // so that the user can press Ok
126
127         show(getParams(arg));
128 }
129
130
131 template <class Inset, class Params>
132 void ControlInset<Inset, Params>::show(Params const & params)
133 {
134         if (params_) delete params_;
135         params_ = new Params(params);
136
137         setDaughterParams();
138
139         static bool isBuilt = false;
140         if (!isBuilt) {
141                 isBuilt = true;
142                 view().build();
143         }
144
145         bc().readOnly(isReadonly());
146         view().show();
147 }
148
149
150 template <class Inset, class Params>
151 void ControlInset<Inset, Params>::hide()
152 {
153         if (params_) {
154                 delete params_;
155                 params_ = 0;
156         }
157         inset_ = 0;
158
159         clearDaughterParams();
160
161         ih_.disconnect();
162         disconnect();
163         view().hide();
164 }
165
166
167 template <class Inset, class Params>
168 void ControlInset<Inset, Params>::update()
169 {
170         if (params_) delete params_;
171
172         if (inset_)
173                 params_ = new Params(getParams(*inset_));
174         else
175                 params_ = new Params();
176
177         bc().readOnly(isReadonly());
178         // Reset the Button Controller to it's initial state
179         bc().invalid();
180         bc().restore();
181
182         view().update();
183 }
184
185
186 template <class Inset, class Params>
187 void ControlInset<Inset, Params>::apply()
188 {
189         if (lv_.buffer()->isReadonly())
190                 return;
191
192         view().apply();
193
194         if (inset_ && params() != getParams(*inset_))
195                 applyParamsToInset();
196         else
197                 applyParamsNoInset();
198
199         *params_ = getParams(string());
200         inset_ = 0;
201         ih_.disconnect();
202
203         view().update();
204 }
205
206
207 template <class Inset, class Params>
208 Params & ControlInset<Inset, Params>::params()
209 {
210         lyx::Assert(params_);
211         return *params_;
212 }
213
214
215 template <class Inset, class Params>
216 Params  const & ControlInset<Inset, Params>::params() const
217 {
218         lyx::Assert(params_);
219         return *params_;
220 }
221
222
223 template <class Inset, class Params>
224 Inset * ControlInset<Inset, Params>::inset() const
225 {
226         lyx::Assert(inset_);
227         return inset_;
228 }
229
230
231 template <class Inset, class Params>
232 void ControlInset<Inset, Params>::updateSlot(bool switched)
233 {
234         if (switched)
235                 hide();
236         else
237                 update();
238 }
239
240
241 template <class Inset, class Params>
242 void ControlInset<Inset, Params>::connectInset(Inset * inset)
243 {
244         // If connected to another inset, disconnect from it.
245         if (inset_) {
246                 ih_.disconnect();
247                 inset_ = 0;
248         }
249
250         if (inset) {
251                 inset_ = inset;
252                 ih_ = inset->hideDialog.connect(
253                         SigC::slot(this, &ControlInset::hide));
254         }
255         connect();
256 }
257 #endif // CONTROLINSET_H