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