]> git.lyx.org Git - features.git/commitdiff
5 new lfuns, move all apply code out of ControlDocument and into the core.
authorAngus Leeming <leeming@lyx.org>
Tue, 30 Mar 2004 16:19:50 +0000 (16:19 +0000)
committerAngus Leeming <leeming@lyx.org>
Tue, 30 Mar 2004 16:19:50 +0000 (16:19 +0000)
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@8565 a592a061-630c-0410-9148-cb99ea01b6c8

src/ChangeLog
src/LyXAction.C
src/frontends/controllers/ChangeLog
src/frontends/controllers/ControlDocument.C
src/frontends/controllers/ControlDocument.h
src/lfuns.h
src/lyxfunc.C
src/lyxtextclass.C
src/lyxtextclass.h

index 0127d69f47780323d6ee59265baa074e1711b746..82788e3b4d59c986d1bdb2d3eb65415f7edc5c74 100644 (file)
@@ -1,3 +1,14 @@
+2004-03-30  Angus Leeming  <leeming@lyx.org>
+
+       * lfuns.h:
+       * LyXAction.C: new lfuns LFUN_LANGUAGE_BUFFER, LFUN_TEXTCLASS_APPLY,
+       LFUN_TEXTCLASS_LOAD, LFUN_SAVE_AS_DEFAULT, LFUN_BUFFERPARAMS_APPLY.
+
+       * lyxfunc.C (getStatus, dispatch): define the actions for these
+       lfuns. Little more than a cut and pste job from ControlDocument.C
+
+       * lyxtextclass.[Ch] (loaded): accessor for the private bool loaded_.
+
 2004-03-30  Angus Leeming  <leeming@lyx.org>
 
        * lfuns.h:
index 5e1def0120b53cf026a358d1403c280425bb6305..f77ed7374f51a3fc19022a34aaadca2c03a6a336 100644 (file)
@@ -329,6 +329,11 @@ void LyXAction::init()
                { LFUN_WORD_FIND, "word-find", Noop },
                { LFUN_WORD_REPLACE, "word-replace", Noop },
                { LFUN_KEYMAP_TOGGLE, "keymap-toggle", Noop },
+               { LFUN_LANGUAGE_BUFFER, "buffer-language", Noop },
+               { LFUN_TEXTCLASS_APPLY, "textclass-apply", Noop },
+               { LFUN_TEXTCLASS_LOAD, "textclass-load", Noop },
+               { LFUN_SAVE_AS_DEFAULT, "buffer-save-as-default", Noop },
+               { LFUN_BUFFERPARAMS_APPLY, "buffer-params-apply", Noop },
                { LFUN_NOACTION, "", Noop }
        };
 
index aa7aa6e8cf9400e50b5ec85ee3631d9f8542099b..ab1ca56ef3723059dc9915fbc93ab25ea7a90318 100644 (file)
@@ -1,3 +1,8 @@
+2004-03-30  Angus Leeming  <leeming@lyx.org>
+
+       * ControlDocument.[Ch]: move all of the 'apply' code into the core
+       and access it by dispatching the appropriate lfuns.
+
 2004-03-30  Angus Leeming  <leeming@lyx.org>
 
        * ControlDocument.C (apply): replace the loop over paragraphs
index 40ae2d630429acaf56f8af3382399cd65bc8ed58..00f32ba1e07a5bafec210d0a4e9dd24cdc2b79d1 100644 (file)
 
 #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;
+#include "support/std_sstream.h"
 
+using std::ostringstream;
 using std::string;
 
 
@@ -52,6 +38,16 @@ ControlDocument::~ControlDocument()
 {}
 
 
+void ControlDocument::setParams()
+{
+       if (!bp_.get())
+               bp_.reset(new BufferParams);
+
+       /// Set the buffer parameters
+       *bp_ = buffer()->params();
+}
+
+
 BufferParams & ControlDocument::params()
 {
        BOOST_ASSERT(bp_.get());
@@ -65,6 +61,19 @@ LyXTextClass ControlDocument::textClass()
 }
 
 
+namespace {
+
+void dispatch_params(LyXView & lv, BufferParams const & bp, kb_action lfun)
+{
+       ostringstream ss;
+       bp.writeFile(ss);
+       ss << "\\end_header\n";
+       lv.dispatch(FuncRequest(lfun, ss.str()));
+}
+
+} // namespace anon
+
+
 void ControlDocument::apply()
 {
        if (!bufferIsAvailable())
@@ -72,22 +81,26 @@ void ControlDocument::apply()
 
        view().apply();
 
-       // this must come first so that a language change
-       // is correctly noticed
+       // This must come first so that a language change is correctly noticed
        setLanguage();
 
-       classApply();
-
-       buffer()->params() = *bp_;
-
-       lv_.view()->redoCurrentBuffer();
+       // Set the document class.
+       lyx::textclass_type const old_class = buffer()->params().textclass;
+       lyx::textclass_type const new_class = bp_->textclass;
 
-       buffer()->markDirty();
+       if (new_class != old_class) {
+               string const name = textclasslist[new_class].name();
+               lv_.dispatch(FuncRequest(LFUN_TEXTCLASS_APPLY, name));
+       }
 
-       lv_.message(_("Document settings applied"));
+       // Apply the BufferParams.
+       dispatch_params(lv_, params(), LFUN_BUFFERPARAMS_APPLY);
 
-       // branches
+       // Generate the colours requested by each new branch.
        BranchList & branchlist = params().branchlist();
+       if (branchlist.empty())
+               return;
+
        BranchList::const_iterator it = branchlist.begin();
        BranchList::const_iterator const end = branchlist.end();
        for (; it != end; ++it) {
@@ -107,93 +120,29 @@ void ControlDocument::apply()
 }
 
 
-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;
+       Language const * const newL = bp_->language;
+       if (buffer()->params().language == newL)
+               return;
 
-       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;
-       lyx::cap::SwitchLayoutsBetweenClasses(old_class, new_class,
-                                                lv_.buffer()->paragraphs(),
-                                                el);
-       bufferErrors(*buffer(), el);
-       bufferview()->showErrorList(_("Class switch"));
+       string const lang_name = newL->lang();
+       lv_.dispatch(FuncRequest(LFUN_LANGUAGE_BUFFER, lang_name));
 }
 
 
 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);
+       string const name = textclasslist[tc].name();
+       lv_.dispatch(FuncRequest(LFUN_TEXTCLASS_LOAD, name));
 
+       // Report back whether we were able to change the class.
+       bool const success = textclasslist[tc].loaded();
        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());
-
+       dispatch_params(lv_, params(), LFUN_SAVE_AS_DEFAULT);
 }
index 3661f93294b4c289d684a096f9053f9c5e5706c4..8aeabc291dc56123a7f993b19a9e59e3001d58f5 100644 (file)
@@ -40,8 +40,6 @@ public:
        ///
        void saveAsDefault();
        ///
-       void classApply();
-       ///
        bool loadTextclass(lyx::textclass_type tc) const;
 private:
        /// apply settings
index 35f52adf1f815a54ff6589ef29af1dc70fc97d8e..026c35f09eec2bd7dedada86c895ef38137fdce4 100644 (file)
@@ -340,6 +340,13 @@ enum kb_action {
        LFUN_KEYMAP_TOGGLE,
        LFUN_NEXT_INSET_TOGGLE,
        LFUN_ALL_INSETS_TOGGLE,
+       // 260
+       LFUN_LANGUAGE_BUFFER,
+       LFUN_TEXTCLASS_APPLY,
+       LFUN_TEXTCLASS_LOAD,
+       LFUN_SAVE_AS_DEFAULT,
+       LFUN_BUFFERPARAMS_APPLY,
+       // 265
 
        LFUN_LASTACTION                  // end of the table
 };
index b4ebfabc8e2db48d0b467c81b0599eda196d5135..fe575daf3cb615146901500da8e2ef63337da889 100644 (file)
 #include "bufferparams.h"
 #include "BufferView.h"
 #include "cursor.h"
+#include "CutAndPaste.h"
 #include "debug.h"
 #include "dispatchresult.h"
 #include "encoding.h"
+#include "errorlist.h"
 #include "exporter.h"
 #include "format.h"
 #include "funcrequest.h"
 #include "insetiterator.h"
 #include "intl.h"
 #include "kbmap.h"
+#include "language.h"
 #include "LColor.h"
 #include "lyx_cb.h"
 #include "LyXAction.h"
 #include "lyxfind.h"
+#include "lyxlex.h"
 #include "lyxrc.h"
 #include "lyxrow.h"
 #include "lyxserver.h"
+#include "lyxtextclasslist.h"
 #include "lyxvc.h"
 #include "paragraph.h"
 #include "pariterator.h"
@@ -496,6 +501,12 @@ FuncStatus LyXFunc::getStatus(FuncRequest const & cmd) const
        case LFUN_FILE_INSERT_ASCII:
        case LFUN_FILE_INSERT_ASCII_PARA:
        case LFUN_ALL_INSETS_TOGGLE:
+       case LFUN_LANGUAGE_BUFFER:
+       case LFUN_TEXTCLASS_APPLY:
+       case LFUN_TEXTCLASS_LOAD:
+       case LFUN_SAVE_AS_DEFAULT:
+       case LFUN_BUFFERPARAMS_APPLY:
+
                // these are handled in our dispatch()
                break;
 
@@ -507,7 +518,7 @@ FuncStatus LyXFunc::getStatus(FuncRequest const & cmd) const
                flag.enabled(false);
 
        // Can we use a readonly buffer?
-       if (buf && buf->isReadonly() 
+       if (buf && buf->isReadonly()
            && !lyxaction.funcHasFlag(cmd.action, LyXAction::ReadOnly)
            && !lyxaction.funcHasFlag(cmd.action, LyXAction::NoBuffer)) {
                setStatusMessage(N_("Document is read-only"));
@@ -817,7 +828,7 @@ void LyXFunc::dispatch(FuncRequest const & cmd, bool verbose)
                                                Systemcall::DontWait,
                                                command + QuoteName(dviname));
                                }
-               
+
                        } else {
                                // case 1: print to a file
                                command += lyxrc.print_to_file
@@ -1271,6 +1282,94 @@ void LyXFunc::dispatch(FuncRequest const & cmd, bool verbose)
                        break;
                }
 
+               case LFUN_LANGUAGE_BUFFER: {
+                       Buffer & buffer = *owner->buffer();
+                       Language const * oldL = buffer.params().language;
+                       Language const * newL = languages.getLanguage(argument);
+                       if (!newL || oldL == newL)
+                               break;
+
+                       if (oldL->RightToLeft() == newL->RightToLeft()
+                           && !buffer.isMultiLingual())
+                               buffer.changeLanguage(oldL, newL);
+                       else
+                               buffer.updateDocLang(newL);
+                       break;
+               }
+
+               case LFUN_BUFFERPARAMS_APPLY: {
+                       istringstream ss(argument);
+                       LyXLex lex(0,0);
+                       lex.setStream(ss);
+                       int const unknown_tokens =
+                               owner->buffer()->readHeader(lex);
+
+                       if (unknown_tokens != 0) {
+                               lyxerr << "Warning in LFUN_BUFFERPARAMS_APPLY!\n"
+                                      << unknown_tokens << " unknown token"
+                                      << (unknown_tokens == 1 ? "" : "s")
+                                      << endl;
+                       }
+                       break;
+               }
+
+               case LFUN_TEXTCLASS_APPLY: {
+                       Buffer * buffer = owner->buffer();
+
+                       lyx::textclass_type const old_class =
+                               buffer->params().textclass;
+
+                       dispatch(FuncRequest(LFUN_TEXTCLASS_LOAD, argument));
+
+                       std::pair<bool, lyx::textclass_type> const tc_pair =
+                               textclasslist.NumberOfClass(argument);
+
+                       if (!tc_pair.first)
+                               break;
+
+                       lyx::textclass_type const new_class = tc_pair.second;
+                       if (old_class == new_class)
+                               // nothing to do
+                               break;
+
+                       owner->message(
+                               _("Converting document to new document class..."));
+
+                       ErrorList el;
+                       lyx::cap::SwitchLayoutsBetweenClasses(
+                               old_class, new_class,
+                               buffer->paragraphs(), el);
+
+                       bufferErrors(*buffer, el);
+                       view()->showErrorList(_("Class switch"));
+                       break;
+               }
+
+               case LFUN_TEXTCLASS_LOAD: {
+                       std::pair<bool, lyx::textclass_type> const tc_pair =
+                               textclasslist.NumberOfClass(argument);
+
+                       if (!tc_pair.first) {
+                               lyxerr << "Document class \"" << argument
+                                      << "\" does not exist."
+                                      << std::endl;
+                               break;
+                       }
+
+                       lyx::textclass_type const tc = tc_pair.second;
+
+                       bool const success = textclasslist[tc].load();
+                       if (success)
+                               break;
+
+                       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);
+
+                       break;
+               }
+
                default: {
                        DispatchResult res = view()->cursor().dispatch(cmd);
                        if (!res.dispatched());
index 34e70f2c7038fc73a3e241689b3478be36509b08..d4f943dc3bfc3abe4337f050b8d5fb47c33a5a58 100644 (file)
@@ -72,7 +72,7 @@ LyXTextClass::LyXTextClass(string const & fn, string const & cln,
        provides_ = nothing;
        titletype_ = TITLE_COMMAND_AFTER;
        titlename_ = "maketitle";
-       loaded = false;
+       loaded_ = false;
 }
 
 
@@ -840,7 +840,7 @@ bool LyXTextClass::delete_layout(string const & name)
 // Load textclass info if not loaded yet
 bool LyXTextClass::load() const
 {
-       if (loaded)
+       if (loaded_)
                return true;
 
        // Read style-file
@@ -852,10 +852,10 @@ bool LyXTextClass::load() const
                       << "'\n(Check `" << name_
                       << "')\nCheck your installation and "
                        "try Options/Reconfigure..." << endl;
-               loaded = false;
+               loaded_ = false;
        }
-       loaded = true;
-       return loaded;
+       loaded_ = true;
+       return loaded_;
 }
 
 
index 5f9ccca6908fb71eb94126feaacc0e33bf431bcf..e400338ae4ad911c8c62b0a6d1efa2b437547b36 100644 (file)
@@ -84,6 +84,8 @@ public:
 
        /// Sees to that the textclass structure has been loaded
        bool load() const;
+       /// Has this layout file been loaded yet?
+       bool loaded() const { return loaded_; }
 
        /// the list of floats defined in the document class
        FloatList & floats();
@@ -235,7 +237,7 @@ private:
        boost::shared_ptr<Counters> ctrs_;
 
        /// Has this layout file been loaded yet?
-       mutable bool loaded;
+       mutable bool loaded_;
 
        /// Is the TeX class available?
        bool texClassAvail_;