2002-10-09 08:59:02 +00:00
|
|
|
// -*- C++ -*-
|
|
|
|
/**
|
|
|
|
* \file ControlDocument.C
|
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
|
|
|
*
|
2002-10-20 01:48:28 +00:00
|
|
|
* \author Edwin Leuven
|
2002-10-09 08:59:02 +00:00
|
|
|
*
|
|
|
|
* Full author contact details are available in file CREDITS
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
|
|
|
|
#include "BufferView.h"
|
|
|
|
#include "ControlDocument.h"
|
|
|
|
#include "ViewBase.h"
|
|
|
|
|
|
|
|
#include "gettext.h"
|
|
|
|
#include "lyxfind.h"
|
|
|
|
|
|
|
|
#include "buffer.h"
|
|
|
|
#include "language.h"
|
|
|
|
#include "lyx_main.h"
|
|
|
|
#include "lyxtextclass.h"
|
|
|
|
#include "lyxtextclasslist.h"
|
|
|
|
#include "CutAndPaste.h"
|
|
|
|
|
|
|
|
#include "frontends/LyXView.h"
|
|
|
|
#include "frontends/Alert.h"
|
|
|
|
|
2003-03-10 03:13:28 +00:00
|
|
|
#include "support/LAssert.h"
|
2002-10-09 08:59:02 +00:00
|
|
|
#include "support/lstrings.h"
|
|
|
|
#include "support/filetools.h"
|
|
|
|
|
2002-12-02 09:59:43 +00:00
|
|
|
#include "BoostFormat.h"
|
2002-12-01 21:10:37 +00:00
|
|
|
|
|
|
|
using std::endl;
|
|
|
|
|
2002-10-09 08:59:02 +00:00
|
|
|
|
|
|
|
ControlDocument::ControlDocument(LyXView & lv, Dialogs & d)
|
|
|
|
: ControlDialogBD(lv, d), bp_(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ControlDocument::~ControlDocument()
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
BufferParams & ControlDocument::params()
|
|
|
|
{
|
|
|
|
lyx::Assert(bp_.get());
|
|
|
|
return *bp_;
|
|
|
|
}
|
|
|
|
|
2002-10-20 01:48:28 +00:00
|
|
|
|
2002-10-09 08:59:02 +00:00
|
|
|
LyXTextClass ControlDocument::textClass()
|
|
|
|
{
|
|
|
|
return textclasslist[bp_->textclass];
|
|
|
|
}
|
|
|
|
|
2002-10-20 01:48:28 +00:00
|
|
|
|
2002-10-09 08:59:02 +00:00
|
|
|
void ControlDocument::apply()
|
|
|
|
{
|
|
|
|
if (!bufferIsAvailable())
|
|
|
|
return;
|
2002-10-20 01:48:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
classApply();
|
|
|
|
|
2002-10-09 08:59:02 +00:00
|
|
|
view().apply();
|
2003-02-25 18:52:13 +00:00
|
|
|
setLanguage();
|
2002-10-09 08:59:02 +00:00
|
|
|
buffer()->params = *bp_;
|
2002-12-01 21:10:37 +00:00
|
|
|
|
2002-11-07 15:48:38 +00:00
|
|
|
lv_.view()->redoCurrentBuffer();
|
2002-10-09 08:59:02 +00:00
|
|
|
|
|
|
|
buffer()->markDirty();
|
2002-10-20 01:48:28 +00:00
|
|
|
|
2002-11-07 15:48:38 +00:00
|
|
|
lv_.message(_("Document settings applied"));
|
2002-10-09 08:59:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ControlDocument::setParams()
|
|
|
|
{
|
|
|
|
if (!bp_.get())
|
|
|
|
bp_.reset(new BufferParams());
|
2002-10-20 01:48:28 +00:00
|
|
|
|
2002-10-09 08:59:02 +00:00
|
|
|
/// Set the buffer parameters
|
|
|
|
*bp_ = buffer()->params;
|
|
|
|
}
|
|
|
|
|
2002-10-21 17:38:09 +00:00
|
|
|
|
2002-10-09 08:59:02 +00:00
|
|
|
void ControlDocument::setLanguage()
|
|
|
|
{
|
|
|
|
Language const * oldL = buffer()->params.language;
|
|
|
|
Language const * newL = bp_->language;
|
2002-10-20 01:48:28 +00:00
|
|
|
|
2002-10-09 08:59:02 +00:00
|
|
|
if (oldL != newL
|
|
|
|
&& oldL->RightToLeft() == newL->RightToLeft()
|
|
|
|
&& !lv_.buffer()->isMultiLingual())
|
|
|
|
lv_.buffer()->changeLanguage(oldL, newL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-11-22 10:47:56 +00:00
|
|
|
void ControlDocument::classApply()
|
2002-10-09 08:59:02 +00:00
|
|
|
{
|
2002-11-07 15:48:38 +00:00
|
|
|
BufferParams & params = buffer()->params;
|
2002-11-22 10:47:56 +00:00
|
|
|
lyx::textclass_type const old_class = params.textclass;
|
|
|
|
lyx::textclass_type const new_class = bp_->textclass;
|
2002-11-07 15:48:38 +00:00
|
|
|
|
2002-11-22 10:47:56 +00:00
|
|
|
// exit if nothing changes or if unable to load the new class
|
|
|
|
if (new_class == old_class || !loadTextclass(new_class))
|
|
|
|
return;
|
2002-10-20 01:48:28 +00:00
|
|
|
|
2002-10-09 08:59:02 +00:00
|
|
|
// successfully loaded
|
2002-11-07 15:48:38 +00:00
|
|
|
view().apply();
|
|
|
|
buffer()->params = *bp_;
|
2002-12-01 21:10:37 +00:00
|
|
|
|
2002-10-20 01:48:28 +00:00
|
|
|
lv_.message(_("Converting document to new document class..."));
|
2002-10-09 08:59:02 +00:00
|
|
|
int ret = CutAndPaste::SwitchLayoutsBetweenClasses(
|
2002-11-07 15:48:38 +00:00
|
|
|
old_class, new_class,
|
2002-10-09 08:59:02 +00:00
|
|
|
&*(lv_.buffer()->paragraphs.begin()),
|
|
|
|
lv_.buffer()->params);
|
|
|
|
if (ret) {
|
|
|
|
string s;
|
|
|
|
if (ret == 1) {
|
|
|
|
s = _("One paragraph couldn't be converted");
|
|
|
|
} else {
|
2002-12-01 21:10:37 +00:00
|
|
|
#if USE_BOOST_FORMAT
|
|
|
|
boost::format fmt(_("%1$s paragraphs couldn't be converted"));
|
|
|
|
fmt % ret;
|
|
|
|
s = fmt.str();
|
|
|
|
#else
|
2002-10-09 08:59:02 +00:00
|
|
|
s += tostr(ret);
|
|
|
|
s += _(" paragraphs couldn't be converted");
|
2002-12-01 21:10:37 +00:00
|
|
|
#endif
|
2002-10-09 08:59:02 +00:00
|
|
|
}
|
|
|
|
Alert::alert(_("Conversion Errors!"),s,
|
|
|
|
_("into chosen document class"));
|
|
|
|
}
|
2002-11-22 10:47:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool ControlDocument::loadTextclass(lyx::textclass_type tc) const
|
|
|
|
{
|
|
|
|
bool const success = textclasslist[tc].load();
|
|
|
|
if (!success) {
|
|
|
|
// problem changing class
|
|
|
|
// -- warn user (to retain old style)
|
|
|
|
Alert::alert(_("Conversion Errors!"),
|
|
|
|
_("Errors loading new document class."),
|
|
|
|
_("Reverting to original document class."));
|
|
|
|
}
|
|
|
|
return success;
|
2002-10-09 08:59:02 +00:00
|
|
|
}
|
|
|
|
|
2002-10-21 17:38:09 +00:00
|
|
|
|
2002-10-09 08:59:02 +00:00
|
|
|
void ControlDocument::saveAsDefault()
|
|
|
|
{
|
2003-03-29 07:09:13 +00:00
|
|
|
// Can somebody justify this ? I think it should be removed - jbl
|
|
|
|
#if 0
|
2002-11-07 15:48:38 +00:00
|
|
|
if (!Alert::askQuestion(_("Do you want to save the current settings"),
|
|
|
|
_("for the document layout as default?"),
|
|
|
|
_("(they will be valid for any new document)")))
|
|
|
|
return;
|
2003-03-29 07:09:13 +00:00
|
|
|
#endif
|
2002-12-01 21:10:37 +00:00
|
|
|
|
2002-10-09 08:59:02 +00:00
|
|
|
lv_.buffer()->params.preamble = bp_->preamble;
|
2002-10-20 01:48:28 +00:00
|
|
|
|
2002-10-09 08:59:02 +00:00
|
|
|
string const fname = AddName(AddPath(user_lyxdir, "templates/"),
|
|
|
|
"defaults.lyx");
|
|
|
|
Buffer defaults(fname);
|
|
|
|
defaults.params = params();
|
|
|
|
|
|
|
|
// add an empty paragraph. Is this enough?
|
2002-10-14 13:25:15 +00:00
|
|
|
Paragraph * par = new Paragraph;
|
|
|
|
par->layout(params().getLyXTextClass().defaultLayout());
|
|
|
|
defaults.paragraphs.set(par);
|
2002-10-20 01:48:28 +00:00
|
|
|
|
2002-10-09 08:59:02 +00:00
|
|
|
defaults.writeFile(defaults.fileName());
|
|
|
|
|
|
|
|
}
|