]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/ControlDocument.C
make the message stuff work better
[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.begin()),
127                 lv_.buffer()->params);
128
129         if (!ret)
130                 return;
131
132         string s;
133 #if USE_BOOST_FORMAT
134         if (ret == 1) {
135                 boost::format fmt(_("One paragraph could not be converted\n"
136                         "into the document class %2$s."));
137                 fmt % textclasslist[new_class].name();
138                 s = fmt.str();
139         } else {
140                 boost::format fmt(_("%1$s paragraphs could not be converted\n"
141                         "into the document class %2$s."));
142                 fmt % tostr(ret);
143                 fmt % textclasslist[new_class].name();
144                 s = fmt.str();
145         }
146 #else
147         if (ret == 1) {
148                 s += _("One paragraph could not be converted\n"
149                         "into the document class ");
150                 s += textclasslist[new_class].name() + ".";
151         } else {
152                 s += tostr(ret);
153                 s += _(" paragraphs could not be converted\n"
154                         "into the document class ");
155                 s += textclasslist[new_class].name() + ".";
156         }
157 #endif
158         Alert::warning(_("Class conversion errors"), s);
159 }
160
161
162 bool ControlDocument::loadTextclass(lyx::textclass_type tc) const
163 {
164         bool const success = textclasslist[tc].load();
165         if (success)
166                 return success;
167
168         string s;
169
170 #if USE_BOOST_FORMAT
171         boost::format fmt(_("The document could not be converted\n"
172                         "into the document class %1$s."));
173         fmt % textclasslist[tc].name();
174         s = fmt.str();
175 #else
176         s += _("The document could not be converted\n"
177                "into the document class ");
178         s += textclasslist[tc].name() + ".";
179 #endif
180         Alert::error(_("Could not change class"), s);
181
182         return success;
183 }
184
185
186 void ControlDocument::saveAsDefault()
187 {
188 // Can somebody justify this ? I think it should be removed - jbl
189 #if 0
190         if (!Alert::askQuestion(_("Do you want to save the current settings"),
191                                 _("for the document layout as default?"),
192                                 _("(they will be valid for any new document)")))
193                 return;
194 #endif
195
196         lv_.buffer()->params.preamble = bp_->preamble;
197
198         string const fname = AddName(AddPath(user_lyxdir, "templates/"),
199                                      "defaults.lyx");
200         Buffer defaults(fname);
201         defaults.params = params();
202
203         // add an empty paragraph. Is this enough?
204         Paragraph * par = new Paragraph;
205         par->layout(params().getLyXTextClass().defaultLayout());
206         defaults.paragraphs.set(par);
207
208         defaults.writeFile(defaults.fileName());
209
210 }