]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/ControlGraphics.C
Learing to fly...
[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 #include "ControlGraphics.h"
15
16 #include "helper_funcs.h"
17
18 #include "BufferView.h"
19 #include "funcrequest.h"
20 #include "gettext.h"
21 #include "lyxrc.h"
22
23 #include "graphics/GraphicsCache.h"
24 #include "graphics/GraphicsCacheItem.h"
25 #include "graphics/GraphicsImage.h"
26
27 #include "insets/insetgraphics.h"
28 #include "insets/insetgraphicsParams.h"
29
30 #include "support/tostr.h"
31 #include "support/filetools.h"
32 #include "support/FileInfo.h"
33 #include "support/path_defines.h"
34
35 using namespace lyx::support;
36
37 using std::pair;
38 using std::make_pair;
39 using std::vector;
40
41 // We need these in the file browser.
42 extern string user_lyxdir;
43
44
45 ControlGraphics::ControlGraphics(Dialog & parent)
46         : Dialog::Controller(parent)
47 {}
48
49
50 bool ControlGraphics::initialiseParams(string const & data)
51 {
52         InsetGraphicsParams params;
53         InsetGraphicsMailer::string2params(data, *kernel().buffer(), params);
54         params_.reset(new InsetGraphicsParams(params));
55         return true;
56 }
57
58
59 void ControlGraphics::clearParams()
60 {
61         params_.reset();
62 }
63
64
65 void ControlGraphics::dispatchParams()
66 {
67         InsetGraphicsParams tmp_params(params());
68         string const lfun =
69                 InsetGraphicsMailer::params2string(tmp_params, *kernel().buffer());
70         kernel().dispatch(FuncRequest(LFUN_INSET_APPLY, lfun));
71 }
72
73
74 string const ControlGraphics::Browse(string const & in_name)
75 {
76         string const title = _("Select graphics file");
77
78         // Does user clipart directory exist?
79         string clipdir = AddName (user_lyxdir, "clipart");
80         FileInfo fileInfo(clipdir);
81         if (!(fileInfo.isOK() && fileInfo.isDir()))
82                 // No - bail out to system clipart directory
83                 clipdir = AddName (system_lyxdir(), "clipart");
84         pair<string, string> dir1(_("Clipart|#C#c"), clipdir);
85         pair<string, string> dir2(_("Documents|#o#O"), string(lyxrc.document_path));
86         // Show the file browser dialog
87         return browseRelFile(in_name, kernel().bufferFilepath(),
88                              title, "*.*", false, dir1, dir2);
89 }
90
91
92 string const ControlGraphics::readBB(string const & file)
93 {
94         string const abs_file =
95                 MakeAbsPath(file, kernel().bufferFilepath());
96
97         // try to get it from the file, if possible. Zipped files are
98         // unzipped in the readBB_from_PSFile-Function
99         string const bb = readBB_from_PSFile(abs_file);
100         if (!bb.empty())
101                 return bb;
102
103         // we don't, so ask the Graphics Cache if it has loaded the file
104         int width = 0;
105         int height = 0;
106
107         lyx::graphics::Cache & gc = lyx::graphics::Cache::get();
108         if (gc.inCache(abs_file)) {
109                 lyx::graphics::Image const * image = gc.item(abs_file)->image();
110
111                 if (image) {
112                         width  = image->getWidth();
113                         height = image->getHeight();
114                 }
115         }
116
117         return ("0 0 " + tostr(width) + ' ' + tostr(height));
118 }
119
120
121 bool ControlGraphics::isFilenameValid(string const & fname) const
122 {
123         // It may be that the filename is relative.
124         string const name = MakeAbsPath(fname, kernel().bufferFilepath());
125         return IsFileReadable(name);
126 }
127
128
129 namespace frnt {
130
131 namespace {
132
133 char const * const bb_units[] = { "bp", "cm", "mm", "in" };
134 size_t const bb_size = sizeof(bb_units) / sizeof(char *);
135
136 // These are the strings that are stored in the LyX file and which
137 // correspond to the LaTeX identifiers shown in the comments at the
138 // end of each line.
139 char const * const rorigin_lyx_strs[] = {
140         // the LaTeX default is leftBaseline
141         "",
142         "leftTop",  "leftBottom", "leftBaseline", // lt lb lB
143         "center", "centerTop", "centerBottom", "centerBaseline", // c ct cb cB
144         "rightTop", "rightBottom", "rightBaseline" }; // rt rb rB
145
146 // These are the strings, corresponding to the above, that the GUI should
147 // use. Note that they can/should be translated.
148 char const * const rorigin_gui_strs[] = {
149         N_("Default"),
150         N_("Top left"), N_("Bottom left"), N_("Left baseline"),
151         N_("Center"), N_("Top center"), N_("Bottom center"), N_("Center baseline"),
152         N_("Top right"), N_("Bottom right"), N_("Right baseline") };
153
154 size_t const rorigin_size = sizeof(rorigin_lyx_strs) / sizeof(char *);
155
156 } // namespace anon
157
158
159 vector<string> const getBBUnits()
160 {
161         return vector<string> (bb_units, bb_units + bb_size);
162 }
163
164
165 vector<RotationOriginPair> getRotationOriginData()
166 {
167         static vector<RotationOriginPair> data;
168         if (!data.empty())
169                 return data;
170
171         data.resize(rorigin_size);
172         for (lyx::size_type i = 0; i < rorigin_size; ++i) {
173                 data[i] = make_pair(_(rorigin_gui_strs[i]),
174                                     rorigin_lyx_strs[i]);
175         }
176
177         return data;
178 }
179
180 } // namespace frnt