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"
|
2003-06-24 20:42:15 +00:00
|
|
|
#include "buffer_funcs.h"
|
2003-05-20 16:51:31 +00:00
|
|
|
#include "errorlist.h"
|
2002-10-09 08:59:02 +00:00
|
|
|
#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"
|
2003-07-27 22:13:29 +00:00
|
|
|
#include "support/path_defines.h"
|
2002-10-09 08:59:02 +00:00
|
|
|
|
2003-06-30 23:56:22 +00:00
|
|
|
using namespace lyx::support;
|
|
|
|
|
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)
|
2003-05-13 09:48:57 +00:00
|
|
|
{}
|
|
|
|
|
2002-10-09 08:59:02 +00:00
|
|
|
|
|
|
|
ControlDocument::~ControlDocument()
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
BufferParams & ControlDocument::params()
|
|
|
|
{
|
2003-06-30 23:56:22 +00:00
|
|
|
Assert(bp_.get());
|
2002-10-09 08:59:02 +00:00
|
|
|
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
|
|
|
|
2002-10-09 08:59:02 +00:00
|
|
|
view().apply();
|
2003-06-06 14:47:43 +00:00
|
|
|
|
|
|
|
// this must come first so that a language change
|
|
|
|
// is correctly noticed
|
2003-02-25 18:52:13 +00:00
|
|
|
setLanguage();
|
2003-06-06 14:47:43 +00:00
|
|
|
|
|
|
|
classApply();
|
|
|
|
|
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
|
|
|
|
2003-04-25 01:54:04 +00:00
|
|
|
if (oldL != newL) {
|
2003-05-06 09:34:56 +00:00
|
|
|
|
2003-04-25 01:54:04 +00:00
|
|
|
if (oldL->RightToLeft() == newL->RightToLeft()
|
|
|
|
&& !lv_.buffer()->isMultiLingual())
|
|
|
|
lv_.buffer()->changeLanguage(oldL, newL);
|
|
|
|
else
|
|
|
|
lv_.buffer()->updateDocLang(newL);
|
|
|
|
}
|
2002-10-09 08:59:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
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..."));
|
2003-03-31 01:15:44 +00:00
|
|
|
|
2003-05-20 16:51:31 +00:00
|
|
|
ErrorList el;
|
|
|
|
CutAndPaste::SwitchLayoutsBetweenClasses(old_class, new_class,
|
|
|
|
lv_.buffer()->paragraphs,
|
|
|
|
el);
|
2003-07-07 08:37:02 +00:00
|
|
|
bufferErrors(*buffer(), el);
|
2003-05-20 16:51:31 +00:00
|
|
|
bufferview()->showErrorList(_("Class switch"));
|
2002-11-22 10:47:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool ControlDocument::loadTextclass(lyx::textclass_type tc) const
|
|
|
|
{
|
|
|
|
bool const success = textclasslist[tc].load();
|
2003-03-31 01:15:44 +00:00
|
|
|
if (success)
|
|
|
|
return success;
|
|
|
|
|
2003-05-13 09:48:57 +00:00
|
|
|
string s = bformat(_("The document could not be converted\n"
|
|
|
|
"into the document class %1$s."),
|
|
|
|
textclasslist[tc].name());
|
2003-03-31 01:15:44 +00:00
|
|
|
Alert::error(_("Could not change class"), s);
|
|
|
|
|
2002-11-22 10:47:56 +00:00
|
|
|
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
|
|
|
|
2003-07-27 22:13:29 +00:00
|
|
|
string const fname = AddName(AddPath(user_lyxdir(), "templates/"),
|
2002-10-09 08:59:02 +00:00
|
|
|
"defaults.lyx");
|
|
|
|
Buffer defaults(fname);
|
|
|
|
defaults.params = params();
|
|
|
|
|
|
|
|
// add an empty paragraph. Is this enough?
|
2003-05-24 11:54:10 +00:00
|
|
|
Paragraph par;
|
|
|
|
par.layout(params().getLyXTextClass().defaultLayout());
|
2003-05-07 09:26:33 +00:00
|
|
|
defaults.paragraphs.push_back(par);
|
2002-10-20 01:48:28 +00:00
|
|
|
|
2002-10-09 08:59:02 +00:00
|
|
|
defaults.writeFile(defaults.fileName());
|
|
|
|
|
|
|
|
}
|