]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormInclude.C
Applied John's FileDialog patch.
[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         : FormCommand(lv, d, _("Include file"), new OkCancelPolicy),
36           dialog_(0)
37 {
38         d->showInclude.connect(slot(this, &FormInclude::showInset));
39         d->createInclude.connect(slot(this, &FormInclude::createInset));
40 }
41
42
43 FormInclude::~FormInclude()
44 {
45         delete dialog_;
46 }
47
48
49 FL_FORM * FormInclude::form() const
50 {
51         if (dialog_) 
52                 return dialog_->form;
53         return 0;
54 }
55
56
57 void FormInclude::build()
58 {
59         dialog_ = build_include();
60
61         // Workaround dumb xforms sizing bug
62         minw_ = form()->w;
63         minh_ = form()->h;
64
65         // Manage the ok and cancel buttons
66         bc_.setOK(dialog_->button_ok);
67         bc_.setCancel(dialog_->button_cancel);
68         bc_.refresh();
69
70         bc_.addReadOnly(dialog_->browsebt);
71         bc_.addReadOnly(dialog_->flag1);
72         bc_.addReadOnly(dialog_->flag2);
73         bc_.addReadOnly(dialog_->flag3);
74         bc_.addReadOnly(dialog_->flag4);
75         bc_.addReadOnly(dialog_->filename);
76         bc_.addReadOnly(dialog_->flag41);
77 }
78
79
80 void FormInclude::update()
81 {
82         bc_.readOnly(lv_->buffer()->isReadonly());
83
84         if (!inset_) {
85                 fl_set_input(dialog_->filename, "");
86                 fl_set_button(dialog_->flag1, 0);
87                 fl_set_button(dialog_->flag2, 0);
88                 fl_set_button(dialog_->flag3, 1);
89                 fl_set_button(dialog_->flag4, 0);
90                 fl_set_button(dialog_->flag41, 0);
91                 fl_deactivate_object(dialog_->flag41);
92                 fl_set_object_lcol(dialog_->flag41, FL_INACTIVE);
93                 return;
94         }
95  
96         fl_set_input(dialog_->filename, params.getContents().c_str());
97  
98         string const cmdname = params.getCmdName();
99  
100         /* FIXME: what do with Don't typeset here ... */
101         //fl_set_button(dialog_->flag1, int(inset_->isNoLoad()));
102  
103         fl_set_button(dialog_->flag2, cmdname == "input");
104         fl_set_button(dialog_->flag3, cmdname == "include");
105         if (cmdname == "verbatiminput" || cmdname == "verbatiminput*") {
106                 fl_set_button(dialog_->flag4, 1);
107                 fl_set_button(dialog_->flag41, cmdname == "verbatiminput*");
108         } else {
109                 fl_set_button(dialog_->flag41, 0);
110                 fl_deactivate_object(dialog_->flag41);
111                 fl_set_object_lcol(dialog_->flag41, FL_INACTIVE);
112         }
113 }
114
115
116 void FormInclude::apply()
117 {
118         if (lv_->buffer()->isReadonly())
119                 return;
120
121         /* FIXME: no way to update internal flags of inset ??? */
122  
123         //inset_->setNoLoad(fl_get_button(dialog_->flag1));
124  
125         params.setContents(fl_get_input(dialog_->filename));
126
127         if (fl_get_button(dialog_->flag2))
128                 params.setCmdName("input");
129         else if (fl_get_button(dialog_->flag3))
130                 params.setCmdName("include");
131         else if (fl_get_button(dialog_->flag4)) {
132                 if (fl_get_button(dialog_->flag41))
133                         params.setCmdName("verbatiminput*");
134                 else
135                         params.setCmdName("verbatiminput");     
136         }
137         
138         if (inset_) {
139                 if (params != inset_->params()) {
140                         inset_->setParams(params);
141                         lv_->view()->updateInset(inset_, true);
142                 }
143         } else
144                 lv_->getLyXFunc()->Dispatch(LFUN_CHILD_INSERT, params.getAsString());
145 }
146  
147 #ifdef WITH_WARNINGS
148 #warning convert this to use the buttoncontroller
149 #endif
150 bool FormInclude::input(FL_OBJECT *, long data)
151 {
152         State state = static_cast<State>(data); 
153  
154         switch (state) {
155                 case BROWSE: {
156                         // Should browsing too be disabled in RO-mode?
157                         FileDialog fileDlg(lv_, _("Select document to include"),
158                                 LFUN_SELECT_FILE_SYNC,
159                                 make_pair(string(_("Documents")), string(lyxrc.document_path)));
160  
161                         string ext;
162                     
163                         /* input TeX, verbatim, or LyX file ? */
164                         if (fl_get_button(dialog_->flag2))
165                                 ext = _("*.tex| LaTeX Documents (*.tex)");
166                         else if (fl_get_button(dialog_->flag4))
167                                 ext = _("*| All files ");
168                         else
169                                 ext = _("*.lyx| LyX Documents (*.lyx)");
170          
171                         string mpath;
172  
173                         /* FIXME: what do I do here ? */ 
174                         //if (inset_)
175                         //      mpath = OnlyPath(inset_->getMasterFilename());
176  
177                         FileDialog::Result result = fileDlg.Select(mpath, ext, fl_get_input(dialog_->filename));
178          
179                         // check selected filename
180                         if (result.second.empty())
181                                 break;
182          
183                         string const filename2 = MakeRelPath(result.second, mpath);
184          
185                         if (prefixIs(filename2, ".."))
186                                 fl_set_input(dialog_->filename, result.second.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                         setEnabled(dialog_->flag41, true);
199                         break;
200          
201                 case INPUTINCLUDE:
202                         /* FIXME: huh ? why doesn't this work ? */ 
203                         setEnabled(dialog_->flag41, false);
204                         fl_set_button(dialog_->flag41, 0);
205                         break;
206         }
207         return true; 
208 }