From 3910647ab4ded6046e82d1a4711aa205495f5e49 Mon Sep 17 00:00:00 2001 From: Angus Leeming Date: Tue, 4 Mar 2003 16:39:13 +0000 Subject: [PATCH] Rearrange Dialog communication code to make it easier to handle similar stuff for other insets. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@6338 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/mathed/ChangeLog | 15 +++++++++++ src/mathed/command_inset.C | 15 +++++++++++ src/mathed/command_inset.h | 2 ++ src/mathed/formulabase.C | 9 ++++--- src/mathed/math_factory.C | 26 +++++++++++++++++++ src/mathed/math_factory.h | 11 +++++--- src/mathed/ref_inset.C | 53 +++++++++----------------------------- src/mathed/ref_inset.h | 6 ----- 8 files changed, 83 insertions(+), 54 deletions(-) diff --git a/src/mathed/ChangeLog b/src/mathed/ChangeLog index 4df2ea1e1f..b9fa3f9598 100644 --- a/src/mathed/ChangeLog +++ b/src/mathed/ChangeLog @@ -1,3 +1,18 @@ +2003-03-04 Angus Leeming + + * command_inset.[Ch] (createDialogStr): a new function to generate + something that the frontend Dialogs will understand. + + * math_factory.[Ch] (createMathInset_fromDialogStr): new function + parses the string passed from the frontends. + + * formulabase.C (localDiapatch): + * ref_inset.C (dispatch): use createDialogStr and + createMathInset_fromDialogStr rather than the current hard-coded + stuff. + + * ref_inset.[Ch] (string2RefInset): goes the way of the dodo. + 2003-03-04 Angus Leeming * formulabase.C (localDispatch): if an inset is found on diff --git a/src/mathed/command_inset.C b/src/mathed/command_inset.C index 22e56af173..f8a03c2e38 100644 --- a/src/mathed/command_inset.C +++ b/src/mathed/command_inset.C @@ -2,6 +2,7 @@ #include "command_inset.h" #include "math_mathmlstream.h" #include "funcrequest.h" +#include "Lsstream.h" CommandInset::CommandInset(string const & data) @@ -51,3 +52,17 @@ string CommandInset::screenLabel() const { return name_; } + + +string const CommandInset::createDialogStr(string const & name) const +{ + ostringstream data; + data << name << " LatexCommand "; + WriteStream wsdata(data); + write(wsdata); + wsdata << "\n\\end_inset\n\n"; + + return data.str(); +} + + diff --git a/src/mathed/command_inset.h b/src/mathed/command_inset.h index df5981f71e..9902afc811 100644 --- a/src/mathed/command_inset.h +++ b/src/mathed/command_inset.h @@ -32,6 +32,8 @@ public: dispatch_result dispatch(FuncRequest const & cmd, idx_type & idx, pos_type & pos); /// string screenLabel() const; + /// generate something that will be understodd by the Dialogs. + string const createDialogStr(string const & name) const; public: string name_; }; diff --git a/src/mathed/formulabase.C b/src/mathed/formulabase.C index cca0e6f1c4..8bdb9e11eb 100644 --- a/src/mathed/formulabase.C +++ b/src/mathed/formulabase.C @@ -797,7 +797,8 @@ dispatch_result InsetFormulaBase::localDispatch(FuncRequest const & cmd) case LFUN_DIALOG_SHOW_NEW_INSET: { string const & name = argument; if (name == "ref") { - string data = "ref LatexCommand \\ref{}\n\\end_inset\n\n"; + RefInset tmp; + string const data = tmp.createDialogStr(name); bv->owner()->getDialogs().show(name, data, 0); } else result = UNDISPATCHED; @@ -806,17 +807,17 @@ dispatch_result InsetFormulaBase::localDispatch(FuncRequest const & cmd) case LFUN_INSET_APPLY: { string const name = cmd.getArg(0); - InsetBase * base = + InsetBase * base = bv->owner()->getDialogs().getOpenInset(name); if (base) { FuncRequest fr(bv, LFUN_INSET_MODIFY, cmd.argument); result = base->localDispatch(fr); } else { - // Turn 'argument' into a temporary RefInset. MathArray ar; - if (string2RefInset(cmd.argument, ar)) { + if (createMathInset_fromDialogStr(cmd.argument, ar)) { mathcursor->insert(ar); + result = DISPATCHED; } else { result = UNDISPATCHED; } diff --git a/src/mathed/math_factory.C b/src/mathed/math_factory.C index 7ad66b051d..68166b7215 100644 --- a/src/mathed/math_factory.C +++ b/src/mathed/math_factory.C @@ -46,6 +46,7 @@ #include "math_support.h" #include "Lsstream.h" #include "support/filetools.h" // LibFileSearch +#include "support/lstrings.h" #include "frontends/lyx_gui.h" #include @@ -299,3 +300,28 @@ MathAtom createMathInset(string const & s) //lyxerr[Debug::MATHED] << "creating inset 2 with name: '" << s << '\'' << endl; return MathAtom(new MathUnknownInset(s)); } + + +bool createMathInset_fromDialogStr(string const & str, MathArray & ar) +{ + // An example str: + // "ref LatexCommand \\ref{sec:Title}\n\\end_inset\n\n"; + string name; + string body = split(str, name, ' '); + + if (name != "ref" ) + return false; + + // body comes with a head "LatexCommand " and a + // tail "\nend_inset\n\n". Strip them off. + string trimmed; + body = split(body, trimmed, ' '); + split(body, trimmed, '\n'); + + mathed_parse_cell(ar, trimmed); + if (ar.size() != 1) + return false; + + return ar[0].nucleus(); +} + diff --git a/src/mathed/math_factory.h b/src/mathed/math_factory.h index 8ddb4413ec..bcecc29d3b 100644 --- a/src/mathed/math_factory.h +++ b/src/mathed/math_factory.h @@ -3,11 +3,16 @@ #include "LString.h" -#include "math_atom.h" -class MathInset; -class latexkeys; +class MathAtom; +class MathArray; MathAtom createMathInset(string const &); +/** Fills ar with the contents of str. + * str is created by the frontend dialog's and returned to the LyX core. + * The function returns true if successful. + */ +bool createMathInset_fromDialogStr(string const &, MathArray &); + #endif diff --git a/src/mathed/ref_inset.C b/src/mathed/ref_inset.C index ca7d8e67ec..7288930658 100644 --- a/src/mathed/ref_inset.C +++ b/src/mathed/ref_inset.C @@ -2,20 +2,18 @@ #include #include "ref_inset.h" -#include "funcrequest.h" -#include "formulabase.h" +#include "math_factory.h" + #include "BufferView.h" -#include "frontends/LyXView.h" -#include "frontends/Painter.h" -#include "frontends/Dialogs.h" -#include "lyxfunc.h" +#include "debug.h" +#include "funcrequest.h" #include "gettext.h" #include "LaTeXFeatures.h" -#include "debug.h" -#include "math_mathmlstream.h" -#include "Lsstream.h" -#include "math_parser.h" -#include "support/lstrings.h" + +#include "frontends/LyXView.h" +#include "frontends/Dialogs.h" + +#include "support/LOstream.h" RefInset::RefInset() @@ -53,14 +51,9 @@ RefInset::dispatch(FuncRequest const & cmd, idx_type & idx, pos_type & pos) if (cmd.button() == mouse_button::button1) { // Eventually trigger dialog with button 3 // not 1 - ostringstream data; - data << "ref LatexCommand "; - WriteStream wsdata(data); - write(wsdata); - wsdata << "\n\\end_inset\n\n"; - + string const data = createDialogStr("ref"); cmd.view()->owner()->getDialogs(). - show("ref", data.str(), this); + show("ref", data, this); return DISPATCHED; } break; @@ -137,7 +130,7 @@ dispatch_result RefInset::localDispatch(FuncRequest const & cmd) return UNDISPATCHED; MathArray ar; - if (!string2RefInset(cmd.argument, ar)) + if (!createMathInset_fromDialogStr(cmd.argument, ar)) return UNDISPATCHED; *this = *ar[0].nucleus()->asRefInset(); @@ -149,28 +142,6 @@ dispatch_result RefInset::localDispatch(FuncRequest const & cmd) } -bool string2RefInset(string const & str, MathArray & ar) -{ - string name; - string body = split(str, name, ' '); - - if (name != "ref") - return false; - - // body comes with a head "LatexCommand " and a - // tail "\nend_inset\n\n". Strip them off. - string trimmed; - body = split(body, trimmed, ' '); - split(body, trimmed, '\n'); - - mathed_parse_cell(ar, trimmed); - if (ar.size() != 1) - return false; - - return ar[0].nucleus()->asRefInset(); -} - - RefInset::ref_type_info RefInset::types[] = { { "ref", N_("Standard"), N_("Ref: ")}, { "pageref", N_("Page Number"), N_("Page: ")}, diff --git a/src/mathed/ref_inset.h b/src/mathed/ref_inset.h index c689c10cb6..0132d78ab3 100644 --- a/src/mathed/ref_inset.h +++ b/src/mathed/ref_inset.h @@ -51,10 +51,4 @@ public: static string const & getName(int type); }; -/** Fills ar with the contents of str. - * str is created by the reference dialog and returned to the LyX core. - * The function returns true if it succeeds in creating a RefInset. - */ -bool string2RefInset(string const & str, MathArray & ar); - #endif