]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/ControlDocument.C
move BoostFormat and boost-inst
[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             && 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         if (ret) {
125                 string s;
126                 if (ret == 1) {
127                         s = _("One paragraph couldn't be converted");
128                 } else {
129 #if USE_BOOST_FORMAT
130                         boost::format fmt(_("%1$s paragraphs couldn't be converted"));
131                         fmt % ret;
132                         s = fmt.str();
133 #else
134                         s += tostr(ret);
135                         s += _(" paragraphs couldn't be converted");
136 #endif
137                 }
138                 Alert::alert(_("Conversion Errors!"),s,
139                              _("into chosen document class"));
140         }
141 }
142
143
144 bool ControlDocument::loadTextclass(lyx::textclass_type tc) const
145 {
146         bool const success = textclasslist[tc].load();
147         if (!success) {
148                 // problem changing class
149                 // -- warn user (to retain old style)
150                 Alert::alert(_("Conversion Errors!"),
151                              _("Errors loading new document class."),
152                              _("Reverting to original document class."));
153         }
154         return success;
155 }
156
157
158 void ControlDocument::saveAsDefault()
159 {
160 // Can somebody justify this ? I think it should be removed - jbl
161 #if 0
162         if (!Alert::askQuestion(_("Do you want to save the current settings"),
163                                 _("for the document layout as default?"),
164                                 _("(they will be valid for any new document)")))
165                 return;
166 #endif
167
168         lv_.buffer()->params.preamble = bp_->preamble;
169
170         string const fname = AddName(AddPath(user_lyxdir, "templates/"),
171                                      "defaults.lyx");
172         Buffer defaults(fname);
173         defaults.params = params();
174
175         // add an empty paragraph. Is this enough?
176         Paragraph * par = new Paragraph;
177         par->layout(params().getLyXTextClass().defaultLayout());
178         defaults.paragraphs.set(par);
179
180         defaults.writeFile(defaults.fileName());
181
182 }