]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/ControlGraphics.C
Rob's patch and some minor cleanup
[lyx.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 <leeming@lyx.org>
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 "buffer.h"
26 #include "BufferView.h"
27 #include "converter.h"
28 #include "gettext.h"
29 #include "lyxrc.h"
30
31 #include "graphics/GraphicsCache.h"
32 #include "graphics/GraphicsCacheItem.h"
33 #include "graphics/GraphicsImage.h"
34
35 #include "insets/insetgraphics.h"
36 #include "insets/insetgraphicsParams.h"
37
38 #include "support/lstrings.h"
39 #include "support/filetools.h"
40 #include "support/FileInfo.h"
41
42 using std::pair;
43 using std::make_pair;
44 using std::vector;
45
46 ControlGraphics::ControlGraphics(LyXView & lv, Dialogs & d)
47         : ControlInset<InsetGraphics, InsetGraphicsParams>(lv, d)
48 {}
49
50
51 InsetGraphicsParams const ControlGraphics::getParams(string const &)
52 {
53         return InsetGraphicsParams();
54 }
55
56
57 InsetGraphicsParams const
58 ControlGraphics::getParams(InsetGraphics const & inset)
59 {
60         return inset.params();
61 }
62
63
64 void ControlGraphics::applyParamsToInset()
65 {
66         // Set the parameters in the inset, it also returns true if the new
67         // parameters are different from what was in the inset already.
68         bool changed = inset()->setParams(params(), buffer()->filePath());
69
70         // Tell LyX we've got a change, and mark the document dirty,
71         // if it changed.
72         bufferview()->updateInset(inset(), changed);
73 }
74
75
76 void ControlGraphics::applyParamsNoInset()
77 {}
78
79
80 // We need these in the file browser.
81 extern string system_lyxdir;
82 extern string user_lyxdir;
83
84
85 string const ControlGraphics::Browse(string const & in_name)
86 {
87         string const title = _("Select graphics file");
88
89         // Does user clipart directory exist?
90         string clipdir = AddName (user_lyxdir, "clipart");
91         FileInfo fileInfo(clipdir);
92         if (!(fileInfo.isOK() && fileInfo.isDir()))
93                 // No - bail out to system clipart directory
94                 clipdir = AddName (system_lyxdir, "clipart");
95         pair<string, string> dir1(_("Clipart|#C#c"), clipdir);
96         pair<string, string> dir2(_("Documents|#o#O"), string(lyxrc.document_path));
97         // Show the file browser dialog
98         return browseRelFile(&lv_, in_name, buffer()->filePath(),
99                              title, "*.*", dir1, dir2);
100 }
101
102
103 string const ControlGraphics::readBB(string const & file)
104 {
105         string const abs_file = MakeAbsPath(file, buffer()->filePath());
106
107         // try to get it from the file, if possible. Zipped files are
108         // unzipped in the readBB_from_PSFile-Function
109         string const bb = readBB_from_PSFile(abs_file);
110         if (!bb.empty())
111                 return bb;
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::Cache & gc = grfx::Cache::get();
118         if (gc.inCache(abs_file)) {
119                 grfx::Image const * image = gc.item(abs_file)->image();
120
121                 if (image) {
122                         width  = image->getWidth();
123                         height = image->getHeight();
124                 }
125         }
126
127         return ("0 0 " + tostr(width) + ' ' + tostr(height));
128 }
129
130
131 bool ControlGraphics::isFilenameValid(string const & fname) const
132 {
133         // It may be that the filename is relative.
134         string const name = MakeAbsPath(fname, buffer()->filePath());
135         return IsFileReadable(name);
136 }
137
138
139 namespace frnt {
140
141 namespace {
142
143 // These are the strings that are stored in the LyX file and which
144 // correspond to the LaTeX identifiers shown in the comments at the
145 // end of each line.
146 char const * const rorigin_lyx_strs[] = {
147         // the LaTeX default is leftBaseline
148         "leftBaseline",   "leftTop",   "leftBottom",             // lB lt lb
149         "center", "centerBaseline", "centerTop", "centerBottom", // c cB ct cb
150         "rightBaseline",  "rightTop",  "rightBottom"  };         // 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_("left baseline"), N_("left top"), N_("left bottom"),
156         N_("center"), N_("center baseline"), N_("center top"), N_("center bottom"),
157         N_("right baseline"), N_("right top"),  N_("right bottom") };
158
159 size_t const rorigin_size = sizeof(rorigin_lyx_strs) / sizeof(char *);
160
161 } // namespace anon
162
163
164 vector<RotationOriginPair> getRotationOriginData()
165 {
166         static vector<RotationOriginPair> data;
167         if (!data.empty())
168                 return data;
169
170         data.resize(rorigin_size);
171         for (lyx::size_type i = 0; i < rorigin_size; ++i) {
172                 data[i] = make_pair(_(rorigin_gui_strs[i]),
173                                     rorigin_lyx_strs[i]);
174         }
175
176         return data;
177 }
178
179 } // namespace frnt