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