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