]> git.lyx.org Git - lyx.git/blob - src/Format.h
Avoid full metrics computation with Update:FitCursor
[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/strfwd.h"
16 #include "support/trivstring.h"
17
18 #include <vector>
19 #include <string>
20
21 namespace lyx {
22
23 namespace support { class FileName; }
24
25 class Buffer;
26
27 enum class Flavor : int;
28
29 class Format {
30 public:
31         /// Flags for some format properties
32         enum Flags {
33                 none = 0,
34                 /// Set if this format is a document format (as opposed to
35                 /// e.g. image formats).
36                 /// Some formats are both (e.g. pdf), they have this flag set.
37                 document = 1,
38                 /// Set if this format can contain vector graphics.
39                 vector = 2,
40                 /// This format should  appear in the File > Export menu
41                 export_menu = 4,
42                 /// This may be a compressed file but doesn't need decompression
43                 zipped_native = 8,
44                 /// This format should not appear in a menu at all
45                 no_menu = 16
46         };
47         ///
48         Format(std::string const & n, std::string const & e, docstring const & p,
49                std::string const & s, std::string const & v, std::string const & ed,
50                std::string const & m, int);
51         ///
52         bool dummy() const;
53         /// Is \p ext a valid filename extension for this format?
54         bool hasExtension(std::string const & ext) const;
55         /// Tell whether this format is a child format.
56         /// Child formats inherit settings like the viewer from their parent.
57         bool isChildFormat() const;
58         /// Name fo the parent format
59         std::string const parentFormat() const;
60         ///
61         std::string const name() const { return name_; }
62         ///
63         void setName(std::string const & v) { name_ = v; }
64         ///
65         std::string const extension() const
66         {
67                 return extension_list_.empty() ? empty_string() : extension_list_[0];
68         }
69         ///
70         std::string const extensions() const;
71         ///
72         void setExtensions(std::string const & v);
73         ///
74         docstring const prettyname() const { return prettyname_; }
75         ///
76         void setPrettyname(docstring const & v) { prettyname_ = v; }
77         ///
78         std::string const shortcut() const { return shortcut_; }
79         ///
80         void setShortcut(std::string const & v) { shortcut_ = v; }
81         ///
82         std::string const viewer() const { return viewer_; }
83         ///
84         void setViewer(std::string const & v) { viewer_ = v; }
85         ///
86         std::string const editor() const { return editor_; }
87         ///
88         void setEditor(std::string const & v) { editor_ = v; }
89         ///
90         std::string const mime() const { return mime_; }
91         ///
92         void setMime(std::string const & m) { mime_ = m; }
93         ///
94         bool documentFormat() const { return flags_ & document; }
95         ///
96         bool vectorFormat() const { return flags_ & vector; }
97         ///
98         void setFlags(int v) { flags_ = v; }
99         ///
100         bool inExportMenu() const { return flags_ & export_menu; }
101         ///
102         bool noMenu() const { return flags_ & no_menu; }
103         ///
104         bool zippedNative() const { return flags_ & zipped_native; }
105         ///
106         static bool formatSorter(Format const * lhs, Format const * rhs);
107
108 private:
109         /// Internal name. Needs to be unique.
110         trivstring name_;
111         /// Filename extensions, the first one being the default
112         std::vector<std::string> extension_list_;
113         /// Name presented to the user. Needs to be unique.
114         trivdocstring prettyname_;
115         /// Keyboard shortcut for the View and Export menu.
116         trivstring shortcut_;
117         /*!
118          * Viewer for this format. Needs to be in the PATH or an absolute
119          * filename.
120          * This format cannot be viewed if \c viewer_ is empty.
121          * If it is \c auto the default viewer of the OS for this format is
122          * used.
123          */
124         trivstring viewer_;
125         /// Editor for this format. \sa viewer_.
126         trivstring editor_;
127         /*!
128          * Full MIME type, e.g. "text/x-tex".
129          * Only types listed by the shared MIME database of freedesktop.org
130          * should be added.
131          * This field may be empty, but it must be unique across all formats
132          * if it is set.
133          */
134         trivstring mime_;
135         ///
136         int flags_;
137 };
138
139
140 bool operator<(Format const & a, Format const & b);
141
142
143 ///
144 class Formats {
145 public:
146         ///
147         typedef std::vector<Format> FormatList;
148         ///
149         typedef FormatList::const_iterator const_iterator;
150         ///
151         Format const & get(FormatList::size_type i) const { return formatlist_[i]; }
152         ///
153         Format & get(FormatList::size_type i) { return formatlist_[i]; }
154         /// \returns format named \p name if it exists, otherwise 0
155         Format const * getFormat(std::string const & name) const;
156         /// \returns format named \p name if it exists, otherwise 0
157         Format * getFormat(std::string const & name);
158         /*!
159          * Get the format of \p filename from file contents or, if this
160          * fails, from file extension.
161          * \returns file format if it could be found, otherwise an empty
162          * string.
163          * This function is expensive.
164          */
165         std::string getFormatFromFile(support::FileName const & filename) const;
166         /// Finds a format from a file extension. Returns string() if not found.
167         std::string getFormatFromExtension(std::string const & ext) const;
168         /** Returns true if the file referenced by \p filename is zipped and
169          ** needs to be unzipped for being handled
170          ** @note For natively zipped formats, such as dia/odg, this returns false.
171          **/
172         bool isZippedFile(support::FileName const & filename) const;
173         /// check for zipped file format
174         static bool isZippedFileFormat(std::string const & format);
175         /// check for PostScript file format
176         static bool isPostScriptFileFormat(std::string const & format);
177         /// Set editor and/or viewer to "auto" for formats that can be
178         /// opened by the OS.
179         void setAutoOpen();
180         ///
181         int getNumber(std::string const & name) const;
182         ///
183         void add(std::string const & name);
184         ///
185         void add(std::string const & name, std::string const & extensions,
186                  docstring const & prettyname, std::string const & shortcut,
187                  std::string const & viewer, std::string const & editor,
188                  std::string const & mime, int flags);
189         ///
190         void erase(std::string const & name);
191         ///
192         void sort();
193         ///
194         void setViewer(std::string const & name, std::string const & command);
195         ///
196         void setEditor(std::string const & name, std::string const & command);
197         /// View the given file. Buffer used for DVI's paper orientation.
198         bool view(Buffer const & buffer, support::FileName const & filename,
199                   std::string const & format_name) const;
200         ///
201         bool edit(Buffer const & buffer, support::FileName const & filename,
202                   std::string const & format_name) const;
203         ///
204         docstring const prettyName(std::string const & name) const;
205         ///
206         std::string const extension(std::string const & name) const;
207         ///
208         std::string const extensions(std::string const & name) const;
209         ///
210         const_iterator begin() const { return formatlist_.begin(); }
211         ///
212         const_iterator end() const { return formatlist_.end(); }
213         ///
214         bool empty() const { return formatlist_.empty(); }
215         ///
216         FormatList::size_type size() const { return formatlist_.size(); }
217 private:
218         ///
219         FormatList formatlist_;
220 };
221
222 ///
223 std::string flavor2format(Flavor flavor);
224 // Not currently used.
225 // Flavor format2flavor(std::string fmt);
226
227 /// The global instance.
228 /// Implementation is in LyX.cpp.
229 extern Formats & theFormats();
230
231 /// The global copy after reading lyxrc.defaults.
232 /// Implementation is in LyX.cpp.
233 extern Formats & theSystemFormats();
234
235 } // namespace lyx
236
237 #endif //FORMAT_H