]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/ControlGraphics.C
Herbert's graphics diff
[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/GraphicsConverter.h"
35
36 #include "insets/insetgraphics.h"
37 #include "insets/insetgraphicsParams.h"
38
39 #include "support/lstrings.h"
40 #include "support/filetools.h"
41 #include "support/FileInfo.h"
42
43 using std::pair;
44 using std::make_pair;
45 using std::vector;
46
47 ControlGraphics::ControlGraphics(LyXView & lv, Dialogs & d)
48         : ControlInset<InsetGraphics, InsetGraphicsParams>(lv, d)
49 {}
50
51
52 InsetGraphicsParams const ControlGraphics::getParams(string const &)
53 {
54         return InsetGraphicsParams();
55 }
56
57
58 InsetGraphicsParams const
59 ControlGraphics::getParams(InsetGraphics const & inset)
60 {
61         return inset.params();
62 }
63
64
65 void ControlGraphics::applyParamsToInset()
66 {
67         // Set the parameters in the inset, it also returns true if the new
68         // parameters are different from what was in the inset already.
69         bool changed = inset()->setParams(params(), lv_.buffer()->filePath());
70         // Tell LyX we've got a change, and mark the document dirty,
71         // if it changed.
72         lv_.view()->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, lv_.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, lv_.buffer()->filePath());
106
107         string const from = getExtFromContents(abs_file);
108         // Check if we have a Postscript file, then it's easy
109         if (contains(from, "ps"))
110                 return readBB_from_PSFile(abs_file);
111
112         // we don't, so ask the Graphics Cache if it has loaded the file
113         grfx::GCache & gc = grfx::GCache::get();
114         return ("0 0 " +
115                 tostr(gc.raw_width(abs_file)) + ' ' +
116                 tostr(gc.raw_height(abs_file)));
117 }
118
119
120 bool ControlGraphics::isFilenameValid(string const & fname) const
121 {
122         // It may be that the filename is relative.
123         string const name = MakeAbsPath(fname, lv_.buffer()->filePath());
124         return IsFileReadable(name);
125 }
126
127
128 namespace frnt {
129
130 namespace {
131
132 // These are the strings that are stored in the LyX file and which
133 // correspond to the LaTeX identifiers shown in the comments at the
134 // end of each line.
135 char const * const rorigin_lyx_strs[] = {
136         "center",                                        // c
137         "leftTop",   "leftBottom",   "leftBaseline",     // lt lb lB
138         "centerTop", "centerBottom", "centerBaseline",   // ct cb cB
139         "rightTop",  "rightBottom",  "rightBaseline" };  // rt rb rB
140
141 // These are the strings, corresponding to the above, that the GUI should
142 // use. Note that they can/should be translated.
143 char const * const rorigin_gui_strs[] = {
144         N_("center"),
145         N_("left top"),   N_("left bottom"),   N_("left baseline"),
146         N_("center top"), N_("center bottom"), N_("center baseline"),
147         N_("right top"),  N_("right bottom"),  N_("right baseline") };
148
149 size_t const rorigin_size = sizeof(rorigin_lyx_strs) / sizeof(char *);
150
151 } // namespace anon
152
153
154 vector<RotationOriginPair> getRotationOriginData()
155 {
156         static vector<RotationOriginPair> data;
157         if (!data.empty())
158                 return data;
159
160         data.resize(rorigin_size);
161         for (lyx::size_type i = 0; i < rorigin_size; ++i) {
162                 data[i] = make_pair(_(rorigin_gui_strs[i]),
163                                     rorigin_lyx_strs[i]);
164         }
165
166         return data;
167 }
168
169 } // namespace frnt