]> git.lyx.org Git - features.git/blob - src/frontends/controllers/ControlDocument.C
kill Alert::alert()
[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
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             && oldL->RightToLeft() == newL->RightToLeft()
100             && !lv_.buffer()->isMultiLingual())
101                 lv_.buffer()->changeLanguage(oldL, newL);
102 }
103
104
105 void ControlDocument::classApply()
106 {
107         BufferParams & params = buffer()->params;
108         lyx::textclass_type const old_class = params.textclass;
109         lyx::textclass_type const new_class = bp_->textclass;
110
111         // exit if nothing changes or if unable to load the new class
112         if (new_class == old_class || !loadTextclass(new_class))
113                 return;
114
115         // successfully loaded
116         view().apply();
117         buffer()->params = *bp_;
118
119         lv_.message(_("Converting document to new document class..."));
120         int ret = CutAndPaste::SwitchLayoutsBetweenClasses(
121                 old_class, new_class,
122                 &*(lv_.buffer()->paragraphs.begin()),
123                 lv_.buffer()->params);
124
125         if (!ret)
126                 return;
127
128         string s;
129 #if USE_BOOST_FORMAT
130         if (ret == 1) {
131                 boost::format fmt(_("One paragraph could not be converted\n"
132                         "into the document class %2$s."));
133                 fmt % textclasslist[new_class].name();
134                 s = fmt.str();
135         } else {
136                 boost::format fmt(_("%1$s paragraphs could not be converted\n"
137                         "into the document class %2$s."));
138                 fmt % tostr(ret);
139                 fmt % textclasslist[new_class].name();
140                 s = fmt.str();
141         }
142 #else
143         if (ret == 1) {
144                 s += _("One paragraph could not be converted\n"
145                         "into the document class ");
146                 s += textclasslist[new_class].name() + ".";
147         } else {
148                 s += tostr(ret);
149                 s += _(" paragraphs could not be converted\n"
150                         "into the document class ");
151                 s += textclasslist[new_class].name() + ".";
152         }
153 #endif
154         Alert::warning(_("Class conversion errors"), s);
155 }
156
157
158 bool ControlDocument::loadTextclass(lyx::textclass_type tc) const
159 {
160         bool const success = textclasslist[tc].load();
161         if (success)
162                 return success;
163
164         string s;
165
166 #if USE_BOOST_FORMAT
167         boost::format fmt(_("The document could not be converted\n"
168                         "into the document class %1$s."));
169         fmt % textclasslist[tc].name();
170         s = fmt.str();
171 #else
172         s += _("The document could not be converted\n"
173                "into the document class ");
174         s += textclasslist[tc].name() + ".";
175 #endif
176         Alert::error(_("Could not change class"), s);
177
178         return success;
179 }
180
181
182 void ControlDocument::saveAsDefault()
183 {
184 // Can somebody justify this ? I think it should be removed - jbl
185 #if 0
186         if (!Alert::askQuestion(_("Do you want to save the current settings"),
187                                 _("for the document layout as default?"),
188                                 _("(they will be valid for any new document)")))
189                 return;
190 #endif
191
192         lv_.buffer()->params.preamble = bp_->preamble;
193
194         string const fname = AddName(AddPath(user_lyxdir, "templates/"),
195                                      "defaults.lyx");
196         Buffer defaults(fname);
197         defaults.params = params();
198
199         // add an empty paragraph. Is this enough?
200         Paragraph * par = new Paragraph;
201         par->layout(params().getLyXTextClass().defaultLayout());
202         defaults.paragraphs.set(par);
203
204         defaults.writeFile(defaults.fileName());
205
206 }