]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/ControlInclude.cpp
fix scrolling bug: 3320 and 3652, maybe not perfect
[lyx.git] / src / frontends / controllers / ControlInclude.cpp
1 /**
2  * \file ControlInclude.cpp
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 "frontend_helpers.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::FileName;
39 using support::isFileReadable;
40 using support::makeAbsPath;
41 using support::onlyPath;
42
43 namespace frontend {
44
45 ControlInclude::ControlInclude(Dialog & parent)
46         : Dialog::Controller(parent), params_("include")
47 {}
48
49
50 bool ControlInclude::initialiseParams(string const & data)
51 {
52         InsetIncludeMailer::string2params(data, params_);
53         return true;
54 }
55
56
57 void ControlInclude::clearParams()
58 {
59         params_.clear();
60 }
61
62
63 void ControlInclude::dispatchParams()
64 {
65         string const lfun = InsetIncludeMailer::params2string(params_);
66         kernel().dispatch(FuncRequest(getLfun(), lfun));
67 }
68
69
70 void ControlInclude::setParams(InsetCommandParams const & params)
71 {
72         params_ = params;
73 }
74
75
76 docstring const ControlInclude::browse(docstring const & in_name, Type in_type) const
77 {
78         docstring const title = _("Select document to include");
79
80         // input TeX, verbatim, or LyX file ?
81         FileFilterList filters;
82         switch (in_type) {
83         case INCLUDE:
84         case INPUT:
85                 filters = FileFilterList(_("LaTeX/LyX Documents (*.tex *.lyx)"));
86                 break;
87         case VERBATIM:
88                 break;
89         case LISTINGS:
90                 break;
91         }
92
93         pair<docstring, docstring> dir1(_("Documents|#o#O"),
94                 lyx::from_utf8(lyxrc.document_path));
95
96         docstring const docpath = lyx::from_utf8(onlyPath(kernel().buffer().fileName()));
97
98         return browseRelFile(in_name, docpath, title,
99                              filters, false, dir1);
100 }
101
102
103 void ControlInclude::load(string const & file)
104 {
105         string const ext = support::getExtension(file);
106         if (ext == "lyx")
107                 kernel().dispatch(FuncRequest(LFUN_BUFFER_CHILD_OPEN, file));
108         else
109                 // tex file or other text file in verbatim mode
110                 formats.edit(kernel().buffer(), FileName(file), "text");
111 }
112
113
114 bool ControlInclude::fileExists(string const & file)
115 {
116         FileName const fileWithAbsPath(
117                 makeAbsPath(file, onlyPath(kernel().buffer().fileName())));
118
119         if (isFileReadable(fileWithAbsPath))
120                 return true;
121
122         return false;
123 }
124
125 } // namespace frontend
126 } // namespace lyx