From 1ce1bd84eab0dce90ad0ecb4d37c6c2993c2512e Mon Sep 17 00:00:00 2001 From: Robin Dunn Date: Sat, 16 Apr 2005 19:41:33 +0000 Subject: [PATCH] Added Python methods to wxStyledTextCtrl that allow to get/set text in UTF8 in either unicode or ansi builds git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@33678 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- contrib/include/wx/stc/stc.h | 4 +- contrib/src/stc/stc.h.in | 3 + include/wx/stc/stc.h | 4 +- src/stc/stc.h.in | 3 + wxPython/contrib/stc/_stc_utf8_methods.py | 117 ++++++++++++++++++++++ wxPython/contrib/stc/gtk/stc.py | 84 ++++++++++++++++ wxPython/docs/CHANGES.txt | 77 +++++++++----- wxPython/setup.py | 4 +- 8 files changed, 269 insertions(+), 27 deletions(-) create mode 100644 wxPython/contrib/stc/_stc_utf8_methods.py diff --git a/contrib/include/wx/stc/stc.h b/contrib/include/wx/stc/stc.h index 7abde48d5e..04caf5a5da 100644 --- a/contrib/include/wx/stc/stc.h +++ b/contrib/include/wx/stc/stc.h @@ -2947,7 +2947,9 @@ public: // Append a string to the end of the document without changing the selection. void AppendTextRaw(const char* text); - +#ifdef SWIG + %pythoncode "_stc_utf8_methods.py" +#endif //---------------------------------------------------------------------- diff --git a/contrib/src/stc/stc.h.in b/contrib/src/stc/stc.h.in index f74a77b56c..c7d2bff632 100644 --- a/contrib/src/stc/stc.h.in +++ b/contrib/src/stc/stc.h.in @@ -259,6 +259,9 @@ public: void AppendTextRaw(const char* text); +#ifdef SWIG + %pythoncode "_stc_utf8_methods.py" +#endif //---------------------------------------------------------------------- diff --git a/include/wx/stc/stc.h b/include/wx/stc/stc.h index 7abde48d5e..04caf5a5da 100644 --- a/include/wx/stc/stc.h +++ b/include/wx/stc/stc.h @@ -2947,7 +2947,9 @@ public: // Append a string to the end of the document without changing the selection. void AppendTextRaw(const char* text); - +#ifdef SWIG + %pythoncode "_stc_utf8_methods.py" +#endif //---------------------------------------------------------------------- diff --git a/src/stc/stc.h.in b/src/stc/stc.h.in index f74a77b56c..c7d2bff632 100644 --- a/src/stc/stc.h.in +++ b/src/stc/stc.h.in @@ -259,6 +259,9 @@ public: void AppendTextRaw(const char* text); +#ifdef SWIG + %pythoncode "_stc_utf8_methods.py" +#endif //---------------------------------------------------------------------- diff --git a/wxPython/contrib/stc/_stc_utf8_methods.py b/wxPython/contrib/stc/_stc_utf8_methods.py new file mode 100644 index 0000000000..6a786897e7 --- /dev/null +++ b/wxPython/contrib/stc/_stc_utf8_methods.py @@ -0,0 +1,117 @@ + +def AddTextUTF8(self, text): + """ + Add UTF8 encoded text to the document at the current position. + Works 'natively' in a unicode build of wxPython, and will also work + in an ansi build if the UTF8 text is compatible with the current + encoding. + """ + if not wx.USE_UNICODE: + u = text.decode('utf-8') + text = u.encode(wx.GetDefaultPyEncoding()) + self.AddTextRaw(text) + + +def InsertTextUTF8(self, pos, text): + """ + Insert UTF8 encoded text at a position. Works 'natively' in a + unicode build of wxPython, and will also work in an ansi build if + the UTF8 text is compatible with the current encoding. + """ + if not wx.USE_UNICODE: + u = text.decode('utf-8') + text = u.encode(wx.GetDefaultPyEncoding()) + self.InsertTextRaw(pos, text) + + +def GetCurLineUTF8(self): + """ + Retrieve the UTF8 text of the line containing the caret, and also + the index of the caret on the line. In an ansi build of wxPython + the text retrieved from the document is assumed to be in the + current default encoding. + """ + text, pos = self.GetCurLineRaw() + if not wx.USE_UNICODE: + u = text.decode(wx.GetDefaultPyEncoding()) + text = u.encode('utf-8') + return text, pos + + +def GetLineUTF8(self, line): + """ + Retrieve the contents of a line as UTF8. In an ansi build of wxPython + the text retrieved from the document is assumed to be in the + current default encoding. + """ + text = self.GetLineRaw(line) + if not wx.USE_UNICODE: + u = text.decode(wx.GetDefaultPyEncoding()) + text = u.encode('utf-8') + return text + + +def GetSelectedTextUTF8(self): + """ + Retrieve the selected text as UTF8. In an ansi build of wxPython + the text retrieved from the document is assumed to be in the + current default encoding. + """ + text = self.GetSelectedTextRaw() + if not wx.USE_UNICODE: + u = text.decode(wx.GetDefaultPyEncoding()) + text = u.encode('utf-8') + return text + + +def GetTextRangeUTF8(self, startPos, endPos): + """ + Retrieve a range of text as UTF8. In an ansi build of wxPython + the text retrieved from the document is assumed to be in the + current default encoding. + """ + text = self.GetTextRangeRaw(startPos, endPos) + if not wx.USE_UNICODE: + u = text.decode(wx.GetDefaultPyEncoding()) + text = u.encode('utf-8') + return text + + +def SetTextUTF8(self, text): + """ + Replace the contents of the document with the UTF8 text given. + Works 'natively' in a unicode build of wxPython, and will also + work in an ansi build if the UTF8 text is compatible with the + current encoding. + """ + if not wx.USE_UNICODE: + u = text.decode('utf-8') + text = u.encode(wx.GetDefaultPyEncoding()) + self.SetTextRaw(text) + + +def GetTextUTF8(self): + """ + Retrieve all the text in the document as UTF8. In an ansi build + of wxPython the text retrieved from the document is assumed to be + in the current default encoding. + """ + text = self.GetTextRaw() + if not wx.USE_UNICODE: + u = text.decode(wx.GetDefaultPyEncoding()) + text = u.encode('utf-8') + return text + + +def AppendTextUTF8(self, text): + """ + Append a UTF8 string to the end of the document without changing + the selection. Works 'natively' in a unicode build of wxPython, + and will also work in an ansi build if the UTF8 text is compatible + with the current encoding. + """ + if not wx.USE_UNICODE: + u = text.decode('utf-8') + text = u.encode(wx.GetDefaultPyEncoding()) + self.AppendTextRaw(text) + diff --git a/wxPython/contrib/stc/gtk/stc.py b/wxPython/contrib/stc/gtk/stc.py index 9d8b41ca36..f76c8f990b 100644 --- a/wxPython/contrib/stc/gtk/stc.py +++ b/wxPython/contrib/stc/gtk/stc.py @@ -2884,6 +2884,90 @@ class StyledTextCtrl(_core.Control): """AppendTextRaw(self, char text)""" return _stc.StyledTextCtrl_AppendTextRaw(*args, **kwargs) + # These functions are inserted as methods in to the Python StyleTextCtrl class + + + + def AddTextUTF8(self, text): + """Add UTF8 encoded text to the document at the current position.""" + if not wx.USE_UNICODE: + u = text.decode('utf-8') + text = u.encode(wx.GetDefaultPyEncoding()) + self.AddTextRaw(text) + + + def InsertTextUTF8(self, pos, text): + """Insert UTF8 encoded text at a position.""" + if not wx.USE_UNICODE: + u = text.decode('utf-8') + text = u.encode(wx.GetDefaultPyEncoding()) + self.InsertTextRaw(pos, text) + + + def GetCurLineUTF8(self): + """ + Retrieve the text of the line containing the caret, and also the + index of the caret on the line. + """ + text, pos = self.GetCurLineRaw() + if not wx.USE_UNICODE: + u = text.decode(wx.GetDefaultPyEncoding()) + text = u.encode('utf-8') + return text, pos + + + def GetLineUTF8(self, line): + """Retrieve the contents of a line.""" + text = self.GetLineRaw(line) + if not wx.USE_UNICODE: + u = text.decode(wx.GetDefaultPyEncoding()) + text = u.encode('utf-8') + return text + + + def GetSelectedTextUTF8(self): + """Retrieve the selected text.""" + text = self.GetSelectedTextRaw() + if not wx.USE_UNICODE: + u = text.decode(wx.GetDefaultPyEncoding()) + text = u.encode('utf-8') + return text + + + def GetTextRangeUTF8(self, startPos, endPos): + """Retrieve a range of text.""" + text = self.GetTextRangeRaw(startPos, endPos) + if not wx.USE_UNICODE: + u = text.decode(wx.GetDefaultPyEncoding()) + text = u.encode('utf-8') + return text + + + def SetTextUTF8(self, text): + """Replace the contents of the document with the argument text.""" + if not wx.USE_UNICODE: + u = text.decode('utf-8') + text = u.encode(wx.GetDefaultPyEncoding()) + self.SetTextRaw(text) + + + def GetTextUTF8(self): + """Retrieve all the text in the document.""" + text = self.GetTextRaw() + if not wx.USE_UNICODE: + u = text.decode(wx.GetDefaultPyEncoding()) + text = u.encode('utf-8') + return text + + + def AppendTextUTF8(self, text): + """Append a string to the end of the document without changing the selection.""" + if not wx.USE_UNICODE: + u = text.decode('utf-8') + text = u.encode(wx.GetDefaultPyEncoding()) + self.AppendTextRaw(text) + + class StyledTextCtrlPtr(StyledTextCtrl): def __init__(self, this): diff --git a/wxPython/docs/CHANGES.txt b/wxPython/docs/CHANGES.txt index c1100cf786..9e27cd9a84 100644 --- a/wxPython/docs/CHANGES.txt +++ b/wxPython/docs/CHANGES.txt @@ -4,9 +4,38 @@ Recent Changes for wxPython 2.6.0.0 ------- -wxGTK: wx.StaticText can wrap text if the width is set to an explicit -value. +wxMSW: Fixed wx.TransientPopupWindow (and therefore wx.TipWindow) to +auto-dismiss when the mouse is clicked outside of the popup like it is +supposed to. +wxMSW: Fixed bug #1167891 wx.Notebook display problem with wx.NB_MULTILINE. + +wxMSW: Fixed bad cliping of hidden windows inside of wx.StaticBox. + +wxGTK: The configure flags for selecting GTK+ 1.2.x or 2.x has +changed slightly. It is now --with-gtk[=VERSION] where VERSION is +either '1', '2' or 'any'. The default is '2'. + +wx.stc.StyledTextCtrl: Added the following methods for alternate ways +to set and fetch text from the document buffer. They work similarly +to the existing methods of the same name, except that they don't go +through the same string/unicode <--> wxString conversions. The "Raw" +methods will do no conversions at all and in a unicode build of wxPython +the strings will be in the utf-8 encoding and in an ansi build no +assumption is made about the encoding. The "UTF8" functions will +attempt to always get/set utf-8 text, which will always be true in a +unicode build, and in an ansi build will also be true as long as the +utf-8 used is compatible with the current encoding. + + AddTextRaw AddTextUTF8 + InsertTextRaw InsertTextUTF8 + GetCurLineRaw GetCurLineUTF8 + GetLineRaw GetLineUTF8 + GetSelectedTextRaw GetSelectedTextUTF8 + GetTextRangeRaw GetTextRangeUTF8 + SetTextRaw SetTextUTF8 + GetTextRaw GetTextUTF8 + AppendTextRaw AppendTextUTF8 @@ -21,15 +50,15 @@ wx.grid.Grid: Fixed bug #1163384. Moved the code that handles activating the cell editors to a EVT_CHAR event handler. This is done so the character inserted into the editor will be the "cooked" char value (including accented or composed keys) rather than the raw code -provided by the EVT_KEY_DOWN event. +provided by the EVT_KEY_DOWN event. Added orient parameter to wx.MDIParentFrame.Tile() wxMSW: wxTextCtrl with wx.TE_RICH2 style now uses RichEdit 4.1 if -available. +available. Added GetCount, GetCountRGB, and GetCountColour methods to -wx.ImageHistogram. +wx.ImageHistogram. wxMSW: wx.Window.Refresh changed to explicitly refresh all children as well as the parent. Previously it was implicitly done because parents @@ -47,7 +76,7 @@ crash when using nVidia cards (patch 1155132) wx.lib.mixins.listctrl: Patches from Toni Brkic: * Bugfix for TextEditMixin when the view can't be scrolled - * Enhancement for ListCtrlAutoWidthMixin, allowing it to manage + * Enhancement for ListCtrlAutoWidthMixin, allowing it to manage the width of any column. wxMac: removal and reusing toolbar tools like the other platforms is @@ -94,7 +123,7 @@ Added wx.GetTopLevelWindows() function which returns a copy of the list of top-level windows that currently exist in the application. Updated docview library modules and sample apps from the ActiveGrid -folks. +folks. Added the ActiveGrid IDE as a sample application. @@ -116,7 +145,7 @@ consecutive (there may be intervening controls). Without this fix, an out-of-sync assert is generated when clicking on a radio button and then calling GetValue(). -Some XRC changes: +Some XRC changes: - Added 'icon' property to wxFrame and wxDialog - No longer ignores menu bitmaps on non-MSW platforms - Notebook page bitmaps are now supported @@ -192,7 +221,7 @@ focus (a slightly modified patch 1084592.) Added wx.EXEC_NODISABLE flag for wx.Execute, which will prevent all the app's windows being disabled while a synchronous child process is -running. +running. wxMSW: Much work to correct painting (or leaving transparent) of control backgrounds, properly using background themes on XP, etc. @@ -314,7 +343,7 @@ erased and repainted, giving a ghostly gradual-redraw effect). May be a temporary 'fix' until properly fixed before 2.6. wxMac: Toolbar is now more native looking with borderless toolbar -buttons. +buttons. wxMac: Switched wx.Bitmap to use newer Quartz object types and APIs internally. This results in faster display and better alpha support. @@ -336,7 +365,7 @@ Added modules from Peter Yared and Morgan Hua that implement the wx Doc/View framework in pure Python code. See wx.lib.docview for the base implementation and wx.lib.pydocview for Python-specific extensions. There are also a couple sample applications located in -samples/docview. +samples/docview. Added GetBitmap, GetIcon to wx.ImageList. @@ -362,10 +391,10 @@ indicate that the entire virtual area is covered simply add a style=wx.BUFFER_VIRTUAL_AREA parameter. wx.gizmos.TreeListCtrl: Add support for the EVT_TREE_ITEM_GETTOOLTIP -event. +event. Added Resize, SetRGBRect, Size, and GetOrFindMaskColour methods to -wx.Image. +wx.Image. Added wx.Rect.IsEmpty @@ -394,11 +423,11 @@ wxMac fix for not sending a native click to a control if it is not enabled (does an enable itself) Added wx.lib.ogl.DrawnShape, and fixed various little bugs in the new -OGL. +OGL. Added support to XRC and XRCed for the 3-state checkbox flags and also for wx.ToggleButton. Updated the generic window styles supported by -XRCed. +XRCed. It is now possible to create "stock" buttons. Basically this means that you only have to provide one of the stock IDs (and either an @@ -410,7 +439,7 @@ button will be used. For example, the following will result in a button with "Cancel" as the label and if run on wxGTK2 then there will also be an image of a red X:: - b = wx.Button(parent, wx.ID_CANCEL) + b = wx.Button(parent, wx.ID_CANCEL) Added wx.lib.ticker.Ticker class from Chris Mellon. @@ -532,7 +561,7 @@ PyPlot updates from Gordon Williams: - Removed FloatDCWrapper for conversion to ints and ints in arguments - Imported modules given leading underscore to name. - - Added Cursor Line Tracking and User Point Labels. + - Added Cursor Line Tracking and User Point Labels. - Demo for Cursor Line Tracking and Point Labels. - Size of plot preview frame adjusted to show page better. - Added helper functions PositionUserToScreen and @@ -558,13 +587,13 @@ Migrarion Guide for more information. Applied patch from Pim Van Heuven that modifies 4 files: - wxPython/demo/ListCtrl_edit.py (new demo) - wxPython/demo/Main.py (include new demo in demo app) - - wxPython/wx/lib/mixins/listctrl.py (several improvements to + - wxPython/wx/lib/mixins/listctrl.py (several improvements to TextEditMixin) - wxPython/wx/lib/wxpTag.py (some small fixes) Added (thanks to Kevin Ollivier!) wrappers for wx.WebKitCtrl for the OSX build. Other platforms will raise an exception if you try to use -it. +it. wxPython on OSX can now be built in Unicode mode, can support multiple version installs, and comes with an uninstaller script. @@ -601,10 +630,10 @@ Predominantly a bug-fix release. of property panels, since Reparent on wxMac is not implemented. * Add support for wxTAB_TRAVERSAL to the XRC handler for - wxScrolledWindow. + wxScrolledWindow. * Add support for all wxListBox styles to the XRC handler for - wxCheckListBox. + wxCheckListBox. * Fix for wx.Listbook.DeleteAllPages to really delete everything. @@ -637,7 +666,7 @@ The changes that implemented the incompatible wx.DC methods in 2.5.1.5 have been reverted. The wx.DC methods are now compatible with the 2.4 implemetation. In addition a set of renamed methods have been added that take wx.Point and/or wx.Size objects instead of individual -parameters. +parameters. Added wx.lib.mixins.listctrl.TextEditMixin, a mixin class that allows all columns of a wx.ListCtrl in report mode to be edited. @@ -818,7 +847,7 @@ menu for controlling this setting if you would like to experiment with it. Updated wx.lib.calendar with many fixes and enhancements from Joerg -"Adi" Sieker. +"Adi" Sieker. Added wx.Display and wx.VideoMode. @@ -2356,7 +2385,7 @@ compatibility with the current wxWindows. What's new in 0.5.0 ------------------- -Changed the import semantics from ``"from wxPython import *"`` to +Changed the import semantics from ``"from wxPython import *"`` to ``"from wxPython.wx import *"`` This is for people who are worried about namespace pollution, they can use "from wxPython import wx" and then prefix all the wxPython identifiers with "wx." diff --git a/wxPython/setup.py b/wxPython/setup.py index 9775964ee1..27b3d7c8e7 100755 --- a/wxPython/setup.py +++ b/wxPython/setup.py @@ -538,7 +538,9 @@ if BUILD_STC: swig_sources = run_swig(['stc.i'], location, GENDIR, PKGDIR, USE_SWIG, swig_force, swig_args + ['-I'+STC_H, '-I'+location], - [opj(STC_H, 'stc.h')] + swig_deps) + [opj(STC_H, 'stc.h'), + opj(location, "_stc_utf8_methods.py"), + ] + swig_deps) ext = Extension('_stc', swig_sources,