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