]> git.saurik.com Git - wxWidgets.git/blob - include/wx/textbuf.h
implemented subclassing in XRC
[wxWidgets.git] / include / wx / textbuf.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: wx/textbuf.h
3 // Purpose: class wxTextBuffer to work with text buffers of _small_ size
4 // (buffer is fully loaded in memory) and which understands CR/LF
5 // differences between platforms.
6 // Created: 14.11.01
7 // Author: Morten Hanssen, Vadim Zeitlin
8 // Copyright: (c) 1998-2001 wxWindows team
9 // Licence: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_TEXTBUFFER_H
13 #define _WX_TEXTBUFFER_H
14
15 #ifdef __GNUG__
16 #pragma interface "textbuf.h"
17 #endif
18
19 #include "wx/defs.h"
20
21 // ----------------------------------------------------------------------------
22 // constants
23 // ----------------------------------------------------------------------------
24
25 // the line termination type (kept wxTextFileType name for compability)
26 enum wxTextFileType
27 {
28 wxTextFileType_None, // incomplete (the last line of the file only)
29 wxTextFileType_Unix, // line is terminated with 'LF' = 0xA = 10 = '\n'
30 wxTextFileType_Dos, // 'CR' 'LF'
31 wxTextFileType_Mac, // 'CR' = 0xD = 13 = '\r'
32 wxTextFileType_Os2 // 'CR' 'LF'
33 };
34
35 #include "wx/string.h"
36
37 #if wxUSE_TEXTBUFFER
38
39 #include "wx/dynarray.h"
40
41 // ----------------------------------------------------------------------------
42 // wxTextBuffer
43 // ----------------------------------------------------------------------------
44
45 WX_DEFINE_EXPORTED_ARRAY(wxTextFileType, ArrayFileType);
46
47 #endif // wxUSE_TEXTBUFFER
48
49 class WXDLLEXPORT wxTextBuffer
50 {
51 public:
52 // constants and static functions
53 // default type for current platform (determined at compile time)
54 static const wxTextFileType typeDefault;
55
56 // this function returns a string which is identical to "text" passed in
57 // except that the line terminator characters are changed to correspond the
58 // given type. Called with the default argument, the function translates
59 // the string to the native format (Unix for Unix, DOS for Windows, ...).
60 static wxString Translate(const wxString& text,
61 wxTextFileType type = typeDefault);
62
63 // get the buffer termination string
64 static const wxChar *GetEOL(wxTextFileType type = typeDefault);
65
66 // the static methods of this class are compiled in even when
67 // !wxUSE_TEXTBUFFER because they are used by the library itself, but the
68 // rest can be left out
69 #if wxUSE_TEXTBUFFER
70
71 // buffer operations
72 // -----------------
73
74 // buffer exists?
75 bool Exists() const;
76
77 // create the buffer if it doesn't already exist
78 bool Create();
79
80 // same as Create() but with (another) buffer name
81 bool Create(const wxString& strBufferName);
82
83 // Open() also loads buffer in memory on success
84 bool Open(wxMBConv& conv = wxConvLibc);
85
86 // same as Open() but with (another) buffer name
87 bool Open(const wxString& strBufferName, wxMBConv& conv = wxConvLibc);
88
89 // closes the buffer and frees memory, losing all changes
90 bool Close();
91
92 // is buffer currently opened?
93 bool IsOpened() const { return m_isOpened; }
94
95 // accessors
96 // ---------
97
98 // get the number of lines in the buffer
99 size_t GetLineCount() const { return m_aLines.Count(); }
100
101 // the returned line may be modified (but don't add CR/LF at the end!)
102 wxString& GetLine(size_t n) const { return m_aLines[n]; }
103 wxString& operator[](size_t n) const { return m_aLines[n]; }
104
105 // the current line has meaning only when you're using
106 // GetFirstLine()/GetNextLine() functions, it doesn't get updated when
107 // you're using "direct access" i.e. GetLine()
108 size_t GetCurrentLine() const { return m_nCurLine; }
109 void GoToLine(size_t n) { m_nCurLine = n; }
110 bool Eof() const { return (m_aLines.Count() == 0 || m_nCurLine == m_aLines.Count() - 1); }
111
112 // these methods allow more "iterator-like" traversal of the list of
113 // lines, i.e. you may write something like:
114 // for ( str = GetFirstLine(); !Eof(); str = GetNextLine() ) { ... }
115
116 // NB: const is commented out because not all compilers understand
117 // 'mutable' keyword yet (m_nCurLine should be mutable)
118 wxString& GetFirstLine() /* const */ { return m_aLines[m_nCurLine = 0]; }
119 wxString& GetNextLine() /* const */ { return m_aLines[++m_nCurLine]; }
120 wxString& GetPrevLine() /* const */
121 { wxASSERT(m_nCurLine > 0); return m_aLines[--m_nCurLine]; }
122 wxString& GetLastLine() /* const */
123 { return m_aLines[m_nCurLine = m_aLines.Count() - 1]; }
124
125 // get the type of the line (see also GetEOL)
126 wxTextFileType GetLineType(size_t n) const { return m_aTypes[n]; }
127
128 // guess the type of buffer
129 wxTextFileType GuessType() const;
130
131 // get the name of the buffer
132 const wxChar *GetName() const { return m_strBufferName.c_str(); }
133
134 // add/remove lines
135 // ----------------
136
137 // add a line to the end
138 void AddLine(const wxString& str, wxTextFileType type = typeDefault)
139 { m_aLines.Add(str); m_aTypes.Add(type); }
140 // insert a line before the line number n
141 void InsertLine(const wxString& str,
142 size_t n,
143 wxTextFileType type = typeDefault)
144 { m_aLines.Insert(str, n); m_aTypes.Insert(type, n); }
145 // delete one line
146 void RemoveLine(size_t n) { m_aLines.RemoveAt(n); m_aTypes.RemoveAt(n); }
147
148 // change the buffer (default argument means "don't change type")
149 // possibly in another format
150 bool Write(wxTextFileType typeNew = wxTextFileType_None,
151 wxMBConv& conv = wxConvLibc);
152
153 // dtor
154 virtual ~wxTextBuffer();
155
156 protected:
157 // ctors
158 // -----
159
160 // default ctor, use Open(string)
161 wxTextBuffer() { }
162
163 // ctor from filename
164 wxTextBuffer(const wxString& strBufferName);
165
166 enum wxTextBufferOpenMode { ReadAccess, WriteAccess };
167
168 // Must implement these in derived classes.
169 virtual bool OnExists() const = 0;
170 virtual bool OnOpen(const wxString &strBufferName,
171 wxTextBufferOpenMode openmode) = 0;
172 virtual bool OnClose() = 0;
173 virtual bool OnRead(wxMBConv& conv) = 0;
174 virtual bool OnWrite(wxTextFileType typeNew,
175 wxMBConv& conv = wxConvLibc) = 0;
176
177 wxString m_strBufferName; // name of the buffer
178
179 private:
180 ArrayFileType m_aTypes; // type of each line
181 wxArrayString m_aLines; // lines of file
182
183 size_t m_nCurLine; // number of current line in the buffer
184
185 bool m_isOpened; // was the buffer successfully opened the last time?
186 #endif // wxUSE_TEXTBUFFER
187
188 // copy ctor/assignment operator not implemented
189 wxTextBuffer(const wxTextBuffer&);
190 wxTextBuffer& operator=(const wxTextBuffer&);
191 };
192
193 #endif // _WX_TEXTBUFFER_H
194