]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormInclude.C
1d33dd9557a5959cdd0bd202d3c8bed61f0d9248
[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
8  */
9 #include <config.h>
10 #include <algorithm>
11 #include <utility>
12
13 #ifdef __GNUG__
14 #pragma implementation
15 #endif
16
17 #include "Dialogs.h"
18 #include "FormInclude.h"
19 #include "insets/insetinclude.h"
20 #include "frontends/FileDialog.h"
21 #include "support/filetools.h"
22 #include "support/lstrings.h"
23 #include "LyXView.h"
24 #include "buffer.h"
25 #include "lyxrc.h"
26 #include "lyxfunc.h"
27 #include "xforms_helpers.h"
28
29 #include "form_include.h"
30
31 using std::make_pair;
32 using std::pair;
33
34 FormInclude::FormInclude(LyXView * lv, Dialogs * d)
35         : FormBaseBD(lv, d, _("Include file")),
36           ih_(0), inset_(0)
37 {
38         d->showInclude.connect(slot(this, &FormInclude::showInclude));
39 }
40
41
42 FL_FORM * FormInclude::form() const
43 {
44         if (dialog_.get()) 
45                 return dialog_->form;
46         return 0;
47 }
48
49
50 void FormInclude::connect()
51 {
52         u_ = d_->updateBufferDependent.
53                  connect(slot(this, &FormInclude::updateSlot));
54         h_ = d_->hideBufferDependent.
55                  connect(slot(this, &FormInclude::hide));
56         FormBaseDeprecated::connect();
57 }
58
59
60 void FormInclude::disconnect()
61 {
62         ih_.disconnect();
63         FormBaseBD::disconnect();
64         inset_ = 0;
65 }
66
67
68 void FormInclude::updateSlot(bool switched)
69 {
70         if (switched)
71                 hide();
72         else
73                 update();
74 }
75
76
77 void FormInclude::build()
78 {
79         dialog_.reset(build_include());
80
81         // Workaround dumb xforms sizing bug
82         minw_ = form()->w;
83         minh_ = form()->h;
84
85         // Manage the ok and cancel buttons
86         bc_.setOK(dialog_->button_ok);
87         bc_.setCancel(dialog_->button_cancel);
88         bc_.refresh();
89
90         bc_.addReadOnly(dialog_->button_browse);
91         bc_.addReadOnly(dialog_->check_verbatim);
92         bc_.addReadOnly(dialog_->check_typeset);
93         bc_.addReadOnly(dialog_->check_useinput);
94         bc_.addReadOnly(dialog_->check_useinclude);
95 }
96
97
98 void FormInclude::showInclude(InsetInclude * inset)
99 {
100         // If connected to another inset, disconnect from it.
101         if (inset_)
102                 ih_.disconnect();
103
104         inset_    = inset;
105         params    = inset->params();
106         ih_ = inset->hideDialog.connect(slot(this, &FormInclude::hide));
107         show();
108 }
109
110
111 void FormInclude::update()
112 {
113         bc().readOnly(lv_->buffer()->isReadonly());
114
115         if (!inset_) {
116                 fl_set_input(dialog_->input_filename, "");
117                 fl_set_button(dialog_->check_typeset, 0);
118                 fl_set_button(dialog_->check_useinput, 0);
119                 fl_set_button(dialog_->check_useinclude, 1);
120                 fl_set_button(dialog_->check_verbatim, 0);
121                 fl_set_button(dialog_->check_visiblespace, 0);
122                 fl_deactivate_object(dialog_->check_visiblespace);
123                 fl_set_object_lcol(dialog_->check_visiblespace, FL_INACTIVE);
124                 return;
125         }
126
127         fl_set_input(dialog_->input_filename, params.cparams.getContents().c_str());
128
129         string const cmdname = params.cparams.getCmdName();
130
131         fl_set_button(dialog_->check_typeset, int(params.noload));
132
133         fl_set_button(dialog_->check_useinput, cmdname == "input");
134         fl_set_button(dialog_->check_useinclude, cmdname == "include");
135         if (cmdname == "verbatiminput" || cmdname == "verbatiminput*") {
136                 fl_set_button(dialog_->check_verbatim, 1);
137                 fl_set_button(dialog_->check_visiblespace, cmdname == "verbatiminput*");
138                 setEnabled(dialog_->check_visiblespace, true);
139         } else {
140                 fl_set_button(dialog_->check_visiblespace, 0);
141                 setEnabled(dialog_->check_visiblespace, false);
142         }
143  
144         if (cmdname.empty())
145                 fl_set_button(dialog_->check_useinclude, 1);
146 }
147
148
149 void FormInclude::apply()
150 {
151         if (lv_->buffer()->isReadonly())
152                 return;
153
154         params.noload = fl_get_button(dialog_->check_typeset);
155
156         params.cparams.setContents(fl_get_input(dialog_->input_filename));
157
158         if (fl_get_button(dialog_->check_useinput))
159                 params.flag = InsetInclude::INPUT;
160         else if (fl_get_button(dialog_->check_useinclude))
161                 params.flag = InsetInclude::INCLUDE;
162         else if (fl_get_button(dialog_->check_verbatim)) {
163                 if (fl_get_button(dialog_->check_visiblespace))
164                         params.flag = InsetInclude::VERBAST;
165                 else
166                         params.flag = InsetInclude::VERB;
167         }
168         
169         inset_->setFromParams(params);
170         lv_->view()->updateInset(inset_, true);
171 }
172
173 #ifdef WITH_WARNINGS
174 #warning convert this to use the buttoncontroller
175 #endif
176 bool FormInclude::input(FL_OBJECT *, long data)
177 {
178         State state = static_cast<State>(data);
179
180         switch (state) {
181                 case BROWSE: {
182                         // Should browsing too be disabled in RO-mode?
183                         FileDialog fileDlg(lv_, _("Select document to include"),
184                                 LFUN_SELECT_FILE_SYNC,
185                                 make_pair(string(_("Documents")), string(lyxrc.document_path)));
186
187                         string ext;
188                    
189                         /* input TeX, verbatim, or LyX file ? */
190                         if (fl_get_button(dialog_->check_useinput))
191                                 ext = _("*.tex| LaTeX Documents (*.tex)");
192                         else if (fl_get_button(dialog_->check_verbatim))
193                                 ext = _("*| All files ");
194                         else
195                                 ext = _("*.lyx| LyX Documents (*.lyx)");
196         
197                         string mpath;
198
199                         mpath = OnlyPath(params.buffer->fileName());
200
201                         FileDialog::Result result = fileDlg.Select(mpath, ext, fl_get_input(dialog_->input_filename));
202         
203                         // check selected filename
204                         if (result.second.empty())
205                                 break;
206         
207                         string const filename2 = MakeRelPath(result.second, mpath);
208         
209                         if (prefixIs(filename2, ".."))
210                                 fl_set_input(dialog_->input_filename, result.second.c_str());
211                         else
212                                 fl_set_input(dialog_->input_filename, filename2.c_str());
213
214                 }       break;
215
216                 case LOAD:
217                         if (compare(fl_get_input(dialog_->input_filename),"")) {
218                                 apply();
219                                 lv_->getLyXFunc()->Dispatch(LFUN_CHILDOPEN, params.cparams.getContents());
220                         }
221                         break;
222
223                 case VERBATIM:
224                         setEnabled(dialog_->check_visiblespace, true);
225                         break;
226         
227                 case INPUTINCLUDE:
228                         fl_set_button(dialog_->check_visiblespace, 0);
229                         setEnabled(dialog_->check_visiblespace, false);
230                         break;
231         }
232         return true;
233 }