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