]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/ControlInset.h
Controller-view split of Graphics and Index popups.
[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 *);
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 ControlBase 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 #include "LyXView.h"
98 #include "support/LAssert.h"
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         bc().readOnly(isReadonly());
140         view().show();
141 }
142
143
144 template <class Inset, class Params>
145 void ControlInset<Inset, Params>::hide()
146 {
147         if (params_) {
148                 delete params_;
149                 params_ = 0;
150         }
151         inset_ = 0;
152
153         clearDaughterParams();
154
155         ih_.disconnect();
156         disconnect();
157         view().hide();
158 }
159
160
161 template <class Inset, class Params>
162 void ControlInset<Inset, Params>::update()
163 {
164         if (params_) delete params_;
165
166         if (inset_)
167                 params_ = new Params(getParams(*inset_));
168         else
169                 params_ = new Params();
170
171         bc().readOnly(isReadonly());
172         view().update();
173 }
174
175
176 template <class Inset, class Params>
177 void ControlInset<Inset, Params>::apply()
178 {
179         if (lv_.buffer()->isReadonly())
180                 return;
181
182         view().apply();
183
184         if (inset_ && params() != getParams(*inset_))
185                 applyParamsToInset();
186         else
187                 applyParamsNoInset();
188 }
189
190
191 template <class Inset, class Params>
192 Params & ControlInset<Inset, Params>::params() const
193 {
194         Assert(params_);
195         return *params_;
196 }
197
198
199 template <class Inset, class Params>
200 Inset * ControlInset<Inset, Params>::inset() const
201 {
202         Assert(inset_);
203         return inset_;
204 }
205
206
207 template <class Inset, class Params>
208 void ControlInset<Inset, Params>::updateSlot(bool switched)
209 {
210         if (switched)
211                 hide();
212         else
213                 update();
214 }
215
216
217 template <class Inset, class Params>
218 void ControlInset<Inset, Params>::connectInset(Inset * inset)
219 {
220         // If connected to another inset, disconnect from it.
221         if (inset_) {
222                 ih_.disconnect();
223                 inset_ = 0;
224         }
225
226         if (inset) {
227                 inset_ = inset;
228                 ih_ = inset->hideDialog.connect(
229                         SigC::slot(this, &ControlInset::hide));
230         }
231         connect();
232 }
233 #endif // CONTROLINSET_H