]> git.lyx.org Git - features.git/blob - src/frontends/controllers/ControlDocument.cpp
distribute content sof tex-strings.cpp to their respective unique places of usage
[features.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 TextClass const & ControlDocument::textClass() const
83 {
84         return textclasslist[bp_->textclass];
85 }
86
87
88 namespace {
89
90 void dispatch_bufferparams(Kernel const & kernel, BufferParams const & bp,
91                            kb_action lfun)
92 {
93         ostringstream ss;
94         ss << "\\begin_header\n";
95         bp.writeFile(ss);
96         ss << "\\end_header\n";
97         kernel.dispatch(FuncRequest(lfun, ss.str()));
98 }
99
100 } // namespace anon
101
102
103 void ControlDocument::dispatchParams()
104 {
105         // This must come first so that a language change is correctly noticed
106         setLanguage();
107
108         // Set the document class.
109         textclass_type const old_class =
110                 kernel().buffer().params().textclass;
111         textclass_type const new_class = bp_->textclass;
112         if (new_class != old_class) {
113                 string const name = textclasslist[new_class].name();
114                 kernel().dispatch(FuncRequest(LFUN_TEXTCLASS_APPLY, name));
115         }
116
117         int const old_secnumdepth = kernel().buffer().params().secnumdepth;
118         int const new_secnumdepth = bp_->secnumdepth;
119
120         // Apply the BufferParams.
121         dispatch_bufferparams(kernel(), params(), LFUN_BUFFER_PARAMS_APPLY);
122         
123         // redo the numbering if necessary
124         if (new_secnumdepth != old_secnumdepth)
125                 updateLabels(kernel().buffer());
126
127         // Generate the colours requested by each new branch.
128         BranchList & branchlist = params().branchlist();
129         if (!branchlist.empty()) {
130                 BranchList::const_iterator it = branchlist.begin();
131                 BranchList::const_iterator const end = branchlist.end();
132                 for (; it != end; ++it) {
133                         docstring const & current_branch = it->getBranch();
134                         Branch const * branch = branchlist.find(current_branch);
135                         string const x11hexname =
136                                         lyx::X11hexname(branch->getColor());
137                         // display the new color
138                         docstring const str = current_branch + ' ' + from_ascii(x11hexname);
139                         kernel().dispatch(FuncRequest(LFUN_SET_COLOR, str));
140                 }
141
142                 // Open insets of selected branches, close deselected ones
143                 kernel().dispatch(FuncRequest(LFUN_ALL_INSETS_TOGGLE,
144                         "assign branch"));
145         }
146         // FIXME: If we used an LFUN, we would not need those two lines:
147         kernel().bufferview()->update();
148         kernel().lyxview().currentWorkArea()->redraw();
149 }
150
151
152 void ControlDocument::setLanguage() const
153 {
154         Language const * const newL = bp_->language;
155         if (kernel().buffer().params().language == newL)
156                 return;
157
158         string const lang_name = newL->lang();
159         kernel().dispatch(FuncRequest(LFUN_BUFFER_LANGUAGE, lang_name));
160 }
161
162
163 bool ControlDocument::loadTextclass(textclass_type tc) const
164 {
165         string const name = textclasslist[tc].name();
166         kernel().dispatch(FuncRequest(LFUN_TEXTCLASS_LOAD, name));
167
168         // Report back whether we were able to change the class.
169         bool const success = textclasslist[tc].loaded();
170         return success;
171 }
172
173
174 void ControlDocument::saveAsDefault() const
175 {
176         dispatch_bufferparams(kernel(), params(), LFUN_BUFFER_SAVE_AS_DEFAULT);
177 }
178
179
180 bool const ControlDocument::isFontAvailable(std::string const & font) const
181 {
182         if (font == "default" || font == "cmr" 
183             || font == "cmss" || font == "cmtt")
184                 // these are standard
185                 return true;
186         else if (font == "lmodern" || font == "lmss" || font == "lmtt")
187                 return LaTeXFeatures::isAvailable("lmodern");
188         else if (font == "times" || font == "palatino" 
189                  || font == "helvet" || font == "courier")
190                 return LaTeXFeatures::isAvailable("psnfss");
191         else if (font == "cmbr" || font == "cmtl")
192                 return LaTeXFeatures::isAvailable("cmbright");
193         else if (font == "utopia")
194                 return LaTeXFeatures::isAvailable("utopia")
195                         || LaTeXFeatures::isAvailable("fourier");
196         else if (font == "beraserif" || font == "berasans" 
197                 || font == "beramono")
198                 return LaTeXFeatures::isAvailable("bera");
199         else
200                 return LaTeXFeatures::isAvailable(font);
201 }
202
203
204 bool const ControlDocument::providesOSF(std::string const & font) const
205 {
206         if (font == "cmr")
207                 return isFontAvailable("eco");
208         else if (font == "palatino")
209                 return isFontAvailable("mathpazo");
210         else
211                 return false;
212 }
213
214
215 bool const ControlDocument::providesSC(std::string const & font) const
216 {
217         if (font == "palatino")
218                 return isFontAvailable("mathpazo");
219         else if (font == "utopia")
220                 return isFontAvailable("fourier");
221         else
222                 return false;
223 }
224
225
226 bool const ControlDocument::providesScale(std::string const & font) const
227 {
228         return (font == "helvet" || font == "luximono"
229                 || font == "berasans"  || font == "beramono");
230 }
231
232
233 } // namespace frontend
234 } // namespace lyx