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