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