]> git.lyx.org Git - lyx.git/blob - src/insets/InsetCommandParams.h
f80dcecf0ebbdb16041c5ffe90981259681b857d
[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         ///
33         class ParamData {
34         // No parameter may be named "preview", because that is a required
35         // flag for all commands.
36         public:
37                 ///
38                 ParamData(std::string const &, bool);
39                 ///
40                 std::string name() const { return name_; }
41                 ///
42                 bool isOptional() const { return optional_; }
43                 ///
44                 bool operator==(ParamData const &) const;
45                 /// 
46                 bool operator!=(ParamData const & rhs) const
47                         { return !(*this == rhs); }
48         private:
49                 ///
50                 std::string name_;
51                 ///
52                 bool optional_;
53         };
54
55         /// adds a new parameter
56         void add(std::string const & name, bool optional);
57         ///
58         bool empty() const { return info_.empty(); }
59         ///
60         size_t size() const { return info_.size(); }
61         ///
62         typedef std::vector<ParamData>::const_iterator const_iterator;
63         ///
64         const_iterator begin() const { return info_.begin(); }
65         ///
66         const_iterator end() const { return info_.end(); }
67         ///
68         bool hasParam(std::string const & name) const;
69         ///
70         bool operator==(ParamInfo const &) const;
71 private:
72         ///
73         std::vector<ParamData> info_;
74 };
75
76
77 class InsetCommandParams {
78 public:
79         /// Construct parameters for inset of type \p code.
80         explicit InsetCommandParams(InsetCode code);
81         /// Construct parameters for inset of type \p code with
82         /// command name \p cmdName.
83         explicit InsetCommandParams(InsetCode code,
84                         std::string const & cmdName);
85         ///
86         std::string insetType() const { return insetName(insetCode_); }
87         ///
88         InsetCode code() const { return insetCode_; }
89         ///
90         void read(Lexer &);
91         /// Parse the command
92         ///
93         void write(std::ostream &) const;
94         /// Build the complete LaTeX command
95         docstring const getCommand() const;
96         /// Return the command name
97         std::string const & getCmdName() const { return cmdName_; }
98         /// Set the name to \p n. This must be a known name. All parameters
99         /// are cleared except those that exist also in the new command.
100         /// What matters here is the parameter name, not position.
101         void setCmdName(std::string const & n);
102         /// FIXME Would be better removed, but is used in BufferView.cpp in 
103         /// ways that make removal hard.
104         docstring const getFirstNonOptParam() const;
105         /// get parameter \p name
106         docstring const & operator[](std::string const & name) const;
107         /// set parameter \p name
108         docstring & operator[](std::string const & name);
109         ///
110         bool preview() const { return preview_; }
111         ///
112         void preview(bool p) { preview_ = p; }
113         /// Clear the values of all parameters
114         void clear();
115
116 private:
117         ///
118         /// Get information for inset type \p code.
119         /// Returns 0 if the inset is not known.
120         static ParamInfo const & findInfo(InsetCode code);
121         /// Get information for \p code and command \p cmdName.
122         /// Returns 0 if the combination is not known.
123         /// Don't call this without first making sure the command name is
124         /// acceptable to the inset.
125         static ParamInfo const & findInfo(InsetCode code,
126                                             std::string const & cmdName);
127         ///
128         static bool isCompatibleCommand(InsetCode code, std::string const & s);
129         ///
130         std::string getDefaultCmd(InsetCode);
131         /// Description of all command properties
132         ParamInfo info_;
133         /// what kind of inset we're the parameters for
134         InsetCode insetCode_;
135         /// The name of this command as it appears in .lyx and .tex files
136         std::string cmdName_;
137         ///
138         typedef std::map<std::string, docstring> ParamMap;
139         /// The parameters, by name.
140         ParamMap params_;
141         ///
142         bool preview_;
143         ///
144         friend bool operator==(InsetCommandParams const &,
145                         InsetCommandParams const &);
146 };
147
148 ///
149 bool operator==(InsetCommandParams const &, InsetCommandParams const &);
150 ///
151 bool operator!=(InsetCommandParams const &, InsetCommandParams const &);
152
153
154 } // namespace lyx
155
156 #endif