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