]> git.lyx.org Git - features.git/blob - src/frontends/controllers/ControlDocument.cpp
This patch continues 19964. It takes advantage of the work there to simplify a few...
[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 BufferId ControlDocument::id() const
83 {
84         return &kernel().buffer();
85 }
86
87
88 TextClass const & ControlDocument::textClass() const
89 {
90         return textclasslist[bp_->getBaseClass()];
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         // Apply the BufferParams. Note that this will set the base class
115         // and then update the buffer's layout.
116         //FIXME Could this be done last? Then, I think, we'd get the automatic
117         //update mentioned in the next FIXME...
118         dispatch_bufferparams(kernel(), params(), LFUN_BUFFER_PARAMS_APPLY);
119
120         // Generate the colours requested by each new branch.
121         BranchList & branchlist = params().branchlist();
122         if (!branchlist.empty()) {
123                 BranchList::const_iterator it = branchlist.begin();
124                 BranchList::const_iterator const end = branchlist.end();
125                 for (; it != end; ++it) {
126                         docstring const & current_branch = it->getBranch();
127                         Branch const * branch = branchlist.find(current_branch);
128                         string const x11hexname =
129                                         lyx::X11hexname(branch->getColor());
130                         // display the new color
131                         docstring const str = current_branch + ' ' + from_ascii(x11hexname);
132                         kernel().dispatch(FuncRequest(LFUN_SET_COLOR, str));
133                 }
134
135                 // Open insets of selected branches, close deselected ones
136                 kernel().dispatch(FuncRequest(LFUN_ALL_INSETS_TOGGLE,
137                         "assign branch"));
138         }
139         // FIXME: If we used an LFUN, we would not need those two lines:
140         kernel().bufferview()->update();
141         kernel().lyxview().currentWorkArea()->redraw();
142 }
143
144
145 void ControlDocument::setLanguage() const
146 {
147         Language const * const newL = bp_->language;
148         if (kernel().buffer().params().language == newL)
149                 return;
150
151         string const lang_name = newL->lang();
152         kernel().dispatch(FuncRequest(LFUN_BUFFER_LANGUAGE, lang_name));
153 }
154
155
156 void ControlDocument::saveAsDefault() const
157 {
158         dispatch_bufferparams(kernel(), params(), LFUN_BUFFER_SAVE_AS_DEFAULT);
159 }
160
161
162 bool const ControlDocument::isFontAvailable(std::string const & font) const
163 {
164         if (font == "default" || font == "cmr"
165             || font == "cmss" || font == "cmtt")
166                 // these are standard
167                 return true;
168         else if (font == "lmodern" || font == "lmss" || font == "lmtt")
169                 return LaTeXFeatures::isAvailable("lmodern");
170         else if (font == "times" || font == "palatino"
171                  || font == "helvet" || font == "courier")
172                 return LaTeXFeatures::isAvailable("psnfss");
173         else if (font == "cmbr" || font == "cmtl")
174                 return LaTeXFeatures::isAvailable("cmbright");
175         else if (font == "utopia")
176                 return LaTeXFeatures::isAvailable("utopia")
177                         || LaTeXFeatures::isAvailable("fourier");
178         else if (font == "beraserif" || font == "berasans"
179                 || font == "beramono")
180                 return LaTeXFeatures::isAvailable("bera");
181         else
182                 return LaTeXFeatures::isAvailable(font);
183 }
184
185
186 bool const ControlDocument::providesOSF(std::string const & font) const
187 {
188         if (font == "cmr")
189                 return isFontAvailable("eco");
190         else if (font == "palatino")
191                 return isFontAvailable("mathpazo");
192         else
193                 return false;
194 }
195
196
197 bool const ControlDocument::providesSC(std::string const & font) const
198 {
199         if (font == "palatino")
200                 return isFontAvailable("mathpazo");
201         else if (font == "utopia")
202                 return isFontAvailable("fourier");
203         else
204                 return false;
205 }
206
207
208 bool const ControlDocument::providesScale(std::string const & font) const
209 {
210         return (font == "helvet" || font == "luximono"
211                 || font == "berasans"  || font == "beramono");
212 }
213
214
215 } // namespace frontend
216 } // namespace lyx