]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/ControlExternal.C
John's external material patch
[lyx.git] / src / frontends / controllers / ControlExternal.C
1 /* This file is part of
2  * ====================================================== 
3  *
4  *           LyX, The Document Processor
5  *
6  *           Copyright 2001 The LyX Team.
7  *
8  * ======================================================
9  *
10  * \file ControlExternal.C
11  * \author Asger Alstrup
12  * \author John Levon, moz@compsoc.man.ac.uk
13  * \author Angus Leeming, a.leeming@ic.ac.uk
14  */
15
16 #include <config.h>
17
18 #include <utility>
19 #include <vector>
20
21 #ifdef __GNUG__
22 #pragma implementation
23 #endif
24
25 #include "ControlExternal.h"
26 #include "buffer.h"
27 #include "Dialogs.h"
28 #include "Liason.h"
29 #include "LyXView.h"
30 #include "support/filetools.h"
31 #include "frontends/FileDialog.h"
32 #include "lyx_gui_misc.h" // WriteAlert
33 #include "gettext.h"
34
35 using std::make_pair;
36 using std::vector;
37
38 ControlExternal::ControlExternal(LyXView & lv, Dialogs & d)
39         : ControlInset<InsetExternal, InsetExternal::Params>(lv, d)
40 {
41         d_.showExternal.connect(SigC::slot(this, &ControlExternal::showInset));
42 }
43
44
45 InsetExternal::Params const ControlExternal::getParams(string const &)
46 {
47         return InsetExternal::Params();
48 }
49
50 InsetExternal::Params const 
51 ControlExternal::getParams(InsetExternal const & inset)
52 {
53         return inset.params();
54 }
55
56
57 void ControlExternal::applyParamsToInset()
58 {
59         inset()->setFromParams(params());
60         lv_.view()->updateInset(inset(), true);
61 }
62
63 void ControlExternal::editExternal()
64 {
65         // fill the local, controller's copy of the Params struct with
66         // the contents of the dialog's fields.
67         view().apply();
68
69         // Create a local copy of the inset and initialise it with this
70         // params struct.
71         boost::scoped_ptr<InsetExternal> ie;
72         ie.reset(static_cast<InsetExternal *>(inset()->Clone(*lv_.buffer())));
73         ie->setFromParams(params());
74
75         ie->editExternal();
76 }
77
78 void ControlExternal::viewExternal()
79 {
80         view().apply();
81
82         boost::scoped_ptr<InsetExternal> ie;
83         ie.reset(static_cast<InsetExternal *>(inset()->Clone(*lv_.buffer())));
84         ie->setFromParams(params());
85
86         ie->viewExternal();
87 }
88
89 void ControlExternal::updateExternal()
90 {
91         view().apply();
92
93         boost::scoped_ptr<InsetExternal> ie;
94         ie.reset(static_cast<InsetExternal *>(inset()->Clone(*lv_.buffer())));
95         ie->setFromParams(params());
96
97         ie->updateExternal();
98 }
99
100 vector<string> const ControlExternal::getTemplates() const
101 {
102         vector<string> result;
103
104         ExternalTemplateManager::Templates::const_iterator i1, i2;
105         i1 = ExternalTemplateManager::get().getTemplates().begin();
106         i2 = ExternalTemplateManager::get().getTemplates().end();
107
108         for (; i1 != i2; ++i1) {
109                 result.push_back(i1->second.lyxName);
110         }
111         return result;
112 }
113
114
115 int ControlExternal::getTemplateNumber(string const & name) const
116 {
117         int i = 0;
118
119         ExternalTemplateManager::Templates::const_iterator i1, i2;
120         i1 = ExternalTemplateManager::get().getTemplates().begin();
121         i2 = ExternalTemplateManager::get().getTemplates().end();
122         for (; i1 != i2; ++i1) {
123                 if (i1->second.lyxName == name)
124                         return i;
125                 ++i;
126         }
127
128         // we can get here if a LyX document has a template not installed
129         // on this machine.
130         return -1;
131 }
132
133
134 ExternalTemplate ControlExternal::getTemplate(int i) const
135 {
136         ExternalTemplateManager::Templates::const_iterator i1;
137         i1 = ExternalTemplateManager::get().getTemplates().begin();
138         for (int n = 1; n < i; ++n)
139                 ++i1;
140
141         return (*i1).second;
142 }
143
144
145 string const ControlExternal::Browse(string const & input) const
146 {
147         string buf  = MakeAbsPath(lv_.buffer()->fileName());
148         string buf2 = OnlyPath(buf);
149
150         if (!input.empty()) {
151                 buf = MakeAbsPath(input, buf2);
152                 buf = OnlyPath(buf);
153         } else {
154                 buf = OnlyPath(lv_.buffer()->fileName());
155         }
156     
157         FileDialog fileDlg(&lv_,
158                            _("Select external file"),
159                            LFUN_SELECT_FILE_SYNC,
160                            make_pair(string(_("Document")), string(buf)));
161         
162         /// Determine the template file extension
163         ExternalTemplate const & et = params().templ;
164
165         string regexp = et.fileRegExp;
166         if (regexp.empty())
167                 regexp = "*";
168
169         // FIXME: a temporary hack until the FileDialog interface is updated
170         regexp += "|";
171
172         static int once = 0;
173         string current_path;
174
175         while (1) {
176                 string const path = (once) ? current_path : buf;
177                 FileDialog::Result result = fileDlg.Select(path, regexp, input);
178
179                 if (result.second.empty())
180                         return string();
181
182                 string p = result.second;
183
184                 buf = MakeRelPath(p, buf2);
185                 current_path = OnlyPath(p);
186                 once = 1;
187                 
188                 if (contains(p, "#") ||
189                     contains(p, "~") ||
190                     contains(p, "$") ||
191                     contains(p, "%")) {
192                         WriteAlert(_("Filename can't contain any "
193                                      "of these characters:"),
194                                    // xgettext:no-c-format
195                                    _("'#', '~', '$' or '%'."));
196                 } else
197                         break;
198         }
199
200         return buf;
201 }