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