]> git.lyx.org Git - features.git/blob - src/frontends/controllers/ControlDocument.C
the ascent/descent/width -> dimensions() change
[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         classApply();
66
67         view().apply();
68         setLanguage();
69         buffer()->params = *bp_;
70
71         lv_.view()->redoCurrentBuffer();
72
73         buffer()->markDirty();
74
75         lv_.message(_("Document settings applied"));
76 }
77
78
79 void ControlDocument::setParams()
80 {
81         if (!bp_.get())
82                 bp_.reset(new BufferParams());
83
84         /// Set the buffer parameters
85         *bp_ = buffer()->params;
86 }
87
88
89 void ControlDocument::setLanguage()
90 {
91         Language const * oldL = buffer()->params.language;
92         Language const * newL = bp_->language;
93
94         if (oldL != newL) {
95
96                 if (oldL->RightToLeft() == newL->RightToLeft()
97                     && !lv_.buffer()->isMultiLingual())
98                         lv_.buffer()->changeLanguage(oldL, newL);
99                 else
100                     lv_.buffer()->updateDocLang(newL);
101         }
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);
123
124         if (!ret)
125                 return;
126
127         string s;
128         if (ret == 1) {
129                 s = bformat(_("One paragraph could not be converted\n"
130                         "into the document class %1$s."),
131                         textclasslist[new_class].name());
132         } else {
133                 s = bformat(_("%1$s paragraphs could not be converted\n"
134                         "into the document class %2$s."),
135                         textclasslist[new_class].name());
136         }
137         Alert::warning(_("Class conversion errors"), s);
138 }
139
140
141 bool ControlDocument::loadTextclass(lyx::textclass_type tc) const
142 {
143         bool const success = textclasslist[tc].load();
144         if (success)
145                 return success;
146
147         string s = bformat(_("The document could not be converted\n"
148                         "into the document class %1$s."),
149                         textclasslist[tc].name());
150         Alert::error(_("Could not change class"), s);
151
152         return success;
153 }
154
155
156 void ControlDocument::saveAsDefault()
157 {
158 // Can somebody justify this ? I think it should be removed - jbl
159 #if 0
160         if (!Alert::askQuestion(_("Do you want to save the current settings"),
161                                 _("for the document layout as default?"),
162                                 _("(they will be valid for any new document)")))
163                 return;
164 #endif
165
166         lv_.buffer()->params.preamble = bp_->preamble;
167
168         string const fname = AddName(AddPath(user_lyxdir, "templates/"),
169                                      "defaults.lyx");
170         Buffer defaults(fname);
171         defaults.params = params();
172
173         // add an empty paragraph. Is this enough?
174         Paragraph * par = new Paragraph;
175         par->layout(params().getLyXTextClass().defaultLayout());
176         defaults.paragraphs.push_back(par);
177
178         defaults.writeFile(defaults.fileName());
179
180 }