]> git.lyx.org Git - features.git/blob - src/frontends/controllers/ControlDocument.C
Overhaul the branches code.
[features.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 "ViewBase.h"
15
16 #include "BranchList.h"
17 #include "buffer.h"
18 #include "buffer_funcs.h"
19 #include "bufferparams.h"
20 #include "BufferView.h"
21 #include "CutAndPaste.h"
22 #include "errorlist.h"
23 #include "gettext.h"
24 #include "iterators.h"
25 #include "language.h"
26 #include "LColor.h"
27 #include "lyxtextclasslist.h"
28 #include "paragraph.h"
29 #include "funcrequest.h"
30 #include "lfuns.h"
31
32 #include "frontends/Alert.h"
33 #include "frontends/LyXView.h"
34
35 #include "support/filetools.h"
36 #include "support/path_defines.h"
37
38 using lyx::support::AddName;
39 using lyx::support::AddPath;
40 using lyx::support::bformat;
41 using lyx::support::user_lyxdir;
42
43 using std::string;
44
45
46 ControlDocument::ControlDocument(LyXView & lv, Dialogs & d)
47         : ControlDialogBD(lv, d), bp_(0)
48 {}
49
50
51 ControlDocument::~ControlDocument()
52 {}
53
54
55 BufferParams & ControlDocument::params()
56 {
57         BOOST_ASSERT(bp_.get());
58         return *bp_;
59 }
60
61
62 LyXTextClass ControlDocument::textClass()
63 {
64         return textclasslist[bp_->textclass];
65 }
66
67
68 void ControlDocument::apply()
69 {
70         if (!bufferIsAvailable())
71                 return;
72
73         view().apply();
74
75         // this must come first so that a language change
76         // is correctly noticed
77         setLanguage();
78
79         classApply();
80
81         buffer()->params() = *bp_;
82
83         lv_.view()->redoCurrentBuffer();
84
85         buffer()->markDirty();
86
87         lv_.message(_("Document settings applied"));
88
89         // branches
90         BranchList & branchlist = params().branchlist();
91         BranchList::const_iterator it = branchlist.begin();
92         BranchList::const_iterator const end = branchlist.end();
93         for (; it != end; ++it) {
94                 string const & current_branch = it->getBranch();
95                 Branch const * branch = branchlist.find(current_branch);
96                 string x11hexname = branch->getColor();
97                 // check that we have a valid color!
98                 if (x11hexname.empty() || x11hexname[0] != '#')
99                         x11hexname = lcolor.getX11Name(LColor::background);
100                 // display the new color
101                 string const str = current_branch  + ' ' + x11hexname;
102                 lv_.dispatch(FuncRequest(LFUN_SET_COLOR, str));
103         }
104
105         // Open insets of selected branches, close deselected ones
106         // Currently only top-level insets in buffer handled (bug).
107         ParIterator pit = buffer()->par_iterator_begin();
108         ParIterator pend = buffer()->par_iterator_end();
109         for (; pit != pend; ++pit) {
110                 pit->insetlist.insetsOpenCloseBranch(*buffer());
111         }
112 }
113
114
115 void ControlDocument::setParams()
116 {
117         if (!bp_.get())
118                 bp_.reset(new BufferParams);
119
120         /// Set the buffer parameters
121         *bp_ = buffer()->params();
122 }
123
124
125 void ControlDocument::setLanguage()
126 {
127         Language const * oldL = buffer()->params().language;
128         Language const * newL = bp_->language;
129
130         if (oldL != newL) {
131
132                 if (oldL->RightToLeft() == newL->RightToLeft()
133                     && !lv_.buffer()->isMultiLingual())
134                         lv_.buffer()->changeLanguage(oldL, newL);
135                 else
136                     lv_.buffer()->updateDocLang(newL);
137         }
138 }
139
140
141 void ControlDocument::classApply()
142 {
143         BufferParams & params = buffer()->params();
144         lyx::textclass_type const old_class = params.textclass;
145         lyx::textclass_type const new_class = bp_->textclass;
146
147         // exit if nothing changes or if unable to load the new class
148         if (new_class == old_class || !loadTextclass(new_class))
149                 return;
150
151         // successfully loaded
152         buffer()->params() = *bp_;
153
154         lv_.message(_("Converting document to new document class..."));
155
156         ErrorList el;
157         CutAndPaste::SwitchLayoutsBetweenClasses(old_class, new_class,
158                                                  lv_.buffer()->paragraphs(),
159                                                  el);
160         bufferErrors(*buffer(), el);
161         bufferview()->showErrorList(_("Class switch"));
162 }
163
164
165 bool ControlDocument::loadTextclass(lyx::textclass_type tc) const
166 {
167         bool const success = textclasslist[tc].load();
168         if (success)
169                 return success;
170
171         string s = bformat(_("The document could not be converted\n"
172                         "into the document class %1$s."),
173                         textclasslist[tc].name());
174         Alert::error(_("Could not change class"), s);
175
176         return success;
177 }
178
179
180 void ControlDocument::saveAsDefault()
181 {
182 // Can somebody justify this ? I think it should be removed - jbl
183 #if 0
184         if (!Alert::askQuestion(_("Do you want to save the current settings"),
185                                 _("for the document layout as default?"),
186                                 _("(they will be valid for any new document)")))
187                 return;
188 #endif
189
190         lv_.buffer()->params().preamble = bp_->preamble;
191
192         string const fname = AddName(AddPath(user_lyxdir(), "templates/"),
193                                      "defaults.lyx");
194         Buffer defaults(fname);
195         defaults.params() = params();
196
197         // add an empty paragraph. Is this enough?
198         Paragraph par;
199         par.layout(params().getLyXTextClass().defaultLayout());
200         defaults.paragraphs().push_back(par);
201
202         defaults.writeFile(defaults.fileName());
203
204 }