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