]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/ControlExternal.C
ws cleanup
[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 "BufferView.h"
26 #include "ButtonControllerBase.h"
27 #include "ControlExternal.h"
28 #include "ControlInset.tmpl"
29 #include "Dialogs.h"
30 #include "Liason.h"
31 #include "LyXView.h"
32 #include "ViewBase.h"
33 #include "buffer.h"
34 #include "frontends/Alert.h"
35 #include "gettext.h"
36 #include "helper_funcs.h"
37 #include "lyxrc.h"
38 #include "support/filetools.h"
39 #include "support/lstrings.h"
40
41 using std::make_pair;
42 using std::vector;
43
44 ControlExternal::ControlExternal(LyXView & lv, Dialogs & d)
45         : ControlInset<InsetExternal, InsetExternal::Params>(lv, d)
46 {
47         d_.showExternal.connect(SigC::slot(this, &ControlExternal::showInset));
48 }
49
50
51 InsetExternal::Params const ControlExternal::getParams(string const &)
52 {
53         return InsetExternal::Params();
54 }
55
56 InsetExternal::Params const
57 ControlExternal::getParams(InsetExternal const & inset)
58 {
59         return inset.params();
60 }
61
62
63 void ControlExternal::applyParamsToInset()
64 {
65         inset()->setFromParams(params());
66         lv_.view()->updateInset(inset(), true);
67 }
68
69 void ControlExternal::editExternal()
70 {
71         // fill the local, controller's copy of the Params struct with
72         // the contents of the dialog's fields.
73         view().apply();
74
75         // Create a local copy of the inset and initialise it with this
76         // params struct.
77         boost::scoped_ptr<InsetExternal> ie;
78         ie.reset(static_cast<InsetExternal *>(inset()->clone(*lv_.buffer())));
79         ie->setFromParams(params());
80
81         ie->editExternal();
82 }
83
84 void ControlExternal::viewExternal()
85 {
86         view().apply();
87
88         boost::scoped_ptr<InsetExternal> ie;
89         ie.reset(static_cast<InsetExternal *>(inset()->clone(*lv_.buffer())));
90         ie->setFromParams(params());
91
92         ie->viewExternal();
93 }
94
95 void ControlExternal::updateExternal()
96 {
97         view().apply();
98
99         boost::scoped_ptr<InsetExternal> ie;
100         ie.reset(static_cast<InsetExternal *>(inset()->clone(*lv_.buffer())));
101         ie->setFromParams(params());
102
103         ie->updateExternal();
104 }
105
106 vector<string> const ControlExternal::getTemplates() const
107 {
108         vector<string> result;
109
110         ExternalTemplateManager::Templates::const_iterator i1, i2;
111         i1 = ExternalTemplateManager::get().getTemplates().begin();
112         i2 = ExternalTemplateManager::get().getTemplates().end();
113
114         for (; i1 != i2; ++i1) {
115                 result.push_back(i1->second.lyxName);
116         }
117         return result;
118 }
119
120
121 int ControlExternal::getTemplateNumber(string const & name) const
122 {
123         int i = 0;
124
125         ExternalTemplateManager::Templates::const_iterator i1, i2;
126         i1 = ExternalTemplateManager::get().getTemplates().begin();
127         i2 = ExternalTemplateManager::get().getTemplates().end();
128         for (; i1 != i2; ++i1) {
129                 if (i1->second.lyxName == name)
130                         return i;
131                 ++i;
132         }
133
134         // we can get here if a LyX document has a template not installed
135         // on this machine.
136         return -1;
137 }
138
139
140 ExternalTemplate ControlExternal::getTemplate(int i) const
141 {
142         ExternalTemplateManager::Templates::const_iterator i1;
143         i1 = ExternalTemplateManager::get().getTemplates().begin();
144         for (int n = 1; n < i; ++n)
145                 ++i1;
146
147         return i1->second;
148 }
149
150
151 string const ControlExternal::Browse(string const & input) const
152 {
153         string const title =  _("Select external file");
154
155         string const bufpath = lv_.buffer()->filePath();
156
157         /// Determine the template file extension
158         ExternalTemplate const & et = params().templ;
159         string pattern = et.fileRegExp;
160         if (pattern.empty())
161                 pattern = "*";
162
163         // FIXME: a temporary hack until the FileDialog interface is updated
164         pattern += "|";
165
166         std::pair<string, string> dir1(N_("Documents|#o#O"),
167                                   string(lyxrc.document_path));
168
169         return browseRelFile(&lv_, input, bufpath, title, pattern, dir1);
170 }