]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormInclude.C
Rewrote the maths panel so that ALL the popups now derive from FormBaseBD,
[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         // Manage the ok and cancel buttons
83         bc_.setOK(dialog_->button_ok);
84         bc_.setCancel(dialog_->button_cancel);
85         bc_.refresh();
86
87         bc_.addReadOnly(dialog_->button_browse);
88         bc_.addReadOnly(dialog_->check_verbatim);
89         bc_.addReadOnly(dialog_->check_typeset);
90         bc_.addReadOnly(dialog_->check_useinput);
91         bc_.addReadOnly(dialog_->check_useinclude);
92 }
93
94
95 void FormInclude::showInclude(InsetInclude * inset)
96 {
97         // If connected to another inset, disconnect from it.
98         if (inset_)
99                 ih_.disconnect();
100
101         inset_    = inset;
102         params    = inset->params();
103         ih_ = inset->hideDialog.connect(slot(this, &FormInclude::hide));
104         show();
105 }
106
107
108 void FormInclude::update()
109 {
110         bc().readOnly(lv_->buffer()->isReadonly());
111
112         if (!inset_) {
113                 fl_set_input(dialog_->input_filename, "");
114                 fl_set_button(dialog_->check_typeset, 0);
115                 fl_set_button(dialog_->check_useinput, 0);
116                 fl_set_button(dialog_->check_useinclude, 1);
117                 fl_set_button(dialog_->check_verbatim, 0);
118                 fl_set_button(dialog_->check_visiblespace, 0);
119                 fl_deactivate_object(dialog_->check_visiblespace);
120                 fl_set_object_lcol(dialog_->check_visiblespace, FL_INACTIVE);
121                 return;
122         }
123
124         fl_set_input(dialog_->input_filename, params.cparams.getContents().c_str());
125
126         string const cmdname = params.cparams.getCmdName();
127
128         fl_set_button(dialog_->check_typeset, int(params.noload));
129
130         fl_set_button(dialog_->check_useinput, cmdname == "input");
131         fl_set_button(dialog_->check_useinclude, cmdname == "include");
132         if (cmdname == "verbatiminput" || cmdname == "verbatiminput*") {
133                 fl_set_button(dialog_->check_verbatim, 1);
134                 fl_set_button(dialog_->check_visiblespace, cmdname == "verbatiminput*");
135                 setEnabled(dialog_->check_visiblespace, true);
136         } else {
137                 fl_set_button(dialog_->check_visiblespace, 0);
138                 setEnabled(dialog_->check_visiblespace, false);
139         }
140  
141         if (cmdname.empty())
142                 fl_set_button(dialog_->check_useinclude, 1);
143 }
144
145
146 void FormInclude::apply()
147 {
148         if (lv_->buffer()->isReadonly())
149                 return;
150
151         params.noload = fl_get_button(dialog_->check_typeset);
152
153         params.cparams.setContents(fl_get_input(dialog_->input_filename));
154
155         if (fl_get_button(dialog_->check_useinput))
156                 params.flag = InsetInclude::INPUT;
157         else if (fl_get_button(dialog_->check_useinclude))
158                 params.flag = InsetInclude::INCLUDE;
159         else if (fl_get_button(dialog_->check_verbatim)) {
160                 if (fl_get_button(dialog_->check_visiblespace))
161                         params.flag = InsetInclude::VERBAST;
162                 else
163                         params.flag = InsetInclude::VERB;
164         }
165         
166         inset_->setFromParams(params);
167         lv_->view()->updateInset(inset_, true);
168 }
169
170 #ifdef WITH_WARNINGS
171 #warning convert this to use the buttoncontroller
172 #endif
173 bool FormInclude::input(FL_OBJECT *, long data)
174 {
175         State state = static_cast<State>(data);
176
177         switch (state) {
178                 case BROWSE: {
179                         // Should browsing too be disabled in RO-mode?
180                         FileDialog fileDlg(lv_, _("Select document to include"),
181                                 LFUN_SELECT_FILE_SYNC,
182                                 make_pair(string(_("Documents")), string(lyxrc.document_path)));
183
184                         string ext;
185                    
186                         /* input TeX, verbatim, or LyX file ? */
187                         if (fl_get_button(dialog_->check_useinput))
188                                 ext = _("*.tex| LaTeX Documents (*.tex)");
189                         else if (fl_get_button(dialog_->check_verbatim))
190                                 ext = _("*| All files ");
191                         else
192                                 ext = _("*.lyx| LyX Documents (*.lyx)");
193         
194                         string mpath;
195
196                         mpath = OnlyPath(params.buffer->fileName());
197
198                         FileDialog::Result result = fileDlg.Select(mpath, ext, fl_get_input(dialog_->input_filename));
199         
200                         // check selected filename
201                         if (result.second.empty())
202                                 break;
203         
204                         string const filename2 = MakeRelPath(result.second, mpath);
205         
206                         if (prefixIs(filename2, ".."))
207                                 fl_set_input(dialog_->input_filename, result.second.c_str());
208                         else
209                                 fl_set_input(dialog_->input_filename, filename2.c_str());
210
211                 }       break;
212
213                 case LOAD:
214                         if (compare(fl_get_input(dialog_->input_filename),"")) {
215                                 apply();
216                                 lv_->getLyXFunc()->Dispatch(LFUN_CHILDOPEN, params.cparams.getContents());
217                         }
218                         break;
219
220                 case VERBATIM:
221                         setEnabled(dialog_->check_visiblespace, true);
222                         break;
223         
224                 case INPUTINCLUDE:
225                         fl_set_button(dialog_->check_visiblespace, 0);
226                         setEnabled(dialog_->check_visiblespace, false);
227                         break;
228         }
229         return true;
230 }