]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/ControlDocument.C
Fix bug 1843
[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 "funcrequest.h"
20 #include "language.h"
21 #include "LColor.h"
22 #include "lyxtextclasslist.h"
23
24 #include <sstream>
25
26 using std::ostringstream;
27 using std::string;
28
29 namespace lyx {
30 namespace frontend {
31
32 ControlDocument::ControlDocument(Dialog & parent)
33         : Dialog::Controller(parent)
34 {}
35
36
37 ControlDocument::~ControlDocument()
38 {}
39
40
41 bool ControlDocument::initialiseParams(std::string const &)
42 {
43         bp_.reset(new BufferParams);
44         *bp_ = kernel().buffer().params();
45         return true;
46 }
47
48
49 void ControlDocument::clearParams()
50 {
51         bp_.reset();
52 }
53
54
55 BufferParams & ControlDocument::params() const
56 {
57         BOOST_ASSERT(bp_.get());
58         return *bp_;
59 }
60
61
62 LyXTextClass const & ControlDocument::textClass() const
63 {
64         return textclasslist[bp_->textclass];
65 }
66
67
68 namespace {
69
70 void dispatch_bufferparams(Kernel const & kernel, BufferParams const & bp,
71                            kb_action lfun)
72 {
73         ostringstream ss;
74         ss << "\\begin_header\n";
75         bp.writeFile(ss);
76         ss << "\\end_header\n";
77         kernel.dispatch(FuncRequest(lfun, ss.str()));
78 }
79
80 } // namespace anon
81
82
83 void ControlDocument::dispatchParams()
84 {
85         // This must come first so that a language change is correctly noticed
86         setLanguage();
87
88         // Set the document class.
89         textclass_type const old_class =
90                 kernel().buffer().params().textclass;
91         textclass_type const new_class = bp_->textclass;
92
93         if (new_class != old_class) {
94                 string const name = textclasslist[new_class].name();
95                 kernel().dispatch(FuncRequest(LFUN_TEXTCLASS_APPLY, name));
96         }
97
98         // Apply the BufferParams.
99         dispatch_bufferparams(kernel(), params(), LFUN_BUFFERPARAMS_APPLY);
100
101         // Generate the colours requested by each new branch.
102         BranchList & branchlist = params().branchlist();
103         if (branchlist.empty())
104                 return;
105
106         BranchList::const_iterator it = branchlist.begin();
107         BranchList::const_iterator const end = branchlist.end();
108         for (; it != end; ++it) {
109                 string const & current_branch = it->getBranch();
110                 Branch const * branch = branchlist.find(current_branch);
111                 string x11hexname = branch->getColor();
112                 // check that we have a valid color!
113                 if (x11hexname.empty() || x11hexname[0] != '#')
114                         x11hexname = lcolor.getX11Name(LColor::background);
115                 // display the new color
116                 string const str = current_branch  + ' ' + x11hexname;
117                 kernel().dispatch(FuncRequest(LFUN_SET_COLOR, str));
118         }
119
120         // Open insets of selected branches, close deselected ones
121         kernel().dispatch(FuncRequest(LFUN_ALL_INSETS_TOGGLE, "assign branch"));
122 }
123
124
125 void ControlDocument::setLanguage() const
126 {
127         Language const * const newL = bp_->language;
128         if (kernel().buffer().params().language == newL)
129                 return;
130
131         string const lang_name = newL->lang();
132         kernel().dispatch(FuncRequest(LFUN_LANGUAGE_BUFFER, lang_name));
133 }
134
135
136 bool ControlDocument::loadTextclass(textclass_type tc) const
137 {
138         string const name = textclasslist[tc].name();
139         kernel().dispatch(FuncRequest(LFUN_TEXTCLASS_LOAD, name));
140
141         // Report back whether we were able to change the class.
142         bool const success = textclasslist[tc].loaded();
143         return success;
144 }
145
146
147 void ControlDocument::saveAsDefault() const
148 {
149         dispatch_bufferparams(kernel(), params(), LFUN_SAVE_AS_DEFAULT);
150 }
151
152 } // namespace frontend
153 } // namespace lyx