]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/math_charinset.C
prepare hanling of chars the nee a backslash when written...
[lyx.git] / src / mathed / math_charinset.C
index a5eca98288fcf9419c8d49b3d6ccec017440a118..bfb6a7c3ab05996af66c92de8a65658abc59bb4e 100644 (file)
@@ -2,6 +2,8 @@
 #pragma implementation
 #endif
 
+#include <cctype>
+
 #include "math_charinset.h"
 #include "LColor.h"
 #include "Painter.h"
 #include "debug.h"
 
 
+MathCharInset::MathCharInset(char c)
+       : char_(c), code_(nativeCode(c)), needbs_(false)
+{
+//lyxerr << "creating char '" << char_ << "' with code " << int(code_) << endl;
+}
+
+
 MathCharInset::MathCharInset(char c, MathTextCodes t)
-       : char_(c)
+       : char_(c), code_((t == LM_TC_MIN) ? nativeCode(c) : t), needbs_(false)
+{
+//lyxerr << "creating char '" << char_ << "' with code " << int(code_) << endl;
+}
+
+
+MathCharInset::MathCharInset(char c, MathTextCodes t, bool needbs)
+       : char_(c), code_((t == LM_TC_MIN) ? nativeCode(c) : t), needbs_(needbs)
 {
-       code(t);
+//lyxerr << "creating char '" << char_ << "' with code " << int(code_) << endl;
+}
+
+
+MathTextCodes MathCharInset::nativeCode(char c) const
+{
+       if (isalpha(c))
+               return LM_TC_VAR;
+       if (strchr("#$%{|}", c))
+               return LM_TC_SPECIAL;
+       //if (strchr("0123456789;:!|[]().,?+/-*<>=", c)
+       return LM_TC_CONST;
 }
 
 
@@ -52,54 +79,56 @@ void MathCharInset::draw(Painter & pain, int x, int y) const
 { 
        xo(x);
        yo(y);
+       //lyxerr << "drawing '" << char_ << "' code: " << code_ << endl;
        drawChar(pain, code_, size_, x, y, char_);
 }
 
 
-void MathCharInset::write(std::ostream & os, bool) const
+void MathCharInset::writeHeader(std::ostream & os) const
 {
-       int brace = 0;
-
-       if (MathIsSymbol(code_)) {
-               latexkeys const * l = lm_get_key_by_id(char_, LM_TK_SYM);
-
-               if (l == 0) 
-                       l = lm_get_key_by_id(char_, LM_TK_BIGSYM);
-
-               if (l) {
-                       os << '\\' << l->name << ' ';
-               } else {
-                       lyxerr << "Could not find the LaTeX name for  " 
-                               << char_ << " and code_ " << code_ << "!" << std::endl;
-               }
-       } else {
-               if (code_ >= LM_TC_RM && code_ <= LM_TC_TEXTRM) 
-                       os << '\\' << math_font_name[code_ - LM_TC_RM] << '{';
-
-               // Is there a standard logical XOR?
-               if ((code_ == LM_TC_TEX && char_ != '{' && char_ != '}') ||
-                               (code_ == LM_TC_SPECIAL))
-                       os << '\\';
-               else {
-                       if (char_ == '{')
-                               ++brace;
-                       if (char_ == '}')
-                               --brace;
-               }
-               if (char_ == '}' && code_ == LM_TC_TEX && brace < 0) 
-                       lyxerr <<"Math warning: Unexpected closing brace.\n";
-               else           
-                       os << char_;
-       }
+       if (code_ >= LM_TC_RM && code_ <= LM_TC_TEXTRM) 
+               os << '\\' << math_font_name[code_ - LM_TC_RM] << '{';
+}
 
+
+void MathCharInset::writeTrailer(std::ostream & os) const
+{
        if (code_ >= LM_TC_RM && code_ <= LM_TC_TEXTRM)
                os << '}';
 }
 
 
+void MathCharInset::writeRaw(std::ostream & os) const
+{
+       if (needbs_)
+               os << "\\";
+       os << char_;
+}
+
+
+void MathCharInset::write(std::ostream & os, bool) const
+{
+       writeHeader(os);
+       writeRaw(os);
+       writeTrailer(os);
+}
+
+
 void MathCharInset::writeNormal(std::ostream & os) const
 {
-       os << "[sqrt ";
-       cell(0).writeNormal(os); 
-       os << "] ";
+       if (needbs_)
+               os << "\\";
+       os << char_;
+}
+
+
+bool MathCharInset::isRelOp() const
+{
+       return char_ == '=' || char_ == '<' || char_ == '>';
+}
+
+
+void MathCharInset::handleFont(MathTextCodes t)
+{
+       code_ = (code_ == t) ? LM_TC_VAR : t;
 }