]> git.lyx.org Git - features.git/blob - src/frontends/controllers/ControlDocument.C
The 'bformat' introduction
[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 "language.h"
23 #include "lyx_main.h"
24 #include "lyxtextclass.h"
25 #include "lyxtextclasslist.h"
26 #include "CutAndPaste.h"
27
28 #include "frontends/LyXView.h"
29 #include "frontends/Alert.h"
30
31 #include "support/LAssert.h"
32 #include "support/lstrings.h"
33 #include "support/filetools.h"
34
35 using std::endl;
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         lyx::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
66         classApply();
67
68         view().apply();
69         setLanguage();
70         buffer()->params = *bp_;
71
72         lv_.view()->redoCurrentBuffer();
73
74         buffer()->markDirty();
75
76         lv_.message(_("Document settings applied"));
77 }
78
79
80 void ControlDocument::setParams()
81 {
82         if (!bp_.get())
83                 bp_.reset(new BufferParams());
84
85         /// Set the buffer parameters
86         *bp_ = buffer()->params;
87 }
88
89
90 void ControlDocument::setLanguage()
91 {
92         Language const * oldL = buffer()->params.language;
93         Language const * newL = bp_->language;
94
95         if (oldL != newL) {
96
97                 if (oldL->RightToLeft() == newL->RightToLeft()
98                     && !lv_.buffer()->isMultiLingual())
99                         lv_.buffer()->changeLanguage(oldL, newL);
100                 else
101                     lv_.buffer()->updateDocLang(newL);
102         }
103 }
104
105
106 void ControlDocument::classApply()
107 {
108         BufferParams & params = buffer()->params;
109         lyx::textclass_type const old_class = params.textclass;
110         lyx::textclass_type const new_class = bp_->textclass;
111
112         // exit if nothing changes or if unable to load the new class
113         if (new_class == old_class || !loadTextclass(new_class))
114                 return;
115
116         // successfully loaded
117         view().apply();
118         buffer()->params = *bp_;
119
120         lv_.message(_("Converting document to new document class..."));
121         int ret = CutAndPaste::SwitchLayoutsBetweenClasses(
122                 old_class, new_class,
123                 lv_.buffer()->paragraphs);
124
125         if (!ret)
126                 return;
127
128         string s;
129         if (ret == 1) {
130                 s = bformat(_("One paragraph could not be converted\n"
131                         "into the document class %1$s."),
132                         textclasslist[new_class].name());
133         } else {
134                 s = bformat(_("%1$s paragraphs could not be converted\n"
135                         "into the document class %2$s."),
136                         textclasslist[new_class].name());
137         }
138         Alert::warning(_("Class conversion errors"), s);
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 = new Paragraph;
176         par->layout(params().getLyXTextClass().defaultLayout());
177         defaults.paragraphs.push_back(par);
178
179         defaults.writeFile(defaults.fileName());
180
181 }