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