]> git.lyx.org Git - lyx.git/blob - src/mathed/formula.C
bug + spped fixes + small stuff
[lyx.git] / src / mathed / formula.C
1 /**
2  * \file formula.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Alejandro Aguilar Sierra
7  * \author André Pönitz
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "formula.h"
15 #include "formulamacro.h"
16 #include "math_data.h"
17 #include "math_hullinset.h"
18 #include "math_mathmlstream.h"
19 #include "math_parser.h"
20
21 #include "BufferView.h"
22 #include "cursor.h"
23 #include "dispatchresult.h"
24 #include "debug.h"
25 #include "funcrequest.h"
26 #include "gettext.h"
27 #include "LaTeXFeatures.h"
28 #include "LColor.h"
29 #include "lyxrc.h"
30 #include "lyxtext.h"
31 #include "textpainter.h"
32
33 #include "frontends/Alert.h"
34
35 #include "support/std_sstream.h"
36
37 using lyx::support::trim;
38
39 using std::endl;
40 using std::max;
41
42 using std::string;
43 using std::auto_ptr;
44 using std::istringstream;
45 using std::ostringstream;
46 using std::pair;
47
48
49 namespace {
50
51 bool openNewInset(LCursor & cur, InsetBase * inset)
52 {
53         if (!cur.bv().insertInset(inset)) {
54                 delete inset;
55                 return false;
56         }
57         inset->edit(cur, true);
58         return true;
59 }
60
61
62 } // namespace anon
63
64
65
66
67 std::auto_ptr<InsetBase> InsetFormula::clone() const
68 {
69         return auto_ptr<InsetBase>(new InsetFormula(*this));
70 }
71
72
73 void InsetFormula::write(Buffer const &, std::ostream & os) const
74 {
75         WriteStream wi(os, false, false);
76         os << fileInsetLabel() << ' ';
77         MathHullInset::write(wi);
78 }
79
80
81 void InsetFormula::read(Buffer const &, LyXLex & lex)
82 {
83         MathAtom at;
84         mathed_parse_normal(at, lex);
85         MathHullInset::operator=(*at->asHullInset());
86 }
87
88
89
90 /////////////////////////////////////////////
91
92 void mathDispatchCreation(LCursor & cur, FuncRequest const & cmd,
93         bool display)
94 {
95         // use selection if available..
96         //string sel;
97         //if (action == LFUN_MATH_IMPORT_SELECTION)
98         //      sel = "";
99         //else
100
101         string sel =
102                 cur.bv().getLyXText()->selectionAsString(*cur.bv().buffer(), false);
103
104         if (sel.empty()) {
105                 InsetBase * f = new MathHullInset;
106                 if (openNewInset(cur, f)) {
107                         cur.inset()->dispatch(cur, FuncRequest(LFUN_MATH_MUTATE, "simple"));
108                         // don't do that also for LFUN_MATH_MODE unless you want end up with
109                         // always changing to mathrm when opening an inlined inset
110                         // -- I really hate "LyXfunc overloading"...
111                         if (display)
112                                 f->dispatch(cur, FuncRequest(LFUN_MATH_DISPLAY));
113                         f->dispatch(cur, FuncRequest(LFUN_INSERT_MATH, cmd.argument));
114                 }
115         } else {
116                 // create a macro if we see "\\newcommand" somewhere, and an ordinary
117                 // formula otherwise
118                 InsetBase * f;
119                 if (sel.find("\\newcommand") == string::npos &&
120                                 sel.find("\\def") == string::npos)
121                         f = new MathHullInset(sel);
122                 else
123                         f = new InsetFormulaMacro(sel);
124                 cur.bv().getLyXText()->cutSelection(true, false);
125                 openNewInset(cur, f);
126         }
127         cmd.message(N_("Math editor mode"));
128 }
129
130
131 void mathDispatch(LCursor & cur, FuncRequest const & cmd)
132 {
133         if (!cur.bv().available())
134                 return;
135
136         switch (cmd.action) {
137
138                 case LFUN_MATH_DISPLAY:
139                         mathDispatchCreation(cur, cmd, true);
140                         break;
141
142                 case LFUN_MATH_MODE:
143                         mathDispatchCreation(cur, cmd, false);
144                         break;
145
146                 case LFUN_MATH_IMPORT_SELECTION:
147                         mathDispatchCreation(cur, cmd, false);
148                         break;
149
150 /*
151                 case LFUN_MATH_MACRO:
152                         if (cmd.argument.empty())
153                                 cmd.errorMessage(N_("Missing argument"));
154                         else {
155                                 string s = cmd.argument;
156                                 string const s1 = token(s, ' ', 1);
157                                 int const nargs = s1.empty() ? 0 : atoi(s1);
158                                 string const s2 = token(s, ' ', 2);
159                                 string const type = s2.empty() ? "newcommand" : s2;
160                                 openNewInset(cur, new InsetFormulaMacro(token(s, ' ', 0), nargs, s2));
161                         }
162                         break;
163
164                 case LFUN_INSERT_MATH:
165                 case LFUN_INSERT_MATRIX:
166                 case LFUN_MATH_DELIM: {
167                         MathHullInset * f = new MathHullInset;
168                         if (openNewInset(cur, f)) {
169                                 cur.inset()->dispatch(cur, FuncRequest(LFUN_MATH_MUTATE, "simple"));
170                                 cur.inset()->dispatch(cur, cmd);
171                         }
172                         break;
173                 }
174 */
175
176                 default:
177                         break;
178         }
179 }