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