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