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