]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormInclude.C
66efb6d0879a9aef9c7c67447056192673c9a41b
[lyx.git] / src / frontends / xforms / FormInclude.C
1 /**
2  * \file FormInclude.C
3  * Copyright 2001 the LyX Team
4  * Read the file COPYING
5  *
6  * \author Alejandro Aguilar Sierra
7  * \author John Levon, moz@compsoc.man.ac.uk
8  * \author Angus Leeming, a.leeming@.ac.uk
9  */
10
11 #include <algorithm>
12 #include <utility>
13
14 #ifdef __GNUG__
15 #pragma implementation
16 #endif
17
18 #include <config.h>
19 #include "xformsBC.h"
20 #include "ControlInclude.h"
21 #include "FormInclude.h"
22 #include "form_include.h"
23 #include "insets/insetinclude.h"
24
25 #include "frontends/FileDialog.h"
26 #include "LyXView.h"
27 #include "buffer.h"
28
29 #include "xforms_helpers.h" // setEnabled
30 #include "support/filetools.h"
31 #include "support/lstrings.h"
32 #include "lyxrc.h"
33
34 using std::make_pair;
35 using std::pair;
36
37 typedef FormCB<ControlInclude, FormDB<FD_form_include> > base_class;
38
39 FormInclude::FormInclude(ControlInclude & c)
40         : base_class(c, _("Include file"))
41 {}
42
43
44 void FormInclude::build()
45 {
46         dialog_.reset(build_include());
47
48         // Manage the ok and cancel buttons
49         bc().setOK(dialog_->button_ok);
50         bc().setCancel(dialog_->button_cancel);
51         bc().refresh();
52
53         bc().addReadOnly(dialog_->button_browse);
54         bc().addReadOnly(dialog_->check_verbatim);
55         bc().addReadOnly(dialog_->check_typeset);
56         bc().addReadOnly(dialog_->check_useinput);
57         bc().addReadOnly(dialog_->check_useinclude);
58 }
59
60
61 void FormInclude::update()
62 {
63         if (controller().params().noload) {
64                 fl_set_input(dialog_->input_filename, "");
65                 fl_set_button(dialog_->check_typeset, 0);
66                 fl_set_button(dialog_->check_useinput, 0);
67                 fl_set_button(dialog_->check_useinclude, 1);
68                 fl_set_button(dialog_->check_verbatim, 0);
69                 fl_set_button(dialog_->check_visiblespace, 0);
70                 fl_deactivate_object(dialog_->check_visiblespace);
71                 fl_set_object_lcol(dialog_->check_visiblespace, FL_INACTIVE);
72                 return;
73         }
74
75         fl_set_input(dialog_->input_filename,
76                      controller().params().cparams.getContents().c_str());
77
78         string const cmdname = controller().params().cparams.getCmdName();
79
80         fl_set_button(dialog_->check_typeset,
81                       int(controller().params().noload));
82
83         fl_set_button(dialog_->check_useinput, cmdname == "input");
84         fl_set_button(dialog_->check_useinclude, cmdname == "include");
85         if (cmdname == "verbatiminput" || cmdname == "verbatiminput*") {
86                 fl_set_button(dialog_->check_verbatim, 1);
87                 fl_set_button(dialog_->check_visiblespace, cmdname == "verbatiminput*");
88                 setEnabled(dialog_->check_visiblespace, true);
89         } else {
90                 fl_set_button(dialog_->check_visiblespace, 0);
91                 setEnabled(dialog_->check_visiblespace, false);
92         }
93  
94         if (cmdname.empty())
95                 fl_set_button(dialog_->check_useinclude, 1);
96 }
97
98
99 void FormInclude::apply()
100 {
101         controller().params().noload = fl_get_button(dialog_->check_typeset);
102
103         controller().params().cparams.
104                 setContents(fl_get_input(dialog_->input_filename));
105
106         if (fl_get_button(dialog_->check_useinput))
107                 controller().params().flag = InsetInclude::INPUT;
108         else if (fl_get_button(dialog_->check_useinclude))
109                 controller().params().flag = InsetInclude::INCLUDE;
110         else if (fl_get_button(dialog_->check_verbatim)) {
111                 if (fl_get_button(dialog_->check_visiblespace))
112                         controller().params().flag = InsetInclude::VERBAST;
113                 else
114                         controller().params().flag = InsetInclude::VERB;
115         }
116 }
117
118
119 ButtonPolicy::SMInput FormInclude::input(FL_OBJECT * ob, long)
120 {
121         if (ob == dialog_->button_browse)
122                 return inputBrowse();
123
124         if (ob == dialog_->button_load) {
125                 if (compare(fl_get_input(dialog_->input_filename),"")) {
126                         ApplyButton();
127                         return ButtonPolicy::SMI_NOOP;
128                 }
129         }
130
131         if (ob == dialog_->check_verbatim) {
132                 setEnabled(dialog_->check_visiblespace, true);
133
134         } else if (ob == dialog_->check_useinclude ||
135                    ob == dialog_->check_useinput) {
136                 fl_set_button(dialog_->check_visiblespace, 0);
137                 setEnabled(dialog_->check_visiblespace, false);
138         }
139         
140         return ButtonPolicy::SMI_VALID;
141 }
142         
143
144 ButtonPolicy::SMInput FormInclude::inputBrowse()
145 {
146         // Should browsing too be disabled in RO-mode?
147         FileDialog fileDlg(controller().lv(),
148                            _("Select document to include"),
149                            LFUN_SELECT_FILE_SYNC,
150                            make_pair(string(_("Documents")),
151                                      string(lyxrc.document_path)));
152
153         string ext;
154                    
155         // input TeX, verbatim, or LyX file ?
156         if (fl_get_button(dialog_->check_useinput))
157                 ext = _("*.tex| LaTeX Documents (*.tex)");
158         else if (fl_get_button(dialog_->check_verbatim))
159                 ext = _("*| All files ");
160         else
161                 ext = _("*.lyx| LyX Documents (*.lyx)");
162         
163         string const mpath =
164                 OnlyPath(controller().params().masterFilename_);
165
166         FileDialog::Result const result =
167                 fileDlg.Select(mpath, ext,
168                                fl_get_input(dialog_->input_filename));
169         
170         // check selected filename
171         if (result.second.empty())
172                 return ButtonPolicy::SMI_NOOP;
173         
174         string const filename2 = MakeRelPath(result.second, mpath);
175
176         if (prefixIs(filename2, ".."))
177                 fl_set_input(dialog_->input_filename, result.second.c_str());
178         else
179                 fl_set_input(dialog_->input_filename, filename2.c_str());
180
181         return ButtonPolicy::SMI_VALID;
182 }