View Issue Details

IDProjectCategoryView StatusLast Update
0037835LazarusWidgetsetpublic2020-10-01 12:53
ReporterDavid Assigned ToDmitry Boyarintsev  
PrioritynormalSeverityminorReproducibilityalways
Status assignedResolutionopen 
PlatformMacOSOSSierra 
Summary0037835: GetTextLen does not work for some controls under Cocoa
DescriptionThe GetTextLen method (?) fails for controls that Apple dond't see as NSControl, this includes TMemo, TRichMemo and TListBox.
Attached is a patch that solves the problem for TMemo and TRichMemo (but sadely, not TListBox).
Steps To ReproduceWhen using Cocoa, call, for example, RichMemo1.getTextLen or Memo1.GetTextLen. Both will return zero even if there is considerable text in the control.
Additional InformationIn RichMemo, the GetTextLen is used quite extensively, it therefore prevents a number of important RichMemo methods working under Cocoa. The issue relates to the fact that not all (Lazarus) Controls are (Apple) NSControls. TMemo, TRichMemo and TListBox are NSScrollView but the existing code, in CocoaWSCommon.pas tests for a control being NSControl and it its not, returns an errror. I have added the ability to return valid data for TMemo and TRichMemo. Thanks to Skalogryz for pointing me in right direction - https://forum.lazarus.freepascal.org/index.php/topic,51596.msg378962.html

TagsNo tags attached.
Fixed in Revision
LazTarget-
WidgetsetCocoa
Attached Files

Activities

David

2020-09-29 10:07

reporter  

cocoa-GTL.patch (1,256 bytes)   
Index: lcl/interfaces/cocoa/cocoawscommon.pas
===================================================================
--- lcl/interfaces/cocoa/cocoawscommon.pas	(revision 63925)
+++ lcl/interfaces/cocoa/cocoawscommon.pas	(working copy)
@@ -1677,16 +1677,21 @@
   Result := AWinControl.HandleAllocated;
   if not Result then
     Exit;
-
   obj := NSObject(AWinControl.Handle);
-  Result := obj.isKindOfClass_(NSControl);
-  if not Result then Exit;
-
-  s := NSControl(obj).stringValue;
+  if obj.isKindOfClass_(NSControl) then
+     s := NSControl(obj).stringValue
+  else          // DRB, not all (Lazarus) controls are (Apple) NSControls.
+     // Note that TListBox is also a NSScrollView but still does not work here.
+     if obj.isKindOfClass_(NSScrollView) and        // maybe 'NSView' is better ??
+        (AWinControl.classnameis('TMemo') or AWinControl.ClassNameIs('TRichMemo')) then
+            S := NSTextView(NSScrollView(AWinControl.Handle).documentView).string_
+     else exit(False);
   if Assigned(s) then
     ALength := s.length
-  else
-    ALength := 0
+  else begin
+    ALength := 0;
+    Result := False;
+  end;
 end;
 
 class function TCocoaWSWinControl.GetClientBounds(const AWincontrol: TWinControl; var ARect: TRect): Boolean;
cocoa-GTL.patch (1,256 bytes)   

David

2020-09-29 10:11

reporter   ~0125953

I am not sure why this has come up under Lazarus CCR, it obviously does not belong there. Sorry !

Issue History

Date Modified Username Field Change
2020-09-29 10:07 David New Issue
2020-09-29 10:07 David File Added: cocoa-GTL.patch
2020-09-29 10:11 David Note Added: 0125953
2020-10-01 12:51 Bart Broersma Project Lazarus CCR => Lazarus
2020-10-01 12:52 Bart Broersma Category LCL => Widgetset
2020-10-01 12:52 Bart Broersma LazTarget => -
2020-10-01 12:52 Bart Broersma Widgetset Cocoa => Cocoa
2020-10-01 12:53 Bart Broersma Assigned To => Dmitry Boyarintsev
2020-10-01 12:53 Bart Broersma Status new => assigned