]> git.lyx.org Git - lyx.git/blob - src/Format.h
LyXText -> Text
[lyx.git] / src / Format.h
1 // -*- C++ -*-
2 /**
3  * \file Format.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Dekel Tsur
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #ifndef FORMAT_H
13 #define FORMAT_H
14
15 #include "support/docstring.h"
16
17 #include <vector>
18
19
20 namespace lyx {
21
22 namespace support { class FileName; }
23
24 class Buffer;
25
26 class Format {
27 public:
28         /// Flags for some format properties
29         enum Flags {
30                 none = 0,
31                 /// Set if this format is a document format (as opposed to
32                 /// e.g. image formats).
33                 /// Some formats are both (e.g. pdf), they have this flag set.
34                 document = 1,
35                 /// Set if this format can contain vector graphics.
36                 vector = 2,
37         };
38         ///
39         Format(std::string const & n, std::string const & e, std::string const & p,
40                std::string const & s, std::string const & v, std::string const & ed,
41                int);
42         ///
43         bool dummy() const;
44         /// Tell whether this format is a child format.
45         /// Child formats inherit settings like the viewer from their parent.
46         bool isChildFormat() const;
47         /// Name fo the parent format
48         std::string const parentFormat() const;
49         ///
50         std::string const & name() const {
51                 return name_;
52         }
53         ///
54         std::string const & extension() const {
55                 return extension_;
56         }
57         ///
58         std::string const & prettyname() const {
59                 return prettyname_;
60         }
61         ///
62         std::string const & shortcut() const {
63                 return shortcut_;
64         }
65         ///
66         std::string const & viewer() const {
67                 return viewer_;
68         }
69         ///
70         void setViewer(std::string const & v) {
71                 viewer_ = v;
72         }
73         ///
74         std::string const & editor() const {
75                 return editor_;
76         }
77         ///
78         void setEditor(std::string const & v) {
79                 editor_ = v;
80         }
81         ///
82         bool documentFormat() const {
83                 return flags_ & document;
84         }
85         ///
86         bool vectorFormat() const {
87                 return flags_ & vector;
88         }
89 private:
90         /// Internal name. Needs to be unique.
91         std::string name_;
92         /// Filename extension
93         std::string extension_;
94         /// Name presented to the user. Needs to be unique.
95         std::string prettyname_;
96         /// Keyboard shortcut for the View and Export menu.
97         std::string shortcut_;
98         /*!
99          * Viewer for this format. Needs to be in the PATH or an absolute
100          * filename.
101          * This format cannot be viewed if \c viewer_ is empty.
102          * If it is \c auto the default viewer of the OS for this format is
103          * used.
104          */
105         std::string viewer_;
106         /// Editor for this format. \sa viewer_.
107         std::string editor_;
108         ///
109         int flags_;
110 };
111
112
113 bool operator<(Format const & a, Format const & b);
114
115
116 ///
117 class Formats {
118 public:
119         ///
120         typedef std::vector<Format> FormatList;
121         ///
122         typedef FormatList::const_iterator const_iterator;
123         ///
124         Format const & get(FormatList::size_type i) const {
125                 return formatlist[i];
126         }
127         /// \returns format named \p name if it exists, otherwise 0
128         Format const * getFormat(std::string const & name) const;
129         /*!
130          * Get the format of \p filename from file contents or, if this
131          * fails, from file extension.
132          * \returns file format if it could be found, otherwise an empty
133          * string.
134          */
135         std::string getFormatFromFile(support::FileName const & filename) const;
136         /// Set editor and/or viewer to "auto" for formats that can be
137         /// opened by the OS.
138         void setAutoOpen();
139         ///
140         int getNumber(std::string const & name) const;
141         ///
142         void add(std::string const & name);
143         ///
144         void add(std::string const & name, std::string const & extension,
145                  std::string const & prettyname, std::string const & shortcut,
146                  std::string const & viewer, std::string const & editor,
147                  int flags);
148         ///
149         void erase(std::string const & name);
150         ///
151         void sort();
152         ///
153         void setViewer(std::string const & name, std::string const & command);
154         ///
155         bool view(Buffer const & buffer, support::FileName const & filename,
156                   std::string const & format_name) const;
157         ///
158         bool edit(Buffer const & buffer, support::FileName const & filename,
159                   std::string const & format_name) const;
160         ///
161         docstring const prettyName(std::string const & name) const;
162         ///
163         std::string const extension(std::string const & name) const;
164         ///
165         const_iterator begin() const { return formatlist.begin(); }
166         ///
167         const_iterator end() const { return formatlist.end(); }
168         ///
169         FormatList::size_type size() const { return formatlist.size(); }
170 private:
171         ///
172         FormatList formatlist;
173 };
174
175 extern Formats formats;
176
177 extern Formats system_formats;
178
179
180 } // namespace lyx
181
182 #endif //FORMAT_H