]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormInclude.C
Rename strip to rtrim
[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, Dialogs & d)
30         : base_class(c, d, _("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 0
56         // I believe this is not needed.
57         // Anyway, it is plain wrong (JSpitzm 3/7/02)
58         if (controller().params().noload) {
59                 fl_set_input(dialog_->input_filename, "");
60                 fl_set_button(dialog_->check_typeset, 0);
61                 fl_set_button(dialog_->radio_useinput, 0);
62                 fl_set_button(dialog_->radio_useinclude, 1);
63                 fl_set_button(dialog_->radio_verbatim, 0);
64                 fl_set_button(dialog_->check_visiblespace, 0);
65                 fl_deactivate_object(dialog_->check_visiblespace);
66                 fl_set_object_lcol(dialog_->check_visiblespace, FL_INACTIVE);
67                 return;
68         }
69         #endif
70
71         fl_set_input(dialog_->input_filename,
72                      controller().params().cparams.getContents().c_str());
73
74         string const cmdname = controller().params().cparams.getCmdName();
75
76         fl_set_button(dialog_->check_typeset,
77                       int(controller().params().noload));
78
79         fl_set_button(dialog_->radio_useinput, cmdname == "input");
80         fl_set_button(dialog_->radio_useinclude, cmdname == "include");
81         if (cmdname == "verbatiminput" || cmdname == "verbatiminput*") {
82                 fl_set_button(dialog_->radio_verbatim, 1);
83                 fl_set_button(dialog_->check_visiblespace, cmdname == "verbatiminput*");
84                 setEnabled(dialog_->check_visiblespace, true);
85                 setEnabled(dialog_->button_load, false);
86         } else {
87                 fl_set_button(dialog_->check_visiblespace, 0);
88                 setEnabled(dialog_->check_visiblespace, false);
89                 setEnabled(dialog_->button_load, true);
90         }
91
92         if (cmdname.empty())
93                 fl_set_button(dialog_->radio_useinclude, 1);
94 }
95
96
97 void FormInclude::apply()
98 {
99         controller().params().noload = fl_get_button(dialog_->check_typeset);
100
101         string const file = fl_get_input(dialog_->input_filename);
102         if (controller().fileExists(file))
103                 controller().params().cparams.setContents(file);
104         else
105                 controller().params().cparams.setContents("");
106
107         if (fl_get_button(dialog_->radio_useinput))
108                 controller().params().flag = InsetInclude::INPUT;
109         else if (fl_get_button(dialog_->radio_useinclude))
110                 controller().params().flag = InsetInclude::INCLUDE;
111         else if (fl_get_button(dialog_->radio_verbatim)) {
112                 if (fl_get_button(dialog_->check_visiblespace))
113                         controller().params().flag = InsetInclude::VERBAST;
114                 else
115                         controller().params().flag = InsetInclude::VERB;
116         }
117 }
118
119
120 ButtonPolicy::SMInput FormInclude::input(FL_OBJECT * ob, long)
121 {
122         ButtonPolicy::SMInput action = ButtonPolicy::SMI_VALID;
123
124         if (ob == dialog_->button_browse) {
125                 ControlInclude::Type type;
126                 if (fl_get_button(dialog_->radio_useinput))
127                         type = ControlInclude::INPUT;
128                 else if (fl_get_button(dialog_->radio_verbatim))
129                         type = ControlInclude::VERBATIM;
130                 else
131                         type = ControlInclude::INCLUDE;
132
133                 string const in_name  = fl_get_input(dialog_->input_filename);
134                 fl_freeze_form(form());
135                 string const out_name = controller().Browse(in_name, type);
136                 fl_set_input(dialog_->input_filename, out_name.c_str());
137                 fl_unfreeze_form(form());
138
139         } else if (ob == dialog_->button_load) {
140                 string const in_name = fl_get_input(dialog_->input_filename);
141                 if (!rtrim(in_name).empty() && controller().fileExists(in_name)) {
142 //                      ApplyButton();
143                         OKButton();
144                         controller().load(rtrim(in_name));
145                         action = ButtonPolicy::SMI_NOOP;
146                 }
147
148         } else if (ob == dialog_->radio_verbatim) {
149                 setEnabled(dialog_->check_visiblespace, true);
150                 setEnabled(dialog_->button_load, false);
151
152         } else if (ob == dialog_->radio_useinclude ||
153                    ob == dialog_->radio_useinput) {
154                 fl_set_button(dialog_->check_visiblespace, 0);
155                 setEnabled(dialog_->check_visiblespace, false);
156                 setEnabled(dialog_->button_load, true);
157
158         } else if (ob == dialog_->input_filename) {
159                 string const in_name = fl_get_input(dialog_->input_filename);
160                 if (rtrim(in_name).empty())
161                         action = ButtonPolicy::SMI_INVALID;
162         }
163
164         return action;
165 }