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