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"
23 #include "ControlConnections.h"
27 template <class Inset, class Params>
28 class ControlInset : public ControlConnectBD
32 ControlInset(LyXView &, Dialogs &);
33 /// Allow the View access to the local copy.
34 Params & params() const;
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;
46 /** These 6 methods are all that the individual daughter classes
47 should need to instantiate. */
49 /// if the inset exists then do this...
50 virtual void applyParamsToInset() = 0;
52 virtual void applyParamsNoInset() = 0;
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;
59 /** Most derived classes won't need these two, so they default to empty.
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() {}
70 /// Instantiation of ControlButtons virtual methods.
72 /// Get changed parameters and Dispatch them to the kernel.
74 /// Disconnect signals and hide View.
76 /// Update the dialog.
77 virtual void update();
79 /** Instantiation of ControlConnectBD private virtual method.
80 Slot connected to update signal. */
81 virtual void updateSlot(bool);
84 void show(Params const &);
86 void connectInset(Inset * = 0);
88 /// pointer to the inset passed through connectInset
90 /// inset::hide connection.
92 /** A local copy of the inset's params.
93 Memory is allocated only whilst the dialog is visible.
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)
106 template <class Inset, class Params>
107 void ControlInset<Inset, Params>::showInset(Inset * inset)
109 if (inset == 0) return; // maybe we should Assert this?
112 show(getParams(*inset));
116 template <class Inset, class Params>
117 void ControlInset<Inset, Params>::createInset(string const & arg)
122 bc().valid(); // so that the user can press Ok
124 show(getParams(arg));
128 template <class Inset, class Params>
129 void ControlInset<Inset, Params>::show(Params const & params)
131 if (params_) delete params_;
132 params_ = new Params(params);
136 bc().readOnly(isReadonly());
141 template <class Inset, class Params>
142 void ControlInset<Inset, Params>::hide()
150 clearDaughterParams();
158 template <class Inset, class Params>
159 void ControlInset<Inset, Params>::update()
161 if (params_) delete params_;
164 params_ = new Params(getParams(*inset_));
166 params_ = new Params();
168 bc().readOnly(isReadonly());
173 template <class Inset, class Params>
174 void ControlInset<Inset, Params>::apply()
176 if (lv_.buffer()->isReadonly())
181 if (inset_ && params() != getParams(*inset_))
182 applyParamsToInset();
184 applyParamsNoInset();
188 template <class Inset, class Params>
189 Params & ControlInset<Inset, Params>::params() const
191 lyx::Assert(params_);
196 template <class Inset, class Params>
197 Inset * ControlInset<Inset, Params>::inset() const
204 template <class Inset, class Params>
205 void ControlInset<Inset, Params>::updateSlot(bool switched)
214 template <class Inset, class Params>
215 void ControlInset<Inset, Params>::connectInset(Inset * inset)
217 // If connected to another inset, disconnect from it.
225 ih_ = inset->hideDialog.connect(
226 SigC::slot(this, &ControlInset::hide));
230 #endif // CONTROLINSET_H