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