]> git.lyx.org Git - lyx.git/blob - src/insets/InsetCommandParams.h
Remove TextClassPtr without losing the type safety it provided.
[lyx.git] / src / insets / InsetCommandParams.h
1 // -*- C++ -*-
2 /**
3  * \file InsetCommandParams.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Angus Leeming
8  * \author Georg Baum
9  * \author Richard Heck
10  *
11  * Full author contact details are available in file CREDITS.
12  */
13
14 #ifndef INSETCOMMANDPARAMS_H
15 #define INSETCOMMANDPARAMS_H
16
17 #include "InsetCode.h"
18 #include "support/docstring.h"
19
20 #include <iosfwd>
21 #include <string>
22 #include <vector>
23 #include <map>
24
25
26 namespace lyx {
27
28 class Lexer;
29
30 class ParamInfo {
31 public:
32         /// Types of parameters
33         /// WARNING: LATEX_KV_* `parameters' aren't really parameters at all
34         /// but merely markers for where the keyval-type parameters should
35         /// appear in the LaTeX output. ParamInfo::hasParam(name) therefore 
36         /// returns FALSE if the corresponding `parameter' is of type
37         /// LATEX_KV_*.
38         /// It is assumed here that there is exactly one argument that accepts
39         /// the key=value pairs.
40         enum ParamType {
41                 LATEX_OPTIONAL,    /// normal optional argument
42                 LATEX_REQUIRED,    /// normal required argument
43                 LATEX_KV_OPTIONAL, /// optional argument that uses keyval
44                 LATEX_KV_REQUIRED, /// required argument that uses keyval
45                 LATEX_KEY,         /// a key to be used with keyval argument
46                 LYX_INTERNAL       /// a parameter used internally by LyX
47         };
48         ///
49         class ParamData {
50         // No parameter may be named "preview", because that is a required
51         // flag for all commands.
52         public:
53                 ///
54                 ParamData(std::string const &, ParamType);
55                 ///
56                 std::string name() const { return name_; }
57                 ///
58                 ParamType type() const { return type_; }
59                 /// whether this is a key for use with keyval
60                 bool isKey() const
61                         { return type_ == LATEX_KEY; }
62                 /// whether this is an optional LaTeX argument
63                 inline bool isOptional() const;
64                 /// whether this is a keyval argument
65                 inline bool isKeyValArg() const;
66 #if 0
67                 //presently unused but perhaps useful at some point
68                 /// whether this is a required LaTeX argument
69                 bool isRequired() const
70                         { return type_ == ParamInfo::LATEX_REQUIRED ||
71                                         type_ == ParamInfo::LATEX_KV_REQUIRED; }
72                 /// whether this is a LaTeX argument
73                 inline bool isLaTeXArgument() const
74                         { return isOptional() || isRequired(); }
75 #endif
76                 ///
77                 bool operator==(ParamData const &) const;
78                 /// 
79                 bool operator!=(ParamData const & rhs) const
80                         { return !(*this == rhs); }
81         private:
82                 ///
83                 std::string name_;
84                 ///
85                 ParamType type_;
86         };
87
88         /// adds a new parameter
89         void add(std::string const & name, ParamType type);
90         ///
91         bool empty() const { return info_.empty(); }
92         ///
93         size_t size() const { return info_.size(); }
94         ///
95         typedef std::vector<ParamData>::const_iterator const_iterator;
96         ///
97         const_iterator const begin() const { return info_.begin(); }
98         ///
99         const_iterator const end() const { return info_.end(); }
100         /// \return true if name corresponds to a parameter of some sort.
101         /// \return false if the parameter does not exist at all of it it 
102         /// corresponds to a `parameter' of type LATEX_KV_*; these do not 
103         /// really represent parameters but just argument places.
104         bool hasParam(std::string const & name) const;
105         ///
106         ParamData const & operator[](std::string const & name) const;
107         ///
108         bool operator==(ParamInfo const &) const;
109 private:
110         ///
111         std::vector<ParamData> info_;
112 };
113
114
115 class InsetCommandParams {
116 public:
117         /// Construct parameters for inset of type \p code.
118         explicit InsetCommandParams(InsetCode code);
119         /// Construct parameters for inset of type \p code with
120         /// command name \p cmdName.
121         explicit InsetCommandParams(InsetCode code,
122                         std::string const & cmdName);
123         ///
124         std::string insetType() const { return insetName(insetCode_); }
125         ///
126         InsetCode code() const { return insetCode_; }
127         ///
128         void read(Lexer &);
129         /// Parse the command
130         ///
131         void write(std::ostream &) const;
132         /// Build the complete LaTeX command
133         docstring const getCommand() const;
134         /// Return the command name
135         std::string const & getCmdName() const { return cmdName_; }
136         /// Set the name to \p n. This must be a known name. All parameters
137         /// are cleared except those that exist also in the new command.
138         /// What matters here is the parameter name, not position.
139         void setCmdName(std::string const & n);
140         /// FIXME Would be better removed, but is used in BufferView.cpp in 
141         /// ways that make removal hard.
142         docstring const getFirstNonOptParam() const;
143         /// get parameter \p name
144         /// WARNING: You cannot access LATEX_KV_* arguments in this way.
145         /// LyX will assert if you attempt to do so.
146         docstring const & operator[](std::string const & name) const;
147         /// set parameter \p name
148         /// WARNING: You cannot access LATEX_KV_* arguments in this way.
149         /// LyX will assert if you attempt to do so.
150         docstring & operator[](std::string const & name);
151         ///
152         bool preview() const { return preview_; }
153         ///
154         void preview(bool p) { preview_ = p; }
155         /// Clear the values of all parameters
156         void clear();
157
158 private:
159         ///
160         /// Get information for inset type \p code.
161         /// Returns 0 if the inset is not known.
162         static ParamInfo const & findInfo(InsetCode code);
163         /// Get information for \p code and command \p cmdName.
164         /// Returns 0 if the combination is not known.
165         /// Don't call this without first making sure the command name is
166         /// acceptable to the inset.
167         static ParamInfo const & findInfo(InsetCode code,
168                                             std::string const & cmdName);
169         ///
170         static bool isCompatibleCommand(InsetCode code, std::string const & s);
171         ///
172         std::string getDefaultCmd(InsetCode);
173         ///
174         docstring makeKeyValArgument() const;
175         /// checks whether we need to write an empty optional parameter
176         /// \return true if a non-empty optional parameter follows ci
177         bool writeEmptyOptional(ParamInfo::const_iterator ci) const;
178         /// Description of all command properties
179         ParamInfo info_;
180         /// what kind of inset we're the parameters for
181         InsetCode insetCode_;
182         /// The name of this command as it appears in .lyx and .tex files
183         std::string cmdName_;
184         ///
185         // if we need to allow more than one value for a parameter, this
186         // could be made a multimap. it may be that the only thing that
187         // would then need changing is operator[].
188         typedef std::map<std::string, docstring> ParamMap;
189         /// The parameters, by name.
190         ParamMap params_;
191         ///
192         bool preview_;
193         ///
194         friend bool operator==(InsetCommandParams const &,
195                         InsetCommandParams const &);
196 };
197
198 ///
199 bool operator==(InsetCommandParams const &, InsetCommandParams const &);
200 ///
201 bool operator!=(InsetCommandParams const &, InsetCommandParams const &);
202
203
204 } // namespace lyx
205
206 #endif