]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/ControlDocument.cpp
next one
[lyx.git] / src / frontends / controllers / ControlDocument.cpp
1 /**
2  * \file ControlDocument.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Edwin Leuven
7  * \author Richard Heck (modules)
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "ControlDocument.h"
15
16 #include "BranchList.h"
17 #include "Buffer.h"
18 #include "BufferParams.h"
19 #include "BufferView.h"
20 #include "buffer_funcs.h"
21 #include "Color.h"
22 #include "FuncRequest.h"
23 #include "gettext.h"
24 #include "Language.h"
25 #include "LaTeXFeatures.h"
26 #include "ModuleList.h"
27 #include "OutputParams.h"
28 #include "TextClassList.h"
29
30 // FIXME: those two headers are needed because of the
31 // WorkArea::redraw() call below.
32 #include "frontends/LyXView.h"
33 #include "frontends/WorkArea.h"
34
35 #include <sstream>
36
37 using std::ostringstream;
38 using std::sort;
39 using std::string;
40 using std::vector;
41
42 namespace lyx {
43 namespace frontend {
44
45 char const * const ControlDocument::fontfamilies[5] = {
46         "default", "rmdefault", "sfdefault", "ttdefault", ""
47 };
48
49
50 char const * ControlDocument::fontfamilies_gui[5] = {
51         N_("Default"), N_("Roman"), N_("Sans Serif"), N_("Typewriter"), ""
52 };
53
54
55 ControlDocument::ControlDocument(Dialog & parent)
56         : Controller(parent)
57 {}
58
59
60 ControlDocument::~ControlDocument()
61 {}
62
63
64 bool ControlDocument::initialiseParams(string const &)
65 {
66         bp_ = buffer().params();
67         loadModuleNames();
68         return true;
69 }
70
71
72 void ControlDocument::clearParams()
73 {
74         bp_ = BufferParams();
75 }
76
77
78 BufferId ControlDocument::id() const
79 {
80         return &buffer();
81 }
82
83
84 vector<string> ControlDocument::getModuleNames()
85 {
86         return moduleNames_;
87 }
88
89
90 vector<string> const & ControlDocument::getSelectedModules()
91 {
92         return params().getModules();
93 }
94
95
96 string ControlDocument::getModuleDescription(string const & modName) const
97 {
98         LyXModule const * const mod = moduleList[modName];
99         if (!mod)
100                 return string("Module unavailable!");
101         return mod->description;
102 }
103
104
105 vector<string>
106 ControlDocument::getPackageList(string const & modName) const
107 {
108         LyXModule const * const mod = moduleList[modName];
109         if (!mod)
110                 return vector<string>(); //empty such thing
111         return mod->packageList;
112 }
113
114
115 TextClass const & ControlDocument::textClass() const
116 {
117         return textclasslist[bp_.getBaseClass()];
118 }
119
120
121 static void dispatch_bufferparams(Controller const & controller,
122         BufferParams const & bp, kb_action lfun)
123 {
124         ostringstream ss;
125         ss << "\\begin_header\n";
126         bp.writeFile(ss);
127         ss << "\\end_header\n";
128         controller.dispatch(FuncRequest(lfun, ss.str()));
129 }
130
131
132 void ControlDocument::dispatchParams()
133 {
134         // This must come first so that a language change is correctly noticed
135         setLanguage();
136
137         // Apply the BufferParams. Note that this will set the base class
138         // and then update the buffer's layout.
139         //FIXME Could this be done last? Then, I think, we'd get the automatic
140         //update mentioned in the next FIXME...
141         dispatch_bufferparams(*this, params(), LFUN_BUFFER_PARAMS_APPLY);
142
143         // Generate the colours requested by each new branch.
144         BranchList & branchlist = params().branchlist();
145         if (!branchlist.empty()) {
146                 BranchList::const_iterator it = branchlist.begin();
147                 BranchList::const_iterator const end = branchlist.end();
148                 for (; it != end; ++it) {
149                         docstring const & current_branch = it->getBranch();
150                         Branch const * branch = branchlist.find(current_branch);
151                         string const x11hexname =
152                                         lyx::X11hexname(branch->getColor());
153                         // display the new color
154                         docstring const str = current_branch + ' ' + from_ascii(x11hexname);
155                         dispatch(FuncRequest(LFUN_SET_COLOR, str));
156                 }
157
158                 // Open insets of selected branches, close deselected ones
159                 dispatch(FuncRequest(LFUN_ALL_INSETS_TOGGLE,
160                         "assign branch"));
161         }
162         // FIXME: If we used an LFUN, we would not need those two lines:
163         bufferview()->update();
164         lyxview().currentWorkArea()->redraw();
165 }
166
167
168 void ControlDocument::setLanguage() const
169 {
170         Language const * const newL = bp_.language;
171         if (buffer().params().language == newL)
172                 return;
173
174         string const & lang_name = newL->lang();
175         dispatch(FuncRequest(LFUN_BUFFER_LANGUAGE, lang_name));
176 }
177
178
179 void ControlDocument::saveAsDefault() const
180 {
181         dispatch_bufferparams(*this, params(), LFUN_BUFFER_SAVE_AS_DEFAULT);
182 }
183
184
185 bool ControlDocument::isFontAvailable(string const & font) const
186 {
187         if (font == "default" || font == "cmr"
188             || font == "cmss" || font == "cmtt")
189                 // these are standard
190                 return true;
191         else if (font == "lmodern" || font == "lmss" || font == "lmtt")
192                 return LaTeXFeatures::isAvailable("lmodern");
193         else if (font == "times" || font == "palatino"
194                  || font == "helvet" || font == "courier")
195                 return LaTeXFeatures::isAvailable("psnfss");
196         else if (font == "cmbr" || font == "cmtl")
197                 return LaTeXFeatures::isAvailable("cmbright");
198         else if (font == "utopia")
199                 return LaTeXFeatures::isAvailable("utopia")
200                         || LaTeXFeatures::isAvailable("fourier");
201         else if (font == "beraserif" || font == "berasans"
202                 || font == "beramono")
203                 return LaTeXFeatures::isAvailable("bera");
204         else
205                 return LaTeXFeatures::isAvailable(font);
206 }
207
208
209 bool ControlDocument::providesOSF(string const & font) const
210 {
211         if (font == "cmr")
212                 return isFontAvailable("eco");
213         else if (font == "palatino")
214                 return isFontAvailable("mathpazo");
215         else
216                 return false;
217 }
218
219
220 bool ControlDocument::providesSC(string const & font) const
221 {
222         if (font == "palatino")
223                 return isFontAvailable("mathpazo");
224         else if (font == "utopia")
225                 return isFontAvailable("fourier");
226         else
227                 return false;
228 }
229
230
231 bool ControlDocument::providesScale(string const & font) const
232 {
233         return (font == "helvet" || font == "luximono"
234                 || font == "berasans"  || font == "beramono");
235 }
236
237
238 void ControlDocument::loadModuleNames ()
239 {
240         moduleNames_.clear();
241         LyXModuleList::const_iterator it = moduleList.begin();
242         for (; it != moduleList.end(); ++it)
243                 moduleNames_.push_back(it->name);
244         if (!moduleNames_.empty())
245                 sort(moduleNames_.begin(), moduleNames_.end());
246 }
247
248
249 } // namespace frontend
250 } // namespace lyx