]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormInclude.C
This file is part of LyX, the document processor.
[lyx.git] / src / frontends / xforms / FormInclude.C
1 /**
2  * \file FormInclude.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 #include <algorithm>
15
16 #ifdef __GNUG__
17 #pragma implementation
18 #endif
19
20 #include "xformsBC.h"
21 #include "ControlInclude.h"
22 #include "FormInclude.h"
23 #include "forms/form_include.h"
24 #include "insets/insetinclude.h"
25 #include "xforms_helpers.h" // setEnabled
26 #include "support/lstrings.h" // strip
27 #include FORMS_H_LOCATION
28
29 typedef FormCB<ControlInclude, FormDB<FD_include> > base_class;
30
31 FormInclude::FormInclude()
32         : base_class(_("Include file"))
33 {}
34
35
36 void FormInclude::build()
37 {
38         dialog_.reset(build_include(this));
39
40         fl_set_input_return(dialog_->input_filename, FL_RETURN_CHANGED);
41         setPrehandler(dialog_->input_filename);
42
43         // Manage the ok and cancel buttons
44         bc().setOK(dialog_->button_ok);
45         bc().setCancel(dialog_->button_close);
46
47         bc().addReadOnly(dialog_->button_browse);
48         bc().addReadOnly(dialog_->radio_verbatim);
49         bc().addReadOnly(dialog_->check_typeset);
50         bc().addReadOnly(dialog_->radio_useinput);
51         bc().addReadOnly(dialog_->radio_useinclude);
52 }
53
54
55 void FormInclude::update()
56 {
57         #if 0
58         // I believe this is not needed.
59         // Anyway, it is plain wrong (JSpitzm 3/7/02)
60         if (controller().params().noload) {
61                 fl_set_input(dialog_->input_filename, "");
62                 fl_set_button(dialog_->check_typeset, 0);
63                 fl_set_button(dialog_->radio_useinput, 0);
64                 fl_set_button(dialog_->radio_useinclude, 1);
65                 fl_set_button(dialog_->radio_verbatim, 0);
66                 fl_set_button(dialog_->check_visiblespace, 0);
67                 fl_deactivate_object(dialog_->check_visiblespace);
68                 fl_set_object_lcol(dialog_->check_visiblespace, FL_INACTIVE);
69                 return;
70         }
71         #endif
72
73         fl_set_input(dialog_->input_filename,
74                      controller().params().cparams.getContents().c_str());
75
76         string const cmdname = controller().params().cparams.getCmdName();
77
78         fl_set_button(dialog_->check_typeset,
79                       int(controller().params().noload));
80
81         if (cmdname == "input")
82                 fl_set_button(dialog_->check_preview,
83                               int(controller().params().cparams.preview()));
84         else
85                 fl_set_button(dialog_->check_preview, 0);
86
87         setEnabled(dialog_->check_preview, (cmdname == "input"));
88
89         fl_set_button(dialog_->radio_useinput, cmdname == "input");
90         fl_set_button(dialog_->radio_useinclude, cmdname == "include");
91         if (cmdname == "verbatiminput" || cmdname == "verbatiminput*") {
92                 fl_set_button(dialog_->radio_verbatim, 1);
93                 fl_set_button(dialog_->check_visiblespace, cmdname == "verbatiminput*");
94                 setEnabled(dialog_->check_visiblespace, true);
95                 setEnabled(dialog_->button_load, false);
96         } else {
97                 fl_set_button(dialog_->check_visiblespace, 0);
98                 setEnabled(dialog_->check_visiblespace, false);
99                 setEnabled(dialog_->button_load, true);
100         }
101
102         if (cmdname.empty())
103                 fl_set_button(dialog_->radio_useinclude, 1);
104 }
105
106
107 void FormInclude::apply()
108 {
109         controller().params().noload = fl_get_button(dialog_->check_typeset);
110         controller().params().cparams
111                 .preview(fl_get_button(dialog_->check_preview));
112
113         string const file = fl_get_input(dialog_->input_filename);
114         if (controller().fileExists(file))
115                 controller().params().cparams.setContents(file);
116         else
117                 controller().params().cparams.setContents("");
118
119         if (fl_get_button(dialog_->radio_useinput))
120                 controller().params().flag = InsetInclude::INPUT;
121         else if (fl_get_button(dialog_->radio_useinclude))
122                 controller().params().flag = InsetInclude::INCLUDE;
123         else if (fl_get_button(dialog_->radio_verbatim)) {
124                 if (fl_get_button(dialog_->check_visiblespace))
125                         controller().params().flag = InsetInclude::VERBAST;
126                 else
127                         controller().params().flag = InsetInclude::VERB;
128         }
129 }
130
131
132 ButtonPolicy::SMInput FormInclude::input(FL_OBJECT * ob, long)
133 {
134         ButtonPolicy::SMInput action = ButtonPolicy::SMI_VALID;
135
136         if (ob == dialog_->button_browse) {
137                 ControlInclude::Type type;
138                 if (fl_get_button(dialog_->radio_useinput))
139                         type = ControlInclude::INPUT;
140                 else if (fl_get_button(dialog_->radio_verbatim))
141                         type = ControlInclude::VERBATIM;
142                 else
143                         type = ControlInclude::INCLUDE;
144
145                 string const in_name  = fl_get_input(dialog_->input_filename);
146                 fl_freeze_form(form());
147                 string const out_name = controller().Browse(in_name, type);
148                 fl_set_input(dialog_->input_filename, out_name.c_str());
149                 fl_unfreeze_form(form());
150
151         } else if (ob == dialog_->button_load) {
152                 string const in_name = fl_get_input(dialog_->input_filename);
153                 if (!rtrim(in_name).empty() && controller().fileExists(in_name)) {
154 //                      ApplyButton();
155                         controller().OKButton();
156                         controller().load(rtrim(in_name));
157                         action = ButtonPolicy::SMI_NOOP;
158                 }
159
160         } else if (ob == dialog_->radio_verbatim) {
161                 setEnabled(dialog_->check_visiblespace, true);
162                 setEnabled(dialog_->button_load, false);
163
164         } else if (ob == dialog_->radio_useinclude ||
165                    ob == dialog_->radio_useinput) {
166                 fl_set_button(dialog_->check_visiblespace, 0);
167                 setEnabled(dialog_->check_visiblespace, false);
168                 setEnabled(dialog_->button_load, true);
169
170         } else if (ob == dialog_->input_filename) {
171                 string const in_name = fl_get_input(dialog_->input_filename);
172                 if (rtrim(in_name).empty())
173                         action = ButtonPolicy::SMI_INVALID;
174         }
175
176         if (ob == dialog_->radio_useinput) {
177                 setEnabled(dialog_->check_preview, true);
178         } else if (ob == dialog_->radio_verbatim ||
179                    ob == dialog_->radio_useinclude) {
180                 fl_set_button(dialog_->check_preview, 0);
181                 setEnabled(dialog_->check_preview, false);
182         }
183
184         return action;
185 }