@@ -12,17 +12,37 @@ import (
1212type RichEdit struct {
1313 WindowBase
1414 OnChanged func ()
15+ lastLen int
1516}
1617
1718func (re * RichEdit ) WndProc (msg uint32 , wParam , lParam uintptr ) uintptr {
19+ maybeEmitChange := func () {
20+ if re .OnChanged == nil || re .hwnd == 0 {
21+ return
22+ }
23+ cur := re .TextLength ()
24+ if re .lastLen != cur {
25+ re .lastLen = cur
26+ re .OnChanged ()
27+ }
28+ }
29+
1830 switch msg {
1931 case win .WM_COMMAND :
2032 switch win .HIWORD (uint32 (wParam )) {
2133 case win .EN_CHANGE :
22- if re .OnChanged != nil && lParam == uintptr (re .hwnd ) {
23- re .OnChanged ()
34+ if lParam == uintptr (re .hwnd ) {
35+ maybeEmitChange ()
36+ }
37+ case win .EN_UPDATE :
38+ if lParam == uintptr (re .hwnd ) {
39+ maybeEmitChange ()
2440 }
2541 }
42+ case win .WM_CHAR , win .WM_PASTE , win .WM_CUT , win .WM_CLEAR , win .WM_UNDO :
43+ ret := re .AsWindowBase ().WndProc (msg , wParam , lParam )
44+ maybeEmitChange ()
45+ return ret
2646 }
2747 return re .AsWindowBase ().WndProc (msg , wParam , lParam )
2848}
@@ -67,7 +87,10 @@ func (re *RichEdit) SetReadOnly(readOnly bool) {
6787
6888// NewRichEdit creates a new RichEdit, need bind to Dialog before use.
6989func NewRichEdit (idd uintptr ) * RichEdit {
70- return & RichEdit {WindowBase : WindowBase {idd : idd }}
90+ return & RichEdit {
91+ WindowBase : WindowBase {idd : idd , Subclassing : true },
92+ lastLen : - 1 ,
93+ }
7194}
7295
7396// BindNewRichEdit creates a new RichEdit and bind to target dlg.
@@ -76,4 +99,3 @@ func BindNewRichEdit(idd uintptr, dlg *Dialog) (*RichEdit, error) {
7699 err := dlg .BindWidgets (re )
77100 return re , err
78101}
79-
0 commit comments