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