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