]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/ControlInclude.C
7e497b9fd55b67fd6cbfd490506cded52abe3ecb
[lyx.git] / src / frontends / controllers / ControlInclude.C
1 /**
2  * \file ControlInclude.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Alejandro Aguilar Sierra
7  * \author John Levon
8  * \author Angus Leeming
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "ControlInclude.h"
16 #include "helper_funcs.h"
17 #include "Kernel.h"
18
19 #include "buffer.h"
20 #include "format.h"
21 #include "funcrequest.h"
22 #include "gettext.h"
23 #include "lyxrc.h"
24
25 #include "insets/insetinclude.h"
26
27 #include "support/filefilterlist.h"
28 #include "support/filetools.h"
29
30 #include <utility>
31
32 using std::pair;
33 using std::string;
34
35 namespace lyx {
36
37 using support::FileFilterList;
38 using support::isFileReadable;
39 using support::makeAbsPath;
40 using support::onlyPath;
41
42 namespace frontend {
43
44 ControlInclude::ControlInclude(Dialog & parent)
45         : Dialog::Controller(parent)
46 {}
47
48
49 bool ControlInclude::initialiseParams(string const & data)
50 {
51         InsetIncludeMailer::string2params(data, params_);
52         return true;
53 }
54
55
56 void ControlInclude::clearParams()
57 {
58         params_ = InsetCommandParams();
59 }
60
61
62 void ControlInclude::dispatchParams()
63 {
64         string const lfun = InsetIncludeMailer::params2string(params_);
65         kernel().dispatch(FuncRequest(getLfun(), lfun));
66 }
67
68
69 void ControlInclude::setParams(InsetCommandParams const & params)
70 {
71         params_ = params;
72 }
73
74
75 string const ControlInclude::browse(string const & in_name, Type in_type) const
76 {
77         string const title = _("Select document to include");
78
79         // input TeX, verbatim, or LyX file ?
80         FileFilterList filters;
81         switch (in_type) {
82         case INCLUDE:
83         case INPUT:
84             filters = FileFilterList(_("LaTeX/LyX Documents (*.tex *.lyx)"));
85             break;
86         case VERBATIM:
87             break;
88         }
89
90         pair<string, string> dir1(N_("Documents|#o#O"),
91                                   string(lyxrc.document_path));
92
93         string const docpath = onlyPath(kernel().buffer().fileName());
94
95         return browseRelFile(in_name, docpath, title,
96                              filters, false, dir1);
97 }
98
99
100 void ControlInclude::load(string const & file)
101 {
102         string const ext = support::getExtension(file);
103         if (ext == "lyx")
104                 kernel().dispatch(FuncRequest(LFUN_BUFFER_CHILD_OPEN, file));
105         else
106                 // tex file or other text file in verbatim mode
107                 formats.edit(kernel().buffer(), file, "text");
108 }
109
110
111 bool ControlInclude::fileExists(string const & file)
112 {
113         string const fileWithAbsPath
114                 = makeAbsPath(file,
115                               onlyPath(kernel().buffer().fileName()));
116
117         if (isFileReadable(fileWithAbsPath))
118                 return true;
119
120         return false;
121 }
122
123 } // namespace frontend
124 } // namespace lyx