]> git.lyx.org Git - lyx.git/blob - src/format.h
Fix bug 2868
[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 #include <string>
19
20 class Buffer;
21
22 class Format {
23 public:
24         /// Flags for some format properties
25         enum Flags {
26                 none = 0,
27                 /// Set if this format is a document format (as opposed to
28                 /// e.g. image formats).
29                 /// Some formats are both (e.g. pdf), they have this flag set.
30                 document = 1,
31                 /// Set if this format can contain vector graphics.
32                 vector = 2,
33         };
34         ///
35         Format(std::string const & n, std::string const & e, std::string const & p,
36                std::string const & s, std::string const & v, std::string const & ed,
37                int);
38         ///
39         bool dummy() const;
40         /// Tell whether this format is a child format.
41         /// Child formats inherit settings like the viewer from their parent.
42         bool isChildFormat() const;
43         /// Name fo the parent format
44         std::string const parentFormat() const;
45         ///
46         std::string const & name() const {
47                 return name_;
48         }
49         ///
50         std::string const & extension() const {
51                 return extension_;
52         }
53         ///
54         std::string const & prettyname() const {
55                 return prettyname_;
56         }
57         ///
58         std::string const & shortcut() const {
59                 return shortcut_;
60         }
61         ///
62         std::string const & viewer() const {
63                 return viewer_;
64         }
65         ///
66         void setViewer(std::string const & v) {
67                 viewer_ = v;
68         }
69         ///
70         std::string const & editor() const {
71                 return editor_;
72         }
73         ///
74         void setEditor(std::string const & v) {
75                 editor_ = v;
76         }
77         ///
78         bool documentFormat() const {
79                 return flags_ & document;
80         }
81         ///
82         bool vectorFormat() const {
83                 return flags_ & vector;
84         }
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 {
121                 return formatlist[i];
122         }
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(std::string 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         bool view(Buffer const & buffer, std::string const & filename,
152                   std::string const & format_name) const;
153         ///
154         bool edit(Buffer const & buffer, std::string const & filename,
155                   std::string const & format_name) const;
156         ///
157         lyx::docstring const prettyName(std::string const & name) const;
158         ///
159         std::string const extension(std::string const & name) const;
160         ///
161         const_iterator begin() const {
162                 return formatlist.begin();
163         }
164         ///
165         const_iterator end() const {
166                 return formatlist.end();
167         }
168         ///
169         FormatList::size_type size() const {
170                 return formatlist.size();
171         }
172 private:
173         ///
174         FormatList formatlist;
175 };
176
177 extern Formats formats;
178
179 extern Formats system_formats;
180
181 #endif //FORMAT_H