]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/ControlGraphics.C
handle some tmpl files in a more sane way
[lyx.git] / src / frontends / controllers / ControlGraphics.C
1 /* This file is part of
2  * ======================================================
3  *
4  *           LyX, The Document Processor
5  *
6  *           Copyright 2001 The LyX Team.
7  *
8  * ======================================================
9  *
10  * \file ControlGraphics.C
11  * \author Angus Leeming <a.leeming@ic.ac.uk>
12  * \author Herbert Voss <voss@perce.de>
13  */
14
15 #include <config.h>
16
17 #ifdef __GNUG__
18 #pragma implementation
19 #endif
20
21 #include "ViewBase.h"
22 #include "ButtonControllerBase.h"
23 #include "ControlGraphics.h"
24
25 #include "helper_funcs.h"
26
27 #include "converter.h"
28 #include "buffer.h"
29 #include "BufferView.h"
30 #include "Dialogs.h"
31 #include "frontends/LyXView.h"
32 #include "gettext.h"
33 #include "lyxrc.h"
34
35 #include "graphics/GraphicsCache.h"
36 #include "graphics/GraphicsConverter.h"
37
38 #include "insets/insetgraphics.h"
39 #include "insets/insetgraphicsParams.h"
40
41 #include "support/lstrings.h"
42 #include "support/filetools.h"
43 #include "support/FileInfo.h"
44
45 #include <boost/bind.hpp>
46
47 #include <fstream>
48
49 using std::pair;
50 using std::make_pair;
51 using std::ifstream;
52 using std::vector;
53
54 namespace {
55
56 // FIXME: currently we need the second '|' to prevent mis-interpretation!
57 // All supported graphic formats with their file-extension and the
58 // gzip-ext for zipped (e)ps-files.
59 // string const grfx_pattern =
60 //      "*.(agr|bmp|eps|epsi|fits|gif|jpg|obj|pdf|pbm|pgm|png|"
61 //      "ppm|ps|tif|tiff|xbm|xpm|xwd|gz)|";
62 vector<string> const grfx_formats()
63 {
64         vector<string> native_formats = grfx::GCache::get().loadableFormats();
65         // We can load any format that can be loaded natively together with
66         // those that can be converted to one of these native formats.
67         vector<string> browsable_formats = native_formats;
68
69         grfx::GConverter const & gconverter = grfx::GConverter::get();
70
71         vector<string>::const_iterator to_end = native_formats.end();
72
73         Formats::const_iterator from_it = formats.begin();
74         Formats::const_iterator from_end = formats.end();
75         for (; from_it != from_end; ++from_it) {
76                 string const from = from_it->name();
77
78                 vector<string>::const_iterator to_it = native_formats.begin();
79                 for (; to_it != to_end; ++to_it) {
80                         if (gconverter.isReachable(from, *to_it)) {
81                                 browsable_formats.push_back(from);
82                                 break;
83                         }
84                 }
85         }
86
87         browsable_formats.push_back("gz");
88
89         return browsable_formats;
90 }
91
92
93 string const xforms_pattern()
94 {
95         vector<string> const browsable_formats = grfx_formats();
96         string const answer =
97                 "*.(" + getStringFromVector(browsable_formats, "|") +")|";
98         return answer;
99 }
100
101 }
102
103
104 ControlGraphics::ControlGraphics(LyXView & lv, Dialogs & d)
105         : ControlInset<InsetGraphics, InsetGraphicsParams>(lv, d)
106 {
107         d_.showGraphics = boost::bind(&ControlGraphics::showInset, this, _1);
108 }
109
110
111 InsetGraphicsParams const ControlGraphics::getParams(string const &)
112 {
113         return InsetGraphicsParams();
114 }
115
116
117 InsetGraphicsParams const
118 ControlGraphics::getParams(InsetGraphics const & inset)
119 {
120         return inset.params();
121 }
122
123
124 void ControlGraphics::applyParamsToInset()
125 {
126         // Set the parameters in the inset, it also returns true if the new
127         // parameters are different from what was in the inset already.
128         bool changed = inset()->setParams(params(), lv_.buffer()->filePath());
129         // Tell LyX we've got a change, and mark the document dirty,
130         // if it changed.
131         lv_.view()->updateInset(inset(), changed);
132 }
133
134
135 void ControlGraphics::applyParamsNoInset()
136 {}
137
138
139 // We need these in the file browser.
140 extern string system_lyxdir;
141 extern string user_lyxdir;
142
143
144 string const ControlGraphics::Browse(string const & in_name)
145 {
146         string const title = _("Select graphics file");
147
148         // Does user clipart directory exist?
149         string clipdir = AddName (user_lyxdir, "clipart");
150         FileInfo fileInfo(clipdir);
151         if (!(fileInfo.isOK() && fileInfo.isDir()))
152                 // No - bail out to system clipart directory
153                 clipdir = AddName (system_lyxdir, "clipart");
154         pair<string, string> dir1(_("Clipart|#C#c"), clipdir);
155         pair<string, string> dir2(_("Documents|#o#O"), string(lyxrc.document_path));
156         // Show the file browser dialog
157         return browseRelFile(&lv_, in_name, lv_.buffer()->filePath(),
158                              title, ::xforms_pattern(), dir1, dir2);
159 }
160
161
162 string const ControlGraphics::readBB(string const & file)
163 {
164         string const abs_file = MakeAbsPath(file, lv_.buffer()->filePath());
165
166         string const from = getExtFromContents(abs_file);
167         // Check if we have a Postscript file, then it's easy
168         if (contains(from, "ps"))
169                 return readBB_from_PSFile(abs_file);
170
171         // we don't, so ask the Graphics Cache if it has loaded the file
172         grfx::GCache & gc = grfx::GCache::get();
173         return ("0 0 " +
174                 tostr(gc.raw_width(abs_file)) + ' ' +
175                 tostr(gc.raw_height(abs_file)));
176 }
177
178
179 bool ControlGraphics::isFilenameValid(string const & fname) const
180 {
181         // It may be that the filename is relative.
182         string const name = MakeAbsPath(fname, lv_.buffer()->filePath());
183         return IsFileReadable(name);
184 }
185
186
187 namespace frnt {
188
189 namespace {
190
191 // These are the strings that are stored in the LyX file and which
192 // correspond to the LaTeX identifiers shown in the comments at the
193 // end of each line.
194 char const * const rorigin_lyx_strs[] = {
195         "center",                                        // c
196         "leftTop",   "leftBottom",   "leftBaseline",     // lt lb lB
197         "centerTop", "centerBottom", "centerBaseline",   // ct cb cB
198         "rightTop",  "rightBottom",  "rightBaseline" };  // rt rb rB
199
200 // These are the strings, corresponding to the above, that the GUI should
201 // use. Note that they can/should be translated.
202 char const * const rorigin_gui_strs[] = {
203         N_("center"),
204         N_("left top"),   N_("left bottom"),   N_("left baseline"),
205         N_("center top"), N_("center bottom"), N_("center baseline"),
206         N_("right top"),  N_("right bottom"),  N_("right baseline") };
207
208 size_t const rorigin_size = sizeof(rorigin_lyx_strs) / sizeof(char *);
209
210 } // namespace anon
211
212 vector<RotationOriginPair> getRotationOriginData()
213 {
214         static vector<RotationOriginPair> data;
215         if (!data.empty())
216                 return data;
217
218         data.resize(rorigin_size);
219         for (lyx::size_type i = 0; i < rorigin_size; ++i) {
220                 data[i] = std::make_pair(_(rorigin_gui_strs[i]),
221                                          rorigin_lyx_strs[i]);
222         }
223
224         return data;
225 }
226
227 } // namespace frnt