2 /* This file is part of
3 * ======================================================
5 * LyX, The Document Processor
7 * Copyright 2001 The LyX Team.
9 * ======================================================
11 * \file ControlInsets.h
12 * \author Angus Leeming <a.leeming@ic.ac.uk>
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.
19 #ifndef CONTROLINSET_H
20 #define CONTROLINSET_H
22 #include "support/LAssert.h"
24 #include "ControlConnections.h"
28 template <class Inset, class Params>
29 class ControlInset : public ControlConnectBD
33 ControlInset(LyXView &, Dialogs &);
34 /// Allow the View access to the local copy.
37 Params const & params() const;
40 /// Slots connected in the daughter classes c-tor.
41 /// Slot launching dialog to (possibly) create a new inset.
42 void createInset(string const &);
43 /// Slot launching dialog to an existing inset.
44 void showInset(Inset *);
45 /// Allow the daughter methods to access the inset.
46 Inset * inset() const;
49 /** These 6 methods are all that the individual daughter classes
50 should need to instantiate. */
52 /// if the inset exists then do this...
53 virtual void applyParamsToInset() = 0;
55 virtual void applyParamsNoInset() = 0;
57 /// get the parameters from the string passed to createInset.
58 virtual Params const getParams(string const &) = 0;
59 /// get the parameters from the inset passed to showInset.
60 virtual Params const getParams(Inset const &) = 0;
62 /** Most derived classes won't need these two, so they default to empty.
65 /// set any daughter class-particular data on show().
66 virtual void setDaughterParams() {}
67 /// clean-up any daughter class-particular data on hide().
68 virtual void clearDaughterParams() {}
73 /// Instantiation of ControlButtons virtual methods.
75 /// Get changed parameters and Dispatch them to the kernel.
77 /// Disconnect signals and hide View.
79 /// Update the dialog.
80 virtual void update();
82 /** Instantiation of ControlConnectBD private virtual method.
83 Slot connected to update signal. */
84 virtual void updateSlot(bool);
87 void show(Params const &);
89 void connectInset(Inset * = 0);
91 /// pointer to the inset passed through connectInset
93 /// inset::hide connection.
95 /** A local copy of the inset's params.
96 Memory is allocated only whilst the dialog is visible.
100 /// is the dialog built ?
106 template <class Inset, class Params>
107 ControlInset<Inset, Params>::ControlInset(LyXView & lv, Dialogs & d)
108 : ControlConnectBD(lv, d),
109 inset_(0), ih_(0), params_(0), dialog_built_(false)
113 template <class Inset, class Params>
114 void ControlInset<Inset, Params>::showInset(Inset * inset)
116 if (inset == 0) return; // maybe we should Assert this?
119 show(getParams(*inset));
123 template <class Inset, class Params>
124 void ControlInset<Inset, Params>::createInset(string const & arg)
129 bc().valid(); // so that the user can press Ok
131 show(getParams(arg));
135 template <class Inset, class Params>
136 void ControlInset<Inset, Params>::show(Params const & params)
138 if (params_) delete params_;
139 params_ = new Params(params);
143 if (!dialog_built_) {
145 dialog_built_ = true;
148 bc().readOnly(isReadonly());
153 template <class Inset, class Params>
154 void ControlInset<Inset, Params>::hide()
162 clearDaughterParams();
170 template <class Inset, class Params>
171 void ControlInset<Inset, Params>::update()
173 if (params_) delete params_;
176 params_ = new Params(getParams(*inset_));
178 params_ = new Params();
180 bc().readOnly(isReadonly());
185 template <class Inset, class Params>
186 void ControlInset<Inset, Params>::apply()
188 if (lv_.buffer()->isReadonly())
193 if (inset_ && params() != getParams(*inset_))
194 applyParamsToInset();
196 applyParamsNoInset();
198 *params_ = getParams(string());
206 template <class Inset, class Params>
207 Params & ControlInset<Inset, Params>::params()
209 lyx::Assert(params_);
214 template <class Inset, class Params>
215 Params const & ControlInset<Inset, Params>::params() const
217 lyx::Assert(params_);
222 template <class Inset, class Params>
223 Inset * ControlInset<Inset, Params>::inset() const
230 template <class Inset, class Params>
231 void ControlInset<Inset, Params>::updateSlot(bool switched)
240 template <class Inset, class Params>
241 void ControlInset<Inset, Params>::connectInset(Inset * inset)
243 // If connected to another inset, disconnect from it.
251 ih_ = inset->hideDialog.connect(
252 SigC::slot(this, &ControlInset::hide));
256 #endif // CONTROLINSET_H