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