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