]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/ControlDocument.C
Convert most of the bibtex machinery to docstring.
[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                         docstring 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                         // FIXME UNICODE
135                         string const str = to_utf8(current_branch) + ' ' + x11hexname;
136                         kernel().dispatch(FuncRequest(LFUN_SET_COLOR, str));
137                 }
138
139                 // Open insets of selected branches, close deselected ones
140                 kernel().dispatch(FuncRequest(LFUN_ALL_INSETS_TOGGLE,
141                         "assign branch"));
142         }
143         // update the bufferview
144         // If we used an LFUN, we would not need that
145         kernel().bufferview()->update();
146 }
147
148
149 void ControlDocument::setLanguage() const
150 {
151         Language const * const newL = bp_->language;
152         if (kernel().buffer().params().language == newL)
153                 return;
154
155         string const lang_name = newL->lang();
156         kernel().dispatch(FuncRequest(LFUN_BUFFER_LANGUAGE, lang_name));
157 }
158
159
160 bool ControlDocument::loadTextclass(textclass_type tc) const
161 {
162         string const name = textclasslist[tc].name();
163         kernel().dispatch(FuncRequest(LFUN_TEXTCLASS_LOAD, name));
164
165         // Report back whether we were able to change the class.
166         bool const success = textclasslist[tc].loaded();
167         return success;
168 }
169
170
171 void ControlDocument::saveAsDefault() const
172 {
173         dispatch_bufferparams(kernel(), params(), LFUN_BUFFER_SAVE_AS_DEFAULT);
174 }
175
176
177 bool const ControlDocument::isFontAvailable(std::string const & font) const
178 {
179         OutputParams runparams;
180         LaTeXFeatures features(kernel().buffer(), kernel().buffer().params(), runparams);
181         if (font == "default" || font == "cmr" 
182             || font == "cmss" || font == "cmtt")
183                 // these are standard
184                 return true;
185         else if (font == "lmodern" || font == "lmss" || font == "lmtt")
186                 return features.isAvailable("lmodern");
187         else if (font == "times" || font == "palatino" 
188                  || font == "helvet" || font == "courier")
189                 return (features.isAvailable("psnfss"));
190         else if (font == "cmbr" || font == "cmtl")
191                 return features.isAvailable("cmbright");
192         else if (font == "utopia")
193                 return (features.isAvailable("utopia")
194                         || features.isAvailable("fourier"));
195         else if (font == "beraserif" || font == "berasans" 
196                 || font == "beramono")
197                 return features.isAvailable("bera");
198         else
199                 return features.isAvailable(font);
200 }
201
202
203 bool const ControlDocument::providesOSF(std::string const & font) const
204 {
205         if (font == "cmr")
206                 return isFontAvailable("eco");
207         else if (font == "palatino")
208                 return isFontAvailable("mathpazo");
209         else
210                 return false;
211 }
212
213
214 bool const ControlDocument::providesSC(std::string const & font) const
215 {
216         if (font == "palatino")
217                 return isFontAvailable("mathpazo");
218         else if (font == "utopia")
219                 return isFontAvailable("fourier");
220         else
221                 return false;
222 }
223
224
225 bool const ControlDocument::providesScale(std::string const & font) const
226 {
227         return (font == "helvet" || font == "luximono"
228                 || font == "berasans"  || font == "beramono");
229 }
230
231
232 } // namespace frontend
233 } // namespace lyx