]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/ControlDocument.C
lang apply fix
[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 #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 "errorlist.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 using std::endl;
37
38
39 ControlDocument::ControlDocument(LyXView & lv, Dialogs & d)
40         : ControlDialogBD(lv, d), bp_(0)
41 {}
42
43
44 ControlDocument::~ControlDocument()
45 {}
46
47
48 BufferParams & ControlDocument::params()
49 {
50         lyx::Assert(bp_.get());
51         return *bp_;
52 }
53
54
55 LyXTextClass ControlDocument::textClass()
56 {
57         return textclasslist[bp_->textclass];
58 }
59
60
61 void ControlDocument::apply()
62 {
63         if (!bufferIsAvailable())
64                 return;
65
66         view().apply();
67
68         // this must come first so that a language change
69         // is correctly noticed
70         setLanguage();
71
72         classApply();
73
74         buffer()->params = *bp_;
75
76         lv_.view()->redoCurrentBuffer();
77
78         buffer()->markDirty();
79
80         lv_.message(_("Document settings applied"));
81 }
82
83
84 void ControlDocument::setParams()
85 {
86         if (!bp_.get())
87                 bp_.reset(new BufferParams());
88
89         /// Set the buffer parameters
90         *bp_ = buffer()->params;
91 }
92
93
94 void ControlDocument::setLanguage()
95 {
96         Language const * oldL = buffer()->params.language;
97         Language const * newL = bp_->language;
98
99         if (oldL != newL) {
100
101                 if (oldL->RightToLeft() == newL->RightToLeft()
102                     && !lv_.buffer()->isMultiLingual())
103                         lv_.buffer()->changeLanguage(oldL, newL);
104                 else
105                     lv_.buffer()->updateDocLang(newL);
106         }
107 }
108
109
110 void ControlDocument::classApply()
111 {
112         BufferParams & params = buffer()->params;
113         lyx::textclass_type const old_class = params.textclass;
114         lyx::textclass_type const new_class = bp_->textclass;
115
116         // exit if nothing changes or if unable to load the new class
117         if (new_class == old_class || !loadTextclass(new_class))
118                 return;
119
120         // successfully loaded
121         buffer()->params = *bp_;
122
123         lv_.message(_("Converting document to new document class..."));
124
125         ErrorList el;
126         CutAndPaste::SwitchLayoutsBetweenClasses(old_class, new_class,
127                                                  lv_.buffer()->paragraphs,
128                                                  el);
129
130         bufferview()->setErrorList(el);
131         bufferview()->showErrorList(_("Class switch"));
132 }
133
134
135 bool ControlDocument::loadTextclass(lyx::textclass_type tc) const
136 {
137         bool const success = textclasslist[tc].load();
138         if (success)
139                 return success;
140
141         string s = bformat(_("The document could not be converted\n"
142                         "into the document class %1$s."),
143                         textclasslist[tc].name());
144         Alert::error(_("Could not change class"), s);
145
146         return success;
147 }
148
149
150 void ControlDocument::saveAsDefault()
151 {
152 // Can somebody justify this ? I think it should be removed - jbl
153 #if 0
154         if (!Alert::askQuestion(_("Do you want to save the current settings"),
155                                 _("for the document layout as default?"),
156                                 _("(they will be valid for any new document)")))
157                 return;
158 #endif
159
160         lv_.buffer()->params.preamble = bp_->preamble;
161
162         string const fname = AddName(AddPath(user_lyxdir, "templates/"),
163                                      "defaults.lyx");
164         Buffer defaults(fname);
165         defaults.params = params();
166
167         // add an empty paragraph. Is this enough?
168         Paragraph par;
169         par.layout(params().getLyXTextClass().defaultLayout());
170         defaults.paragraphs.push_back(par);
171
172         defaults.writeFile(defaults.fileName());
173
174 }