]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/math_cursor.h
merge CursorItem and MathPos
[lyx.git] / src / mathed / math_cursor.h
index 978da16e401f66b4d4c0e481632dcf460d4e5b09..59d6a18e630bc7757d873f43586f1f11e69d4323 100644 (file)
 // -*- C++ -*-
-/*
- *  File:        math_cursor.h
- *  Purpose:     Declaration of interaction classes for mathed
- *  Author:      Alejandro Aguilar Sierra <asierra@servidor.unam.mx> 
- *  Created:     January 1996
- *  Description: MathCursor control all user interaction
+/**
+ * \file math_cursor.h
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
  *
- *  Dependencies: Xlib, XForms
+ * \author Alejandro Aguilar Sierra
+ * \author André Pönitz
  *
- *  Copyright: 1996, Alejandro Aguilar Sierra
- *
- *   You are free to use and modify this code under the terms of
- *   the GNU General Public Licence version 2 or later.
+ * Full author contact details are available in file CREDITS.
  */
 
 #ifndef MATH_CURSOR
 #define MATH_CURSOR
 
-#ifdef __GNUG__
-#pragma interface
-#endif
+#include "math_inset.h"
+#include "math_data.h"
+#include "math_iterator.h"
+
+#include <string>
 
-#include "math_defs.h"
 
-class MathInset;
-class MathAtom;
-class MathArrayInset;
-class MathFuncInset;
-class MathMatrixInset;
-class MathScriptInset;
-class MathSpaceInset;
 class InsetFormulaBase;
-class MathArray;
-class MathXArray;
-class Painter;
-class latexkeys;
+class BufferView;
+class PainterInfo;
+class MathUnknownInset;
 
-/// Description of a position 
-struct MathCursorPos {
-       /// inset
-       MathInset * par_;
-       /// cell index
-       unsigned int idx_;
-       /// cell position
-       unsigned int pos_;
-       /// returns cell corresponding to this position
-       MathArray & cell() const;
-       /// returns cell corresponding to this position
-       MathArray & cell(unsigned int idx) const;
-       /// returns xcell corresponding to this position
-       MathXArray & xcell() const;
-       /// returns xcell corresponding to this position
-       MathXArray & xcell(unsigned int idx) const;
-};
+/**
+
+[Have a look at math_inset.h first]
 
-/// 
-bool operator==(MathCursorPos const &, MathCursorPos const &);
-/// 
-bool operator<(MathCursorPos const &, MathCursorPos const &);
+The MathCursor is different from the kind of cursor used in the Outer
+World. It contains a stack of CursorSlice, each of which is made
+up of a inset pointer, an index and a position offset, marking a path from
+this formula's MathHullInset to the current position.
+
+*/
 
 
-/// This is the external interface of Math's subkernel
 class MathCursor {
 public:
+       /// short of anything else reasonable
+       typedef MathInset::size_type       size_type;
+       /// type for column numbers
+       typedef MathArray::difference_type difference_type;
+       /// type for cursor positions within a cell
+       typedef MathInset::pos_type        pos_type;
+       /// type for cell indices
+       typedef MathInset::idx_type        idx_type;
+       /// type for row numbers
+       typedef MathInset::row_type        row_type;
+       /// type for column numbers
+       typedef MathInset::col_type        col_type;
+
        ///
-       explicit MathCursor(InsetFormulaBase *);
-       ///
-       void insert(MathInset *);
-       ///
-       void insert(MathArray const &);
-       ///
-       void erase();
+       explicit MathCursor(InsetFormulaBase *, bool left);
        ///
-       void backspace();
+       ~MathCursor();
        ///
-       void home(bool sel = false);
+       void insert(MathAtom const &);
        ///
-       void end(bool sel = false);
+       void insert(MathArray const &);
        ///
+       void insert2(std::string const &);
+       ///
+       void paste(std::string const & data);
+       /// return false for empty math insets
+       bool erase();
+       /// return false for empty math insets
+       bool backspace();
+       /// called for LFUN_HOME etc
+       bool home(bool sel = false);
+       /// called for LFUN_END etc
+       bool end(bool sel = false);
+       /// called for LFUN_RIGHT and LFUN_RIGHTSEL
        bool right(bool sel = false);
-       ///
+       /// called for LFUN_LEFT etc
        bool left(bool sel = false);
-       ///
+       /// called for LFUN_UP etc
        bool up(bool sel = false);
-       ///
+       /// called for LFUN_DOWN etc
        bool down(bool sel = false);
        /// Put the cursor in the first position
        void first();
        /// Put the cursor in the last position
        void last();
-       /// moves cursor position one cell to the left
-       bool posLeft();
-       /// moves cursor position one cell to the right
-       bool posRight();
-       /// moves cursor index one cell to the left
-       bool idxLeft();
-       /// moves cursor index one cell to the right
-       bool idxRight();
-       /// moves position somehow up
-       bool goUp();
-       /// moves position somehow down
-       bool goDown();
-       ///
+       /// move to next cell in current inset
        void idxNext();
-       ///
+       /// move to previous cell in current inset
        void idxPrev();
        ///
        void plainErase();
        ///
-       void plainInsert(MathInset * p);
+       void plainInsert(MathAtom const &);
        ///
-       void niceInsert(MathInset * p);
+       void niceInsert(MathAtom const &);
        ///
-       void delLine();
-       /// This is in pixels from (maybe?) the top of inset
-       void setPos(int, int);
-       ///
-       void getPos(int & x, int & y);
-       ///
-       MathInset * par() const;
+       void niceInsert(std::string const &);
+
+       /// in pixels from top of screen
+       void setPos(int x, int y);
+       /// in pixels from top of screen
+       void getPos(int & x, int & y) const;
+       /// in pixels from left of screen
+       int targetX() const;
+       /// current inset
+       MathInset * inset() const;
        /// return the next enclosing grid inset and the cursor's index in it
-       MathArrayInset * enclosingArray(unsigned int &) const;
-       ///
-       InsetFormulaBase const * formula();
-       ///
-       unsigned int pos() const;
-       ///
-       unsigned int idx() const;
-       ///
-       unsigned int size() const;
-       ///
-       void interpret(string const &);
-       ///
-       void setSize(MathStyles);
-       ///
-       bool toggleLimits();
-       ///
-       // Macro mode methods
+       MathGridInset * enclosingGrid(idx_type & idx) const;
+       /// go up to enclosing grid
+       void popToEnclosingGrid();
+       /// go up to the hull inset
+       void popToEnclosingHull();
+       /// go up to the hull inset
+       void popToHere(MathInset const * p);
+       /// adjust anchor position after deletions/insertions
+       void adjust(pos_type from, difference_type diff);
+       ///
+       InsetFormulaBase * formula() const;
+       /// current offset in the current cell
+       pos_type pos() const;
+       /// current cell
+       idx_type idx() const;
+       /// size of current cell
+       size_type size() const;
+       ///
+       bool script(bool);
+       ///
+       bool interpret(char);
+       /// interpret name a name of a macro
        void macroModeClose();
-       ///
+       /// are we currently typing the name of a macro?
        bool inMacroMode() const;
-       
+       /// get access to the macro we are currently typing
+       MathUnknownInset * activeMacro();
+       /// get access to the macro we are currently typing
+       MathUnknownInset const * activeMacro() const;
+       /// are we currently typing '#1' or '#2' or...?
+       bool inMacroArgMode() const;
+       /// are we in math mode (1), text mode (-1) or unsure?
+       MathInset::mode_type currentMode() const;
+
        // Local selection methods
        ///
        bool selection() const;
@@ -150,52 +149,43 @@ public:
        void selCut();
        ///
        void selDel();
-       ///
-       void selPaste();
+       /// pastes n-th element of cut buffer
+       void selPaste(size_t n);
        ///
        void selHandle(bool);
        ///
        void selStart();
        ///
        void selClear();
-       ///
-       void drawSelection(Painter & pain) const;
-       ///
-       void handleFont(MathTextCodes t);
-       ///
-       void handleDelim(string const & l, string const & r);
-       ///
-       void handleNest(MathInset * p);
-       /// Splits cells and shifts right part to the next cell
-       void splitCell();
-       /// Splits line and insert new row of cell 
-       void breakLine();
-       ///
-       MathTextCodes getLastCode() const;
-       ///
-       void pullArg(bool goright);
+       /// clears or deletes selection depending on lyxrc setting
+       void selClearOrDel();
+       /// draws light-blue selection background
+       void drawSelection(PainterInfo & pi) const;
+       /// replace selected stuff with at, placing the former
+       // selection in given cell of atom
+       void handleNest(MathAtom const & at, int cell = 0);
+       /// remove this as soon as LyXFunc::getStatus is "localized"
+       std::string getLastCode() const { return "mathnormal"; }
        ///
        bool isInside(MathInset const *) const;
        ///
-       MathTextCodes nextCode() const;
-       ///
        char valign() const;
        ///
        char halign() const;
-       ///
-       unsigned int col() const;
-       ///
-       unsigned int row() const;
 
+       /// make sure cursor position is valid
+       void normalize();
+       /// mark current cursor trace for redraw
+       void touch();
        ///
-       MathStyles style() const;
-       /// Make sure cursor position is valid
-       void normalize() const;
-       
+       UpdatableInset * asHyperActiveInset() const;
+
+       /// enter a MathInset
+       void push(MathAtom & par);
        /// enter a MathInset from the front
-       void pushLeft(MathInset * par);
+       void pushLeft(MathAtom & par);
        /// enter a MathInset from the back
-       void pushRight(MathInset * par);
+       void pushRight(MathAtom & par);
        /// leave current MathInset to the left
        bool popLeft();
        /// leave current MathInset to the left
@@ -204,86 +194,112 @@ public:
        ///
        MathArray & array() const;
        ///
-       MathXArray & xarray() const;
+       bool hasPrevAtom() const;
        ///
-       MathAtom const * prevAtom() const;
+       bool hasNextAtom() const;
        ///
-       MathAtom * prevAtom();
+       MathAtom const & prevAtom() const;
        ///
-       MathAtom const * nextAtom() const;
+       MathAtom & prevAtom();
        ///
-       MathAtom * nextAtom();
+       MathAtom const & nextAtom() const;
+       ///
+       MathAtom & nextAtom();
 
        /// returns the selection
-       void getSelection(MathCursorPos &, MathCursorPos &) const;
+       void getSelection(CursorSlice &, CursorSlice &) const;
        /// returns the normalized anchor of the selection
-       MathCursorPos normalAnchor() const;
-
-       /// path of positions the cursor had to go if it were leving each inset
-       std::vector<MathCursorPos> Cursor_;
-       /// path of positions the anchor had to go if it were leving each inset
-       std::vector<MathCursorPos> Anchor_;
-
-       /// reference to the last item of the path
-       MathCursorPos & cursor();
-       ///
-       MathCursorPos const & cursor() const;
-
+       CursorSlice normalAnchor() const;
 
-       ///  
-       unsigned int last() const;
-       ///
-       MathInset * parInset(int i) const;
-       ///
-       MathMatrixInset * outerPar() const;
-       ///
+       /// reference to the last item of the path, i.e. "The Cursor"
+       CursorSlice & cursor();
+       /// reference to the last item of the path, i.e. "The Cursor"
+       CursorSlice const & cursor() const;
+       /// how deep are we nested?
+       unsigned depth() const;
+       /// describe the situation
+       std::string info() const;
+       /// dump selection information for debugging
        void seldump(char const * str) const;
-       ///
+       /// dump selection information for debugging
        void dump(char const * str) const;
+       /// moves on
+       void setSelection(MathIterator const & where, size_type n);
+       /// grab selection marked by anchor and current cursor
+       std::string grabSelection() const;
+       /// guess what
+       std::string grabAndEraseSelection();
+       ///
+       void insert(char c);
+       ///
+       void insert(std::string const & str);
+       /// lock/unlock inset
+       void insetToggle();
 
-       ///
-       void merge(MathArray const & arr);
-       ///
-       MathInset * nextInset() const;
-       ///
-       MathInset * prevInset() const;
-       ///
-       MathScriptInset * prevScriptInset() const;
-       ///
-       MathSpaceInset * prevSpaceInset() const;
+       /// hack for reveal codes
+       void markInsert();
+       void markErase();
+       /// injects content of a cell into parent
+       void pullArg();
+       /// split font inset etc
+       void handleFont(std::string const & font);
+       ///
+       DispatchResult
+       dispatch(FuncRequest const & cmd);
 private:
-       ///
-       string macroName() const;
-       ///
-       void insert(char, MathTextCodes t = LM_TC_MIN);
-       /// can we enter the inset? 
-       bool openable(MathInset *, bool selection) const;
-       /// can the setPos routine enter that inset?
-       MathInset * positionable(MathAtom *, int x, int y) const;
+       /// moves cursor index one cell to the left
+       bool idxLeft();
+       /// moves cursor index one cell to the right
+       bool idxRight();
+       /// moves cursor to end of last cell of current line
+       bool idxLineLast();
+       /// moves cursor position one cell to the left
+       bool posLeft();
+       /// moves cursor position one cell to the right
+       bool posRight();
+       /// moves position somehow up or down
+       bool goUpDown(bool up);
+       /// moves position closest to (x, y) in given box
+       bool bruteFind(int x, int y, int xlow, int xhigh, int ylow, int yhigh);
+       /// moves position closest to (x, y) in current cell
+       void bruteFind2(int x, int y);
+       /// are we in a nucleus of a script inset?
+       bool inNucleus() const;
+
+       /// erase the selected part and re-sets the cursor
+       void eraseSelection();
+
+       /// the name of the macro we are currently inputting
+       std::string macroName() const;
+       /// where in the curent cell does the macro name start?
+       difference_type macroNamePos() const;
+       /// can we enter the inset?
+       bool openable(MathAtom const &, bool selection) const;
        /// write access to cursor cell position
-       unsigned int & pos();
+       pos_type & pos();
        /// write access to cursor cell index
-       unsigned int & idx();
-       /// x-offset of current cell relative to par xo
-       unsigned int cellXOffset() const;
-       /// y-offset of current cell relative to par yo
-       unsigned int cellYOffset() const;
-       /// current x position relative to par xo
-       int xpos() const;
-       /// current y position relative to par yo
-       int ypos() const;
-       /// adjust position in current cell according to x. idx is not changed.
-       void gotoX(int x);
+       idx_type & idx();
 
-       ///
-       InsetFormulaBase * const formula_;
-       ///
-       MathTextCodes lastcode_;
+       /// path of positions the cursor had to go if it were leaving each inset
+       MathIterator Cursor_;
+       /// path of positions the anchor had to go if it were leaving each inset
+       mutable MathIterator Anchor_;
+       /// pointer to enclsing LyX inset
+       InsetFormulaBase * formula_;
        // Selection stuff
+       /// text code of last char entered
+       //MathTextCodes lastcode_;
+       /// do we allow autocorrection
+       bool autocorrect_;
        /// do we currently select
        bool selection_;
+       /// are we entering a macro name?
+       bool macromode_;
+       /// are we targeting a certain x coordinate, if so, which one?
+       int targetx_;
 };
 
 extern MathCursor * mathcursor;
+void releaseMathCursor(BufferView * bv);
 
 #endif