]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/ControlInset.h
add Lsstream.h
[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 popups that display and
14  * can perhaps modify the contents of an individual inset. An example being the
15  * ubiquitous Citation popup.
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 * inset);
40         /// Allow the daughter methods to access the inset.
41         Inset * inset() const;
42
43 private:
44         /** These 5 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         /// clean-up any daughter class-particular data on hide.
52         virtual void clearDaughterParams() = 0;
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         /// Instantiation of ControlBase virtual methods.
59
60         /// Get changed parameters and Dispatch them to the kernel.
61         virtual void apply();
62         /// Disconnect signals and hide View.
63         virtual void hide();
64         /// Update the dialog.
65         virtual void update();
66
67         /** Instantiation of ControlConnectBD private virtual method.
68             Slot connected to update signal. */
69         virtual void updateSlot(bool);
70
71         /// Show the dialog.
72         void show(Params const &);
73         /// Connect signals
74         void connectInset(Inset * = 0);
75
76         /// pointer to the inset passed through connectInset
77         Inset * inset_; 
78         /// inset::hide connection.
79         SigC::Connection ih_;
80         /** A local copy of the inset's params.
81             Memory is allocated only whilst the dialog is visible.
82         */
83         Params * params_;
84 };
85
86
87 #include "LyXView.h"
88 #include "support/LAssert.h"
89
90
91
92 template <class Inset, class Params>
93 ControlInset<Inset, Params>::ControlInset(LyXView & lv, Dialogs & d)
94         : ControlConnectBD(lv, d),
95           inset_(0), ih_(0), params_(0)
96 {}
97
98
99 template <class Inset, class Params>
100 void ControlInset<Inset, Params>::showInset(Inset * inset)
101 {
102         if (inset == 0) return;  // maybe we should Assert this?
103
104         connectInset(inset);
105         show(getParams(*inset));
106 }
107
108
109 template <class Inset, class Params>
110 void ControlInset<Inset, Params>::createInset(string const & arg)
111 {
112         connectInset();
113
114         if ( !arg.empty() )
115                 bc().valid(); // so that the user can press Ok
116
117         show(getParams(arg));
118 }
119
120
121 template <class Inset, class Params>
122 void ControlInset<Inset, Params>::show(Params const & params)
123 {
124         if (params_) delete params_;
125         params_ = new Params(params);
126
127         bc().readOnly(isReadonly());
128         view().show();
129 }
130
131
132 template <class Inset, class Params>
133 void ControlInset<Inset, Params>::hide()
134 {
135         if (params_) {
136                 delete params_;
137                 params_ = 0;
138         }
139         inset_ = 0;
140
141         clearDaughterParams();
142
143         ih_.disconnect();
144         disconnect();
145         view().hide();
146 }
147
148
149 template <class Inset, class Params>
150 void ControlInset<Inset, Params>::update()
151 {
152         if (params_) delete params_;
153
154         if (inset_)
155                 params_ = new Params(getParams(*inset_));
156         else
157                 params_ = new Params();
158
159         bc().readOnly(isReadonly());
160         view().update();
161 }
162
163
164 template <class Inset, class Params>
165 void ControlInset<Inset, Params>::apply()
166 {
167         if (lv_.buffer()->isReadonly() || !inset_)
168                 return;
169
170         view().apply();
171
172         if (inset_) {
173                 if (params() != getParams(*inset_)) applyParamsToInset();
174         } else
175                 applyParamsNoInset();
176 }
177
178
179 template <class Inset, class Params>
180 Params & ControlInset<Inset, Params>::params() const
181 {
182         Assert(params_);
183         return *params_;
184 }
185
186
187 template <class Inset, class Params>
188 Inset * ControlInset<Inset, Params>::inset() const
189 {
190         Assert(inset_);
191         return inset_;
192 }
193
194
195 template <class Inset, class Params>
196 void ControlInset<Inset, Params>::updateSlot(bool switched)
197 {
198         if (switched)
199                 hide();
200         else
201                 update();
202 }
203
204
205 template <class Inset, class Params>
206 void ControlInset<Inset, Params>::connectInset(Inset * inset)
207 {
208         // If connected to another inset, disconnect from it.
209         if (inset_) {
210                 ih_.disconnect();
211                 inset_ = 0;
212         }
213
214         if (inset) {
215                 inset_ = inset;
216                 ih_ = inset->hideDialog.connect(
217                         SigC::slot(this, &ControlInset::hide));
218         }
219         connect();
220 }
221 #endif // CONTROLINSET_H