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