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