lyx_mirror/src/frontends/controllers/ControlDocument.C

200 lines
4.4 KiB
C++
Raw Normal View History

/**
* \file ControlDocument.C
* This file is part of LyX, the document processor.
* Licence details can be found in the file COPYING.
*
* \author Edwin Leuven
*
* Full author contact details are available in file CREDITS.
*/
#include <config.h>
#include "ControlDocument.h"
#include "ViewBase.h"
#include "BranchList.h"
#include "buffer.h"
#include "buffer_funcs.h"
#include "bufferparams.h"
#include "BufferView.h"
#include "CutAndPaste.h"
#include "errorlist.h"
#include "funcrequest.h"
#include "gettext.h"
#include "language.h"
#include "LColor.h"
#include "lyxtextclasslist.h"
#include "lfuns.h"
#include "paragraph.h"
#include "ParagraphList_fwd.h"
#include "frontends/Alert.h"
#include "frontends/LyXView.h"
#include "support/filetools.h"
#include "support/path_defines.h"
using lyx::support::AddName;
using lyx::support::AddPath;
using lyx::support::bformat;
using lyx::support::user_lyxdir;
using std::string;
ControlDocument::ControlDocument(LyXView & lv, Dialogs & d)
: ControlDialogBD(lv, d), bp_(0)
{}
ControlDocument::~ControlDocument()
{}
BufferParams & ControlDocument::params()
{
BOOST_ASSERT(bp_.get());
return *bp_;
}
LyXTextClass ControlDocument::textClass()
{
return textclasslist[bp_->textclass];
}
void ControlDocument::apply()
{
if (!bufferIsAvailable())
return;
view().apply();
// this must come first so that a language change
// is correctly noticed
setLanguage();
classApply();
buffer()->params() = *bp_;
lv_.view()->redoCurrentBuffer();
buffer()->markDirty();
lv_.message(_("Document settings applied"));
// branches
BranchList & branchlist = params().branchlist();
BranchList::const_iterator it = branchlist.begin();
BranchList::const_iterator const end = branchlist.end();
for (; it != end; ++it) {
string const & current_branch = it->getBranch();
Branch const * branch = branchlist.find(current_branch);
string x11hexname = branch->getColor();
// check that we have a valid color!
if (x11hexname.empty() || x11hexname[0] != '#')
x11hexname = lcolor.getX11Name(LColor::background);
// display the new color
string const str = current_branch + ' ' + x11hexname;
lv_.dispatch(FuncRequest(LFUN_SET_COLOR, str));
}
// Open insets of selected branches, close deselected ones
lv_.dispatch(FuncRequest(LFUN_ALL_INSETS_TOGGLE, "toggle branch"));
}
void ControlDocument::setParams()
{
if (!bp_.get())
bp_.reset(new BufferParams);
/// Set the buffer parameters
*bp_ = buffer()->params();
}
void ControlDocument::setLanguage()
{
Language const * oldL = buffer()->params().language;
Language const * newL = bp_->language;
if (oldL != newL) {
if (oldL->RightToLeft() == newL->RightToLeft()
&& !lv_.buffer()->isMultiLingual())
lv_.buffer()->changeLanguage(oldL, newL);
else
lv_.buffer()->updateDocLang(newL);
}
}
void ControlDocument::classApply()
{
BufferParams & params = buffer()->params();
lyx::textclass_type const old_class = params.textclass;
lyx::textclass_type const new_class = bp_->textclass;
// exit if nothing changes or if unable to load the new class
if (new_class == old_class || !loadTextclass(new_class))
return;
// successfully loaded
buffer()->params() = *bp_;
lv_.message(_("Converting document to new document class..."));
ErrorList el;
the stuff from the sneak preview: For one, it still contains a few things that are already in CVS (the 'brown paperbag' changes). Secondly, this changes the ParagraphList to a std::vector but does not yet take full advantage of it except removing LyXText::parOffset() and similar. I had an extensive talk with my profiler and we are happy nevertheless. This also moves almost all Cut&Paste specific stuff from text.C to CutAndPaste.C. Much smaller interface now... Namespace CutAndPaste is now lyx::cap::. Was inconsistent with the rest.... Make ParagraphList a proper class. We'll need this later for a specialized erase/insert. Remove some unneeded prototypes and function declarations Use ParameterStruct directly instead of ShareContainer<ParameterStruct> Inline a few accesses to CursorSlice members as suggested by the profiler. Fix commandline conversion crash reported by Kayvan. Replace PosIterator by DocumentIterator. The latter can also iterate through math and nested text in math... Remove math specific hack from Documentiterator Derive InsetCollapsable from InsetText instead of using an InsetText member. This give us the opportunity to get rid of the InsetOld::owner_ backpointer. Cosmetics in CutAndPaste.C and cursor.C. Fix nasty crash (popping slices off an empty selection anchor). Add a few asserts. Remove all 'manual' update calls. We do now one per user interaction which is completely sufficient. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@8527 a592a061-630c-0410-9148-cb99ea01b6c8
2004-03-25 09:16:36 +00:00
lyx::cap::SwitchLayoutsBetweenClasses(old_class, new_class,
lv_.buffer()->paragraphs(),
el);
bufferErrors(*buffer(), el);
bufferview()->showErrorList(_("Class switch"));
}
bool ControlDocument::loadTextclass(lyx::textclass_type tc) const
{
bool const success = textclasslist[tc].load();
if (success)
return success;
string s = bformat(_("The document could not be converted\n"
"into the document class %1$s."),
textclasslist[tc].name());
Alert::error(_("Could not change class"), s);
return success;
}
void ControlDocument::saveAsDefault()
{
// Can somebody justify this ? I think it should be removed - jbl
#if 0
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;
#endif
lv_.buffer()->params().preamble = bp_->preamble;
string const fname = AddName(AddPath(user_lyxdir(), "templates/"),
"defaults.lyx");
Buffer defaults(fname);
defaults.params() = params();
// add an empty paragraph. Is this enough?
Paragraph par;
par.layout(params().getLyXTextClass().defaultLayout());
defaults.paragraphs().push_back(par);
defaults.writeFile(defaults.fileName());
}