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