]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/ControlGraphics.C
Herbert's and my fixes to the graphics inset.
[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 #include <fstream>
17
18 #ifdef __GNUG__
19 #pragma implementation
20 #endif
21
22 #include "ViewBase.h"
23 #include "ButtonControllerBase.h"
24 #include "ControlGraphics.h"
25 #include "ControlInset.tmpl"
26 #include "buffer.h"
27 #include "BufferView.h"
28 #include "Dialogs.h"
29 #include "LyXView.h"
30 #include "gettext.h"
31 #include "lyxrc.h"
32
33 #include "insets/insetgraphics.h"
34 #include "insets/insetgraphicsParams.h" // need operator!=()
35
36 #include "support/FileInfo.h"  // for FileInfo
37 #include "helper_funcs.h"
38 #include "support/lstrings.h"
39 #include "support/filetools.h" // for AddName, zippedFile
40
41 using std::pair;
42 using std::make_pair;
43 using std::ifstream;
44
45 ControlGraphics::ControlGraphics(LyXView & lv, Dialogs & d)
46         : ControlInset<InsetGraphics, InsetGraphicsParams>(lv, d)
47 {
48         d_.showGraphics.connect(SigC::slot(this, &ControlGraphics::showInset));
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 = N_("Select graphics file");
88         // FIXME: currently we need the second '|' to prevent mis-interpretation
89         string const pattern = "*.(ps|eps|png|jpeg|jpg|gif|gz)|";
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(N_("Clipart|#C#c"), clipdir);
98         pair<string, string> dir2(N_("Documents|#o#O"), string(lyxrc.document_path));
99         // Show the file browser dialog
100         return browseRelFile(&lv_, in_name, lv_.buffer()->filePath(),
101                              title, pattern, dir1, dir2);
102 }
103
104
105 string const ControlGraphics::readBB(string const & file)
106 {
107         // in a file it's an entry like %%BoundingBox:23 45 321 345
108         // The first number can following without a space, so we have
109         // to be a little careful.
110         // On the other hand some plot programs write the bb at the
111         // end of the file. Than we have in the header:
112         // %%BoundingBox: (atend)
113         // In this case we must check the end.
114         string file_ = file;
115         if (zippedFile(file_))
116             file_ = unzipFile(file_);
117
118         string const format = getExtFromContents(file_);
119         if (format != "eps" && format != "ps")
120                 return string();
121
122         std::ifstream is(file_.c_str());
123         while (is) {
124                 string s;
125                 is >> s;
126                 if (contains(s,"%%BoundingBox:")) {
127                         string a, b, c, d;
128                         is >> a >> b >> c >> d;
129                         if (is && !contains(a,"atend")) { // bb at the end?
130                                 if (s != "%%BoundingBox:")
131                                     return (s.substr(14)+" "+a+" "+b+" "+c+" ");
132                                 else
133                                     return (a+" "+b+" "+c+" "+d+" ");
134                         }
135                 }
136         }
137         return string();
138 }
139
140
141 bool ControlGraphics::isFilenameValid(string const & fname) const
142 {
143         // It may be that the filename is relative.
144         string const name = MakeAbsPath(fname, lv_.buffer()->filePath());
145         return IsFileReadable(name);
146 }