]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormInclude.C
formskdepatchthingie
[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
12 #ifdef __GNUG__
13 #pragma implementation
14 #endif
15
16 #include "Dialogs.h"
17 #include "FormInclude.h"
18 #include "insets/insetinclude.h" 
19 #include "filedlg.h"
20 #include "support/filetools.C"
21 #include "support/lstrings.h" 
22 #include "LyXView.h"
23 #include "buffer.h"
24 #include "lyxrc.h" 
25 #include "lyxfunc.h" 
26  
27 #include "form_include.h"
28
29 FormInclude::FormInclude(LyXView * lv, Dialogs * d)
30         : FormCommand(lv, d, _("Include file"), new OkCancelPolicy),
31           dialog_(0)
32 {
33         // let the dialog be shown
34         // These are permanent connections so we won't bother
35         // storing a copy because we won't be disconnecting.
36         d->showInclude.connect(slot(this, &FormInclude::showInset));
37         d->createInclude.connect(slot(this, &FormInclude::createInset));
38 }
39
40
41 FormInclude::~FormInclude()
42 {
43         delete dialog_;
44 }
45
46
47 FL_FORM * FormInclude::form() const
48 {
49         if (dialog_) 
50                 return dialog_->form;
51         return 0;
52 }
53
54
55 void FormInclude::build()
56 {
57         dialog_ = build_include();
58
59         // Workaround dumb xforms sizing bug
60         minw_ = form()->w;
61         minh_ = form()->h;
62
63         // Manage the ok and cancel buttons
64         bc_.setOK(dialog_->button_ok);
65         bc_.setCancel(dialog_->button_cancel);
66         bc_.refresh();
67
68         bc_.addReadOnly(dialog_->browsebt);
69         bc_.addReadOnly(dialog_->flag1);
70         bc_.addReadOnly(dialog_->flag2);
71         bc_.addReadOnly(dialog_->flag3);
72         bc_.addReadOnly(dialog_->flag4);
73         bc_.addReadOnly(dialog_->filename);
74         bc_.addReadOnly(dialog_->flag41);
75 }
76
77
78 void FormInclude::update()
79 {
80         bc_.readOnly(lv_->buffer()->isReadonly());
81
82         if (!inset_) {
83                 fl_set_input(dialog_->filename, "");
84                 fl_set_button(dialog_->flag1, 0);
85                 fl_set_button(dialog_->flag2, 0);
86                 fl_set_button(dialog_->flag3, 1);
87                 fl_set_button(dialog_->flag4, 0);
88                 fl_set_button(dialog_->flag41, 0);
89                 fl_deactivate_object(dialog_->flag41);
90                 fl_set_object_lcol(dialog_->flag41, FL_INACTIVE);
91                 return;
92         }
93  
94         fl_set_input(dialog_->filename, params.getContents().c_str());
95  
96         string const cmdname = params.getCmdName();
97  
98         /* FIXME: what do with Don't typeset here ... */
99         //fl_set_button(dialog_->flag1, int(inset_->isNoLoad()));
100  
101         fl_set_button(dialog_->flag2, cmdname == "input");
102         fl_set_button(dialog_->flag3, cmdname == "include");
103         if (cmdname == "verbatiminput" || cmdname == "verbatiminput*") {
104                 fl_set_button(dialog_->flag4, 1);
105                 fl_set_button(dialog_->flag41, cmdname == "verbatiminput*");
106         } else {
107                 fl_set_button(dialog_->flag41, 0);
108                 fl_deactivate_object(dialog_->flag41);
109                 fl_set_object_lcol(dialog_->flag41, FL_INACTIVE);
110         }
111 }
112
113
114 void FormInclude::apply()
115 {
116         if (lv_->buffer()->isReadonly())
117                 return;
118
119         /* FIXME: no way to update internal flags of inset ??? */
120  
121         //inset_->setNoLoad(fl_get_button(dialog_->flag1));
122  
123         params.setContents(fl_get_input(dialog_->filename));
124         cout << params.getContents() << endl; 
125         if (fl_get_button(dialog_->flag2))
126                 params.setCmdName("input");
127         else if (fl_get_button(dialog_->flag3))
128                 params.setCmdName("include");
129         else if (fl_get_button(dialog_->flag4)) {
130                 if (fl_get_button(dialog_->flag41))
131                         params.setCmdName("verbatiminput*");
132                 else
133                         params.setCmdName("verbatiminput");     
134         }
135         
136         if (inset_) {
137                 if (params != inset_->params()) {
138                         inset_->setParams(params);
139                         lv_->view()->updateInset(inset_, true);
140                 }
141         } else
142                 lv_->getLyXFunc()->Dispatch(LFUN_CHILD_INSERT, params.getAsString());
143 }
144  
145 #ifdef WITH_WARNINGS
146 #warning convert this to use the buttoncontroller
147 #endif
148 bool FormInclude::input(FL_OBJECT *, long data)
149 {
150         State state = static_cast<State>(data); 
151  
152         switch (state) {
153                 case BROWSE: {
154                         // Should browsing too be disabled in RO-mode?
155                         LyXFileDlg fileDlg;
156  
157                         string ext;
158                     
159                         fileDlg.SetButton(0, _("Documents"), lyxrc.document_path);
160
161                         /* input TeX, verbatim, or LyX file ? */
162                         if (fl_get_button(dialog_->flag2))
163                                 ext = "*.tex";
164                         else if (fl_get_button(dialog_->flag4))
165                                 ext = "*";
166                         else
167                                 ext = "*.lyx";
168          
169                         string mpath;
170  
171                         /* FIXME: what do I do here ? */ 
172                         //if (inset_)
173                         //      mpath = OnlyPath(inset_->getMasterFilename());
174  
175                         string const filename = fileDlg.Select(_("Select Child Document"),
176                                                 mpath, ext, fl_get_input(dialog_->filename));
177                         XFlush(fl_get_display());
178          
179                         // check selected filename
180                         if (filename.empty())
181                                 break;
182          
183                         string const filename2 = MakeRelPath(filename, mpath);
184          
185                         if (prefixIs(filename2, ".."))
186                                 fl_set_input(dialog_->filename, filename.c_str());
187                         else
188                                 fl_set_input(dialog_->filename, filename2.c_str());
189  
190                 }       break;
191
192                 case LOAD:
193                         apply();
194                         lv_->getLyXFunc()->Dispatch(LFUN_CHILDOPEN, params.getContents());
195                         break;
196
197                 case VERBATIM:
198                         fl_activate_object(dialog_->flag41);
199                         fl_set_object_lcol(dialog_->flag41, FL_BLACK); 
200                         break;
201          
202                 case INPUTINCLUDE:
203                         cout << "inputinclude" << endl;
204                         /* huh ? why doesn't this work ? */ 
205                         fl_deactivate_object(dialog_->flag41);
206                         fl_set_object_lcol(dialog_->flag41, FL_INACTIVE);
207                         fl_set_button(dialog_->flag41, 0);
208                         break;
209         }
210         return true; 
211 }