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