1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 2001 The LyX Team.
8 * ======================================================
10 * \file ControlInsets.h
11 * \author Angus Leeming <a.leeming@ic.ac.uk>
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.
18 #ifndef CONTROLINSET_H
19 #define CONTROLINSET_H
21 #include "support/LAssert.h"
22 #include "ControlConnections.h"
26 template <class Inset, class Params>
27 class ControlInset : public ControlConnectBD
31 ControlInset(LyXView &, Dialogs &);
32 /// Allow the View access to the local copy.
33 Params & params() const;
36 /// Slots connected in the daughter classes c-tor.
37 /// Slot launching dialog to (possibly) create a new inset.
38 void createInset(string const &);
39 /// Slot launching dialog to an existing inset.
40 void showInset(Inset *);
41 /// Allow the daughter methods to access the inset.
42 Inset * inset() const;
45 /** These 6 methods are all that the individual daughter classes
46 should need to instantiate. */
48 /// if the inset exists then do this...
49 virtual void applyParamsToInset() = 0;
51 virtual void applyParamsNoInset() = 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;
58 /** Most derived classes won't need these two, so they default to empty.
61 /// set any daughter class-particular data on show().
62 virtual void setDaughterParams() {}
63 /// clean-up any daughter class-particular data on hide().
64 virtual void clearDaughterParams() {}
69 /// Instantiation of ControlButtons virtual methods.
71 /// Get changed parameters and Dispatch them to the kernel.
73 /// Disconnect signals and hide View.
75 /// Update the dialog.
76 virtual void update();
78 /** Instantiation of ControlConnectBD private virtual method.
79 Slot connected to update signal. */
80 virtual void updateSlot(bool);
83 void show(Params const &);
85 void connectInset(Inset * = 0);
87 /// pointer to the inset passed through connectInset
89 /// inset::hide connection.
91 /** A local copy of the inset's params.
92 Memory is allocated only whilst the dialog is visible.
98 template <class Inset, class Params>
99 ControlInset<Inset, Params>::ControlInset(LyXView & lv, Dialogs & d)
100 : ControlConnectBD(lv, d),
101 inset_(0), ih_(0), params_(0)
105 template <class Inset, class Params>
106 void ControlInset<Inset, Params>::showInset(Inset * inset)
108 if (inset == 0) return; // maybe we should Assert this?
111 show(getParams(*inset));
115 template <class Inset, class Params>
116 void ControlInset<Inset, Params>::createInset(string const & arg)
121 bc().valid(); // so that the user can press Ok
123 show(getParams(arg));
127 template <class Inset, class Params>
128 void ControlInset<Inset, Params>::show(Params const & params)
130 if (params_) delete params_;
131 params_ = new Params(params);
135 bc().readOnly(isReadonly());
140 template <class Inset, class Params>
141 void ControlInset<Inset, Params>::hide()
149 clearDaughterParams();
157 template <class Inset, class Params>
158 void ControlInset<Inset, Params>::update()
160 if (params_) delete params_;
163 params_ = new Params(getParams(*inset_));
165 params_ = new Params();
167 bc().readOnly(isReadonly());
172 template <class Inset, class Params>
173 void ControlInset<Inset, Params>::apply()
175 if (lv_.buffer()->isReadonly())
180 if (inset_ && params() != getParams(*inset_))
181 applyParamsToInset();
183 applyParamsNoInset();
187 template <class Inset, class Params>
188 Params & ControlInset<Inset, Params>::params() const
190 lyx::Assert(params_);
195 template <class Inset, class Params>
196 Inset * ControlInset<Inset, Params>::inset() const
203 template <class Inset, class Params>
204 void ControlInset<Inset, Params>::updateSlot(bool switched)
213 template <class Inset, class Params>
214 void ControlInset<Inset, Params>::connectInset(Inset * inset)
216 // If connected to another inset, disconnect from it.
224 ih_ = inset->hideDialog.connect(
225 SigC::slot(this, &ControlInset::hide));
229 #endif // CONTROLINSET_H