OSDN Git Service

Fix check for pointer/value by using bit-shift instead of a cast
[winmerge-jp/winmerge-jp.git] / Src / editlib / ccrystaltextbuffer.h
1 ////////////////////////////////////////////////////////////////////////////
2 //  File:       ccrystaltextbuffer.h
3 //  Version:    1.0.0.0
4 //  Created:    29-Dec-1998
5 //
6 //  Author:     Stcherbatchenko Andrei
7 //  E-mail:     windfall@gmx.de
8 //
9 //  Interface of the CCrystalTextBuffer class, a part of Crystal Edit -
10 //  syntax coloring text editor.
11 //
12 //  You are free to use or modify this code to the following restrictions:
13 //  - Acknowledge me somewhere in your about box, simple "Parts of code by.."
14 //  will be enough. If you can't (or don't want to), contact me personally.
15 //  - LEAVE THIS HEADER INTACT
16 ////////////////////////////////////////////////////////////////////////////
17
18 ////////////////////////////////////////////////////////////////////////////
19 //  19-Jul-99
20 //      Ferdinand Prantl:
21 //  +   FEATURE: see cpps ...
22 //
23 //  ... it's being edited very rapidly so sorry for non-commented
24 //        and maybe "ugly" code ...
25 ////////////////////////////////////////////////////////////////////////////
26
27 #if !defined(AFX_CCRYSTALTEXTBUFFER_H__AD7F2F49_6CB3_11D2_8C32_0080ADB86836__INCLUDED_)
28 #define AFX_CCRYSTALTEXTBUFFER_H__AD7F2F49_6CB3_11D2_8C32_0080ADB86836__INCLUDED_
29
30 #if _MSC_VER >= 1000
31 #pragma once
32 #endif // _MSC_VER >= 1000
33
34 #include "ccrystaltextview.h"
35
36 #ifndef __AFXTEMPL_H__
37 #pragma message("Include <afxtempl.h> in your stdafx.h to avoid this message")
38 #include <afxtempl.h>
39 #endif
40
41 #define UNDO_DESCRIP_BUF        32
42
43 enum LINEFLAGS
44 {
45   LF_BOOKMARK_FIRST = 0x00000001L,
46   LF_EXECUTION = 0x00010000L,
47   LF_BREAKPOINT = 0x00020000L,
48   LF_COMPILATION_ERROR = 0x00040000L,
49   LF_BOOKMARKS = 0x00080000L,
50   LF_INVALID_BREAKPOINT = 0x00100000L
51 };
52
53 #define LF_BOOKMARK(id)     (LF_BOOKMARK_FIRST << id)
54
55 enum CRLFSTYLE
56 {
57   CRLF_STYLE_AUTOMATIC = -1,
58   CRLF_STYLE_DOS = 0,
59   CRLF_STYLE_UNIX = 1,
60   CRLF_STYLE_MAC = 2
61 };
62
63 enum
64 {
65   CE_ACTION_UNKNOWN = 0,
66   CE_ACTION_PASTE = 1,
67   CE_ACTION_DELSEL = 2,
68   CE_ACTION_CUT = 3,
69   CE_ACTION_TYPING = 4,
70   CE_ACTION_BACKSPACE = 5,
71   CE_ACTION_INDENT = 6,
72   CE_ACTION_DRAGDROP = 7,
73   CE_ACTION_REPLACE = 8,
74   CE_ACTION_DELETE = 9,
75   CE_ACTION_AUTOINDENT = 10,
76   CE_ACTION_AUTOCOMPLETE = 11,
77   CE_ACTION_AUTOEXPAND = 12,
78   CE_ACTION_LOWERCASE = 13,
79   CE_ACTION_UPPERCASE = 14,
80   CE_ACTION_SWAPCASE = 15,
81   CE_ACTION_CAPITALIZE = 16,
82   CE_ACTION_SENTENCIZE = 17,
83   CE_ACTION_RECODE  = 18, 
84   CE_ACTION_SPELL = 19
85                          //  ...
86                          //  Expandable: user actions allowed
87 };
88
89
90 /////////////////////////////////////////////////////////////////////////////
91 // CUpdateContext class
92
93 class EDITPADC_CLASS CUpdateContext
94   {
95 public :
96     virtual void RecalcPoint (CPoint & ptPoint) = 0;
97   };
98
99
100 /////////////////////////////////////////////////////////////////////////////
101 // CCrystalTextBuffer command target
102
103 class EDITPADC_CLASS CCrystalTextBuffer : public CCmdTarget
104   {
105 public:
106     DECLARE_DYNCREATE (CCrystalTextBuffer)
107
108     int m_nSourceEncoding;
109     static int m_nDefaultEncoding;
110     DWORD m_dwCurrentRevisionNumber;
111     DWORD m_dwRevisionNumberOnSave;
112     BOOL IsTextBufferInitialized () const { return m_bInit; }
113
114 protected :
115     BOOL m_bInit;
116     BOOL m_bReadOnly;
117     BOOL m_bModified;
118     int m_nCRLFMode;
119     BOOL m_IgnoreEol;
120     BOOL m_bCreateBackupFile;
121     int m_nUndoBufSize;
122     BOOL m_bInsertTabs;
123     int  m_nTabSize;
124     int FindLineWithFlag (DWORD dwFlag);
125
126 protected :
127 #pragma pack(push, 1)
128     //  Nested class declarations
129     struct SLineInfo
130       {
131         TCHAR *m_pcLine;
132         int m_nLength, m_nMax;
133         int m_nEolChars; // # of eolchars
134         DWORD m_dwFlags;
135         DWORD m_dwRevisionNumber;
136
137         int FullLength() const { return m_nLength+m_nEolChars; }
138         int Length() const { return m_nLength; }
139
140         SLineInfo ()
141         {
142           memset (this, 0, sizeof (SLineInfo));
143         };
144       };
145
146     enum
147     {
148       UNDO_INSERT = 0x0001,
149       UNDO_BEGINGROUP = 0x0100
150     };
151
152     //  [JRT] Support For Descriptions On Undo/Redo Actions
153     struct SUndoRecord
154       {
155         DWORD m_dwFlags;
156         
157         CPoint m_ptStartPos, m_ptEndPos;  //  Block of text participating
158         int m_nAction;            //  For information only: action type
159         CDWordArray *m_paSavedRevisonNumbers;
160
161 private :
162         //  TCHAR   *m_pcText;
163         //  Since in most cases we have 1 character here,
164         //  we should invent a better way. Note: 2 * sizeof(WORD) <= sizeof(TCHAR*)
165         //
166         //  Here we will use the following trick: on Win32 platforms high-order word
167         //  of any pointer will be != 0. So we can store 1 character strings without
168         //  allocating memory.
169         //
170
171         union
172           {
173             TCHAR *m_pszText;     //  For cases when we have > 1 character strings
174             TCHAR m_szText[2];    //  For single-character strings
175           };
176
177 public :
178         SUndoRecord () // default constructor
179         {
180           memset (this, 0, sizeof (SUndoRecord));
181         }
182         SUndoRecord (const SUndoRecord & src) // copy constructor
183         {
184           memset (this, 0, sizeof (SUndoRecord));
185           (*this)=src;
186         }
187         SUndoRecord & operator=(const SUndoRecord & src) // copy assignment
188         {
189           m_dwFlags = src.m_dwFlags;
190           m_ptStartPos = src.m_ptStartPos;
191           m_ptEndPos = src.m_ptEndPos;
192           m_nAction = src.m_nAction;
193           SetText(src.GetText());
194           INT_PTR size = src.m_paSavedRevisonNumbers->GetSize();
195           m_paSavedRevisonNumbers = new CDWordArray();
196           m_paSavedRevisonNumbers->SetSize(size);
197           INT_PTR i;
198           for (i = 0; i < size; i++)
199             (*m_paSavedRevisonNumbers)[i] = (*src.m_paSavedRevisonNumbers)[i];
200           return *this;
201         }
202         ~SUndoRecord () // destructor
203         {
204           FreeText();
205           if (m_paSavedRevisonNumbers)
206                 delete m_paSavedRevisonNumbers;
207         }
208
209         void SetText (LPCTSTR pszText);
210         void FreeText ();
211
212         LPCTSTR GetText () const
213         {
214           // See the m_szText/m_pszText definition
215           // Check if m_pszText is a pointer by removing bits having
216           // possible char value
217           if (((INT_PTR)m_pszText >> 16) != 0)
218             return m_pszText;
219           return m_szText;
220         };
221       };
222
223 #pragma pack(pop)
224
225 class EDITPADC_CLASS CInsertContext : public CUpdateContext
226       {
227 public :
228         CPoint m_ptStart, m_ptEnd;
229         virtual void RecalcPoint (CPoint & ptPoint);
230       };
231
232 class EDITPADC_CLASS CDeleteContext : public CUpdateContext
233       {
234 public :
235         CPoint m_ptStart, m_ptEnd;
236         virtual void RecalcPoint (CPoint & ptPoint);
237       };
238
239     //  Lines of text
240     CArray < SLineInfo, SLineInfo & >m_aLines;
241
242     //  Undo
243     CArray < SUndoRecord, SUndoRecord & >m_aUndoBuf;
244     int m_nUndoPosition;
245     int m_nSyncPosition;
246     BOOL m_bUndoGroup, m_bUndoBeginGroup;
247
248     //BEGIN SW
249     /** Position where the last change was made. */
250     CPoint m_ptLastChange;
251     //END SW
252
253     //  Connected views
254     CList < CCrystalTextView *, CCrystalTextView * >m_lpViews;
255
256     //  Helper methods
257     void InsertLine (LPCTSTR pszLine, int nLength = -1, int nPosition = -1, int nCount = 1);
258     void AppendLine (int nLineIndex, LPCTSTR pszChars, int nLength = -1);
259     void MoveLine(int line1, int line2, int newline1);
260     void SetEmptyLine(int nPosition, int nCount = 1);
261
262     //  Implementation
263     BOOL InternalInsertText (CCrystalTextView * pSource, int nLine, int nPos, LPCTSTR pszText, int &nEndLine, int &nEndChar);
264     BOOL InternalDeleteText (CCrystalTextView * pSource, int nStartLine, int nStartPos, int nEndLine, int nEndPos);
265     CString StripTail (int i, int bytes);
266
267     //  [JRT] Support For Descriptions On Undo/Redo Actions
268     virtual void AddUndoRecord (BOOL bInsert, const CPoint & ptStartPos, const CPoint & ptEndPos,
269                                 LPCTSTR pszText, int nActionType = CE_ACTION_UNKNOWN, CDWordArray *paSavedRevisonNumbers = NULL);
270
271     //  Overridable: provide action description
272     virtual BOOL GetActionDescription (int nAction, CString & desc);
273
274     // Operations
275 public :
276     //  Construction/destruction code
277     CCrystalTextBuffer ();
278     ~CCrystalTextBuffer ();
279
280     //  Basic functions
281     BOOL InitNew (int nCrlfStyle = CRLF_STYLE_DOS);
282
283 // WinMerge has own routines for loading and saving
284 #ifdef CRYSTALEDIT_ENABLELOADER
285     BOOL LoadFromFile (LPCTSTR pszFileName, int nCrlfStyle = CRLF_STYLE_AUTOMATIC);
286 #endif
287 #ifdef CRYSTALEDIT_ENABLESAVER
288     BOOL SaveToFile(LPCTSTR pszFileName, int nCrlfStyle = CRLF_STYLE_AUTOMATIC, 
289     BOOL bClearModifiedFlag = TRUE);
290 #endif
291
292     void FreeAll ();
293
294     void ResetInit() { FreeAll(); InitNew(); } // reset new or used buffer
295
296     //  'Dirty' flag
297     virtual void SetModified (BOOL bModified = TRUE);
298     BOOL IsModified () const;
299
300     //  Connect/disconnect views
301     void AddView (CCrystalTextView * pView);
302     void RemoveView (CCrystalTextView * pView);
303
304     //  Text access functions
305     int GetLineCount () const;
306     int GetLineLength (int nLine) const;
307     int GetFullLineLength (int nLine) const; // including EOLs
308     LPCTSTR GetLineEol (int nLine) const;
309     BOOL ChangeLineEol (int nLine, LPCTSTR lpEOL);
310     LPTSTR GetLineChars (int nLine) const;
311     DWORD GetLineFlags (int nLine) const;
312     DWORD GetLineRevisionNumber (int nLine) const;
313     int GetLineWithFlag (DWORD dwFlag);
314     void SetLineFlag (int nLine, DWORD dwFlag, BOOL bSet, BOOL bRemoveFromPreviousLine = TRUE, BOOL bUpdate=TRUE);
315     void GetText (int nStartLine, int nStartChar, int nEndLine, int nEndChar, CString & text, LPCTSTR pszCRLF = NULL);
316     virtual void GetTextWithoutEmptys (int nStartLine, int nStartChar, int nEndLine, int nEndChar, CString &text, int nCrlfStyle =CRLF_STYLE_AUTOMATIC );
317
318     //  Attributes
319     int GetCRLFMode ();
320     void SetCRLFMode (int nCRLFMode);
321     /// Adjust all the lines in the buffer to the buffer default EOL Mode
322     virtual BOOL applyEOLMode();
323     LPCTSTR CCrystalTextBuffer::GetDefaultEol() const;
324     LPCTSTR CCrystalTextBuffer::GetStringEol(int nCRLFMode) const;
325     BOOL GetReadOnly () const;
326     void SetReadOnly (BOOL bReadOnly = TRUE);
327
328     void SetIgnoreEol(BOOL IgnoreEol) { m_IgnoreEol = IgnoreEol; }
329
330     //  Text modification functions
331     virtual BOOL InsertText (CCrystalTextView * pSource, int nLine, int nPos, LPCTSTR pszText, int &nEndLine, int &nEndChar, int nAction = CE_ACTION_UNKNOWN, BOOL bHistory =TRUE);
332     virtual BOOL DeleteText (CCrystalTextView * pSource, int nStartLine, int nStartPos, int nEndLine, int nEndPos, int nAction = CE_ACTION_UNKNOWN, BOOL bHistory =TRUE);
333
334     //  Undo/Redo
335     BOOL CanUndo ();
336     BOOL CanRedo ();
337     virtual BOOL Undo (CCrystalTextView * pSource, CPoint & ptCursorPos);
338     virtual BOOL Redo (CCrystalTextView * pSource, CPoint & ptCursorPos);
339
340     //  Undo grouping
341     virtual void BeginUndoGroup (BOOL bMergeWithPrevious = FALSE);
342     virtual void FlushUndoGroup (CCrystalTextView * pSource);
343
344     //BEGIN SW
345     /**
346     Returns the position where the last changes where made.
347     */
348     CPoint GetLastChangePos() const;
349     //END SW
350     void RestoreLastChangePos(CPoint pt);
351     void DeleteLine(int line, int nCount = 1);
352
353
354     //  Browse undo sequence
355     POSITION GetUndoActionCode (int & nAction, POSITION pos = NULL);
356     POSITION GetRedoActionCode (int & nAction, POSITION pos = NULL);
357     POSITION GetUndoDescription (CString & desc, POSITION pos = NULL);
358     POSITION GetRedoDescription (CString & desc, POSITION pos = NULL);
359
360     //  Notify all connected views about changes in name of file
361     CCrystalTextView::TextDefinition *RetypeViews (LPCTSTR lpszFileName);
362     //  Notify all connected views about changes in text
363     void UpdateViews (CCrystalTextView * pSource, CUpdateContext * pContext,
364                       DWORD dwUpdateFlags, int nLineIndex = -1);
365
366     // Tabs/space inserting
367     BOOL GetInsertTabs() const;
368     void SetInsertTabs(BOOL bInsertTabs);
369
370     // Tabbing
371     int  GetTabSize();
372     void SetTabSize(int nTabSize);
373
374     // More bookmarks
375     int FindNextBookmarkLine (int nCurrentLine = 0);
376     int FindPrevBookmarkLine (int nCurrentLine = 0);
377
378     BOOL IsMBSLead (int nLine, int nCol);
379     BOOL IsMBSTrail (int nLine, int nCol);
380
381     // Overrides
382     // ClassWizard generated virtual function overrides
383     //{{AFX_VIRTUAL(CCrystalTextBuffer)
384     //}}AFX_VIRTUAL
385
386
387     // Generated message map functions
388     //{{AFX_MSG(CCrystalTextBuffer)
389     //}}AFX_MSG
390
391     DECLARE_MESSAGE_MAP ()
392   };
393
394 #if ! (defined(CE_FROM_DLL) || defined(CE_DLL_BUILD))
395 #include "ccrystaltextbuffer.inl"
396 #endif
397
398 /////////////////////////////////////////////////////////////////////////////
399
400 //{{AFX_INSERT_LOCATION}}
401 // Microsoft Developer Studio will insert additional declarations immediately before the previous line.
402
403 #endif // !defined(AFX_CCRYSTALTEXTBUFFER_H__AD7F2F49_6CB3_11D2_8C32_0080ADB86836__INCLUDED_)