2002-10-09 08:59:02 +00:00
|
|
|
/**
|
|
|
|
* \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
|
|
|
*
|
2003-08-23 00:17:00 +00:00
|
|
|
* Full author contact details are available in file CREDITS.
|
2002-10-09 08:59:02 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include "ControlDocument.h"
|
|
|
|
#include "ViewBase.h"
|
|
|
|
|
|
|
|
#include "buffer.h"
|
2003-06-24 20:42:15 +00:00
|
|
|
#include "buffer_funcs.h"
|
2003-09-09 11:24:33 +00:00
|
|
|
#include "bufferparams.h"
|
2003-09-06 18:38:02 +00:00
|
|
|
#include "BufferView.h"
|
|
|
|
#include "CutAndPaste.h"
|
|
|
|
#include "errorlist.h"
|
|
|
|
#include "gettext.h"
|
2003-09-04 03:54:04 +00:00
|
|
|
#include "iterators.h"
|
2002-10-09 08:59:02 +00:00
|
|
|
#include "language.h"
|
|
|
|
#include "lyxtextclasslist.h"
|
2003-09-06 17:23:08 +00:00
|
|
|
#include "paragraph.h"
|
2002-10-09 08:59:02 +00:00
|
|
|
|
|
|
|
#include "frontends/Alert.h"
|
2003-09-06 18:38:02 +00:00
|
|
|
#include "frontends/LyXView.h"
|
2002-10-09 08:59:02 +00:00
|
|
|
|
|
|
|
#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-09-09 22:13:45 +00:00
|
|
|
using lyx::support::AddName;
|
|
|
|
using lyx::support::AddPath;
|
|
|
|
using lyx::support::bformat;
|
|
|
|
using lyx::support::user_lyxdir;
|
2003-06-30 23:56:22 +00:00
|
|
|
|
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-09-09 17:25:35 +00:00
|
|
|
BOOST_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();
|
|
|
|
|
2003-09-09 09:47:59 +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"));
|
2003-08-17 11:28:23 +00:00
|
|
|
|
|
|
|
// Open insets of selected branches, close deselected ones
|
|
|
|
// Currently only top-level insets in buffer handled (bug).
|
|
|
|
ParIterator pit = buffer()->par_iterator_begin();
|
|
|
|
ParIterator pend = buffer()->par_iterator_end();
|
|
|
|
for (; pit != pend; ++pit) {
|
|
|
|
pit->insetlist.insetsOpenCloseBranch(bufferview());
|
|
|
|
}
|
2002-10-09 08:59:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ControlDocument::setParams()
|
|
|
|
{
|
|
|
|
if (!bp_.get())
|
2003-09-21 16:02:54 +00:00
|
|
|
bp_.reset(new BufferParams);
|
2002-10-20 01:48:28 +00:00
|
|
|
|
2002-10-09 08:59:02 +00:00
|
|
|
/// Set the buffer parameters
|
2003-09-09 09:47:59 +00:00
|
|
|
*bp_ = buffer()->params();
|
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::setLanguage()
|
|
|
|
{
|
2003-09-09 09:47:59 +00:00
|
|
|
Language const * oldL = buffer()->params().language;
|
2002-10-09 08:59:02 +00:00
|
|
|
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
|
|
|
{
|
2003-09-09 09:47:59 +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
|
2003-09-09 09:47:59 +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,
|
2003-09-09 09:47:59 +00:00
|
|
|
lv_.buffer()->paragraphs(),
|
2003-05-20 16:51:31 +00:00
|
|
|
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
|
|
|
|
2003-09-09 09:47:59 +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);
|
2003-09-09 09:47:59 +00:00
|
|
|
defaults.params() = params();
|
2002-10-09 08:59:02 +00:00
|
|
|
|
|
|
|
// add an empty paragraph. Is this enough?
|
2003-05-24 11:54:10 +00:00
|
|
|
Paragraph par;
|
|
|
|
par.layout(params().getLyXTextClass().defaultLayout());
|
2003-09-09 09:47:59 +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());
|
|
|
|
|
|
|
|
}
|