This commit was manufactured by cvs2svn to create tag 'lyx-1_3_7'.
[lyx.git] / src / frontends / controllers / ControlDocument.C
blob76d4817c5575c4d4d7720f128100b9c6814946d0
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  */
12 #include <config.h>
14 #include "BufferView.h"
15 #include "ControlDocument.h"
16 #include "ViewBase.h"
18 #include "gettext.h"
19 #include "lyxfind.h"
21 #include "buffer.h"
22 #include "language.h"
23 #include "lyxtextclass.h"
24 #include "lyxtextclasslist.h"
25 #include "CutAndPaste.h"
27 #include "frontends/LyXView.h"
28 #include "frontends/Alert.h"
30 #include "support/filetools.h"
31 #include "support/lstrings.h"
32 #include "support/package.h"
34 #include "BoostFormat.h"
36 using std::endl;
39 ControlDocument::ControlDocument(LyXView & lv, Dialogs & d)
40         : ControlDialogBD(lv, d), bp_(0)
44 ControlDocument::~ControlDocument()
48 BufferParams & ControlDocument::params()
50         lyx::Assert(bp_.get());
51         return *bp_;
55 LyXTextClass ControlDocument::textClass()
57         return textclasslist[bp_->textclass];
61 void ControlDocument::apply()
63         if (!bufferIsAvailable())
64                 return;
66         view().apply();
68         // this must come first so that a language change
69         // is correctly noticed
70         setLanguage();
72         classApply();
74         buffer()->params = *bp_;
76         lv_.view()->redoCurrentBuffer();
78         buffer()->markDirty();
80         lv_.message(_("Document settings applied"));
84 void ControlDocument::setParams()
86         if (!bp_.get())
87                 bp_.reset(new BufferParams());
89         /// Set the buffer parameters
90         *bp_ = buffer()->params;
94 void ControlDocument::setLanguage()
96         Language const * oldL = buffer()->params.language;
97         Language const * newL = bp_->language;
99         if (oldL != newL
100             && oldL->RightToLeft() == newL->RightToLeft()
101             && !lv_.buffer()->isMultiLingual())
102                 lv_.buffer()->changeLanguage(oldL, newL);
106 void ControlDocument::classApply()
108         BufferParams & params = buffer()->params;
109         lyx::textclass_type const old_class = params.textclass;
110         lyx::textclass_type const new_class = bp_->textclass;
112         // exit if nothing changes or if unable to load the new class
113         if (new_class == old_class || !loadTextclass(new_class))
114                 return;
116         // successfully loaded
117         buffer()->params = *bp_;
119         lv_.message(_("Converting document to new document class..."));
120         int ret = CutAndPaste::SwitchLayoutsBetweenClasses(
121                 old_class, new_class,
122                 &*(lv_.buffer()->paragraphs.begin()),
123                 lv_.buffer()->params);
124         if (ret) {
125                 string s;
126                 if (ret == 1) {
127                         s = _("One paragraph couldn't be converted");
128                 } else {
129 #if USE_BOOST_FORMAT
130                         boost::format fmt(_("%1$s paragraphs couldn't be converted"));
131                         fmt % ret;
132                         s = fmt.str();
133 #else
134                         s += tostr(ret);
135                         s += _(" paragraphs couldn't be converted");
136 #endif
137                 }
138                 Alert::alert(_("Conversion Errors!"),s,
139                              _("into chosen document class"));
140         }
144 bool ControlDocument::loadTextclass(lyx::textclass_type tc) const
146         bool const success = textclasslist[tc].load();
147         if (!success) {
148                 // problem changing class
149                 // -- warn user (to retain old style)
150                 Alert::alert(_("Conversion Errors!"),
151                              _("Errors loading new document class."),
152                              _("Reverting to original document class."));
153         }
154         return success;
158 void ControlDocument::saveAsDefault()
160         if (!Alert::askQuestion(_("Do you want to save the current settings"),
161                                 _("for the document layout as default?"),
162                                 _("(they will be valid for any new document)")))
163                 return;
165         lv_.buffer()->params.preamble = bp_->preamble;
167         string const & user_support = lyx::package().user_support();
168         string const fname = AddName(AddPath(user_support, "templates/"),
169                                      "defaults.lyx");
170         Buffer defaults(fname);
171         defaults.params = params();
173         // add an empty paragraph. Is this enough?
174         Paragraph * par = new Paragraph;
175         par->layout(params().getLyXTextClass().defaultLayout());
176         defaults.paragraphs.set(par);
178         defaults.writeFile(defaults.fileName());