| 1 | //kt -- Modified with SourceScanner on 8/7/2007
 | 
|---|
| 2 | unit uAccessibleRichEdit;
 | 
|---|
| 3 | 
 | 
|---|
| 4 | interface
 | 
|---|
| 5 | 
 | 
|---|
| 6 | uses
 | 
|---|
| 7 |   ComObj, ActiveX, AxCtrls, Classes, CPRSChart_TLB, StdVcl, Accessibility_TLB,
 | 
|---|
| 8 |   ORCtrls, Variants;
 | 
|---|
| 9 | 
 | 
|---|
| 10 | type
 | 
|---|
| 11 |   TChildType = (ctInvalid, ctNoChild, ctChild);
 | 
|---|
| 12 | 
 | 
|---|
| 13 |   TAccessibleRichEdit = class(TAutoObject, IAccessibleRichEdit, IAccessible)
 | 
|---|
| 14 |   private
 | 
|---|
| 15 |     FDefaultObject: IAccessible;
 | 
|---|
| 16 |     FDefaultObjectLoaded: boolean;
 | 
|---|
| 17 |     FControl: TCaptionRichEdit;
 | 
|---|
| 18 |     function GetDefaultObject: IAccessible;
 | 
|---|
| 19 |   protected {IAccessible}
 | 
|---|
| 20 |     function accHitTest(xLeft, yTop: Integer): OleVariant; safecall;
 | 
|---|
| 21 |     function accNavigate(navDir: Integer; varStart: OleVariant): OleVariant;
 | 
|---|
| 22 |       safecall;
 | 
|---|
| 23 |     function Get_accChild(varChild: OleVariant): IDispatch; safecall;
 | 
|---|
| 24 |     function Get_accChildCount: Integer; safecall;
 | 
|---|
| 25 |     function Get_accDefaultAction(varChild: OleVariant): WideString; safecall;
 | 
|---|
| 26 |     function Get_accDescription(varChild: OleVariant): WideString; safecall;
 | 
|---|
| 27 |     function Get_accFocus: OleVariant; safecall;
 | 
|---|
| 28 |     function Get_accHelp(varChild: OleVariant): WideString; safecall;
 | 
|---|
| 29 |     function Get_accHelpTopic(out pszHelpFile: WideString;
 | 
|---|
| 30 |       varChild: OleVariant): Integer; safecall;
 | 
|---|
| 31 |     function Get_accKeyboardShortcut(varChild: OleVariant): WideString;
 | 
|---|
| 32 |       safecall;
 | 
|---|
| 33 |     function Get_accName(varChild: OleVariant): WideString; safecall;
 | 
|---|
| 34 |     function Get_accParent: IDispatch; safecall;
 | 
|---|
| 35 |     function Get_accRole(varChild: OleVariant): OleVariant; safecall;
 | 
|---|
| 36 |     function Get_accSelection: OleVariant; safecall;
 | 
|---|
| 37 |     function Get_accState(varChild: OleVariant): OleVariant; safecall;
 | 
|---|
| 38 |     function Get_accValue(varChild: OleVariant): WideString; safecall;
 | 
|---|
| 39 |     procedure accDoDefaultAction(varChild: OleVariant); safecall;
 | 
|---|
| 40 |     procedure accLocation(out pxLeft, pyTop, pcxWidth, pcyHeight: Integer;
 | 
|---|
| 41 |       varChild: OleVariant); safecall;
 | 
|---|
| 42 |     procedure accSelect(flagsSelect: Integer; varChild: OleVariant); safecall;
 | 
|---|
| 43 |     procedure Set_accName(varChild: OleVariant; const pszName: WideString);
 | 
|---|
| 44 |       safecall;
 | 
|---|
| 45 |     procedure Set_accValue(varChild: OleVariant; const pszValue: WideString);
 | 
|---|
| 46 |       safecall;
 | 
|---|
| 47 |   protected
 | 
|---|
| 48 |     property DefaultObject: IAccessible read GetDefaultObject write FDefaultObject;
 | 
|---|
| 49 |   public
 | 
|---|
| 50 |     property Control: TCaptionRichEdit read FControl write FControl;
 | 
|---|
| 51 |     function ChildType( varChild: OleVariant): TChildType;
 | 
|---|
| 52 |     class procedure WrapControl( Control: TCaptionRichEdit);
 | 
|---|
| 53 |     class procedure UnwrapControl( Control: TCaptionRichEdit);
 | 
|---|
| 54 |   public {but it wouldn't be in a perfect world}
 | 
|---|
| 55 |     function SafeCallException(ExceptObject: TObject; ExceptAddr: Pointer): HResult; override;
 | 
|---|
| 56 |   end;
 | 
|---|
| 57 | 
 | 
|---|
| 58 | implementation
 | 
|---|
| 59 | 
 | 
|---|
| 60 | uses uComServ, SysUtils, uAccessAPI, Windows, Controls;
 | 
|---|
| 61 | 
 | 
|---|
| 62 | var
 | 
|---|
| 63 |   UserIsRestricted: boolean = False;
 | 
|---|
| 64 | 
 | 
|---|
| 65 | function TAccessibleRichEdit.accHitTest(xLeft,
 | 
|---|
| 66 |   yTop: Integer): OleVariant;
 | 
|---|
| 67 | begin
 | 
|---|
| 68 |   result := Null;
 | 
|---|
| 69 |   if Assigned(DefaultObject) then
 | 
|---|
| 70 |     result := DefaultObject.accHitTest(xLeft,yTop);
 | 
|---|
| 71 | end;
 | 
|---|
| 72 | 
 | 
|---|
| 73 | function TAccessibleRichEdit.accNavigate(navDir: Integer;
 | 
|---|
| 74 |   varStart: OleVariant): OleVariant;
 | 
|---|
| 75 | begin
 | 
|---|
| 76 |   result := Null;
 | 
|---|
| 77 |   if Assigned(DefaultObject) then
 | 
|---|
| 78 |     result := DefaultObject.accNavigate(navDir, varStart);
 | 
|---|
| 79 | end;
 | 
|---|
| 80 | 
 | 
|---|
| 81 | function TAccessibleRichEdit.Get_accChild(
 | 
|---|
| 82 |   varChild: OleVariant): IDispatch;
 | 
|---|
| 83 | begin
 | 
|---|
| 84 |   result := nil;
 | 
|---|
| 85 |   if Assigned(DefaultObject) then
 | 
|---|
| 86 |     result := DefaultObject.Get_accChild(varChild);
 | 
|---|
| 87 | end;
 | 
|---|
| 88 | 
 | 
|---|
| 89 | function TAccessibleRichEdit.Get_accChildCount: Integer;
 | 
|---|
| 90 | begin
 | 
|---|
| 91 |   result := 0;
 | 
|---|
| 92 |   if Assigned(DefaultObject) then
 | 
|---|
| 93 |     result := DefaultObject.Get_accChildCount;
 | 
|---|
| 94 | end;
 | 
|---|
| 95 | 
 | 
|---|
| 96 | function TAccessibleRichEdit.Get_accDefaultAction(
 | 
|---|
| 97 |   varChild: OleVariant): WideString;
 | 
|---|
| 98 | begin
 | 
|---|
| 99 |   result := '';
 | 
|---|
| 100 |   if Assigned(DefaultObject) then
 | 
|---|
| 101 |     result := DefaultObject.Get_accDefaultAction(varChild);
 | 
|---|
| 102 | end;
 | 
|---|
| 103 | 
 | 
|---|
| 104 | function TAccessibleRichEdit.Get_accDescription(
 | 
|---|
| 105 |   varChild: OleVariant): WideString;
 | 
|---|
| 106 | begin
 | 
|---|
| 107 |   result := '';
 | 
|---|
| 108 |   if Assigned(DefaultObject) then
 | 
|---|
| 109 |     result := DefaultObject.Get_accDescription(varChild);
 | 
|---|
| 110 | end;
 | 
|---|
| 111 | 
 | 
|---|
| 112 | function TAccessibleRichEdit.Get_accFocus: OleVariant;
 | 
|---|
| 113 | begin
 | 
|---|
| 114 |   result := NULL;
 | 
|---|
| 115 |   if Assigned(DefaultObject) then
 | 
|---|
| 116 |     result := DefaultObject.Get_accFocus;
 | 
|---|
| 117 | end;
 | 
|---|
| 118 | 
 | 
|---|
| 119 | function TAccessibleRichEdit.Get_accHelp(
 | 
|---|
| 120 |   varChild: OleVariant): WideString;
 | 
|---|
| 121 | begin
 | 
|---|
| 122 |   result := '';
 | 
|---|
| 123 |   if Assigned(DefaultObject) then
 | 
|---|
| 124 |     result := DefaultObject.Get_accHelp(varChild);
 | 
|---|
| 125 | end;
 | 
|---|
| 126 | 
 | 
|---|
| 127 | function TAccessibleRichEdit.Get_accHelpTopic(
 | 
|---|
| 128 |   out pszHelpFile: WideString; varChild: OleVariant): Integer;
 | 
|---|
| 129 | begin
 | 
|---|
| 130 |   result := 0;
 | 
|---|
| 131 |   if Assigned(DefaultObject) then
 | 
|---|
| 132 |     result := DefaultObject.Get_accHelpTopic(pszHelpFile, varChild);
 | 
|---|
| 133 | end;
 | 
|---|
| 134 | 
 | 
|---|
| 135 | function TAccessibleRichEdit.Get_accKeyboardShortcut(
 | 
|---|
| 136 |   varChild: OleVariant): WideString;
 | 
|---|
| 137 | begin
 | 
|---|
| 138 |   result := '';
 | 
|---|
| 139 |   if Assigned(DefaultObject) then
 | 
|---|
| 140 |     result := DefaultObject.Get_accKeyboardShortcut(varChild);
 | 
|---|
| 141 | end;
 | 
|---|
| 142 | 
 | 
|---|
| 143 | function TAccessibleRichEdit.Get_accName(
 | 
|---|
| 144 |   varChild: OleVariant): WideString;
 | 
|---|
| 145 | begin
 | 
|---|
| 146 |   result := '';
 | 
|---|
| 147 |   if ChildType(varChild) = ctNoChild then
 | 
|---|
| 148 |   begin
 | 
|---|
| 149 |     if Assigned(FControl) then
 | 
|---|
| 150 |       result := FControl.Caption;
 | 
|---|
| 151 |   end
 | 
|---|
| 152 |   else if Assigned(DefaultObject) then
 | 
|---|
| 153 |     result := DefaultObject.Get_accName(varChild);
 | 
|---|
| 154 | end;
 | 
|---|
| 155 | 
 | 
|---|
| 156 | function TAccessibleRichEdit.Get_accParent: IDispatch;
 | 
|---|
| 157 | begin
 | 
|---|
| 158 |   result := nil;
 | 
|---|
| 159 |   if Assigned(DefaultObject) then
 | 
|---|
| 160 |     result := DefaultObject.Get_accParent;
 | 
|---|
| 161 | end;
 | 
|---|
| 162 | 
 | 
|---|
| 163 | function TAccessibleRichEdit.Get_accRole(
 | 
|---|
| 164 |   varChild: OleVariant): OleVariant;
 | 
|---|
| 165 | begin
 | 
|---|
| 166 |   if ChildType(varChild) = ctNoChild then
 | 
|---|
| 167 |     result := ROLE_SYSTEM_TEXT
 | 
|---|
| 168 |   else
 | 
|---|
| 169 |     result := ROLE_SYSTEM_CLIENT;
 | 
|---|
| 170 | end;
 | 
|---|
| 171 | 
 | 
|---|
| 172 | function TAccessibleRichEdit.Get_accSelection: OleVariant;
 | 
|---|
| 173 | begin
 | 
|---|
| 174 |   result := NULL;
 | 
|---|
| 175 |   if Assigned(DefaultObject) then
 | 
|---|
| 176 |     result := DefaultObject.Get_accSelection;
 | 
|---|
| 177 | end;
 | 
|---|
| 178 | 
 | 
|---|
| 179 | function TAccessibleRichEdit.Get_accState(
 | 
|---|
| 180 |   varChild: OleVariant): OleVariant;
 | 
|---|
| 181 | begin
 | 
|---|
| 182 |   result := NULL;
 | 
|---|
| 183 |   if Assigned(DefaultObject) then
 | 
|---|
| 184 |     result := DefaultObject.Get_accState(varChild);
 | 
|---|
| 185 | end;
 | 
|---|
| 186 | 
 | 
|---|
| 187 | function TAccessibleRichEdit.Get_accValue(
 | 
|---|
| 188 |   varChild: OleVariant): WideString;
 | 
|---|
| 189 | begin
 | 
|---|
| 190 |   //This is the crux of the issue: RichEdit controls return what should be the
 | 
|---|
| 191 |   //Value as the Name.
 | 
|---|
| 192 |   result := '';
 | 
|---|
| 193 |   if Assigned(DefaultObject) then
 | 
|---|
| 194 |     result := DefaultObject.Get_accName(varChild);
 | 
|---|
| 195 | end;
 | 
|---|
| 196 | 
 | 
|---|
| 197 | procedure TAccessibleRichEdit.accDoDefaultAction(varChild: OleVariant);
 | 
|---|
| 198 | begin
 | 
|---|
| 199 |   if Assigned(DefaultObject) then
 | 
|---|
| 200 |     DefaultObject.accDoDefaultAction(varChild);
 | 
|---|
| 201 | end;
 | 
|---|
| 202 | 
 | 
|---|
| 203 | procedure TAccessibleRichEdit.accLocation(out pxLeft, pyTop, pcxWidth,
 | 
|---|
| 204 |   pcyHeight: Integer; varChild: OleVariant);
 | 
|---|
| 205 | begin
 | 
|---|
| 206 |   if Assigned(DefaultObject) then
 | 
|---|
| 207 |     DefaultObject.accLocation(pxLeft,pyTop,pcxWidth,pcyHeight,VarChild);
 | 
|---|
| 208 | end;
 | 
|---|
| 209 | 
 | 
|---|
| 210 | procedure TAccessibleRichEdit.accSelect(flagsSelect: Integer;
 | 
|---|
| 211 |   varChild: OleVariant);
 | 
|---|
| 212 | begin
 | 
|---|
| 213 |   if Assigned(DefaultObject) then
 | 
|---|
| 214 |     DefaultObject.accSelect(flagsSelect, varChild);
 | 
|---|
| 215 | end;
 | 
|---|
| 216 | 
 | 
|---|
| 217 | procedure TAccessibleRichEdit.Set_accName(varChild: OleVariant;
 | 
|---|
| 218 |   const pszName: WideString);
 | 
|---|
| 219 | begin
 | 
|---|
| 220 |   if Assigned(DefaultObject) then
 | 
|---|
| 221 |     DefaultObject.Set_accName(varChild, pszName);
 | 
|---|
| 222 | end;
 | 
|---|
| 223 | 
 | 
|---|
| 224 | procedure TAccessibleRichEdit.Set_accValue(varChild: OleVariant;
 | 
|---|
| 225 |   const pszValue: WideString);
 | 
|---|
| 226 | begin
 | 
|---|
| 227 |   if Assigned(DefaultObject) then
 | 
|---|
| 228 |     DefaultObject.Set_accValue(varChild, pszValue);
 | 
|---|
| 229 | end;
 | 
|---|
| 230 | 
 | 
|---|
| 231 | function TAccessibleRichEdit.ChildType(varChild: OleVariant): TChildType;
 | 
|---|
| 232 | begin
 | 
|---|
| 233 |   if VarType(varChild) <> varInteger then
 | 
|---|
| 234 |     result := ctInvalid
 | 
|---|
| 235 |   else if varChild = CHILDID_SELF then
 | 
|---|
| 236 |     result := ctNoChild
 | 
|---|
| 237 |   else
 | 
|---|
| 238 |     result := ctChild;
 | 
|---|
| 239 | end;
 | 
|---|
| 240 | 
 | 
|---|
| 241 | function TAccessibleRichEdit.GetDefaultObject: IAccessible;
 | 
|---|
| 242 | begin
 | 
|---|
| 243 |   if Assigned(FControl) and not FDefaultObjectLoaded then begin
 | 
|---|
| 244 |     FDefaultObject := uAccessAPI.GetDefaultObject(FControl);
 | 
|---|
| 245 |     FDefaultObjectLoaded := True;
 | 
|---|
| 246 |   end;
 | 
|---|
| 247 |   Result := FDefaultObject;
 | 
|---|
| 248 | end;
 | 
|---|
| 249 | 
 | 
|---|
| 250 | function TAccessibleRichEdit.SafeCallException(ExceptObject: TObject;
 | 
|---|
| 251 |   ExceptAddr: Pointer): HResult;
 | 
|---|
| 252 | begin
 | 
|---|
| 253 |   if (ExceptObject is EOleSysError) then
 | 
|---|
| 254 |     result := EOleSysError(ExceptObject).ErrorCode
 | 
|---|
| 255 |   else
 | 
|---|
| 256 |     result := inherited SafeCallException(ExceptObject, ExceptAddr);
 | 
|---|
| 257 | end;
 | 
|---|
| 258 | 
 | 
|---|
| 259 | class procedure TAccessibleRichEdit.UnwrapControl(
 | 
|---|
| 260 |   Control: TCaptionRichEdit);
 | 
|---|
| 261 | begin
 | 
|---|
| 262 |   if not UserIsRestricted then
 | 
|---|
| 263 |     Control.MakeAccessible(nil);
 | 
|---|
| 264 | end;
 | 
|---|
| 265 | 
 | 
|---|
| 266 | class procedure TAccessibleRichEdit.WrapControl(
 | 
|---|
| 267 |   Control: TCaptionRichEdit);
 | 
|---|
| 268 | var
 | 
|---|
| 269 |   AccessibleRichEdit: TAccessibleRichEdit;
 | 
|---|
| 270 |   {Using Accessible here is probably just interface reference count paranoia}
 | 
|---|
| 271 |   Accessible: IAccessible;
 | 
|---|
| 272 | begin
 | 
|---|
| 273 |   if not UserIsRestricted then
 | 
|---|
| 274 |   begin
 | 
|---|
| 275 |     AccessibleRichEdit := TAccessibleRichEdit.Create;
 | 
|---|
| 276 |     Accessible := AccessibleRichEdit;
 | 
|---|
| 277 |     AccessibleRichEdit.Control := Control;
 | 
|---|
| 278 |     Control.MakeAccessible(Accessible);
 | 
|---|
| 279 |   end;
 | 
|---|
| 280 | end;
 | 
|---|
| 281 | 
 | 
|---|
| 282 | initialization
 | 
|---|
| 283 |   try
 | 
|---|
| 284 |     TAutoObjectFactory.Create(ComServer, TAccessibleRichEdit, Class_AccessibleRichEdit,
 | 
|---|
| 285 |       ciMultiInstance, tmApartment);
 | 
|---|
| 286 |   except
 | 
|---|
| 287 |     {Let the poor restricted users pass!}
 | 
|---|
| 288 |     UserIsRestricted := True;
 | 
|---|
| 289 |   end;
 | 
|---|
| 290 | end.
 | 
|---|
| 291 | 
 | 
|---|