]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/ControlDocument.cpp
Fixed some lines that were too long. It compiled afterwards.
[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  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "ControlDocument.h"
14 #include "Kernel.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 "FuncRequest.h"
22 #include "gettext.h"
23 #include "Language.h"
24 #include "LaTeXFeatures.h"
25 #include "Color.h"
26 #include "OutputParams.h"
27 #include "TextClassList.h"
28
29 // FIXME: those two headers are needed because of the
30 // WorkArea::redraw() call below.
31 #include "frontends/LyXView.h"
32 #include "frontends/WorkArea.h"
33
34 #include <sstream>
35
36 using std::ostringstream;
37 using std::string;
38
39 namespace lyx {
40 namespace frontend {
41
42 char const * const ControlDocument::fontfamilies[5] = {
43         "default", "rmdefault", "sfdefault", "ttdefault", ""
44 };
45
46
47 char const * ControlDocument::fontfamilies_gui[5] = {
48         N_("Default"), N_("Roman"), N_("Sans Serif"), N_("Typewriter"), ""
49 };
50
51
52 ControlDocument::ControlDocument(Dialog & parent)
53         : Dialog::Controller(parent)
54 {}
55
56
57 ControlDocument::~ControlDocument()
58 {}
59
60
61 bool ControlDocument::initialiseParams(std::string const &)
62 {
63         bp_.reset(new BufferParams);
64         *bp_ = kernel().buffer().params();
65         return true;
66 }
67
68
69 void ControlDocument::clearParams()
70 {
71         bp_.reset();
72 }
73
74
75 BufferParams & ControlDocument::params() const
76 {
77         BOOST_ASSERT(bp_.get());
78         return *bp_;
79 }
80
81
82 BufferId ControlDocument::id() const
83 {
84         return &kernel().buffer();
85 }
86
87
88 TextClass const & ControlDocument::textClass() const
89 {
90         return textclasslist[bp_->textclass];
91 }
92
93
94 namespace {
95
96 void dispatch_bufferparams(Kernel const & kernel, BufferParams const & bp,
97                            kb_action lfun)
98 {
99         ostringstream ss;
100         ss << "\\begin_header\n";
101         bp.writeFile(ss);
102         ss << "\\end_header\n";
103         kernel.dispatch(FuncRequest(lfun, ss.str()));
104 }
105
106 } // namespace anon
107
108
109 void ControlDocument::dispatchParams()
110 {
111         // This must come first so that a language change is correctly noticed
112         setLanguage();
113
114         // Set the document class.
115         textclass_type const old_class =
116                 kernel().buffer().params().textclass;
117         textclass_type const new_class = bp_->textclass;
118         if (new_class != old_class) {
119                 string const name = textclasslist[new_class].name();
120                 kernel().dispatch(FuncRequest(LFUN_TEXTCLASS_APPLY, name));
121         }
122
123         int const old_secnumdepth = kernel().buffer().params().secnumdepth;
124         int const new_secnumdepth = bp_->secnumdepth;
125
126         // Apply the BufferParams.
127         dispatch_bufferparams(kernel(), params(), LFUN_BUFFER_PARAMS_APPLY);
128
129         // redo the numbering if necessary
130         if (new_secnumdepth != old_secnumdepth)
131                 updateLabels(kernel().buffer());
132
133         // Generate the colours requested by each new branch.
134         BranchList & branchlist = params().branchlist();
135         if (!branchlist.empty()) {
136                 BranchList::const_iterator it = branchlist.begin();
137                 BranchList::const_iterator const end = branchlist.end();
138                 for (; it != end; ++it) {
139                         docstring const & current_branch = it->getBranch();
140                         Branch const * branch = branchlist.find(current_branch);
141                         string const x11hexname =
142                                         lyx::X11hexname(branch->getColor());
143                         // display the new color
144                         docstring const str = current_branch + ' ' + from_ascii(x11hexname);
145                         kernel().dispatch(FuncRequest(LFUN_SET_COLOR, str));
146                 }
147
148                 // Open insets of selected branches, close deselected ones
149                 kernel().dispatch(FuncRequest(LFUN_ALL_INSETS_TOGGLE,
150                         "assign branch"));
151         }
152         // FIXME: If we used an LFUN, we would not need those two lines:
153         kernel().bufferview()->update();
154         kernel().lyxview().currentWorkArea()->redraw();
155 }
156
157
158 void ControlDocument::setLanguage() const
159 {
160         Language const * const newL = bp_->language;
161         if (kernel().buffer().params().language == newL)
162                 return;
163
164         string const lang_name = newL->lang();
165         kernel().dispatch(FuncRequest(LFUN_BUFFER_LANGUAGE, lang_name));
166 }
167
168
169 bool ControlDocument::loadTextclass(textclass_type tc) const
170 {
171         string const name = textclasslist[tc].name();
172         kernel().dispatch(FuncRequest(LFUN_TEXTCLASS_LOAD, name));
173
174         // Report back whether we were able to change the class.
175         bool const success = textclasslist[tc].loaded();
176         return success;
177 }
178
179
180 void ControlDocument::saveAsDefault() const
181 {
182         dispatch_bufferparams(kernel(), params(), LFUN_BUFFER_SAVE_AS_DEFAULT);
183 }
184
185
186 bool const ControlDocument::isFontAvailable(std::string const & font) const
187 {
188         if (font == "default" || font == "cmr"
189             || font == "cmss" || font == "cmtt")
190                 // these are standard
191                 return true;
192         else if (font == "lmodern" || font == "lmss" || font == "lmtt")
193                 return LaTeXFeatures::isAvailable("lmodern");
194         else if (font == "times" || font == "palatino"
195                  || font == "helvet" || font == "courier")
196                 return LaTeXFeatures::isAvailable("psnfss");
197         else if (font == "cmbr" || font == "cmtl")
198                 return LaTeXFeatures::isAvailable("cmbright");
199         else if (font == "utopia")
200                 return LaTeXFeatures::isAvailable("utopia")
201                         || LaTeXFeatures::isAvailable("fourier");
202         else if (font == "beraserif" || font == "berasans"
203                 || font == "beramono")
204                 return LaTeXFeatures::isAvailable("bera");
205         else
206                 return LaTeXFeatures::isAvailable(font);
207 }
208
209
210 bool const ControlDocument::providesOSF(std::string const & font) const
211 {
212         if (font == "cmr")
213                 return isFontAvailable("eco");
214         else if (font == "palatino")
215                 return isFontAvailable("mathpazo");
216         else
217                 return false;
218 }
219
220
221 bool const ControlDocument::providesSC(std::string const & font) const
222 {
223         if (font == "palatino")
224                 return isFontAvailable("mathpazo");
225         else if (font == "utopia")
226                 return isFontAvailable("fourier");
227         else
228                 return false;
229 }
230
231
232 bool const ControlDocument::providesScale(std::string const & font) const
233 {
234         return (font == "helvet" || font == "luximono"
235                 || font == "berasans"  || font == "beramono");
236 }
237
238
239 } // namespace frontend
240 } // namespace lyx