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