]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/ControlDocument.C
A slightly buggy lfun all-insets-toggle.
[lyx.git] / src / frontends / controllers / ControlDocument.C
1 /**
2  * \file ControlDocument.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Edwin Leuven
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "ControlDocument.h"
14 #include "ViewBase.h"
15
16 #include "BranchList.h"
17 #include "buffer.h"
18 #include "buffer_funcs.h"
19 #include "bufferparams.h"
20 #include "BufferView.h"
21 #include "CutAndPaste.h"
22 #include "errorlist.h"
23 #include "funcrequest.h"
24 #include "gettext.h"
25 #include "language.h"
26 #include "LColor.h"
27 #include "lyxtextclasslist.h"
28 #include "lfuns.h"
29 #include "paragraph.h"
30 #include "ParagraphList_fwd.h"
31
32 #include "frontends/Alert.h"
33 #include "frontends/LyXView.h"
34
35 #include "support/filetools.h"
36 #include "support/path_defines.h"
37
38 using lyx::support::AddName;
39 using lyx::support::AddPath;
40 using lyx::support::bformat;
41 using lyx::support::user_lyxdir;
42
43 using std::string;
44
45
46 ControlDocument::ControlDocument(LyXView & lv, Dialogs & d)
47         : ControlDialogBD(lv, d), bp_(0)
48 {}
49
50
51 ControlDocument::~ControlDocument()
52 {}
53
54
55 BufferParams & ControlDocument::params()
56 {
57         BOOST_ASSERT(bp_.get());
58         return *bp_;
59 }
60
61
62 LyXTextClass ControlDocument::textClass()
63 {
64         return textclasslist[bp_->textclass];
65 }
66
67
68 void ControlDocument::apply()
69 {
70         if (!bufferIsAvailable())
71                 return;
72
73         view().apply();
74
75         // this must come first so that a language change
76         // is correctly noticed
77         setLanguage();
78
79         classApply();
80
81         buffer()->params() = *bp_;
82
83         lv_.view()->redoCurrentBuffer();
84
85         buffer()->markDirty();
86
87         lv_.message(_("Document settings applied"));
88
89         // branches
90         BranchList & branchlist = params().branchlist();
91         BranchList::const_iterator it = branchlist.begin();
92         BranchList::const_iterator const end = branchlist.end();
93         for (; it != end; ++it) {
94                 string const & current_branch = it->getBranch();
95                 Branch const * branch = branchlist.find(current_branch);
96                 string x11hexname = branch->getColor();
97                 // check that we have a valid color!
98                 if (x11hexname.empty() || x11hexname[0] != '#')
99                         x11hexname = lcolor.getX11Name(LColor::background);
100                 // display the new color
101                 string const str = current_branch  + ' ' + x11hexname;
102                 lv_.dispatch(FuncRequest(LFUN_SET_COLOR, str));
103         }
104
105         // Open insets of selected branches, close deselected ones
106         lv_.dispatch(FuncRequest(LFUN_ALL_INSETS_TOGGLE, "toggle branch"));
107 }
108
109
110 void ControlDocument::setParams()
111 {
112         if (!bp_.get())
113                 bp_.reset(new BufferParams);
114
115         /// Set the buffer parameters
116         *bp_ = buffer()->params();
117 }
118
119
120 void ControlDocument::setLanguage()
121 {
122         Language const * oldL = buffer()->params().language;
123         Language const * newL = bp_->language;
124
125         if (oldL != newL) {
126
127                 if (oldL->RightToLeft() == newL->RightToLeft()
128                     && !lv_.buffer()->isMultiLingual())
129                         lv_.buffer()->changeLanguage(oldL, newL);
130                 else
131                     lv_.buffer()->updateDocLang(newL);
132         }
133 }
134
135
136 void ControlDocument::classApply()
137 {
138         BufferParams & params = buffer()->params();
139         lyx::textclass_type const old_class = params.textclass;
140         lyx::textclass_type const new_class = bp_->textclass;
141
142         // exit if nothing changes or if unable to load the new class
143         if (new_class == old_class || !loadTextclass(new_class))
144                 return;
145
146         // successfully loaded
147         buffer()->params() = *bp_;
148
149         lv_.message(_("Converting document to new document class..."));
150
151         ErrorList el;
152         lyx::cap::SwitchLayoutsBetweenClasses(old_class, new_class,
153                                                  lv_.buffer()->paragraphs(),
154                                                  el);
155         bufferErrors(*buffer(), el);
156         bufferview()->showErrorList(_("Class switch"));
157 }
158
159
160 bool ControlDocument::loadTextclass(lyx::textclass_type tc) const
161 {
162         bool const success = textclasslist[tc].load();
163         if (success)
164                 return success;
165
166         string s = bformat(_("The document could not be converted\n"
167                         "into the document class %1$s."),
168                         textclasslist[tc].name());
169         Alert::error(_("Could not change class"), s);
170
171         return success;
172 }
173
174
175 void ControlDocument::saveAsDefault()
176 {
177 // Can somebody justify this ? I think it should be removed - jbl
178 #if 0
179         if (!Alert::askQuestion(_("Do you want to save the current settings"),
180                                 _("for the document layout as default?"),
181                                 _("(they will be valid for any new document)")))
182                 return;
183 #endif
184
185         lv_.buffer()->params().preamble = bp_->preamble;
186
187         string const fname = AddName(AddPath(user_lyxdir(), "templates/"),
188                                      "defaults.lyx");
189         Buffer defaults(fname);
190         defaults.params() = params();
191
192         // add an empty paragraph. Is this enough?
193         Paragraph par;
194         par.layout(params().getLyXTextClass().defaultLayout());
195         defaults.paragraphs().push_back(par);
196
197         defaults.writeFile(defaults.fileName());
198
199 }