]> git.lyx.org Git - lyx.git/blob - src/insets/InsetCommandParams.h
Get rid of Qt resources
[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
19 #include "OutputParams.h"
20
21 #include "support/docstring.h"
22
23 #include <string>
24 #include <vector>
25 #include <map>
26
27
28 namespace lyx {
29
30 class Lexer;
31 class Buffer;
32
33 class ParamInfo {
34 public:
35         ///
36         ParamInfo() {}
37         /// Types of parameters
38         enum ParamType {
39                 LATEX_OPTIONAL,    /// normal optional argument
40                 LATEX_REQUIRED,    /// normal required argument
41                 LYX_INTERNAL       /// a parameter used internally by LyX
42         };
43         /// Special handling on output
44         enum ParamHandling {
45                 HANDLING_NONE = 1,    /// no special handling
46                 HANDLING_ESCAPE = 2,  /// escape special characters
47                 HANDLING_LATEXIFY = 4, /// transform special characters to LaTeX macros
48                 HANDLING_LTRIM = 8, /// trim blanks on the left
49                 HANDLING_INDEX_ESCAPE = 16, /// escape makeindex special chars
50         };
51         ///
52         class ParamData {
53         // No parameter may be named "preview", because that is a required
54         // flag for all commands.
55         public:
56                 ///
57                 ParamData(std::string const &, ParamType, ParamHandling = HANDLING_NONE,
58                           bool ignore = false,
59                           docstring default_value = docstring());
60                 ///
61                 std::string name() const { return name_; }
62                 ///
63                 ParamType type() const { return type_; }
64                 ///
65                 ParamHandling handling() const { return handling_; }
66                 /// whether this is an optional LaTeX argument
67                 bool isOptional() const;
68                 ///
69                 bool ignore() const { return ignore_; }
70                 ///
71                 docstring const & defaultValue() const { return default_value_; }
72                 ///
73                 bool operator==(ParamData const &) const;
74                 ///
75                 bool operator!=(ParamData const & rhs) const
76                         { return !(*this == rhs); }
77         private:
78                 ///
79                 std::string name_;
80                 ///
81                 ParamType type_;
82                 /// do we need special handling on latex output?
83                 ParamHandling handling_;
84                 ///
85                 bool ignore_;
86                 ///
87                 docstring default_value_;
88         };
89
90         /// adds a new parameter
91         /// If ignoreval is true, then the parameter is never saved, and is always
92         /// given the default value.
93         void add(std::string const & name, ParamType type,
94                  ParamHandling = HANDLING_NONE, bool ignoreval = false,
95                  docstring default_value = docstring());
96         ///
97         bool empty() const { return info_.empty(); }
98         ///
99         size_t size() const { return info_.size(); }
100         ///
101         typedef std::vector<ParamData>::const_iterator const_iterator;
102         ///
103         const_iterator const begin() const { return info_.begin(); }
104         ///
105         const_iterator const end() const { return info_.end(); }
106         /// \return true if name corresponds to a parameter of some sort.
107         /// \return false if the parameter does not exist at all of it it
108         bool hasParam(std::string const & name) const;
109         ///
110         ParamData const & operator[](std::string const & name) const;
111         ///
112         bool operator==(ParamInfo const &) const;
113 private:
114         ///
115         std::vector<ParamData> info_;
116 };
117
118
119 class InsetCommandParams {
120 public:
121         /// Construct parameters for inset of type \p code.
122         explicit InsetCommandParams(InsetCode code);
123         /// Construct parameters for inset of type \p code with
124         /// command name \p cmdName.
125         explicit InsetCommandParams(InsetCode code,
126                         std::string const & cmdName);
127         ///
128         std::string insetType() const;
129         ///
130         InsetCode code() const { return insetCode_; }
131         /// Parse the command
132         void read(Lexer &);
133         ///
134         void Read(Lexer &, Buffer const *);
135         ///
136         void write(std::ostream &) const;
137         ///
138         void Write(std::ostream & os, Buffer const * buf) const;
139         /// Build the complete LaTeX command
140         docstring getCommand(OutputParams const &) const;
141         /// Return the command name
142         std::string const & getCmdName() const { return cmdName_; }
143         /// Set the name to \p n. This must be a known name. All parameters
144         /// are cleared except those that exist also in the new command.
145         /// What matters here is the parameter name, not position.
146         void setCmdName(std::string const & n);
147         /// FIXME Would be better removed, but is used in BufferView.cpp in
148         /// ways that make removal hard.
149         docstring getFirstNonOptParam() const;
150         /// get parameter \p name
151         /// LyX will assert if name is not a valid parameter.
152         docstring const & operator[](std::string const & name) const;
153         /// set parameter \p name
154         /// LyX will assert if name is not a valid parameter.
155         docstring & operator[](std::string const & name);
156         ///
157         bool preview() const { return preview_; }
158         ///
159         void preview(bool p) { preview_ = p; }
160         /// Clear the values of all parameters
161         void clear();
162         ///
163         static bool isCompatibleCommand(InsetCode code, std::string const & s);
164         ///
165         ParamInfo const & info() const { return info_; }
166         ///
167         docstring prepareCommand(OutputParams const & runparams,
168                 docstring const & command, ParamInfo::ParamHandling handling) const;
169 private:
170         std::string getDefaultCmd(InsetCode code);
171         /// checks whether we need to write an empty optional parameter
172         /// \return true if a non-empty optional parameter follows ci
173         bool writeEmptyOptional(ParamInfo::const_iterator ci) const;
174
175         /// Description of all command properties
176         ParamInfo info_;
177         /// what kind of inset we're the parameters for
178         InsetCode insetCode_;
179         /// The name of this command as it appears in .lyx and .tex files
180         std::string cmdName_;
181         ///
182         // if we need to allow more than one value for a parameter, this
183         // could be made a multimap. it may be that the only thing that
184         // would then need changing is operator[].
185         typedef std::map<std::string, docstring> ParamMap;
186         /// The parameters, by name.
187         ParamMap params_;
188         ///
189         bool preview_;
190         ///
191         friend bool operator==(InsetCommandParams const &,
192                         InsetCommandParams const &);
193 };
194
195 ///
196 bool operator==(InsetCommandParams const &, InsetCommandParams const &);
197 ///
198 bool operator!=(InsetCommandParams const &, InsetCommandParams const &);
199
200
201 } // namespace lyx
202
203 #endif