]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/ControlDocument.C
fix crash due to invalidated iterator
[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
109         // Generate the colours requested by each new branch.
110         BranchList & branchlist = params().branchlist();
111         if (!branchlist.empty()) {
112                 BranchList::const_iterator it = branchlist.begin();
113                 BranchList::const_iterator const end = branchlist.end();
114                 for (; it != end; ++it) {
115                         string const & current_branch = it->getBranch();
116                         Branch const * branch = branchlist.find(current_branch);
117                         string x11hexname = branch->getColor();
118                         // check that we have a valid color!
119                         if (x11hexname.empty() || x11hexname[0] != '#')
120                                 x11hexname = 
121                                         lcolor.getX11Name(LColor::background);
122                         // display the new color
123                         string const str = current_branch  + ' ' + x11hexname;
124                         kernel().dispatch(FuncRequest(LFUN_SET_COLOR, str));
125                 }
126         
127                 // Open insets of selected branches, close deselected ones
128                 kernel().dispatch(FuncRequest(LFUN_ALL_INSETS_TOGGLE, 
129                         "assign branch"));
130         }
131         // update the bufferview
132         kernel().bufferview()->update();
133 }
134
135
136 void ControlDocument::setLanguage() const
137 {
138         Language const * const newL = bp_->language;
139         if (kernel().buffer().params().language == newL)
140                 return;
141
142         string const lang_name = newL->lang();
143         kernel().dispatch(FuncRequest(LFUN_LANGUAGE_BUFFER, lang_name));
144 }
145
146
147 bool ControlDocument::loadTextclass(textclass_type tc) const
148 {
149         string const name = textclasslist[tc].name();
150         kernel().dispatch(FuncRequest(LFUN_TEXTCLASS_LOAD, name));
151
152         // Report back whether we were able to change the class.
153         bool const success = textclasslist[tc].loaded();
154         return success;
155 }
156
157
158 void ControlDocument::saveAsDefault() const
159 {
160         dispatch_bufferparams(kernel(), params(), LFUN_SAVE_AS_DEFAULT);
161 }
162
163 } // namespace frontend
164 } // namespace lyx