]> git.lyx.org Git - features.git/blob - src/frontends/controllers/ControlGraphics.C
5b94c7b66bc9d7e4d84a36b6f025442e86676784
[features.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 "Dialogs.h"
28 #include "LyXView.h"
29 #include "gettext.h"
30 #include "lyxrc.h"
31
32 #include "insets/insetgraphics.h"
33 #include "insets/insetgraphicsParams.h" // need operator!=()
34
35 #include "support/FileInfo.h"  // for FileInfo
36 #include "helper_funcs.h"      // for browseFile
37 #include "support/lstrings.h"
38 #include "support/filetools.h" // for AddName
39 #include "support/syscall.h"    // for zippedFile()
40 #include "BufferView.h"
41
42 using std::pair;
43 using std::make_pair;
44 using std::ifstream;
45
46 ControlGraphics::ControlGraphics(LyXView & lv, Dialogs & d)
47         : ControlInset<InsetGraphics, InsetGraphicsParams>(lv, d)
48 {
49         d_.showGraphics.connect(SigC::slot(this, &ControlGraphics::showInset));
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.getParams();
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());
71         // Tell LyX we've got a change, and mark the document dirty,
72         // if it changed.
73         lv_.view()->updateInset(inset(), changed);
74 }
75
76
77 void ControlGraphics::applyParamsNoInset()
78 {}
79
80
81 // We need these in the file browser.
82 extern string system_lyxdir;
83 extern string user_lyxdir;
84
85 string const ControlGraphics::Browse(string const & in_name)
86 {
87         string const title = N_("Graphics");
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 browseFile(&lv_, in_name, title, pattern, dir1, dir2);
101 }
102
103 string const ControlGraphics::readBB(string const & file)
104 {
105 // in a file it's an entry like %%BoundingBox:23 45 321 345
106 // the first number can following without a space, so we have
107 // to check a bit more. 
108 // on the other hand some plot programs write the bb at the
109 // end of the file. Than we have in the header a
110 //      %%BoundingBox: (atend)
111 // In this case we must check until the end.
112         string file_ = file;
113         if (zippedFile(file_))
114             file_ = unzipFile(file_);
115         std::ifstream is(file_.c_str());
116         if (!contains(getExtFromContents(file_),"ps"))  // bb exists? 
117             return string();
118         while (is) {
119                 string s;
120                 is >> s;
121                 if (contains(s,"%%BoundingBox:")) {
122                         string a, b, c, d;
123                         is >> a >> b >> c >> d;
124                         if (is && !contains(a,"atend")) { // bb at the end?
125                                 if (s != "%%BoundingBox:") 
126                                     return (s.substr(14)+" "+a+" "+b+" "+c+" ");
127                                 else
128                                     return (a+" "+b+" "+c+" "+d+" ");
129                         }
130                 }
131         }
132         return string();
133 }
134
135 void ControlGraphics::help() const
136 {
137         lv_.getDialogs()->showFile(i18nLibFileSearch("help","Graphics.hlp"));
138 }
139