]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/ControlGraphics.C
Herbert's bb units fix
[lyx.git] / src / frontends / controllers / ControlGraphics.C
1 /**
2  * \file ControlGraphics.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Angus Leeming
7  * \author Herbert Voss
8  *
9  * Full author contact details are available in file CREDITS
10  */
11
12 #include <config.h>
13
14 #ifdef __GNUG__
15 #pragma implementation
16 #endif
17
18 #include "ControlGraphics.h"
19
20 #include "helper_funcs.h"
21
22 #include "buffer.h"
23 #include "BufferView.h"
24 #include "converter.h"
25 #include "gettext.h"
26 #include "lyxrc.h"
27
28 #include "graphics/GraphicsCache.h"
29 #include "graphics/GraphicsCacheItem.h"
30 #include "graphics/GraphicsImage.h"
31
32 #include "insets/insetgraphics.h"
33 #include "insets/insetgraphicsParams.h"
34
35 #include "support/lstrings.h"
36 #include "support/filetools.h"
37 #include "support/FileInfo.h"
38
39 using std::pair;
40 using std::make_pair;
41 using std::vector;
42
43 // We need these in the file browser.
44 extern string system_lyxdir;
45 extern string user_lyxdir;
46
47
48 ControlGraphics::ControlGraphics(LyXView & lv, Dialogs & d)
49         : ControlInset<InsetGraphics, InsetGraphicsParams>(lv, d)
50 {}
51
52
53 InsetGraphicsParams const ControlGraphics::getParams(string const &)
54 {
55         return InsetGraphicsParams();
56 }
57
58
59 InsetGraphicsParams const
60 ControlGraphics::getParams(InsetGraphics const & inset)
61 {
62         return inset.params();
63 }
64
65
66 void ControlGraphics::applyParamsToInset()
67 {
68         // Set the parameters in the inset, it also returns true if the new
69         // parameters are different from what was in the inset already.
70         bool changed = inset()->setParams(params(), buffer()->filePath());
71
72         // Tell LyX we've got a change, and mark the document dirty,
73         // if it changed.
74         bufferview()->updateInset(inset(), changed);
75 }
76
77
78 void ControlGraphics::applyParamsNoInset()
79 {}
80
81
82 string const ControlGraphics::Browse(string const & in_name)
83 {
84         string const title = _("Select graphics file");
85
86         // Does user clipart directory exist?
87         string clipdir = AddName (user_lyxdir, "clipart");
88         FileInfo fileInfo(clipdir);
89         if (!(fileInfo.isOK() && fileInfo.isDir()))
90                 // No - bail out to system clipart directory
91                 clipdir = AddName (system_lyxdir, "clipart");
92         pair<string, string> dir1(_("Clipart|#C#c"), clipdir);
93         pair<string, string> dir2(_("Documents|#o#O"), string(lyxrc.document_path));
94         // Show the file browser dialog
95         return browseRelFile(&lv_, in_name, buffer()->filePath(),
96                              title, "*.*", dir1, dir2);
97 }
98
99
100 string const ControlGraphics::readBB(string const & file)
101 {
102         string const abs_file = MakeAbsPath(file, buffer()->filePath());
103
104         // try to get it from the file, if possible. Zipped files are
105         // unzipped in the readBB_from_PSFile-Function
106         string const bb = readBB_from_PSFile(abs_file);
107         if (!bb.empty())
108                 return bb;
109
110         // we don't, so ask the Graphics Cache if it has loaded the file
111         int width = 0;
112         int height = 0;
113
114         grfx::Cache & gc = grfx::Cache::get();
115         if (gc.inCache(abs_file)) {
116                 grfx::Image const * image = gc.item(abs_file)->image();
117
118                 if (image) {
119                         width  = image->getWidth();
120                         height = image->getHeight();
121                 }
122         }
123
124         return ("0 0 " + tostr(width) + ' ' + tostr(height));
125 }
126
127
128 bool ControlGraphics::isFilenameValid(string const & fname) const
129 {
130         // It may be that the filename is relative.
131         string const name = MakeAbsPath(fname, buffer()->filePath());
132         return IsFileReadable(name);
133 }
134
135
136 namespace {
137
138 static char const * bb_units[] = { "bp", "cm", "mm", "in"};
139 size_t const bb_size = sizeof(bb_units) / sizeof(char *);
140
141 }
142
143
144 vector<string> const ControlGraphics::getUnits()
145 {
146         static vector<string> data;
147         if (!data.empty())
148                 return data;
149
150         data.resize(bb_size);
151         for (lyx::size_type i = 0; i < bb_size; ++i) {
152                 data[i] = bb_units[i];
153         }
154         return data;
155 }
156
157
158 namespace frnt {
159
160 namespace {
161
162 // These are the strings that are stored in the LyX file and which
163 // correspond to the LaTeX identifiers shown in the comments at the
164 // end of each line.
165 char const * const rorigin_lyx_strs[] = {
166         // the LaTeX default is leftBaseline
167         "leftTop",  "leftBottom", "leftBaseline", // lt lb lB
168         "center", "centerTop", "centerBottom", "centerBaseline", // c ct cb cB
169         "rightTop", "rightBottom", "rightBaseline" }; // rt rb rB
170
171 // These are the strings, corresponding to the above, that the GUI should
172 // use. Note that they can/should be translated.
173 char const * const rorigin_gui_strs[] = {
174         N_("Top left"), N_("Bottom left"), N_("Left baseline"),
175         N_("Center"), N_("Top center"), N_("Bottom center"), N_("Center baseline"),
176         N_("Top right"), N_("Bottom right"), N_("Right baseline") };
177
178 size_t const rorigin_size = sizeof(rorigin_lyx_strs) / sizeof(char *);
179
180 } // namespace anon
181
182
183 vector<RotationOriginPair> getRotationOriginData()
184 {
185         static vector<RotationOriginPair> data;
186         if (!data.empty())
187                 return data;
188
189         data.resize(rorigin_size);
190         for (lyx::size_type i = 0; i < rorigin_size; ++i) {
191                 data[i] = make_pair(_(rorigin_gui_strs[i]),
192                                     rorigin_lyx_strs[i]);
193         }
194
195         return data;
196 }
197
198 } // namespace frnt