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