]> git.lyx.org Git - lyx.git/blob - src/insets/InsetCommandParams.h
- Simplify prefs, graphics and external display options which are now true or false.
[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 "support/docstring.h"
20
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         enum ParamType {
34                 LATEX_OPTIONAL,    /// normal optional argument
35                 LATEX_REQUIRED,    /// normal required argument
36                 LYX_INTERNAL       /// a parameter used internally by LyX
37         };
38         ///
39         class ParamData {
40         // No parameter may be named "preview", because that is a required
41         // flag for all commands.
42         public:
43                 ///
44                 ParamData(std::string const &, ParamType);
45                 ///
46                 std::string name() const { return name_; }
47                 ///
48                 ParamType type() const { return type_; }
49                 /// whether this is an optional LaTeX argument
50                 bool isOptional() const;
51                 ///
52                 bool operator==(ParamData const &) const;
53                 /// 
54                 bool operator!=(ParamData const & rhs) const
55                         { return !(*this == rhs); }
56         private:
57                 ///
58                 std::string name_;
59                 ///
60                 ParamType type_;
61         };
62
63         /// adds a new parameter
64         void add(std::string const & name, ParamType type);
65         ///
66         bool empty() const { return info_.empty(); }
67         ///
68         size_t size() const { return info_.size(); }
69         ///
70         typedef std::vector<ParamData>::const_iterator const_iterator;
71         ///
72         const_iterator const begin() const { return info_.begin(); }
73         ///
74         const_iterator const end() const { return info_.end(); }
75         /// \return true if name corresponds to a parameter of some sort.
76         /// \return false if the parameter does not exist at all of it it 
77         bool hasParam(std::string const & name) const;
78         ///
79         ParamData const & operator[](std::string const & name) const;
80         ///
81         bool operator==(ParamInfo const &) const;
82 private:
83         ///
84         std::vector<ParamData> info_;
85 };
86
87
88 class InsetCommandParams {
89 public:
90         /// Construct parameters for inset of type \p code.
91         explicit InsetCommandParams(InsetCode code);
92         /// Construct parameters for inset of type \p code with
93         /// command name \p cmdName.
94         explicit InsetCommandParams(InsetCode code,
95                         std::string const & cmdName);
96         ///
97         std::string insetType() const;
98         ///
99         InsetCode code() const { return insetCode_; }
100         ///
101         void read(Lexer &);
102         /// Parse the command
103         ///
104         void write(std::ostream &) const;
105         /// Build the complete LaTeX command
106         docstring getCommand() const;
107         /// Return the command name
108         std::string const & getCmdName() const { return cmdName_; }
109         /// Set the name to \p n. This must be a known name. All parameters
110         /// are cleared except those that exist also in the new command.
111         /// What matters here is the parameter name, not position.
112         void setCmdName(std::string const & n);
113         /// FIXME Would be better removed, but is used in BufferView.cpp in 
114         /// ways that make removal hard.
115         docstring getFirstNonOptParam() const;
116         /// get parameter \p name
117         /// LyX will assert if name is not a valid parameter.
118         docstring const & operator[](std::string const & name) const;
119         /// set parameter \p name
120         /// LyX will assert if name is not a valid parameter.
121         docstring & operator[](std::string const & name);
122         ///
123         bool preview() const { return preview_; }
124         ///
125         void preview(bool p) { preview_ = p; }
126         /// Clear the values of all parameters
127         void clear();
128         ///
129         static bool isCompatibleCommand(InsetCode code, std::string const & s);
130
131 private:
132         std::string getDefaultCmd(InsetCode code);
133         /// checks whether we need to write an empty optional parameter
134         /// \return true if a non-empty optional parameter follows ci
135         bool writeEmptyOptional(ParamInfo::const_iterator ci) const;
136
137         /// Description of all command properties
138         ParamInfo info_;
139         /// what kind of inset we're the parameters for
140         InsetCode insetCode_;
141         /// The name of this command as it appears in .lyx and .tex files
142         std::string cmdName_;
143         ///
144         // if we need to allow more than one value for a parameter, this
145         // could be made a multimap. it may be that the only thing that
146         // would then need changing is operator[].
147         typedef std::map<std::string, docstring> ParamMap;
148         /// The parameters, by name.
149         ParamMap params_;
150         ///
151         bool preview_;
152         ///
153         friend bool operator==(InsetCommandParams const &,
154                         InsetCommandParams const &);
155 };
156
157 ///
158 bool operator==(InsetCommandParams const &, InsetCommandParams const &);
159 ///
160 bool operator!=(InsetCommandParams const &, InsetCommandParams const &);
161
162
163 } // namespace lyx
164
165 #endif