]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/ControlGraphics.C
(Herbert): expand the list of recognised graphics formats.
[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 namespace {
51
52 // FIXME: currently we need the second '|' to prevent mis-interpretation!
53 // All supported graphic formats with their file-extension and the
54 // gzip-ext for zipped (e)ps-files.
55 string const grfx_pattern = 
56         "*.(agr|bmp|eps|epsi|fits|gif|jpg|obj|pdf|pbm|pgm|png|"
57         "ppm|ps|tif|tiff|xbm|xpm|xwd|gz)|";             
58
59 }
60
61  
62 ControlGraphics::ControlGraphics(LyXView & lv, Dialogs & d)
63         : ControlInset<InsetGraphics, InsetGraphicsParams>(lv, d)
64 {
65         d_.showGraphics.connect(SigC::slot(this, &ControlGraphics::showInset));
66 }
67
68
69 InsetGraphicsParams const ControlGraphics::getParams(string const &)
70 {
71         return InsetGraphicsParams();
72 }
73
74
75 InsetGraphicsParams const
76 ControlGraphics::getParams(InsetGraphics const & inset)
77 {
78         return inset.params();
79 }
80
81
82 void ControlGraphics::applyParamsToInset()
83 {
84         // Set the parameters in the inset, it also returns true if the new
85         // parameters are different from what was in the inset already.
86         bool changed = inset()->setParams(params(), lv_.buffer()->filePath());
87         // Tell LyX we've got a change, and mark the document dirty,
88         // if it changed.
89         lv_.view()->updateInset(inset(), changed);
90 }
91
92
93 void ControlGraphics::applyParamsNoInset()
94 {}
95
96
97 // We need these in the file browser.
98 extern string system_lyxdir;
99 extern string user_lyxdir;
100
101
102 string const ControlGraphics::Browse(string const & in_name)
103 {
104         string const title = _("Select graphics file");
105
106         // Does user clipart directory exist?
107         string clipdir = AddName (user_lyxdir, "clipart");
108         FileInfo fileInfo(clipdir);
109         if (!(fileInfo.isOK() && fileInfo.isDir()))
110                 // No - bail out to system clipart directory
111                 clipdir = AddName (system_lyxdir, "clipart");
112         pair<string, string> dir1(_("Clipart|#C#c"), clipdir);
113         pair<string, string> dir2(_("Documents|#o#O"), string(lyxrc.document_path));
114         // Show the file browser dialog
115         return browseRelFile(&lv_, in_name, lv_.buffer()->filePath(),
116                              title, ::grfx_pattern, dir1, dir2);
117 }
118
119
120 string const ControlGraphics::readBB(string const & file)
121 {
122         string const abs_file = MakeAbsPath(file, lv_.buffer()->filePath());
123
124         string const from = getExtFromContents(abs_file);       
125         // Check if we have a Postscript file, then it's easy
126         if (contains(from, "ps"))
127                 return readBB_from_PSFile(abs_file);
128
129         // we don't, so ask the Graphics Cache if it has loaded the file
130         grfx::GCache & gc = grfx::GCache::get();
131         return ("0 0 " + 
132                 tostr(gc.raw_width(abs_file)) + ' ' + 
133                 tostr(gc.raw_height(abs_file)));
134 }
135
136
137 bool ControlGraphics::isFilenameValid(string const & fname) const
138 {
139         // It may be that the filename is relative.
140         string const name = MakeAbsPath(fname, lv_.buffer()->filePath());
141         return IsFileReadable(name);
142 }