| 1 | //*************************************************************
|
|---|
| 2 | // EwbCoreTools *
|
|---|
| 3 | // *
|
|---|
| 4 | // Freeware Unit *
|
|---|
| 5 | // For Delphi *
|
|---|
| 6 | // Developing Team: *
|
|---|
| 7 | // Serge Voloshenyuk (SergeV@bsalsa.com) *
|
|---|
| 8 | // Eran Bodankin (bsalsa) -(bsalsa@gmail.com) *
|
|---|
| 9 | // *
|
|---|
| 10 | // Documentation and updated versions: *
|
|---|
| 11 | // *
|
|---|
| 12 | // http://www.bsalsa.com *
|
|---|
| 13 | //*************************************************************
|
|---|
| 14 | {LICENSE:
|
|---|
| 15 | THIS SOFTWARE IS PROVIDED TO YOU "AS IS" WITHOUT WARRANTY OF ANY KIND,
|
|---|
| 16 | EITHER EXPRESSED OR IMPLIED INCLUDING BUT NOT LIMITED TO THE APPLIED
|
|---|
| 17 | WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
|
|---|
| 18 | YOU ASSUME THE ENTIRE RISK AS TO THE ACCURACY AND THE USE OF THE SOFTWARE
|
|---|
| 19 | AND ALL OTHER RISK ARISING OUT OF THE USE OR PERFORMANCE OF THIS SOFTWARE
|
|---|
| 20 | AND DOCUMENTATION. BSALSA PRODUCTIONS DOES NOT WARRANT THAT THE SOFTWARE IS ERROR-FREE
|
|---|
| 21 | OR WILL OPERATE WITHOUT INTERRUPTION. THE SOFTWARE IS NOT DESIGNED, INTENDED
|
|---|
| 22 | OR LICENSED FOR USE IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE CONTROLS,
|
|---|
| 23 | INCLUDING WITHOUT LIMITATION, THE DESIGN, CONSTRUCTION, MAINTENANCE OR
|
|---|
| 24 | OPERATION OF NUCLEAR FACILITIES, AIRCRAFT NAVIGATION OR COMMUNICATION SYSTEMS,
|
|---|
| 25 | AIR TRAFFIC CONTROL, AND LIFE SUPPORT OR WEAPONS SYSTEMS. BSALSA PRODUCTIONS SPECIFICALLY
|
|---|
| 26 | DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR SUCH PURPOSE.
|
|---|
| 27 |
|
|---|
| 28 | You may use/ change/ modify the component under 3 conditions:
|
|---|
| 29 | 1. In your website, add a link to "http://www.bsalsa.com"
|
|---|
| 30 | 2. In your application, add credits to "Embedded Web Browser"
|
|---|
| 31 | 3. Mail me (bsalsa@gmail.com) any code change in the unit for the benefit
|
|---|
| 32 | of the other users.
|
|---|
| 33 | 4. Please, consider donation in our web site!
|
|---|
| 34 | {*******************************************************************************}
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 | unit EwbCoreTools;
|
|---|
| 38 |
|
|---|
| 39 | {$I EWB.inc}
|
|---|
| 40 |
|
|---|
| 41 | interface
|
|---|
| 42 |
|
|---|
| 43 | uses
|
|---|
| 44 | Graphics, ActiveX, Mshtml_Ewb, Windows, SysUtils;
|
|---|
| 45 |
|
|---|
| 46 | function IsWinXPSP2OrLater(): Boolean;
|
|---|
| 47 | function ColorToHTML(const Color: TColor): string;
|
|---|
| 48 | function WideStringToLPOLESTR(const Source: Widestring): POleStr;
|
|---|
| 49 | function XPath4Node(node: IHTMLElement): string;
|
|---|
| 50 | function TaskAllocWideString(const S: string): PWChar;
|
|---|
| 51 | function AnsiIndexStr(const AText: string; const AValues: array of string): Integer;
|
|---|
| 52 | function GetPos(const SubSt, Text: string; StartPos: Integer = -1): Integer;
|
|---|
| 53 | function _CharPos(const C: Char; const S: string): Integer;
|
|---|
| 54 | function CutString(var Text: string; const Delimiter: string = ' ';
|
|---|
| 55 | const Remove: Boolean = True): string;
|
|---|
| 56 | procedure FormatPath(Path: string);
|
|---|
| 57 | function GetWinText(WinHandle: THandle): string;
|
|---|
| 58 | function GetWinClass(Handle: Hwnd): WideString;
|
|---|
| 59 | function GetParentWinByClass(ChildHandle: HWND; const ClassName: string): HWND;
|
|---|
| 60 | {$IFDEF DELPHI5}
|
|---|
| 61 | function DirectoryExists(const Directory: string): Boolean;
|
|---|
| 62 | function VarSupports(const V: Variant; const IID: TGUID; out Intf): Boolean;
|
|---|
| 63 | {$ENDIF}
|
|---|
| 64 | {$IFNDEF DELPHI12_UP}
|
|---|
| 65 | function CharInSet(C: Char; const CharSet: TSysCharSet): Boolean;
|
|---|
| 66 | {$ENDIF}
|
|---|
| 67 | function AddBackSlash(const S: string): string;
|
|---|
| 68 |
|
|---|
| 69 | const
|
|---|
| 70 | WM_SETWBFOCUS = $0400 {WM_USER} + $44;
|
|---|
| 71 |
|
|---|
| 72 | implementation
|
|---|
| 73 |
|
|---|
| 74 | uses
|
|---|
| 75 | IeConst, EwbAcc;
|
|---|
| 76 |
|
|---|
| 77 | type
|
|---|
| 78 | {VerifyVersion}
|
|---|
| 79 | fn_VerifyVersionInfo = function(var VersionInformation: OSVERSIONINFOEX;
|
|---|
| 80 | dwTypeMask: DWORD; dwlConditionMask: LONGLONG): BOOL; stdcall;
|
|---|
| 81 | fn_VerSetConditionMask = function(ConditionMask: LONGLONG; TypeMask: DWORD;
|
|---|
| 82 | Condition: Byte): LONGLONG; stdcall;
|
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 85 | function IsWinXPSP2OrLater(): Boolean;
|
|---|
| 86 | var
|
|---|
| 87 | osvi: TOSVersionInfoEx;
|
|---|
| 88 | dwlConditionMask: LONGLONG;
|
|---|
| 89 | op: Integer;
|
|---|
| 90 | hlib: THandle;
|
|---|
| 91 | VerifyVersionInfo: fn_VerifyVersionInfo;
|
|---|
| 92 | VerSetConditionMask: fn_VerSetConditionMask;
|
|---|
| 93 | begin
|
|---|
| 94 | Result := False;
|
|---|
| 95 | hLib := GetModuleHandle('kernel32.dll');
|
|---|
| 96 | if hLib = 0 then
|
|---|
| 97 | hLib := LoadLibrary('kernel32.dll');
|
|---|
| 98 | if (hLib <> 0) then
|
|---|
| 99 | begin
|
|---|
| 100 | @VerifyVersionInfo := GetProcAddress(hLib, 'VerifyVersionInfoA');
|
|---|
| 101 | @VerSetConditionMask := GetProcAddress(hLib, 'VerSetConditionMask');
|
|---|
| 102 | if ((@VerifyVersionInfo = nil) or (@VerSetConditionMask = nil)) then Exit;
|
|---|
| 103 |
|
|---|
| 104 | dwlConditionMask := 0;
|
|---|
| 105 | op := VER_GREATER_EQUAL;
|
|---|
| 106 |
|
|---|
| 107 | // Initialize the OSVERSIONINFOEX structure.
|
|---|
| 108 | ZeroMemory(@osvi, SizeOf(OSVERSIONINFOEX));
|
|---|
| 109 | osvi.dwOSVersionInfoSize := SizeOf(OSVERSIONINFOEX);
|
|---|
| 110 | osvi.dwMajorVersion := 5;
|
|---|
| 111 | osvi.dwMinorVersion := 1;
|
|---|
| 112 | osvi.wServicePackMajor := 2;
|
|---|
| 113 | osvi.wServicePackMinor := 0;
|
|---|
| 114 |
|
|---|
| 115 | // Initialize the condition mask.
|
|---|
| 116 | dwlConditionMask := VerSetConditionMask(dwlConditionMask, VER_MAJORVERSION, op);
|
|---|
| 117 | dwlConditionMask := VerSetConditionMask(dwlConditionMask, VER_MINORVERSION, op);
|
|---|
| 118 | dwlConditionMask := VerSetConditionMask(dwlConditionMask, VER_SERVICEPACKMAJOR, op);
|
|---|
| 119 | dwlConditionMask := VerSetConditionMask(dwlConditionMask, VER_SERVICEPACKMINOR, op);
|
|---|
| 120 |
|
|---|
| 121 | // Perform the test.
|
|---|
| 122 | Result := VerifyVersionInfo(osvi, VER_MAJORVERSION or VER_MINORVERSION or
|
|---|
| 123 | VER_SERVICEPACKMAJOR or VER_SERVICEPACKMINOR, dwlConditionMask);
|
|---|
| 124 | end;
|
|---|
| 125 | end;
|
|---|
| 126 |
|
|---|
| 127 | function GetParentWinByClass(ChildHandle: HWND; const ClassName: string): HWND;
|
|---|
| 128 | var
|
|---|
| 129 | szClass: array[0..255] of Char;
|
|---|
| 130 | begin
|
|---|
| 131 | Result := GetParent(ChildHandle);
|
|---|
| 132 | while IsWindow(Result) do
|
|---|
| 133 | begin
|
|---|
| 134 | if (GetClassName(Result, szClass, SizeOf(szClass)) > 0) and
|
|---|
| 135 | (AnsiStrComp(PChar(ClassName), szClass) = 0) then Exit;
|
|---|
| 136 | Result := GetParent(Result);
|
|---|
| 137 | end;
|
|---|
| 138 | end;
|
|---|
| 139 |
|
|---|
| 140 |
|
|---|
| 141 |
|
|---|
| 142 | {$IFNDEF DELPHI12_UP}
|
|---|
| 143 |
|
|---|
| 144 | function CharInSet(C: Char; const CharSet: TSysCharSet): Boolean;
|
|---|
| 145 | begin
|
|---|
| 146 | Result := C in CharSet;
|
|---|
| 147 | end;
|
|---|
| 148 | {$ENDIF}
|
|---|
| 149 |
|
|---|
| 150 | {$IFDEF DELPHI5}
|
|---|
| 151 |
|
|---|
| 152 | function DirectoryExists(const Directory: string): Boolean;
|
|---|
| 153 | var
|
|---|
| 154 | Code: Integer;
|
|---|
| 155 | begin
|
|---|
| 156 | {$RANGECHECKS OFF}
|
|---|
| 157 | Code := GetFileAttributes(PChar(Directory));
|
|---|
| 158 | Result := (Code <> -1) and (FILE_ATTRIBUTE_DIRECTORY and Code <> 0);
|
|---|
| 159 | {$RANGECHECKS ON}
|
|---|
| 160 | end;
|
|---|
| 161 |
|
|---|
| 162 | function VarSupports(const V: Variant; const IID: TGUID; out Intf): Boolean;
|
|---|
| 163 | begin
|
|---|
| 164 | Result := Supports(V, IID, Intf);
|
|---|
| 165 | end;
|
|---|
| 166 | {$ENDIF}
|
|---|
| 167 |
|
|---|
| 168 | function AddBackSlash(const S: string): string;
|
|---|
| 169 | begin
|
|---|
| 170 | {$IFDEF DELPHI5}
|
|---|
| 171 | Result := IncludeTrailingBackslash(S);
|
|---|
| 172 | {$ELSE}
|
|---|
| 173 | {$IFDEF DELPHI6UP}
|
|---|
| 174 | Result := IncludeTrailingPathDelimiter(S);
|
|---|
| 175 | {$ELSE}
|
|---|
| 176 | if Copy(S, Length(S), 1) = '\' then
|
|---|
| 177 | Result := S
|
|---|
| 178 | else
|
|---|
| 179 | Result := S + '\';
|
|---|
| 180 | {$ENDIF}
|
|---|
| 181 | {$ENDIF}
|
|---|
| 182 | end;
|
|---|
| 183 |
|
|---|
| 184 | function CutString(var Text: string; const Delimiter: string = ' ';
|
|---|
| 185 | const Remove: Boolean = True): string;
|
|---|
| 186 | var
|
|---|
| 187 | IdxPos: Integer;
|
|---|
| 188 | begin
|
|---|
| 189 | if Delimiter = #0 then
|
|---|
| 190 | IdxPos := Pos(Delimiter, Text)
|
|---|
| 191 | else
|
|---|
| 192 | IdxPos := AnsiPos(Delimiter, Text);
|
|---|
| 193 |
|
|---|
| 194 | if (IdxPos = 0) then
|
|---|
| 195 | begin
|
|---|
| 196 | Result := Text;
|
|---|
| 197 | if Remove then
|
|---|
| 198 | Text := '';
|
|---|
| 199 | end
|
|---|
| 200 | else
|
|---|
| 201 | begin
|
|---|
| 202 | Result := Copy(Text, 1, IdxPos - 1);
|
|---|
| 203 | if Remove then
|
|---|
| 204 | Delete(Text, 1, IdxPos + Length(Delimiter) - 1);
|
|---|
| 205 | end;
|
|---|
| 206 | end;
|
|---|
| 207 |
|
|---|
| 208 |
|
|---|
| 209 | function GetPos(const SubSt, Text: string; StartPos: Integer = -1): Integer;
|
|---|
| 210 | var
|
|---|
| 211 | i: Integer;
|
|---|
| 212 | LStartPos: Integer;
|
|---|
| 213 | LTokenLen: Integer;
|
|---|
| 214 | begin
|
|---|
| 215 | result := 0;
|
|---|
| 216 | LTokenLen := Length(SubSt);
|
|---|
| 217 | if StartPos = -1 then
|
|---|
| 218 | begin
|
|---|
| 219 | StartPos := Length(Text);
|
|---|
| 220 | end;
|
|---|
| 221 | if StartPos < (Length(Text) - LTokenLen + 1) then
|
|---|
| 222 | begin
|
|---|
| 223 | LStartPos := StartPos;
|
|---|
| 224 | end
|
|---|
| 225 | else
|
|---|
| 226 | begin
|
|---|
| 227 | LStartPos := (Length(Text) - LTokenLen + 1);
|
|---|
| 228 | end;
|
|---|
| 229 | for i := LStartPos downto 1 do
|
|---|
| 230 | begin
|
|---|
| 231 | if AnsiSameText(Copy(Text, i, LTokenLen), SubSt) then
|
|---|
| 232 | begin
|
|---|
| 233 | Result := i;
|
|---|
| 234 | Break;
|
|---|
| 235 | end;
|
|---|
| 236 | end;
|
|---|
| 237 | end;
|
|---|
| 238 |
|
|---|
| 239 | function _CharPos(const C: Char; const S: string): Integer;
|
|---|
| 240 | begin
|
|---|
| 241 | for Result := 1 to Length(S) do
|
|---|
| 242 | if S[Result] = C then Exit;
|
|---|
| 243 | Result := 0;
|
|---|
| 244 | end;
|
|---|
| 245 |
|
|---|
| 246 | procedure FormatPath(Path: string);
|
|---|
| 247 | var
|
|---|
| 248 | i: Integer;
|
|---|
| 249 | begin
|
|---|
| 250 | i := 1;
|
|---|
| 251 | while i <= Length(Path) do
|
|---|
| 252 | begin
|
|---|
| 253 | if CharInSet(Path[i], LeadBytes) then
|
|---|
| 254 | Inc(i, 2)
|
|---|
| 255 | else
|
|---|
| 256 | if Path[i] = '\' then
|
|---|
| 257 | begin
|
|---|
| 258 | Path[i] := '/';
|
|---|
| 259 | Inc(i, 1);
|
|---|
| 260 | end
|
|---|
| 261 | else
|
|---|
| 262 | Inc(i, 1);
|
|---|
| 263 | end;
|
|---|
| 264 | end;
|
|---|
| 265 |
|
|---|
| 266 | function AnsiIndexStr(const AText: string; const AValues: array of string): Integer;
|
|---|
| 267 | var
|
|---|
| 268 | I: Integer;
|
|---|
| 269 | begin
|
|---|
| 270 | Result := -1;
|
|---|
| 271 | for I := Low(AValues) to High(AValues) do
|
|---|
| 272 | if AnsiSameStr(AText, AValues[I]) then
|
|---|
| 273 | begin
|
|---|
| 274 | Result := I;
|
|---|
| 275 | Break;
|
|---|
| 276 | end;
|
|---|
| 277 | end;
|
|---|
| 278 |
|
|---|
| 279 |
|
|---|
| 280 | function TaskAllocWideString(const S: string): PWChar;
|
|---|
| 281 | var
|
|---|
| 282 | WideLength: integer;
|
|---|
| 283 | Wide: PWideChar;
|
|---|
| 284 | begin
|
|---|
| 285 | WideLength := Length(S) + 1;
|
|---|
| 286 | Wide := CoTaskMemAlloc(WideLength * SizeOf(WideChar));
|
|---|
| 287 | StringToWideChar(S, Wide, WideLength);
|
|---|
| 288 | Result := Wide;
|
|---|
| 289 | end;
|
|---|
| 290 |
|
|---|
| 291 | {
|
|---|
| 292 | function TaskAllocWideString(const S: string): PWChar;
|
|---|
| 293 | var
|
|---|
| 294 | Len: Integer;
|
|---|
| 295 | begin
|
|---|
| 296 | Len := Length(S) + 1;
|
|---|
| 297 | Result := CoTaskMemAlloc(2 * Len);
|
|---|
| 298 | StringToWideChar(S, Result, Len);
|
|---|
| 299 | end;
|
|---|
| 300 | }
|
|---|
| 301 |
|
|---|
| 302 | function WideStringToLPOLESTR(const Source: Widestring): POleStr;
|
|---|
| 303 | var
|
|---|
| 304 | Len: Integer;
|
|---|
| 305 | begin
|
|---|
| 306 | Len := Length(Source) * SizeOf(WideChar);
|
|---|
| 307 | Result := CoTaskMemAlloc(Len + 2);
|
|---|
| 308 | FillChar(Result^, Len + 2, 0);
|
|---|
| 309 | Move(Result^, PWideString(Source)^, Len);
|
|---|
| 310 | end;
|
|---|
| 311 |
|
|---|
| 312 | function ColorToHTML(const Color: TColor): string;
|
|---|
| 313 | var
|
|---|
| 314 | ColorRGB: LongWord;
|
|---|
| 315 | begin
|
|---|
| 316 | ColorRGB := ColorToRGB(Color);
|
|---|
| 317 | FmtStr(Result, '#%0.2X%0.2X%0.2X',
|
|---|
| 318 | [Byte(ColorRGB), Byte(ColorRGB shr 8), Byte(ColorRGB shr 16)]);
|
|---|
| 319 | end;
|
|---|
| 320 |
|
|---|
| 321 | function GetWinText(WinHandle: THandle): string;
|
|---|
| 322 | var
|
|---|
| 323 | DlgName: string;
|
|---|
| 324 | TxtLength: Integer;
|
|---|
| 325 | begin
|
|---|
| 326 | TxtLength := GetWindowTextLength(WinHandle);
|
|---|
| 327 | SetLength(DlgName, TxtLength + 1);
|
|---|
| 328 | GetWindowText(WinHandle, PChar(DlgName), TxtLength + 1);
|
|---|
| 329 | Result := DlgName;
|
|---|
| 330 | end;
|
|---|
| 331 |
|
|---|
| 332 |
|
|---|
| 333 | function GetWinClass(Handle: Hwnd): WideString;
|
|---|
| 334 | var
|
|---|
| 335 | pwc: PWideChar;
|
|---|
| 336 | const
|
|---|
| 337 | maxbufsize = 32767 * SizeOf(WideChar);
|
|---|
| 338 | begin
|
|---|
| 339 | Result := '';
|
|---|
| 340 | if IsWindow(Handle) then
|
|---|
| 341 | begin
|
|---|
| 342 | pwc := GetMemory(maxbufsize);
|
|---|
| 343 | if Assigned(pwc) then
|
|---|
| 344 | try
|
|---|
| 345 | ZeroMemory(pwc, maxbufsize);
|
|---|
| 346 | if GetClassnameW(Handle, pwc, maxbufsize) > 0 then
|
|---|
| 347 | SetString(Result, pwc, lstrlenW(pwc));
|
|---|
| 348 | finally
|
|---|
| 349 | FreeMemory(pwc);
|
|---|
| 350 | end;
|
|---|
| 351 | end;
|
|---|
| 352 | end;
|
|---|
| 353 |
|
|---|
| 354 | {
|
|---|
| 355 | function GetWinClass(WinHandle: THANDLE): string;
|
|---|
| 356 | begin
|
|---|
| 357 | SetLength(Result, 80);
|
|---|
| 358 | SetLength(Result, GetClassName(WinHandle, PChar(Result), Length(Result)));
|
|---|
| 359 | end;
|
|---|
| 360 | }
|
|---|
| 361 |
|
|---|
| 362 |
|
|---|
| 363 | function XPath4Node(node: IHTMLElement): string;
|
|---|
| 364 |
|
|---|
| 365 | function NodePosition(elem: IHTMLElement): string;
|
|---|
| 366 | var tag: Widestring;
|
|---|
| 367 | Idx: Integer;
|
|---|
| 368 | n: IHTMLElement;
|
|---|
| 369 | cl: IHTMLElementCollection;
|
|---|
| 370 | itm: IDispatch;
|
|---|
| 371 | I, C, mI: Integer;
|
|---|
| 372 | begin
|
|---|
| 373 | Result := '';
|
|---|
| 374 | if (elem.parentElement = nil) or
|
|---|
| 375 | not Supports(elem.parentElement.children, IHTMLElementCollection, cl) then Exit;
|
|---|
| 376 |
|
|---|
| 377 | Tag := elem.tagName;
|
|---|
| 378 | Idx := elem.sourceIndex;
|
|---|
| 379 | C := 0;
|
|---|
| 380 | mI := -1;
|
|---|
| 381 |
|
|---|
| 382 | for I := 0 to cl.length - 1 do
|
|---|
| 383 | begin
|
|---|
| 384 | itm := cl.item(I, I);
|
|---|
| 385 | if Supports(itm, IHTMLElement, n) then
|
|---|
| 386 | begin
|
|---|
| 387 | if n.tagName = Tag then
|
|---|
| 388 | begin
|
|---|
| 389 | if n.sourceIndex = Idx then mI := C;
|
|---|
| 390 | Inc(C);
|
|---|
| 391 | end;
|
|---|
| 392 | end;
|
|---|
| 393 | end;
|
|---|
| 394 | if (mI > 0) or (C > 1) then Result := Format('[%d]', [mI]);
|
|---|
| 395 | end;
|
|---|
| 396 |
|
|---|
| 397 | var id: string;
|
|---|
| 398 | begin
|
|---|
| 399 | if node <> nil then
|
|---|
| 400 | begin
|
|---|
| 401 | id := node.id;
|
|---|
| 402 | if id <> '' then
|
|---|
| 403 | Result := Format('%s[@id="%s"]', [node.tagName, id])
|
|---|
| 404 | else if node.parentElement = nil then
|
|---|
| 405 | Result := '/' + node.tagName
|
|---|
| 406 | else Result := Format('%s/%s%s',
|
|---|
| 407 | [XPath4Node(node.parentElement), node.tagName, NodePosition(node)]);
|
|---|
| 408 | end else Result := '';
|
|---|
| 409 | end;
|
|---|
| 410 |
|
|---|
| 411 | end.
|
|---|