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