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