]> git.lyx.org Git - features.git/blob - src/frontends/controllers/ControlDocument.cpp
shuffle some frontend stuff around. merge controller(base) and "Kernel". Make fronten...
[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
15 #include "BranchList.h"
16 #include "Buffer.h"
17 #include "BufferParams.h"
18 #include "BufferView.h"
19 #include "buffer_funcs.h"
20 #include "FuncRequest.h"
21 #include "gettext.h"
22 #include "Language.h"
23 #include "LaTeXFeatures.h"
24 #include "Color.h"
25 #include "OutputParams.h"
26 #include "TextClassList.h"
27
28 // FIXME: those two headers are needed because of the
29 // WorkArea::redraw() call below.
30 #include "frontends/LyXView.h"
31 #include "frontends/WorkArea.h"
32
33 #include <sstream>
34
35 using std::ostringstream;
36 using std::string;
37
38 namespace lyx {
39 namespace frontend {
40
41 char const * const ControlDocument::fontfamilies[5] = {
42         "default", "rmdefault", "sfdefault", "ttdefault", ""
43 };
44
45
46 char const * ControlDocument::fontfamilies_gui[5] = {
47         N_("Default"), N_("Roman"), N_("Sans Serif"), N_("Typewriter"), ""
48 };
49
50
51 ControlDocument::ControlDocument(Dialog & parent)
52         : Controller(parent)
53 {}
54
55
56 ControlDocument::~ControlDocument()
57 {}
58
59
60 bool ControlDocument::initialiseParams(std::string const &)
61 {
62         bp_.reset(new BufferParams);
63         *bp_ = buffer().params();
64         return true;
65 }
66
67
68 void ControlDocument::clearParams()
69 {
70         bp_.reset();
71 }
72
73
74 BufferParams & ControlDocument::params() const
75 {
76         BOOST_ASSERT(bp_.get());
77         return *bp_;
78 }
79
80
81 BufferId ControlDocument::id() const
82 {
83         return &buffer();
84 }
85
86
87 TextClass const & ControlDocument::textClass() const
88 {
89         return textclasslist[bp_->getBaseClass()];
90 }
91
92
93 static void dispatch_bufferparams(Controller const & controller,
94         BufferParams const & bp, kb_action lfun)
95 {
96         ostringstream ss;
97         ss << "\\begin_header\n";
98         bp.writeFile(ss);
99         ss << "\\end_header\n";
100         controller.dispatch(FuncRequest(lfun, ss.str()));
101 }
102
103
104 void ControlDocument::dispatchParams()
105 {
106         // This must come first so that a language change is correctly noticed
107         setLanguage();
108
109         // Apply the BufferParams. Note that this will set the base class
110         // and then update the buffer's layout.
111         //FIXME Could this be done last? Then, I think, we'd get the automatic
112         //update mentioned in the next FIXME...
113         dispatch_bufferparams(*this, params(), LFUN_BUFFER_PARAMS_APPLY);
114
115         // Generate the colours requested by each new branch.
116         BranchList & branchlist = params().branchlist();
117         if (!branchlist.empty()) {
118                 BranchList::const_iterator it = branchlist.begin();
119                 BranchList::const_iterator const end = branchlist.end();
120                 for (; it != end; ++it) {
121                         docstring const & current_branch = it->getBranch();
122                         Branch const * branch = branchlist.find(current_branch);
123                         string const x11hexname =
124                                         lyx::X11hexname(branch->getColor());
125                         // display the new color
126                         docstring const str = current_branch + ' ' + from_ascii(x11hexname);
127                         dispatch(FuncRequest(LFUN_SET_COLOR, str));
128                 }
129
130                 // Open insets of selected branches, close deselected ones
131                 dispatch(FuncRequest(LFUN_ALL_INSETS_TOGGLE,
132                         "assign branch"));
133         }
134         // FIXME: If we used an LFUN, we would not need those two lines:
135         bufferview()->update();
136         lyxview().currentWorkArea()->redraw();
137 }
138
139
140 void ControlDocument::setLanguage() const
141 {
142         Language const * const newL = bp_->language;
143         if (buffer().params().language == newL)
144                 return;
145
146         string const lang_name = newL->lang();
147         dispatch(FuncRequest(LFUN_BUFFER_LANGUAGE, lang_name));
148 }
149
150
151 void ControlDocument::saveAsDefault() const
152 {
153         dispatch_bufferparams(*this, params(), LFUN_BUFFER_SAVE_AS_DEFAULT);
154 }
155
156
157 bool const ControlDocument::isFontAvailable(std::string const & font) const
158 {
159         if (font == "default" || font == "cmr"
160             || font == "cmss" || font == "cmtt")
161                 // these are standard
162                 return true;
163         else if (font == "lmodern" || font == "lmss" || font == "lmtt")
164                 return LaTeXFeatures::isAvailable("lmodern");
165         else if (font == "times" || font == "palatino"
166                  || font == "helvet" || font == "courier")
167                 return LaTeXFeatures::isAvailable("psnfss");
168         else if (font == "cmbr" || font == "cmtl")
169                 return LaTeXFeatures::isAvailable("cmbright");
170         else if (font == "utopia")
171                 return LaTeXFeatures::isAvailable("utopia")
172                         || LaTeXFeatures::isAvailable("fourier");
173         else if (font == "beraserif" || font == "berasans"
174                 || font == "beramono")
175                 return LaTeXFeatures::isAvailable("bera");
176         else
177                 return LaTeXFeatures::isAvailable(font);
178 }
179
180
181 bool const ControlDocument::providesOSF(std::string const & font) const
182 {
183         if (font == "cmr")
184                 return isFontAvailable("eco");
185         else if (font == "palatino")
186                 return isFontAvailable("mathpazo");
187         else
188                 return false;
189 }
190
191
192 bool const ControlDocument::providesSC(std::string const & font) const
193 {
194         if (font == "palatino")
195                 return isFontAvailable("mathpazo");
196         else if (font == "utopia")
197                 return isFontAvailable("fourier");
198         else
199                 return false;
200 }
201
202
203 bool const ControlDocument::providesScale(std::string const & font) const
204 {
205         return (font == "helvet" || font == "luximono"
206                 || font == "berasans"  || font == "beramono");
207 }
208
209
210 } // namespace frontend
211 } // namespace lyx