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