]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/ControlDocument.C
Some cleanup, and prepare for new feature.
[lyx.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
15 #include "BufferView.h"
16 #include "ControlDocument.h"
17 #include "ViewBase.h"
18
19 #include "gettext.h"
20 #include "lyxfind.h"
21
22 #include "buffer.h"
23 #include "language.h"
24 #include "lyx_main.h"
25 #include "lyxtextclass.h"
26 #include "lyxtextclasslist.h"
27 #include "CutAndPaste.h"
28
29 #include "frontends/LyXView.h"
30 #include "frontends/Alert.h"
31
32 #include "support/LAssert.h"
33 #include "support/lstrings.h"
34 #include "support/filetools.h"
35
36 #include "support/BoostFormat.h"
37
38 using std::endl;
39
40
41 ControlDocument::ControlDocument(LyXView & lv, Dialogs & d)
42         : ControlDialogBD(lv, d), bp_(0)
43 {
44 }
45
46 ControlDocument::~ControlDocument()
47 {}
48
49
50 BufferParams & ControlDocument::params()
51 {
52         lyx::Assert(bp_.get());
53         return *bp_;
54 }
55
56
57 LyXTextClass ControlDocument::textClass()
58 {
59         return textclasslist[bp_->textclass];
60 }
61
62
63 void ControlDocument::apply()
64 {
65         if (!bufferIsAvailable())
66                 return;
67
68
69         classApply();
70
71         view().apply();
72         setLanguage();
73         buffer()->params = *bp_;
74
75         lv_.view()->redoCurrentBuffer();
76
77         buffer()->markDirty();
78
79         lv_.message(_("Document settings applied"));
80 }
81
82
83 void ControlDocument::setParams()
84 {
85         if (!bp_.get())
86                 bp_.reset(new BufferParams());
87
88         /// Set the buffer parameters
89         *bp_ = buffer()->params;
90 }
91
92
93 void ControlDocument::setLanguage()
94 {
95         Language const * oldL = buffer()->params.language;
96         Language const * newL = bp_->language;
97
98         if (oldL != newL) {
99
100                 if (oldL->RightToLeft() == newL->RightToLeft()
101                     && !lv_.buffer()->isMultiLingual())
102                         lv_.buffer()->changeLanguage(oldL, newL);
103                 else
104                     lv_.buffer()->updateDocLang(newL);
105         }
106 }
107
108
109 void ControlDocument::classApply()
110 {
111         BufferParams & params = buffer()->params;
112         lyx::textclass_type const old_class = params.textclass;
113         lyx::textclass_type const new_class = bp_->textclass;
114
115         // exit if nothing changes or if unable to load the new class
116         if (new_class == old_class || !loadTextclass(new_class))
117                 return;
118
119         // successfully loaded
120         view().apply();
121         buffer()->params = *bp_;
122
123         lv_.message(_("Converting document to new document class..."));
124         int ret = CutAndPaste::SwitchLayoutsBetweenClasses(
125                 old_class, new_class,
126                 lv_.buffer()->paragraphs);
127
128         if (!ret)
129                 return;
130
131         string s;
132 #if USE_BOOST_FORMAT
133         if (ret == 1) {
134                 boost::format fmt(_("One paragraph could not be converted\n"
135                         "into the document class %2$s."));
136                 fmt % textclasslist[new_class].name();
137                 s = fmt.str();
138         } else {
139                 boost::format fmt(_("%1$s paragraphs could not be converted\n"
140                         "into the document class %2$s."));
141                 fmt % tostr(ret);
142                 fmt % textclasslist[new_class].name();
143                 s = fmt.str();
144         }
145 #else
146         if (ret == 1) {
147                 s += _("One paragraph could not be converted\n"
148                         "into the document class ");
149                 s += textclasslist[new_class].name() + ".";
150         } else {
151                 s += tostr(ret);
152                 s += _(" paragraphs could not be converted\n"
153                         "into the document class ");
154                 s += textclasslist[new_class].name() + ".";
155         }
156 #endif
157         Alert::warning(_("Class conversion errors"), s);
158 }
159
160
161 bool ControlDocument::loadTextclass(lyx::textclass_type tc) const
162 {
163         bool const success = textclasslist[tc].load();
164         if (success)
165                 return success;
166
167         string s;
168
169 #if USE_BOOST_FORMAT
170         boost::format fmt(_("The document could not be converted\n"
171                         "into the document class %1$s."));
172         fmt % textclasslist[tc].name();
173         s = fmt.str();
174 #else
175         s += _("The document could not be converted\n"
176                "into the document class ");
177         s += textclasslist[tc].name() + ".";
178 #endif
179         Alert::error(_("Could not change class"), s);
180
181         return success;
182 }
183
184
185 void ControlDocument::saveAsDefault()
186 {
187 // Can somebody justify this ? I think it should be removed - jbl
188 #if 0
189         if (!Alert::askQuestion(_("Do you want to save the current settings"),
190                                 _("for the document layout as default?"),
191                                 _("(they will be valid for any new document)")))
192                 return;
193 #endif
194
195         lv_.buffer()->params.preamble = bp_->preamble;
196
197         string const fname = AddName(AddPath(user_lyxdir, "templates/"),
198                                      "defaults.lyx");
199         Buffer defaults(fname);
200         defaults.params = params();
201
202         // add an empty paragraph. Is this enough?
203         Paragraph * par = new Paragraph;
204         par->layout(params().getLyXTextClass().defaultLayout());
205         defaults.paragraphs.set(par);
206
207         defaults.writeFile(defaults.fileName());
208
209 }