Opened 5 years ago

Closed 5 years ago

#15611 closed defect (fixed)

Inconsisten font encoding used in wxTextCtrl::LoadFile/SaveFile() functions

Reported by: michalbliznak Owned by: vadz
Priority: normal Milestone: 3.0.0
Component: wxMSW Version: 2.9.5
Keywords: Cc:
Blocked By: Blocking:
Patch: no


Inconsistent font encoding is used in wxTextCtrl::LoadFile() a wxTextCtrl::SaveFile() functions (at least) on Windows.

When a text content from a text control is saved to a file via wxTextCtrl::SaveText() function then local (current) font encoding is used (in my case it is WINDOWS-1250), but when loaded back to the text control via wxTextCtrl::LoadFile() function, then the non-ascii text is corrupted. IMHO it is caused by inconsistent calls to wxFFile::ReadAll() and wxFFile::Write() functions in textcmn.cpp where the first one uses default wxConvAuto values while the second one uses *wxConvCurrent. On my system, the system font encoding is really Windows-1250 while the default font encoding from fallback mode defined in wxConvAuto is ISO-8859-1. The workaround for this issue is to set proper fallback encoding, i.e. to call:

wxConvAuto::SetFallbackEncoding( wxLocale::GetSystemEncoding() );

Of course, the problem can be more serious and it could be burried somewhere deeper in wxWidgets...

Change History (2)

comment:1 Changed 5 years ago by vadz

  • Owner set to vadz
  • Status changed from new to accepted

The decision not to use the system default encoding is intentional, see the comment at source:wxWidgets/trunk/src/common/convauto.cpp#L27. But the problem you point out is still real and I think the only way to fix it is to remove wxConvCurrent from wxFFile::Write() call in DoSaveFile().

This would be a backwards incompatible change as the files would now be saved in UTF-8 instead of the current locale encoding but this is the right thing to do (UTF-8 ought to be used for the external data) and at least it will be consistent with DoLoadFile(), so even if we're so late in 3.0 release process, I'm still going to do it unless someone objects very soon.

comment:2 Changed 5 years ago by VZ

  • Resolution set to fixed
  • Status changed from accepted to closed

(In [75073]) Use UTF-8 for saving wxTextCtrl contents now.

This ensures that the files created by wxTextCtrl::SaveFile() can be read back
by wxTextCtrl::LoadFile() as previously the files were saved using the current
locale encoding but read back using Latin1 (after first trying, and failing,
to read them as UTF-8).

This is a backwards incompatible change but it ensures that wxTextCtrl can
load its own files and is also consistent with the use of UTF-8 by default in
other places. Finally, and perhaps most importantly, this ensures that the
file contents can always be saved, i.e. there is no risk of conversion errors
any more.

Closes #15611.

Note: See TracTickets for help on using tickets.