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