]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/ControlGraphics.C
(Herbert): discover the "Bounding Box" of non-(e)ps graphics files, and
[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
27 #include "helper_funcs.h"
28
29 #include "buffer.h"
30 #include "BufferView.h"
31 #include "Dialogs.h"
32 #include "LyXView.h"
33 #include "gettext.h"
34 #include "lyxrc.h"
35
36 #include "graphics/GraphicsCache.h"
37
38 #include "insets/insetgraphics.h"
39 #include "insets/insetgraphicsParams.h"
40
41 #include "support/lstrings.h"
42 #include "support/filetools.h"
43 #include "support/FileInfo.h"
44
45
46 using std::pair;
47 using std::make_pair;
48 using std::ifstream;
49  
50 ControlGraphics::ControlGraphics(LyXView & lv, Dialogs & d)
51         : ControlInset<InsetGraphics, InsetGraphicsParams>(lv, d)
52 {
53         d_.showGraphics.connect(SigC::slot(this, &ControlGraphics::showInset));
54 }
55
56
57 InsetGraphicsParams const ControlGraphics::getParams(string const &)
58 {
59         return InsetGraphicsParams();
60 }
61
62
63 InsetGraphicsParams const
64 ControlGraphics::getParams(InsetGraphics const & inset)
65 {
66         return inset.params();
67 }
68
69
70 void ControlGraphics::applyParamsToInset()
71 {
72         // Set the parameters in the inset, it also returns true if the new
73         // parameters are different from what was in the inset already.
74         bool changed = inset()->setParams(params(), lv_.buffer()->filePath());
75         // Tell LyX we've got a change, and mark the document dirty,
76         // if it changed.
77         lv_.view()->updateInset(inset(), changed);
78 }
79
80
81 void ControlGraphics::applyParamsNoInset()
82 {}
83
84
85 // We need these in the file browser.
86 extern string system_lyxdir;
87 extern string user_lyxdir;
88
89
90 string const ControlGraphics::Browse(string const & in_name)
91 {
92         string const title = _("Select graphics file");
93         // FIXME: currently we need the second '|' to prevent mis-interpretation
94         string const pattern = "*.(ps|eps|png|jpeg|jpg|gif|gz)|";
95
96         // Does user clipart directory exist?
97         string clipdir = AddName (user_lyxdir, "clipart");
98         FileInfo fileInfo(clipdir);
99         if (!(fileInfo.isOK() && fileInfo.isDir()))
100                 // No - bail out to system clipart directory
101                 clipdir = AddName (system_lyxdir, "clipart");
102         pair<string, string> dir1(_("Clipart|#C#c"), clipdir);
103         pair<string, string> dir2(_("Documents|#o#O"), string(lyxrc.document_path));
104         // Show the file browser dialog
105         return browseRelFile(&lv_, in_name, lv_.buffer()->filePath(),
106                              title, pattern, dir1, dir2);
107 }
108
109
110 string const ControlGraphics::readBB(string const & file)
111 {
112         string const abs_file = MakeAbsPath(file, lv_.buffer()->filePath());
113
114         string const from = getExtFromContents(abs_file);       
115         // Check if we have a Postscript file, then it's easy
116         if (contains(from, "ps"))
117                 return readBB_from_PSFile(abs_file);
118
119         // we don't, so ask the Graphics Cache if it has loaded the file
120         grfx::GCache & gc = grfx::GCache::get();
121         return ("0 0 " + 
122                 tostr(gc.raw_width(abs_file)) + ' ' + 
123                 tostr(gc.raw_height(abs_file)));
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, lv_.buffer()->filePath());
131         return IsFileReadable(name);
132 }