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