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