]> git.lyx.org Git - features.git/blob - src/frontends/controllers/ControlDocument.C
Use a control-view split for the xforms document dialog.
[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 #ifdef __GNUG__
15 #pragma implementation
16 #endif
17
18 #include "BufferView.h"
19 #include "ControlDocument.h"
20 #include "ViewBase.h"
21
22 #include "gettext.h"
23 #include "lyxfind.h"
24
25 #include "buffer.h"
26 #include "language.h"
27 #include "lyx_main.h"
28 #include "lyxtextclass.h"
29 #include "lyxtextclasslist.h"
30 #include "CutAndPaste.h"
31
32 #include "frontends/LyXView.h"
33 #include "frontends/Alert.h"
34
35 #include "support/lstrings.h"
36 #include "support/filetools.h"
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         setLanguage();
67
68         classApply();
69
70         view().apply();
71         buffer()->params = *bp_;
72         
73         lv_.view()->redoCurrentBuffer();
74
75         buffer()->markDirty();
76
77         lv_.message(_("Document settings applied"));
78 }
79
80
81 void ControlDocument::setParams()
82 {
83         if (!bp_.get())
84                 bp_.reset(new BufferParams());
85
86         /// Set the buffer parameters
87         *bp_ = buffer()->params;
88 }
89
90
91 void ControlDocument::setLanguage()
92 {
93         Language const * oldL = buffer()->params.language;
94         Language const * newL = bp_->language;
95
96         if (oldL != newL
97             && oldL->RightToLeft() == newL->RightToLeft()
98             && !lv_.buffer()->isMultiLingual())
99                 lv_.buffer()->changeLanguage(oldL, newL);
100 }
101
102
103 void ControlDocument::classApply()
104 {
105         BufferParams & params = buffer()->params;
106         lyx::textclass_type const old_class = params.textclass;
107         lyx::textclass_type const new_class = bp_->textclass;
108
109         // exit if nothing changes or if unable to load the new class
110         if (new_class == old_class || !loadTextclass(new_class))
111                 return;
112
113         // successfully loaded
114         view().apply();
115         buffer()->params = *bp_;
116         lv_.message(_("Converting document to new document class..."));
117         int ret = CutAndPaste::SwitchLayoutsBetweenClasses(
118                 old_class, new_class,
119                 &*(lv_.buffer()->paragraphs.begin()),
120                 lv_.buffer()->params);
121         if (ret) {
122                 string s;
123                 if (ret == 1) {
124                         s = _("One paragraph couldn't be converted");
125                 } else {
126                         s += tostr(ret);
127                         s += _(" paragraphs couldn't be converted");
128                 }
129                 Alert::alert(_("Conversion Errors!"),s,
130                              _("into chosen document class"));
131         }
132 }
133
134
135 bool ControlDocument::loadTextclass(lyx::textclass_type tc) const
136 {
137         bool const success = textclasslist[tc].load();
138         if (!success) {
139                 // problem changing class
140                 // -- warn user (to retain old style)
141                 Alert::alert(_("Conversion Errors!"),
142                              _("Errors loading new document class."),
143                              _("Reverting to original document class."));
144         }
145         return success;
146 }
147
148
149 void ControlDocument::saveAsDefault()
150 {
151         if (!Alert::askQuestion(_("Do you want to save the current settings"),
152                                 _("for the document layout as default?"),
153                                 _("(they will be valid for any new document)")))
154                 return;
155                 
156         lv_.buffer()->params.preamble = bp_->preamble;
157
158         string const fname = AddName(AddPath(user_lyxdir, "templates/"),
159                                      "defaults.lyx");
160         Buffer defaults(fname);
161         defaults.params = params();
162
163         // add an empty paragraph. Is this enough?
164         Paragraph * par = new Paragraph;
165         par->layout(params().getLyXTextClass().defaultLayout());
166         defaults.paragraphs.set(par);
167
168         defaults.writeFile(defaults.fileName());
169
170 }