]> git.lyx.org Git - lyx.git/blob - src/mover.h
GTK graphics dialog: Default to scaling 100% when no scaling or size is given
[lyx.git] / src / mover.h
1 // -*- C++ -*-
2 /**
3  * \file mover.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Angus Leeming
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #ifndef MOVER_H
13 #define MOVER_H
14
15 #include <map>
16 #include <string>
17
18 /**
19  *  Utility to copy a file of a specified format from one place to another.
20  *  This base class simply invokes the command support::copy().
21  */
22 class Mover
23 {
24 public:
25         virtual ~Mover() {}
26
27         /** Copy file @c from to @c to.
28          *  This version should be used to copy files from the original
29          *  location to the temporary directory, since @c to and @c latex
30          *  would be equal in this case.
31          *  \returns true if successful.
32          */
33         bool
34         copy(std::string const & from, std::string const & to) const
35         {
36                 return do_copy(from, to, to);
37         }
38
39         /** Copy file @c from to @c to.
40          *  \see SpecialisedMover::SpecialisedMover() for an explanation of
41          *  @c latex.
42          *  This version should be used to copy files from the temporary
43          *  directory to the export location, since @c to and @c latex may
44          *  not be equal in this case.
45          *  \returns true if successful.
46          */
47         bool
48         copy(std::string const & from, std::string const & to,
49              std::string const & latex) const
50         {
51                 return do_copy(from, to, latex);
52         }
53
54         /** Rename file @c from as @c to.
55          *  This version should be used to move files from the original
56          *  location to the temporary directory, since @c to and @c latex
57          *  would be equal in this case.
58          *  \returns true if successful.
59          */
60         bool
61         rename(std::string const & from, std::string const & to) const
62         {
63                 return do_rename(from, to, to);
64         }
65
66         /** Rename file @c from as @c to.
67          *  \see SpecialisedMover::SpecialisedMover() for an explanation of
68          *  @c latex.
69          *  This version should be used to move files from the temporary
70          *  directory to the export location, since @c to and @c latex may
71          *  not be equal in this case.
72          *  \returns true if successful.
73          */
74         bool
75         rename(std::string const & from, std::string const & to,
76                std::string const & latex) const
77         {
78                 return do_rename(from, to, latex);
79         }
80
81 protected:
82         virtual bool
83         do_copy(std::string const & from, std::string const & to,
84                 std::string const &) const;
85
86         virtual bool
87         do_rename(std::string const & from, std::string const & to,
88                   std::string const &) const;
89 };
90
91
92 /**
93  *  Specialisation of the Mover concept that uses an external command
94  *  to copy a file.
95  *
96  *  For example, an XFig .fig file can contain references to external
97  *  picture files. If such a reference has a relative path, then the
98  *  copied .fig file will require a transformation of the picture file
99  *  reference if it is to be found by XFig.
100  */
101 class SpecialisedMover : public Mover
102 {
103 public:
104         SpecialisedMover() {}
105
106         /** @c command should be of the form
107          *  <code>
108          *      sh $$s/scripts/fig_copy.sh $$i $$o $$l
109          *  </code>
110          *  where $$s is a placeholder for the lyx support directory,
111          *        $$i is a placeholder for the name of the file to be moved,
112          *        $$o is a placeholder for the name of the file after moving,
113          *        $$l is a placeholder for the name of the file after moving,
114          *        suitable as argument to a latex include command. This is
115          *        either an absolute filename or relative to the master
116          *        document.
117          *        $$o and $$l can only differ if the file is copied from the
118          *        temporary directory to the export location. If it is copied
119          *        from the original location to the temporary directory, they
120          *        are the same, so $$l may be ommitted in this case.
121          */
122         SpecialisedMover(std::string const & command)
123                 : command_(command) {}
124
125         /// The template used to launch the external command.
126         std::string const & command() const { return command_; }
127
128 private:
129         virtual bool
130         do_copy(std::string const & from, std::string const & to,
131                 std::string const & latex) const;
132
133         virtual bool
134         do_rename(std::string const & from, std::string const & to,
135                   std::string const & latex) const;
136
137         std::string command_;
138 };
139
140
141 /**
142  *  Manage the store of (Mover)s.
143  */
144 class Movers
145 {
146 public:
147         /** Register a specialised @c command to be used to copy a file
148          *  of format @c fmt.
149          */
150         void set(std::string const & fmt, std::string const & command);
151
152         /// @c returns the Mover registered for format @c fmt.
153         Mover const & operator()(std::string  const & fmt) const;
154
155         /** @returns the command template if @c fmt 'finds' a
156          *  SpecialisedMover. Otherwise, returns an empty string.
157          */
158         std::string const command(std::string  const & fmt) const;
159
160 private:
161         typedef std::map<std::string, SpecialisedMover> SpecialsMap;
162
163 public:
164         typedef SpecialsMap::const_iterator iterator;
165         iterator begin() const { return specials_.begin(); }
166         iterator end() const { return specials_.end(); }
167
168 private:
169         Mover default_;
170         SpecialsMap specials_;
171 };
172
173
174 extern Movers movers;
175 extern Movers system_movers;
176
177 #endif // MOVER_H