]> git.lyx.org Git - features.git/blob - src/frontends/controllers/ControlGraphics.C
6bc9979f828a30865d27092f1752b96c28d98901
[features.git] / src / frontends / controllers / ControlGraphics.C
1 /* This file is part of
2  * ======================================================
3  *
4  *           LyX, The Document Processor
5  *
6  *           Copyright 2001 The LyX Team.
7  *
8  * ======================================================
9  *
10  * \file ControlGraphics.C
11  * \author Angus Leeming <a.leeming@ic.ac.uk>
12  * \author Herbert Voss <voss@perce.de>
13  */
14
15 #include <config.h>
16
17 #ifdef __GNUG__
18 #pragma implementation
19 #endif
20
21 #include "ControlGraphics.h"
22
23 #include "helper_funcs.h"
24
25 #include "converter.h"
26 #include "buffer.h"
27 #include "BufferView.h"
28 #include "Dialogs.h"
29 #include "frontends/LyXView.h"
30 #include "gettext.h"
31 #include "lyxrc.h"
32
33 #include "graphics/GraphicsCache.h"
34 #include "graphics/GraphicsConverter.h"
35 #include "graphics/GraphicsImage.h"
36
37 #include "insets/insetgraphics.h"
38 #include "insets/insetgraphicsParams.h"
39
40 #include "support/lstrings.h"
41 #include "support/filetools.h"
42 #include "support/FileInfo.h"
43
44 using std::pair;
45 using std::make_pair;
46 using std::vector;
47
48 ControlGraphics::ControlGraphics(LyXView & lv, Dialogs & d)
49         : ControlInset<InsetGraphics, InsetGraphicsParams>(lv, d)
50 {}
51
52
53 InsetGraphicsParams const ControlGraphics::getParams(string const &)
54 {
55         return InsetGraphicsParams();
56 }
57
58
59 InsetGraphicsParams const
60 ControlGraphics::getParams(InsetGraphics const & inset)
61 {
62         return inset.params();
63 }
64
65
66 void ControlGraphics::applyParamsToInset()
67 {
68         // Set the parameters in the inset, it also returns true if the new
69         // parameters are different from what was in the inset already.
70         bool changed = inset()->setParams(params(), lv_.buffer()->filePath());
71         // Tell LyX we've got a change, and mark the document dirty,
72         // if it changed.
73         lv_.view()->updateInset(inset(), changed);
74 }
75
76
77 void ControlGraphics::applyParamsNoInset()
78 {}
79
80
81 // We need these in the file browser.
82 extern string system_lyxdir;
83 extern string user_lyxdir;
84
85
86 string const ControlGraphics::Browse(string const & in_name)
87 {
88         string const title = _("Select graphics file");
89
90         // Does user clipart directory exist?
91         string clipdir = AddName (user_lyxdir, "clipart");
92         FileInfo fileInfo(clipdir);
93         if (!(fileInfo.isOK() && fileInfo.isDir()))
94                 // No - bail out to system clipart directory
95                 clipdir = AddName (system_lyxdir, "clipart");
96         pair<string, string> dir1(_("Clipart|#C#c"), clipdir);
97         pair<string, string> dir2(_("Documents|#o#O"), string(lyxrc.document_path));
98         // Show the file browser dialog
99         return browseRelFile(&lv_, in_name, lv_.buffer()->filePath(),
100                              title, "*.*", dir1, dir2);
101 }
102
103
104 string const ControlGraphics::readBB(string const & file)
105 {
106         string const abs_file = MakeAbsPath(file, lv_.buffer()->filePath());
107
108         string const from = getExtFromContents(abs_file);
109         // Check if we have a Postscript file, then it's easy
110         if (contains(from, "ps"))
111                 return readBB_from_PSFile(abs_file);
112
113         // we don't, so ask the Graphics Cache if it has loaded the file
114         int width = 0;
115         int height = 0;
116
117         grfx::GCache & gc = grfx::GCache::get();
118         grfx::ImagePtr const image = gc.image(abs_file);
119
120         if (image.get()) {
121                 width  = image->getWidth();
122                 height = image->getHeight();
123         }
124
125         return ("0 0 " + tostr(width) + ' ' + tostr(height));
126 }
127
128
129 bool ControlGraphics::isFilenameValid(string const & fname) const
130 {
131         // It may be that the filename is relative.
132         string const name = MakeAbsPath(fname, lv_.buffer()->filePath());
133         return IsFileReadable(name);
134 }
135
136
137 namespace frnt {
138
139 namespace {
140
141 // These are the strings that are stored in the LyX file and which
142 // correspond to the LaTeX identifiers shown in the comments at the
143 // end of each line.
144 char const * const rorigin_lyx_strs[] = {
145         "center",                                        // c
146         "leftTop",   "leftBottom",   "leftBaseline",     // lt lb lB
147         "centerTop", "centerBottom", "centerBaseline",   // ct cb cB
148         "rightTop",  "rightBottom",  "rightBaseline" };  // rt rb rB
149
150 // These are the strings, corresponding to the above, that the GUI should
151 // use. Note that they can/should be translated.
152 char const * const rorigin_gui_strs[] = {
153         N_("center"),
154         N_("left top"),   N_("left bottom"),   N_("left baseline"),
155         N_("center top"), N_("center bottom"), N_("center baseline"),
156         N_("right top"),  N_("right bottom"),  N_("right baseline") };
157
158 size_t const rorigin_size = sizeof(rorigin_lyx_strs) / sizeof(char *);
159
160 } // namespace anon
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