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