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