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