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