]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/ControlGraphics.C
63aa81561b8d3d3c2b6c1af063d50d902b8e6956
[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 "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 "BufferView.h"
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.getParams();
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());
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 string const ControlGraphics::Browse(string const & in_name)
85 {
86         string const title = N_("Graphics");
87         // FIXME: currently we need the second '|' to prevent mis-interpretation 
88         string const pattern = "*.(ps|eps|png|jpeg|jpg|gif|gz)|";
89
90         // Does user clipart directory exist?
91         string clipdir = AddName (user_lyxdir, "clipart");
92         FileInfo fileInfo(clipdir);
93         if (!(fileInfo.isOK() && fileInfo.isDir()))
94                 // No - bail out to system clipart directory
95                 clipdir = AddName (system_lyxdir, "clipart");
96         pair<string, string> dir1(N_("Clipart|#C#c"), clipdir);
97         pair<string, string> dir2(N_("Documents|#o#O"), string(lyxrc.document_path));
98         // Show the file browser dialog
99         return browseFile(&lv_, in_name, title, pattern, dir1, dir2);
100 }
101
102 string const ControlGraphics::readBB(string const & file)
103 {
104 // in a file it's an entry like %%BoundingBox:23 45 321 345
105 // the first number can following without a space, so we have
106 // to check a bit more. 
107 // on the other hand some plot programs write the bb at the
108 // end of the file. Than we have in the header a
109 //      %%BoundingBox: (atend)
110 // In this case we must check until the end.
111         std::ifstream is(file.c_str());
112         if (!contains(getExtFromContents(file),"ps"))   // bb exists? 
113             return string();
114         while (is) {
115                 string s;
116                 is >> s;
117                 if (contains(s,"%%BoundingBox:")) {
118                         string a, b, c, d;
119                         is >> a >> b >> c >> d;
120                         if (is && !contains(a,"atend")) { // bb at the end?
121                                 if (s != "%%BoundingBox:") 
122                                     return (s.substr(14)+" "+a+" "+b+" "+c+" ");
123                                 else
124                                     return (a+" "+b+" "+c+" "+d+" ");
125                         }
126                 }
127         }
128         return string();
129 }
130
131 void ControlGraphics::help() const
132 {
133         lv_.getDialogs()->showFile(i18nLibFileSearch("help","Graphics.hlp"));
134 }
135