]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/ControlGraphics.C
fix tooltips in toolbar
[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 <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/GraphicsCacheItem.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
72         // Tell LyX we've got a change, and mark the document dirty,
73         // if it changed.
74         lv_.view()->updateInset(inset(), changed);
75 }
76
77
78 void ControlGraphics::applyParamsNoInset()
79 {}
80
81
82 // We need these in the file browser.
83 extern string system_lyxdir;
84 extern string user_lyxdir;
85
86
87 string const ControlGraphics::Browse(string const & in_name)
88 {
89         string const title = _("Select graphics file");
90
91         // Does user clipart directory exist?
92         string clipdir = AddName (user_lyxdir, "clipart");
93         FileInfo fileInfo(clipdir);
94         if (!(fileInfo.isOK() && fileInfo.isDir()))
95                 // No - bail out to system clipart directory
96                 clipdir = AddName (system_lyxdir, "clipart");
97         pair<string, string> dir1(_("Clipart|#C#c"), clipdir);
98         pair<string, string> dir2(_("Documents|#o#O"), string(lyxrc.document_path));
99         // Show the file browser dialog
100         return browseRelFile(&lv_, in_name, lv_.buffer()->filePath(),
101                              title, "*.*", dir1, dir2);
102 }
103
104
105 string const ControlGraphics::readBB(string const & file)
106 {
107         string const abs_file = MakeAbsPath(file, lv_.buffer()->filePath());
108
109         // try to get it from the file, if possible. Zipped files are
110         // unzipped in the readBB_from_PSFile-Function
111         string const bb = readBB_from_PSFile(abs_file);
112         if (!bb.empty())
113                 return bb;
114
115         // we don't, so ask the Graphics Cache if it has loaded the file
116         int width = 0;
117         int height = 0;
118
119         grfx::Cache & gc = grfx::Cache::get();
120         if (gc.inCache(abs_file)) {
121                 grfx::Image const * image = gc.item(abs_file)->image();
122
123                 if (image) {
124                         width  = image->getWidth();
125                         height = image->getHeight();
126                 }
127         }
128
129         return ("0 0 " + tostr(width) + ' ' + tostr(height));
130 }
131
132
133 bool ControlGraphics::isFilenameValid(string const & fname) const
134 {
135         // It may be that the filename is relative.
136         string const name = MakeAbsPath(fname, lv_.buffer()->filePath());
137         return IsFileReadable(name);
138 }
139
140
141 namespace frnt {
142
143 namespace {
144
145 // These are the strings that are stored in the LyX file and which
146 // correspond to the LaTeX identifiers shown in the comments at the
147 // end of each line.
148 char const * const rorigin_lyx_strs[] = {
149         // the LaTeX default is leftBaseline
150         "center",                                        // c
151         "leftTop",   "leftBottom",   "leftBaseline",     // lt lb lB
152         "centerTop", "centerBottom", "centerBaseline",   // ct cb cB
153         "rightTop",  "rightBottom",  "rightBaseline" };  // rt rb rB
154
155 // These are the strings, corresponding to the above, that the GUI should
156 // use. Note that they can/should be translated.
157 char const * const rorigin_gui_strs[] = {
158         N_("center"),
159         N_("left top"),   N_("left bottom"),   N_("left baseline"),
160         N_("center top"), N_("center bottom"), N_("center baseline"),
161         N_("right top"),  N_("right bottom"),  N_("right baseline") };
162
163 size_t const rorigin_size = sizeof(rorigin_lyx_strs) / sizeof(char *);
164
165 } // namespace anon
166
167
168 vector<RotationOriginPair> getRotationOriginData()
169 {
170         static vector<RotationOriginPair> data;
171         if (!data.empty())
172                 return data;
173
174         data.resize(rorigin_size);
175         for (lyx::size_type i = 0; i < rorigin_size; ++i) {
176                 data[i] = make_pair(_(rorigin_gui_strs[i]),
177                                     rorigin_lyx_strs[i]);
178         }
179
180         return data;
181 }
182
183 } // namespace frnt