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