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