]> git.saurik.com Git - wxWidgets.git/blame - src/common/textbuf.cpp
only set native window level, when not using a wrapped native window, see #14739
[wxWidgets.git] / src / common / textbuf.cpp
CommitLineData
a3a584a7
VZ
1///////////////////////////////////////////////////////////////////////////////
2// Name: src/common/textbuf.cpp
3// Purpose: implementation of wxTextBuffer class
4// Created: 14.11.01
5// Author: Morten Hanssen, Vadim Zeitlin
55034339 6// RCS-ID: $Id$
77ffb593 7// Copyright: (c) 1998-2001 wxWidgets team
65571936 8// Licence: wxWindows licence
a3a584a7
VZ
9///////////////////////////////////////////////////////////////////////////////
10
11// ============================================================================
12// headers
13// ============================================================================
14
a3a584a7
VZ
15#include "wx/wxprec.h"
16
17#ifdef __BORLANDC__
18 #pragma hdrstop
19#endif //__BORLANDC__
20
21#ifndef WX_PRECOMP
22 #include "wx/string.h"
23 #include "wx/intl.h"
24 #include "wx/log.h"
25#endif
26
27#include "wx/textbuf.h"
28
29// ============================================================================
30// wxTextBuffer class implementation
31// ============================================================================
32
33// ----------------------------------------------------------------------------
34// static methods (always compiled in)
35// ----------------------------------------------------------------------------
36
37// default type is the native one
a3a584a7
VZ
38
39const wxTextFileType wxTextBuffer::typeDefault =
bd362275 40#if defined(__WINDOWS__) || defined(__DOS__)
a3a584a7
VZ
41 wxTextFileType_Dos;
42#elif defined(__UNIX__)
43 wxTextFileType_Unix;
55034339 44#elif defined(__OS2__)
a3a584a7
VZ
45 wxTextFileType_Os2;
46#else
47 wxTextFileType_None;
48 #error "wxTextBuffer: unsupported platform."
49#endif
50
51const wxChar *wxTextBuffer::GetEOL(wxTextFileType type)
52{
53 switch ( type ) {
54 default:
55 wxFAIL_MSG(wxT("bad buffer type in wxTextBuffer::GetEOL."));
56 // fall through nevertheless - we must return something...
57
525d8583 58 case wxTextFileType_None: return wxEmptyString;
a3a584a7
VZ
59 case wxTextFileType_Unix: return wxT("\n");
60 case wxTextFileType_Dos: return wxT("\r\n");
61 case wxTextFileType_Mac: return wxT("\r");
62 }
63}
64
65wxString wxTextBuffer::Translate(const wxString& text, wxTextFileType type)
66{
67 // don't do anything if there is nothing to do
68 if ( type == wxTextFileType_None )
69 return text;
70
71 // nor if it is empty
4055ed82 72 if ( text.empty() )
a3a584a7
VZ
73 return text;
74
75 wxString eol = GetEOL(type), result;
76
77 // optimization: we know that the length of the new string will be about
769dd0ce 78 // the same as the length of the old one, so prealloc memory to avoid
a3a584a7
VZ
79 // unnecessary relocations
80 result.Alloc(text.Len());
81
82 wxChar chLast = 0;
769dd0ce 83 for ( wxString::const_iterator i = text.begin(); i != text.end(); ++i )
a3a584a7 84 {
769dd0ce 85 wxChar ch = *i;
a3a584a7 86 switch ( ch ) {
9a83f860 87 case wxT('\n'):
a3a584a7
VZ
88 // Dos/Unix line termination
89 result += eol;
90 chLast = 0;
91 break;
92
9a83f860
VZ
93 case wxT('\r'):
94 if ( chLast == wxT('\r') ) {
a3a584a7
VZ
95 // Mac empty line
96 result += eol;
97 }
98 else {
99 // just remember it: we don't know whether it is just "\r"
100 // or "\r\n" yet
9a83f860 101 chLast = wxT('\r');
a3a584a7
VZ
102 }
103 break;
104
105 default:
9a83f860 106 if ( chLast == wxT('\r') ) {
a3a584a7
VZ
107 // Mac line termination
108 result += eol;
109
110 // reset chLast to avoid inserting another eol before the
111 // next character
112 chLast = 0;
113 }
114
115 // add to the current line
116 result += ch;
117 }
118 }
119
120 if ( chLast ) {
121 // trailing '\r'
122 result += eol;
123 }
124
125 return result;
126}
127
128#if wxUSE_TEXTBUFFER
129
4ae776b7
VZ
130wxString wxTextBuffer::ms_eof;
131
a3a584a7
VZ
132// ----------------------------------------------------------------------------
133// ctors & dtor
134// ----------------------------------------------------------------------------
135
136wxTextBuffer::wxTextBuffer(const wxString& strBufferName)
137 : m_strBufferName(strBufferName)
138{
139 m_nCurLine = 0;
cb719f2e 140 m_isOpened = false;
a3a584a7
VZ
141}
142
5e233068
WS
143wxTextBuffer::~wxTextBuffer()
144{
145 // required here for Darwin
146}
147
a3a584a7
VZ
148// ----------------------------------------------------------------------------
149// buffer operations
150// ----------------------------------------------------------------------------
151
152bool wxTextBuffer::Exists() const
153{
154 return OnExists();
155}
156
157bool wxTextBuffer::Create(const wxString& strBufferName)
158{
159 m_strBufferName = strBufferName;
160
161 return Create();
162}
163
164bool wxTextBuffer::Create()
165{
166 // buffer name must be either given in ctor or in Create(const wxString&)
4055ed82 167 wxASSERT( !m_strBufferName.empty() );
a3a584a7
VZ
168
169 // if the buffer already exists do nothing
cb719f2e
WS
170 if ( Exists() ) return false;
171
a3a584a7 172 if ( !OnOpen(m_strBufferName, WriteAccess) )
cb719f2e 173 return false;
a3a584a7
VZ
174
175 OnClose();
cb719f2e 176 return true;
a3a584a7
VZ
177}
178
830f8f11 179bool wxTextBuffer::Open(const wxString& strBufferName, const wxMBConv& conv)
a3a584a7
VZ
180{
181 m_strBufferName = strBufferName;
182
183 return Open(conv);
184}
185
830f8f11 186bool wxTextBuffer::Open(const wxMBConv& conv)
a3a584a7
VZ
187{
188 // buffer name must be either given in ctor or in Open(const wxString&)
4055ed82 189 wxASSERT( !m_strBufferName.empty() );
a3a584a7
VZ
190
191 // open buffer in read-only mode
192 if ( !OnOpen(m_strBufferName, ReadAccess) )
cb719f2e 193 return false;
a3a584a7
VZ
194
195 // read buffer into memory
196 m_isOpened = OnRead(conv);
197
198 OnClose();
199
200 return m_isOpened;
201}
202
203// analyse some lines of the buffer trying to guess it's type.
204// if it fails, it assumes the native type for our platform.
205wxTextFileType wxTextBuffer::GuessType() const
206{
207 wxASSERT( IsOpened() );
208
209 // scan the buffer lines
210 size_t nUnix = 0, // number of '\n's alone
211 nDos = 0, // number of '\r\n'
212 nMac = 0; // number of '\r's
213
214 // we take MAX_LINES_SCAN in the beginning, middle and the end of buffer
215 #define MAX_LINES_SCAN (10)
b4a980f4 216 size_t nCount = m_aLines.GetCount() / 3,
a3a584a7
VZ
217 nScan = nCount > 3*MAX_LINES_SCAN ? MAX_LINES_SCAN : nCount / 3;
218
219 #define AnalyseLine(n) \
220 switch ( m_aTypes[n] ) { \
221 case wxTextFileType_Unix: nUnix++; break; \
222 case wxTextFileType_Dos: nDos++; break; \
223 case wxTextFileType_Mac: nMac++; break; \
9a83f860 224 default: wxFAIL_MSG(wxT("unknown line terminator")); \
a3a584a7
VZ
225 }
226
227 size_t n;
228 for ( n = 0; n < nScan; n++ ) // the beginning
229 AnalyseLine(n);
230 for ( n = (nCount - nScan)/2; n < (nCount + nScan)/2; n++ )
231 AnalyseLine(n);
232 for ( n = nCount - nScan; n < nCount; n++ )
233 AnalyseLine(n);
234
235 #undef AnalyseLine
236
237 // interpret the results (FIXME far from being even 50% fool proof)
238 if ( nScan > 0 && nDos + nUnix + nMac == 0 ) {
239 // no newlines at all
240 wxLogWarning(_("'%s' is probably a binary buffer."), m_strBufferName.c_str());
241 }
242 else {
243 #define GREATER_OF(t1, t2) n##t1 == n##t2 ? typeDefault \
244 : n##t1 > n##t2 \
245 ? wxTextFileType_##t1 \
246 : wxTextFileType_##t2
247
6d3d756a 248#if !defined(__WATCOMC__) || wxCHECK_WATCOM_VERSION(1,4)
a3a584a7
VZ
249 if ( nDos > nUnix )
250 return GREATER_OF(Dos, Mac);
251 else if ( nDos < nUnix )
252 return GREATER_OF(Unix, Mac);
253 else {
254 // nDos == nUnix
255 return nMac > nDos ? wxTextFileType_Mac : typeDefault;
256 }
257#endif // __WATCOMC__
258
259 #undef GREATER_OF
260 }
261
262 return typeDefault;
263}
264
265
266bool wxTextBuffer::Close()
267{
3ae00f5b 268 Clear();
cb719f2e 269 m_isOpened = false;
a3a584a7 270
cb719f2e 271 return true;
a3a584a7
VZ
272}
273
830f8f11 274bool wxTextBuffer::Write(wxTextFileType typeNew, const wxMBConv& conv)
a3a584a7
VZ
275{
276 return OnWrite(typeNew, conv);
277}
278
279#endif // wxUSE_TEXTBUFFER