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