Class: Wx::ConvAuto

Inherits:
MBConv show all
Defined in:
lib/wx/doc/mbconv.rb

Overview

This class implements a Unicode to/from multibyte converter capable of automatically recognizing the encoding of the multibyte text on input.

The logic used is very simple: the class uses the BOM (byte order mark) if it's present and tries to interpret the input as UTF-8 otherwise. If this fails, the input is interpreted as being in the default multibyte encoding which can be specified in the constructor of a ConvAuto instance and, in turn, defaults to a fallback encoding if not explicitly given.

For the conversion from Unicode to multibyte, the same encoding as was previously used for multibyte to Unicode conversion is reused. If there had been no previous multibyte to Unicode conversion, UTF-8 is used by default. Notice that once the multibyte encoding is automatically detected, it doesn't change any more, i.e. it is entirely determined by the first use of wxConvAuto object in the multibyte-to-Unicode direction. However creating a copy of wxConvAuto object, either via the usual copy constructor or assignment operator, or using wxMBConv::Clone(), resets the automatically detected encoding so that the new copy will try to detect the encoding of the input on first use.

This class is used by default in wxWidgets classes and functions reading text from files such as wxFile, wxFFile, wxTextFile, wxFileConfig and various stream classes so the encoding set with its SetFallbackEncoding() method will affect how these classes treat input files. In particular, use this method to change the fall-back multibyte encoding used to interpret the contents of the files whose contents isn't valid UTF-8 or to disallow it completely.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(enc = Wx::FONTENCODING_DEFAULT) ⇒ ConvAuto

Constructs a new Wx::ConvAuto instance.

The object will try to detect the input of the given multibyte text automatically but if the automatic detection of Unicode encodings fails, the fall-back encoding enc will be used to interpret it as multibyte text.

The default value of enc, FONTENCODING_DEFAULT, means that the global default value (which can be set using #set_fallback_encoding) should be used. As with that method, passing Wx::FONTENCODING_MAX inhibits using this encoding completely so the input multibyte text will always be interpreted as UTF-8 in the absence of BOM and the conversion will fail if the input doesn't form valid UTF-8 sequence.

Another special value is FONTENCODING_SYSTEM which means to use the encoding currently used on the user system, i.e. the encoding returned by Locale#get_system_encoding. Any other encoding will be used as is, e.g. passing FONTENCODING_ISO8859_1 ensures that non-UTF-8 input will be treated as latin1.

Parameters:



54
# File 'lib/wx/doc/mbconv.rb', line 54

def initialize(enc=Wx::FONTENCODING_DEFAULT); end

Class Method Details

.disable_fallback_encodingObject

Disable the use of the fallback encoding: if the input doesn't have a BOM and is not valid UTF-8, the conversion will fail.



58
# File 'lib/wx/doc/mbconv.rb', line 58

def self.disable_fallback_encoding; end

.get_fallback_encodingWx::FontEncoding

Returns the encoding used by default by Wx::ConvAuto if no other encoding is explicitly specified in constructor.

By default, returns FONTENCODING_ISO8859_1 but can be changed using #set_fallback_encoding.

Returns:



64
# File 'lib/wx/doc/mbconv.rb', line 64

def self.get_fallback_encoding; end

.set_fallback_encoding(enc) ⇒ Object

Changes the encoding used by default by Wx::ConvAuto if no other encoding is explicitly specified in constructor.

The default value, which can be retrieved using #get_fallback_encoding, is FONTENCODING_ISO8859_1.

Special values of FONTENCODING_SYSTEM or FONTENCODING_MAX can be used for the enc parameter to use the encoding of the current user locale as fall back or not use any encoding for fall back at all, respectively (just as with the similar constructor parameter). However, FONTENCODING_DEFAULT can't be used here.

Parameters:



74
# File 'lib/wx/doc/mbconv.rb', line 74

def self.set_fallback_encoding(enc); end