]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/ControlExternal.C
do not include language.h and gettext.h in lyxfont.h and lyxparagraph.h
[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
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         InsetExternal ie;
72         ie.setFromParams(params());
73
74         ie.editExternal();
75 }
76
77 void ControlExternal::viewExternal()
78 {
79         view().apply();
80
81         InsetExternal ie;
82         ie.setFromParams(params());
83
84         ie.viewExternal();
85 }
86
87 void ControlExternal::updateExternal()
88 {
89         view().apply();
90
91         InsetExternal ie;
92         ie.setFromParams(params());
93
94         ie.updateExternal();
95 }
96
97 vector<string> const ControlExternal::getTemplates() const
98 {
99         vector<string> result;
100
101         ExternalTemplateManager::Templates::const_iterator i1, i2;
102         i1 = ExternalTemplateManager::get().getTemplates().begin();
103         i2 = ExternalTemplateManager::get().getTemplates().end();
104
105         for (; i1 != i2; ++i1) {
106                 result.push_back(i1->second.lyxName);
107         }
108         return result;
109 }
110
111
112 int ControlExternal::getTemplateNumber(string const & name) const
113 {
114         int i = 0;
115
116         ExternalTemplateManager::Templates::const_iterator i1, i2;
117         i1 = ExternalTemplateManager::get().getTemplates().begin();
118         i2 = ExternalTemplateManager::get().getTemplates().end();
119         for (; i1 != i2; ++i1) {
120                 if (i1->second.lyxName == name)
121                         return i;
122                 ++i;
123         }
124
125         // we can get here if a LyX document has a template not installed
126         // on this machine.
127         return -1;
128 }
129
130
131 ExternalTemplate ControlExternal::getTemplate(int i) const
132 {
133         ExternalTemplateManager::Templates::const_iterator i1;
134         i1 = ExternalTemplateManager::get().getTemplates().begin();
135         for (int n = 1; n < i; ++n)
136                 ++i1;
137
138         return (*i1).second;
139 }
140
141
142 string const ControlExternal::Browse(string const & input) const
143 {
144         string buf  = MakeAbsPath(lv_.buffer()->fileName());
145         string buf2 = OnlyPath(buf);
146
147         if (!input.empty()) {
148                 buf = MakeAbsPath(input, buf2);
149                 buf = OnlyPath(buf);
150         } else {
151                 buf = OnlyPath(lv_.buffer()->fileName());
152         }
153     
154         FileDialog fileDlg(&lv_,
155                            _("Select external file"),
156                            LFUN_SELECT_FILE_SYNC,
157                            make_pair(string(_("Document")), string(buf)));
158         
159         /// Determine the template file extension
160         ExternalTemplate const & et = params().templ;
161
162         string regexp = et.fileRegExp;
163         if (regexp.empty())
164                 regexp = "*";
165
166         // FIXME: a temporary hack until the FileDialog interface is updated
167         regexp += "|";
168
169         static int once = 0;
170         string current_path;
171
172         while (1) {
173                 string const path = (once) ? current_path : buf;
174                 FileDialog::Result result = fileDlg.Select(path, regexp, input);
175
176                 if (result.second.empty())
177                         return string();
178
179                 string p = result.second;
180
181                 buf = MakeRelPath(p, buf2);
182                 current_path = OnlyPath(p);
183                 once = 1;
184                 
185                 if (contains(p, "#") ||
186                     contains(p, "~") ||
187                     contains(p, "$") ||
188                     contains(p, "%")) {
189                         WriteAlert(_("Filename can't contain any "
190                                      "of these characters:"),
191                                    // xgettext:no-c-format
192                                    _("'#', '~', '$' or '%'."));
193                 } else
194                         break;
195         }
196
197         return buf;
198 }