coloredw.h

Go to the documentation of this file.
00001 #ifndef GENERATING_DOXYGEN_OUTPUT
00002 /*********************************************************************
00003 
00004    Copyright (C) 2001 Smaller Animals Software, Inc.
00005 
00006    This software is provided 'as-is', without any express or implied
00007    warranty.  In no event will the authors be held liable for any damages
00008    arising from the use of this software.
00009 
00010    Permission is granted to anyone to use this software for any purpose,
00011    including commercial applications, and to alter it and redistribute it
00012    freely, subject to the following restrictions:
00013 
00014    1. The origin of this software must not be misrepresented; you must not
00015      claim that you wrote the original software. If you use this software
00016      in a product, an acknowledgment in the product documentation would be
00017      appreciated but is not required.
00018 
00019    2. Altered source versions must be plainly marked as such, and must not be
00020      misrepresented as being the original software.
00021 
00022    3. This notice may not be removed or altered from any source distribution.
00023 
00024    http://!www.smalleranimals.com
00025    smallest@smalleranimals.com
00026 
00027    --------
00028 
00029    This code is based, in part, on:
00030 
00031       "Syntax Coloring Text Edit Window Class " - Randy More (9, May 1998)
00032       http://!www.codeguru.com/editctrl/syntax_coloring.shtml
00033 
00034    Though probably only 5% of the original code remains.
00035 
00036  *
00037  * \if NODOC
00038  * $Log: coloredw.h_v $
00039  * Revision 1.4  2004/02/16 20:38:27  mju
00040  * Remove legacy mfc declare_control macro.
00041  *
00042  * Revision 1.3  2003/10/03 19:58:19  linux32build!build
00043  * Doxygen
00044  *
00045  * Revision 1.2  2003/09/15 13:49:32  fileserver!dwilliss
00046  * Doxygen
00047  *
00048  * \endif
00049 **********************************************************************/
00050 
00051 #ifndef INC_MGUI_COLOREDW_H
00052 #define INC_MGUI_COLOREDW_H
00053 
00054 
00055 /////////////////////////////////////////////////////////////////////////////!
00056 //!Header for the ColorEditWnd class
00057 /////////////////////////////////////////////////////////////////////////////!
00058 
00059 
00060 #if !defined(_COLORIZED_VIEW_) && defined(WIN32_MFC)
00061 #define _COLORIZED_VIEW_
00062 
00063 #include <mgui/colorizr.h>
00064 #include <mgui/scripttx.h>
00065 #include <mgui/undofram.h>
00066 
00067 class ColorEditWnd : public CWnd
00068 {
00069 
00070 public:
00071 //DECLARE_CONTROL(ColorEditWnd, CWnd);
00072 DECLARE_DYNAMIC(ColorEditWnd);
00073 
00074    ColorEditWnd();
00075 
00076    ColorEditWnd(CWnd * parent, 
00077                const CRect & rect,           //!< where to put it
00078                int nID,                //!< control ID. this control will post messages to the parent
00079                                        //! (EN_CHANGE, EN_KILLFOCUS, EN_SETFOCUS, etc) with this ID
00080 
00081                int iTabSize,           //!< spaces per tab. all tabs are spaces
00082                int iFontSize,          //!< font size in 10ths of a point (uses CreatePointFont)
00083                const CString& csFontName);     //! font name. only use this with fixed width fonts!!!!!
00084 
00085    
00086    void Create(CWnd * parent, 
00087                const CRect & rect,           //!< where to put it
00088                int nID,                //!< control ID. this control will post messages to the parent
00089                                        //! (EN_CHANGE, EN_KILLFOCUS, EN_SETFOCUS, etc) with this ID
00090 
00091                int iTabSize,           //!< spaces per tab. all tabs are spaces
00092                int iFontSize,          //!< font size in 10ths of a point (uses CreatePointFont)
00093                const CString& csFontName);     //! font name. only use this with fixed width fonts!!!!!
00094 
00095    //! this is how we pick up the cut/copy/paste/undo/redo messages
00096    //!
00097    //! feel free to disable this, if you want to control these functions
00098    //! externally.
00099    void     UseKeyboardAccelerators(bool b)  {m_bUseKBAccel = b;}
00100 
00101 public:
00102    ///////////////////////////////////////////////////////////////////////////!
00103    //! clear all text
00104    void Reset();
00105 
00106    ///////////////////////////////////////////////////////////////////////////!
00107    //! clear and set text to pInText
00108    void LoadText(const char *pInText);
00109 
00110    //! get a single string copy of the contents
00111    void UnloadText(CString &pText) const;
00112 
00113    ///////////////////////////////////////////////////////////////////////////!
00114    //! number of text lines in the buffer
00115    _inline int GetLineCount()      {return GetTextSize();}
00116 
00117    ///////////////////////////////////////////////////////////////////////////!
00118    //! position of cursor and anchor. 
00119    //! if these are different positions, the characters between are selected.
00120    void  SetCursorPos(CPoint pos);
00121    void  SetAnchorPos(CPoint pos);
00122 
00123    _inline CPoint GetCursorPos()   {return CursorPos();}
00124    _inline CPoint GetAnchorPos()   {return AnchorPos();}
00125 
00126    //! another way to set selection
00127    void  SetSelection(int charPos1, int line1, int charPos2, int line2);
00128    void  ReplaceSelText(const char *pText);
00129 
00130    //! set this line as the top line in the window
00131    void ScrollToLine(int iLine);
00132 
00133    //! scroll the window to show the cursor
00134    void ScrollToCursor();
00135 
00136    //! get one line of text
00137    CString GetLine(int iLine) const      {return GetTextLine(iLine);}
00138 
00139    //! set a single line of text
00140    void SetLine(const char *pText, int iLine);
00141 
00142    ///////////////////////////////////////////////////////////////////////////!
00143    //! client rect of the text at a given line
00144    CRect GetLineRect(int iLine) const;
00145 
00146    //! client rect that encompasses both lines
00147    CRect GetLineRect(int iLine1, int iLine2) const;
00148 
00149    //! number of visible lines
00150    int VisibleLines() const;
00151 
00152    //! last and first visible line numbers
00153    int LastVisibleLine() const;
00154    int FirstVisibleLine() const;
00155 
00156    //! characters on screen
00157    int VisibleColumns() const;
00158 
00159    ///////////////////////////////////////////////////////////////////////////!
00160    //! the parent window can call these to do cut/paste/etc. functions.
00161 
00162    //! remove selection to the clipboard
00163    void DoCut();
00164 
00165    //! copy selection to the clipboard
00166    void DoCopy();
00167 
00168    //! paste clipboard. replaces any selection.
00169    void DoPaste();
00170 
00171    //! undo the last action
00172    void DoUndo();
00173 
00174    //! redo the last undo'd action
00175    void DoRedo();
00176 
00177    //! additioinal horizontal spacing
00178    void SetCharXSpacing(int i);
00179 
00180    //! additional vertical spacing
00181    void SetCharYSpacing(int i);
00182 
00183    //! use your own CColorizer class. 
00184    //! this can actually be changed on-the-fly, if you 
00185    //! need to use a different coloring scheme at will.
00186    void SetColorizer(CColorizer * pColorizer);
00187 
00188    //! basic editing stuff
00189    void SetBackgroundColor(COLORREF clr);
00190    void SetHighlightColor(COLORREF clr);
00191    void SetHighlightTextColor(COLORREF clr);
00192 
00193    //! make this a read-only control
00194    void SetReadOnly(bool b)      {m_bReadOnly = b;}
00195 
00196    void ClearUndoHistory();
00197 
00198 public:
00199    virtual ~ColorEditWnd();
00200 
00201 private:
00202    #ifndef GENERATING_DOXYGEN_OUTPUT
00203    //! cursor/anchor position 
00204    void ClipAnchorPosition(int &x, int &y);
00205    void ClipCursorPosition(int &x, int &y);
00206    void ClipAnchorPosition(CPoint &p);
00207    void ClipCursorPosition(CPoint &p);
00208    
00209    //! warning: these only affect the internal state. they do not update the UI!
00210    _inline void _SetAnchorPosX(int x)      {m_anchorPos.x = x;}
00211    _inline void _SetAnchorPosY(int y)      {m_anchorPos.y = y;}
00212    _inline void _SetAnchorPos(CPoint pt)   {m_anchorPos = pt;}
00213    _inline void _SetAnchorPos(int x, int y) {m_anchorPos.x = x; m_anchorPos.y = y;}
00214 
00215    //! warning: these only set the internal data. they do not update the UI!
00216    _inline void _SetCursorPosX(int x)      {m_cursorPos.x = x;}
00217    _inline void _SetCursorPosY(int y)      {m_cursorPos.y = y;}
00218    _inline void _SetCursorPos(CPoint pt)   {m_cursorPos = pt;}
00219    _inline void _SetCursorPos(int x, int y) {m_cursorPos.x= x; m_cursorPos.y = y;}
00220 
00221    _inline CPoint   CursorPos() const     {return m_cursorPos;}
00222    _inline int   CursorPosX() const       {return m_cursorPos.x;}
00223    _inline int   CursorPosY() const       {return m_cursorPos.y;}
00224 
00225    _inline CPoint   AnchorPos() const     {return m_anchorPos;}
00226    _inline int   AnchorPosX() const       {return m_anchorPos.x;}
00227    _inline int   AnchorPosY() const       {return m_anchorPos.y;}
00228 
00229    //! number of lines
00230    _inline int GetTextSize() const        {return m_text._GetSize();}
00231 
00232    //! get a single line
00233    _inline CString GetTextLine(int i) const       {return m_text._GetLine(i);}
00234 
00235    //! insert/delete
00236 
00237    //! insert a single line at pos 'i'. 
00238    void InsertTextLine(const CString &line, 
00239                         int i, 
00240                         bool bUpdate=TRUE,      //!< redraw and recolor. in general, TRUE
00241                         bool bNoUndo = false);  //! don't add to undo frame
00242 
00243    //! insert a string at the current cursor pos
00244    void InsertText(CString &pString);
00245 
00246    //! insert a single char at the current cursor pos
00247    void InsertChar(int pChar, bool pUpdate = TRUE);
00248 
00249    //! insert a string at the current cursor pos
00250    void InsertString(CString &pString, bool pUpdate = TRUE);
00251 
00252    //! insert a single tab (as spaces) at the current cursor pos
00253    void  InsertTab();
00254 
00255    //! set a single text line. replaces old contents
00256    void SetTextLine(const CString &line, int i, bool bNoUndo = false, bool pUpdate = TRUE);
00257 
00258    //! remove the line at 'i'
00259    void RemoveTextLine(int i, bool bNoUndo = false, bool pUpdate = TRUE);                     
00260 
00261    //! clear all lines
00262    void RemoveAllText();                           
00263 
00264    void  _ReplaceSelText(const char *pText);
00265 
00266    CString MakeTab(int iCursorXPos) const;
00267 
00268    //! for the backspace and delete keys
00269    void DeleteNextChar();
00270    void DeletePrevChar();
00271 
00272    //! handle enter key
00273    void DoEnter();
00274 
00275    //! selection
00276    bool IsSelected(int pX,int pY);
00277    bool Selected();
00278    void UpdateAnchor(bool pTestShift);
00279    void RemoveSelected();
00280 
00281    //! scroll
00282    void UpdateScrollSizes();
00283    
00284    CPoint GetDeviceScrollPosition() const;
00285    CPoint GetScrollPosition() const;
00286 
00287    //! output
00288    void DrawCurrentLine();
00289    void DrawLine(int pLine);
00290    CPoint DrawLine(CDC *pDC, const CString &line, int lineidx, int y);
00291    void DrawCursor(CDC * pDC, bool pShow);
00292    void DrawCursor(bool pShow);
00293 
00294    void ShiftDisplay(int dx, int dy);
00295 
00296 
00297    //! undo stuff
00298    void  StartUndoFrame();
00299    void  EndUndoFrame();
00300    void ClearUndoStack(undostack &inStack);
00301    void DoUndoRedo(undostack &inStack, undostack& outStack);
00302 
00303    void StartAction();
00304    void EndAction();
00305 
00306    //! post a windows message to the parent
00307    void  NotifyParent(UINT msg);
00308 
00309    //! state
00310    int m_iCharDX;
00311    int m_iCharDY;
00312    int m_iTabSize;
00313    HCURSOR m_cursor;
00314    CFont cfont;
00315 
00316    //! offsets for scrolling
00317    long        m_iFirstCharacter;
00318    long        m_iFirstLine;
00319 
00320    //!caret and cursor postion
00321    CPoint      m_anchorPos;
00322    CPoint      m_cursorPos;
00323 
00324 
00325    COLORREF m_backgroundColor;
00326    COLORREF m_hilightColor;
00327    COLORREF m_hilightTextColor;
00328 
00329    //! the text
00330    CScriptText    m_text;
00331 
00332    bool  m_bMaintainIndent;
00333 
00334    bool m_bIsSelected;
00335    bool m_bLBDown;
00336    bool m_bCaretOn;
00337    CPoint   m_startPointDrag;
00338    bool m_bUseKBAccel;
00339 
00340    //! undo/redo state
00341 
00342    //! current action frame. an action can be composed of one or more sub-actions.
00343    //! for a single keystroke, the undo frame holds one action: the keystroke.
00344    //! for complex actions, like a paste or a cut, the frame will contain all of the 
00345    //! actions required to perform the meta-action.
00346    CUndoFrame  m_currentUndoFrame;
00347 
00348    //! holds all of our actions - this might get big. feel free to clear it once in a while.
00349    undostack   m_undoStack;
00350 
00351    //! when the user starts undo-ing stuff, the undo's are placed here. 
00352    //! this is cleared when a new undo frame is created, ie. when a new edit action occurs.
00353    undostack   m_redoStack;
00354 
00355    UINT  m_ID; //!< dialog ID
00356 
00357    bool  m_bReadOnly;
00358 public:
00359    //! ClassWizard generated virtual function overrides
00360    //!{{AFX_VIRTUAL(ColorEditWnd)
00361    public:
00362    virtual BOOL PreTranslateMessage(MSG* pMsg);
00363    //!}}AFX_VIRTUAL
00364 
00365    //!{{AFX_MSG(ColorEditWnd)
00366    afx_msg BOOL OnEraseBkgnd(CDC* pDC);
00367    afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);
00368    afx_msg void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags);
00369    afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
00370    afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
00371    afx_msg void OnMouseMove(UINT nFlags, CPoint point);
00372    afx_msg void OnPaint();
00373    afx_msg void OnSize(UINT nType, int cx, int cy);
00374    afx_msg UINT OnGetDlgCode();
00375    afx_msg void OnVScroll( UINT nSBCode, UINT nPos, CScrollBar* pScrollBar );
00376    afx_msg void OnHScroll( UINT nSBCode, UINT nPos, CScrollBar* pScrollBar );
00377    afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
00378    afx_msg BOOL OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message);
00379    afx_msg void OnShowWindow(BOOL bShow, UINT nStatus);
00380    afx_msg BOOL OnMouseWheel( UINT nFlags, short zDelta, CPoint pt );
00381    afx_msg void OnContextMenu(CWnd* pWnd, CPoint point);
00382    afx_msg void OnUpdateCopy(CCmdUI* pCmdUI);
00383    afx_msg void OnUpdateCut(CCmdUI* pCmdUI);
00384    afx_msg void OnUpdatePaste(CCmdUI* pCmdUI);
00385    afx_msg void OnUpdateSelectall(CCmdUI* pCmdUI);
00386    afx_msg void OnUpdateUndo(CCmdUI* pCmdUI);
00387    afx_msg void OnUpdateRedo(CCmdUI* pCmdUI);
00388    afx_msg void OnCopy();
00389    afx_msg void OnCut();
00390    afx_msg void OnPaste();
00391    afx_msg void OnSelectall();
00392    afx_msg void OnUndo();
00393    afx_msg void OnRedo();
00394    afx_msg void OnKillFocus(CWnd* pNewWnd);
00395    afx_msg void OnSetFocus( CWnd* pOldWnd );
00396    afx_msg void OnLButtonDblClk( UINT, CPoint );
00397    //!}}AFX_MSG
00398    DECLARE_MESSAGE_MAP()
00399 #endif // GENERATING_DOXYGEN_OUTPUT
00400 };
00401 
00402 
00403 /////////////////////////////////////////////////////////////////////////////!
00404 #endif
00405 #endif // GENERATING_DOXYGEN_OUTPUT
00406 
00407 #endif  // INC_MGUI_COLOREDW_H

Generated on Tue Dec 14 13:18:15 2004 for TNTsdk by  doxygen 1.3.8-20040913