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