]> git.lyx.org Git - lyx.git/blob - src/Format.h
623ed26f7df18d39cbbbb816393ced5587ec8bbd
[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 "OutputParams.h"
18
19 #include <vector>
20
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         };
42         ///
43         Format(std::string const & n, std::string const & e, std::string const & p,
44                std::string const & s, std::string const & v, std::string const & ed,
45                int);
46         ///
47         bool dummy() const;
48         /// Is \p ext a valid filename extension for this format?
49         bool hasExtension(std::string const & ext) const;
50         /// Tell whether this format is a child format.
51         /// Child formats inherit settings like the viewer from their parent.
52         bool isChildFormat() const;
53         /// Name fo the parent format
54         std::string const parentFormat() const;
55         ///
56         std::string const & name() const { return name_; }
57         ///
58         void setName(std::string const & v) { name_ = v; }
59         ///
60         std::string const & extension() const
61         {
62                 return extension_list_.empty() ? empty_string() : extension_list_[0];
63         }
64         ///
65         std::string const & extensions() const { return extensions_; }
66         ///
67         void setExtensions(std::string const & v);
68         ///
69         std::string const & prettyname() const { return prettyname_; }
70         ///
71         void setPrettyname(std::string const & v) { prettyname_ = v; }
72         ///
73         std::string const & shortcut() const { return shortcut_; }
74         ///
75         void setShortcut(std::string const & v) { shortcut_ = v; }
76         ///
77         std::string const & viewer() const { return viewer_; }
78         ///
79         void setViewer(std::string const & v) { viewer_ = v; }
80         ///
81         std::string const & editor() const { return editor_; }
82         ///
83         void setEditor(std::string const & v) { editor_ = v; }
84         ///
85         bool documentFormat() const { return flags_ & document; }
86         ///
87         bool vectorFormat() const { return flags_ & vector; }
88         ///
89         void setFlags(int v) { flags_ = v; }
90         ///
91         bool inExportMenu() const { return flags_ & export_menu; }
92 private:
93         /// Internal name. Needs to be unique.
94         std::string name_;
95         /// Filename extensions, the first one being the default
96         mutable std::vector<std::string> extension_list_;
97         /// All filename extensions
98         std::string extensions_;
99         /// Name presented to the user. Needs to be unique.
100         std::string prettyname_;
101         /// Keyboard shortcut for the View and Export menu.
102         std::string shortcut_;
103         /*!
104          * Viewer for this format. Needs to be in the PATH or an absolute
105          * filename.
106          * This format cannot be viewed if \c viewer_ is empty.
107          * If it is \c auto the default viewer of the OS for this format is
108          * used.
109          */
110         std::string viewer_;
111         /// Editor for this format. \sa viewer_.
112         std::string editor_;
113         ///
114         int flags_;
115 };
116
117
118 bool operator<(Format const & a, Format const & b);
119
120
121 ///
122 class Formats {
123 public:
124         ///
125         typedef std::vector<Format> FormatList;
126         ///
127         typedef FormatList::const_iterator const_iterator;
128         ///
129         Format const & get(FormatList::size_type i) const { return formatlist[i]; }
130         ///
131         Format & get(FormatList::size_type i) { return formatlist[i]; }
132         /// \returns format named \p name if it exists, otherwise 0
133         Format const * getFormat(std::string const & name) const;
134         /*!
135          * Get the format of \p filename from file contents or, if this
136          * fails, from file extension.
137          * \returns file format if it could be found, otherwise an empty
138          * string.
139          */
140         std::string getFormatFromFile(support::FileName const & filename) const;
141         /// Set editor and/or viewer to "auto" for formats that can be
142         /// opened by the OS.
143         void setAutoOpen();
144         ///
145         int getNumber(std::string const & name) const;
146         ///
147         void add(std::string const & name);
148         ///
149         void add(std::string const & name, std::string const & extensions,
150                  std::string const & prettyname, std::string const & shortcut,
151                  std::string const & viewer, std::string const & editor,
152                  int flags);
153         ///
154         void erase(std::string const & name);
155         ///
156         void sort();
157         ///
158         void setViewer(std::string const & name, std::string const & command);
159         ///
160         void setEditor(std::string const & name, std::string const & command);
161         /// View the given file. Buffer used for DVI's paper orientation.
162         bool view(Buffer const & buffer, support::FileName const & filename,
163                   std::string const & format_name) const;
164         ///
165         bool edit(Buffer const & buffer, support::FileName const & filename,
166                   std::string const & format_name) const;
167         ///
168         docstring const prettyName(std::string const & name) const;
169         ///
170         std::string const extension(std::string const & name) const;
171         ///
172         std::string const extensions(std::string const & name) const;
173         ///
174         const_iterator begin() const { return formatlist.begin(); }
175         ///
176         const_iterator end() const { return formatlist.end(); }
177         ///
178         FormatList::size_type size() const { return formatlist.size(); }
179 private:
180         ///
181         FormatList formatlist;
182 };
183
184 ///
185 std::string flavor2format(OutputParams::FLAVOR flavor);
186 // Not currently used.
187 // OutputParams::FLAVOR format2flavor(std::string fmt);
188
189 extern Formats formats;
190
191 extern Formats system_formats;
192
193
194 } // namespace lyx
195
196 #endif //FORMAT_H