]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/ControlDocument.C
obvious stuff
[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         bp.writeFile(ss);
75         ss << "\\end_header\n";
76         kernel.dispatch(FuncRequest(lfun, ss.str()));
77 }
78
79 } // namespace anon
80
81
82 void ControlDocument::dispatchParams()
83 {
84         // This must come first so that a language change is correctly noticed
85         setLanguage();
86
87         // Set the document class.
88         textclass_type const old_class =
89                 kernel().buffer().params().textclass;
90         textclass_type const new_class = bp_->textclass;
91
92         if (new_class != old_class) {
93                 string const name = textclasslist[new_class].name();
94                 kernel().dispatch(FuncRequest(LFUN_TEXTCLASS_APPLY, name));
95         }
96
97         // Apply the BufferParams.
98         dispatch_bufferparams(kernel(), params(), LFUN_BUFFERPARAMS_APPLY);
99
100         // Generate the colours requested by each new branch.
101         BranchList & branchlist = params().branchlist();
102         if (branchlist.empty())
103                 return;
104
105         BranchList::const_iterator it = branchlist.begin();
106         BranchList::const_iterator const end = branchlist.end();
107         for (; it != end; ++it) {
108                 string const & current_branch = it->getBranch();
109                 Branch const * branch = branchlist.find(current_branch);
110                 string x11hexname = branch->getColor();
111                 // check that we have a valid color!
112                 if (x11hexname.empty() || x11hexname[0] != '#')
113                         x11hexname = lcolor.getX11Name(LColor::background);
114                 // display the new color
115                 string const str = current_branch  + ' ' + x11hexname;
116                 kernel().dispatch(FuncRequest(LFUN_SET_COLOR, str));
117         }
118
119         // Open insets of selected branches, close deselected ones
120         kernel().dispatch(FuncRequest(LFUN_ALL_INSETS_TOGGLE, "assign branch"));
121 }
122
123
124 void ControlDocument::setLanguage() const
125 {
126         Language const * const newL = bp_->language;
127         if (kernel().buffer().params().language == newL)
128                 return;
129
130         string const lang_name = newL->lang();
131         kernel().dispatch(FuncRequest(LFUN_LANGUAGE_BUFFER, lang_name));
132 }
133
134
135 bool ControlDocument::loadTextclass(textclass_type tc) const
136 {
137         string const name = textclasslist[tc].name();
138         kernel().dispatch(FuncRequest(LFUN_TEXTCLASS_LOAD, name));
139
140         // Report back whether we were able to change the class.
141         bool const success = textclasslist[tc].loaded();
142         return success;
143 }
144
145
146 void ControlDocument::saveAsDefault() const
147 {
148         dispatch_bufferparams(kernel(), params(), LFUN_SAVE_AS_DEFAULT);
149 }
150
151 } // namespace frontend
152 } // namespace lyx