]> git.lyx.org Git - lyx.git/blob - src/format.h
minimal effort implementation of:
[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 <vector>
16 #include <string>
17
18 class Buffer;
19
20 class Format {
21 public:
22         ///
23         Format(std::string const & n, std::string const & e, std::string const & p,
24                std::string const & s, std::string const & v, std::string const & ed,
25                bool ex);
26         ///
27         bool dummy() const;
28         /// Tell whether this format is a child format.
29         /// Child formats inherit settings like the viewer from their parent.
30         bool isChildFormat() const;
31         /// Name fo the parent format
32         std::string const parentFormat() const;
33         ///
34         std::string const & name() const {
35                 return name_;
36         }
37         ///
38         std::string const & extension() const {
39                 return extension_;
40         }
41         ///
42         std::string const & prettyname() const {
43                 return prettyname_;
44         }
45         ///
46         std::string const & shortcut() const {
47                 return shortcut_;
48         }
49         ///
50         std::string const & viewer() const {
51                 return viewer_;
52         }
53         ///
54         void setViewer(std::string const & v) {
55                 viewer_ = v;
56         }
57         ///
58         std::string const & editor() const {
59                 return editor_;
60         }
61         ///
62         void setEditor(std::string const & v) {
63                 editor_ = v;
64         }
65         ///
66         bool documentFormat() const {
67                 return document_;
68         }
69 private:
70         /// Internal name. Needs to be unique.
71         std::string name_;
72         /// Filename extension
73         std::string extension_;
74         /// Name presented to the user. Needs to be unique.
75         std::string prettyname_;
76         /// Keyboard shortcut for the View and Export menu.
77         std::string shortcut_;
78         /*!
79          * Viewer for this format. Needs to be in the PATH or an absolute
80          * filename.
81          * This format cannot be viewed if \c viewer_ is empty.
82          * If it is \c auto the default viewer of the OS for this format is
83          * used.
84          */
85         std::string viewer_;
86         /// Editor for this format. \sa viewer_.
87         std::string editor_;
88         /// Is this format a document format? (as opposed to e.g. image formats)
89         /// Some formats are both (e.g. pdf), they have this flag set.
90         bool document_;
91 };
92
93
94 bool operator<(Format const & a, Format const & b);
95
96 ///
97 class Formats {
98 public:
99         ///
100         typedef std::vector<Format> FormatList;
101         ///
102         typedef FormatList::const_iterator const_iterator;
103         ///
104         Format const & get(FormatList::size_type i) const {
105                 return formatlist[i];
106         }
107         /// \returns format named \p name if it exists, otherwise 0
108         Format const * getFormat(std::string const & name) const;
109         /*!
110          * Get the format of \p filename from file contents or, if this
111          * fails, from file extension.
112          * \returns file format if it could be found, otherwise an empty
113          * string.
114          */
115         std::string getFormatFromFile(std::string const & filename) const;
116         /// Set editor and/or viewer to "auto" for formats that can be
117         /// opened by the OS.
118         void setAutoOpen();
119         ///
120         int getNumber(std::string const & name) const;
121         ///
122         void add(std::string const & name);
123         ///
124         void add(std::string const & name, std::string const & extension,
125                  std::string const & prettyname, std::string const & shortcut,
126                  std::string const & viewer, std::string const & editor,
127                  bool document);
128         ///
129         void erase(std::string const & name);
130         ///
131         void sort();
132         ///
133         void setViewer(std::string const & name, std::string const & command);
134         ///
135         bool view(Buffer const & buffer, std::string const & filename,
136                   std::string const & format_name) const;
137         ///
138         bool edit(Buffer const & buffer, std::string const & filename,
139                   std::string const & format_name) const;
140         ///
141         std::string const prettyName(std::string const & name) const;
142         ///
143         std::string const extension(std::string const & name) const;
144         ///
145         const_iterator begin() const {
146                 return formatlist.begin();
147         }
148         ///
149         const_iterator end() const {
150                 return formatlist.end();
151         }
152         ///
153         FormatList::size_type size() const {
154                 return formatlist.size();
155         }
156 private:
157         ///
158         FormatList formatlist;
159 };
160
161 extern Formats formats;
162
163 extern Formats system_formats;
164
165 #endif //FORMAT_H