1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
|
{
/***************************************************************************
QtCaret.pas - Qt Caret Emulation
-------------------------------------
copyright (c) Andreas Hausladen
adopted for Lazarus and Qt5 by Lazarus Team
***************************************************************************/
*****************************************************************************
This file is part of the Lazarus Component Library (LCL)
See the file COPYING.modifiedLGPL.txt, included in this distribution,
for details about the license.
*****************************************************************************
}
unit QtCaret;
{$mode objfpc}{$H+}
interface
{off $DEFINE VerboseQtCaret}
{$I qtdefines.inc}
uses
// Bindings
qt6,
// Free Pascal
Classes, SysUtils, Types,
// Widgetset
QtObjects, QtWidgets,
// LCL
LCLType, LCLIntf, Graphics, ExtCtrls;
type
{ TEmulatedCaret }
TEmulatedCaret = class(TComponent)
private
FCaretDirty: Boolean;
FCaretDirtyPos: TQtPoint;
FTimer: TTimer;
FOldRect: TRect;
FWidget: TQtWidget;
FPixmap: QPixmapH;
FLastValidWidth, FLastValidHeight: Integer;
FWidth, FHeight: Integer;
FPos: TQtPoint;
FVisible: Boolean;
FVisibleState: Boolean;
FRespondToFocus: Boolean;
FCritSect: TCriticalSection;
procedure SetPos(const Value: TQtPoint);
protected
procedure DoTimer(Sender: TObject);
procedure DrawCaret; virtual;
function CreateColorPixmap(Color: PtrUInt): QPixmapH;
procedure SetWidget(AWidget: TQtWidget);
procedure UpdateCaret(const AForceUpdate: Boolean = False);
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Lock;
procedure Unlock;
function CreateCaret(AWidget: TQtWidget; Pixmap: QPixmapH; Width, Height: Integer): Boolean;
function DestroyCaret: Boolean;
function IsValid: Boolean;
function Show(AWidget: TQtWidget): Boolean;
function Hide: Boolean;
property CaretDirty: Boolean read FCaretDirty;
property Timer: TTimer read FTimer;
property Pos: TQtPoint read FPos write SetPos;
property RespondToFocus: Boolean read FRespondToFocus write FRespondToFocus;
end;
function CreateCaret(Widget: TQtWidget; Pixmap: QPixmapH; Width, Height: Integer): Boolean; overload;
function CreateCaret(Widget: TQtWidget; ColorCaret: PtrUInt; Width, Height: Integer): Boolean; overload;
function HideCaret(Widget: TQtWidget): Boolean;
function ShowCaret(Widget: TQtWidget): Boolean;
function SetCaretPos(X, Y: Integer): Boolean;
function GetCaretPos(var Pt: TPoint): Boolean;
function GetQtCaretRespondToFocus: Boolean;
procedure SetQtCaretRespondToFocus(Value: Boolean);
function DestroyCaret: Boolean;
procedure DrawCaret;
procedure DestroyGlobalCaret;
function GlobalCaretDirty: Boolean;
implementation
uses
qtint
{$IFDEF VerboseQtCaret}
,LCLProc
{$ENDIF}
;
var
GlobalCaret: TEmulatedCaret = nil;
procedure GlobalCaretNeeded;
begin
if GlobalCaret = nil then
GlobalCaret := TEmulatedCaret.Create(nil);
end;
function GlobalCaretDirty: Boolean;
begin
Result := False;
if GlobalCaret <> nil then
Result := GlobalCaret.CaretDirty;
end;
procedure DrawCaret;
begin
GlobalCaretNeeded;
if Assigned(GlobalCaret) then
begin
GlobalCaret.Lock;
try
GlobalCaret.DrawCaret;
finally
GlobalCaret.Unlock;
end;
end;
end;
procedure DestroyGlobalCaret;
begin
FreeAndNil(GlobalCaret);
end;
function CreateCaret(Widget: TQtWidget; Pixmap: QPixmapH; Width, Height: Integer): Boolean;
begin
GlobalCaretNeeded;
GlobalCaret.Lock;
try
Result := GlobalCaret.CreateCaret(Widget, Pixmap, Width, Height);
finally
GlobalCaret.Unlock;
end;
end;
function CreateCaret(Widget: TQtWidget; ColorCaret: PtrUInt; Width, Height: Integer): Boolean;
begin
Result := CreateCaret(Widget, QPixmapH(ColorCaret), Width, Height);
end;
function GetCaretBlinkTime: Cardinal;
var
FlashTime: Integer;
begin
FlashTime := QApplication_cursorFlashTime;
if FlashTime > 0 then
Result := FlashTime div 2
else
Result := 600; // our default value
end;
function SetCaretBlinkTime(uMSeconds: Cardinal): LongBool;
begin
Result := True;
try
QApplication_setCursorFlashTime(uMSeconds);
if assigned(GlobalCaret) then
begin
GlobalCaret.Lock;
try
GlobalCaret.Timer.Interval := GetCaretBlinkTime;
finally
GlobalCaret.Unlock;
end;
end;
except
Result := False;
end;
end;
function HideCaret(Widget: TQtWidget): Boolean;
begin
GlobalCaretNeeded;
if Assigned(GlobalCaret) then
begin
GlobalCaret.Lock;
try
Result := GlobalCaret.Hide;
finally
GlobalCaret.Unlock;
end;
end
else
Result := false;
end;
function ShowCaret(Widget: TQtWidget): Boolean;
begin
GlobalCaretNeeded;
GlobalCaret.Lock;
try
Result := GlobalCaret.Show(Widget);
finally
GlobalCaret.Unlock;
end;
end;
function SetCaretPos(X, Y: Integer): Boolean;
begin
Result := True;
GlobalCaretNeeded;
GlobalCaret.Lock;
try
GlobalCaret.Pos := QtPoint(X, Y);
finally
GlobalCaret.Unlock;
end;
end;
function GetCaretPos(var Pt: TPoint): Boolean;
begin
Result := True;
GlobalCaretNeeded;
GlobalCaret.Lock;
try
with GlobalCaret.Pos do
begin
Pt.x := X;
Pt.y := Y;
end;
finally
GlobalCaret.Unlock;
end;
end;
function GetQtCaretRespondToFocus: Boolean;
begin
if Assigned(GlobalCaret) then
Result := GlobalCaret.RespondToFocus
else
Result := False;
end;
procedure SetQtCaretRespondToFocus(Value: Boolean);
begin
if Assigned(GlobalCaret) then
GlobalCaret.RespondToFocus := Value;
end;
function DestroyCaret: Boolean;
begin
if Assigned(GlobalCaret) then
begin
GlobalCaret.Lock;
try
Result := GlobalCaret.DestroyCaret;
finally
GlobalCaret.Unlock;
end;
end
else
Result := False;
end;
{ TEmulatedCaret }
constructor TEmulatedCaret.Create(AOwner: TComponent);
begin
{$IFDEF VerboseQtCaret}
writeln('TEmulatedCaret.Create()');
{$ENDIF}
inherited Create(AOwner);
InitializeCriticalSection(FCritSect);
FLastValidWidth := 0;
FLastValidHeight := 0;
FCaretDirty := False;
FOldRect := Rect(0, 0, 1, 1);
FPos := QtPoint(0, 0);
FCaretDirtyPos := FPos;
FTimer := TTimer.Create(self);
FTimer.Enabled := False;
FTimer.Interval := GetCaretBlinkTime;
FTimer.OnTimer := @DoTimer;
FRespondToFocus := False;
end;
destructor TEmulatedCaret.Destroy;
begin
{$IFDEF VerboseQtCaret}
writeln('TEmulatedCaret.Destroy()');
{$ENDIF}
DestroyCaret;
DeleteCriticalSection(FCritSect);
inherited Destroy;
end;
function TEmulatedCaret.CreateCaret(AWidget: TQtWidget; Pixmap: QPixmapH;
Width, Height: Integer): Boolean;
begin
DestroyCaret;
SetWidget(AWidget);
FWidth := Width;
FHeight := Height;
if FWidth > 0 then
FLastValidWidth := FWidth;
if FHeight > 0 then
FLastValidHeight := FHeight;
if PtrUInt(Pixmap) > $FFFF then
FPixmap := QPixmap_create(Pixmap)
else
FPixmap := CreateColorPixmap(PtrUInt(Pixmap));
{$IFDEF VerboseQtCaret}
writeln('TEmulatedCaret.CreateCaret IsValid=',IsValid,' FVis=',FVisible,
' FVisState=',FVisibleState,' FPixmap=',dbghex(PtrUInt(FPixmap)),
' FWidth=',FWidth,' FHeight=',FHeight,' FWidget=',dbghex(PtrUInt(FWidget)));
{$ENDIF}
Result := Assigned(FPixmap);
FTimer.Enabled := True;
end;
function TEmulatedCaret.DestroyCaret: Boolean;
begin
{$IFDEF VerboseQtCaret}
writeln('TEmulatedCaret.DestroyCaret IsValid=',IsValid,' FVis=',FVisible,
' FVisState=',FVisibleState,' FPixmap=',dbghex(PtrUInt(FPixmap)),
' FWidth=',FWidth,' FHeight=',FHeight,' FWidget=',dbghex(PtrUInt(FWidget)));
{$ENDIF}
FTimer.Enabled := False;
Hide;
if Assigned(FPixmap) then
QPixmap_destroy(FPixmap);
FWidget := nil;
FPixmap := nil;
FWidth := 0;
FHeight := 0;
Result := not IsValid;
end;
procedure TEmulatedCaret.DrawCaret;
var
R: TRect;
begin
{$IFDEF VerboseQtCaret}
writeln('TEmulatedCaret.DrawCaret IsValid=',IsValid,' FVis=',FVisible,
' FVisState=',FVisibleState,' FPixmap=',dbghex(PtrUInt(FPixmap)),
' FWidth=',FWidth,' FHeight=',FHeight,' FWidget=',dbghex(PtrUInt(FWidget)));
{$ENDIF}
if IsValid and FVisible and FVisibleState then
begin
FCaretDirty := False;
R := Rect(0, 0, QPixmap_width(FPixmap), QPixmap_height(FPixmap));
TQtDeviceContext(FWidget.Context).save;
TQtDeviceContext(FWidget.Context).setCompositionMode(QPainterRasterOp_NotSourceXorDestination);
TQtDeviceContext(FWidget.Context).drawPixmap(PQtPoint(@FPos), FPixmap, PRect(@R));
TQtDeviceContext(FWidget.Context).restore;
end;
end;
function TEmulatedCaret.Show(AWidget: TQtWidget): Boolean;
var
Pt: TQtPoint;
begin
{$IFDEF VerboseQtCaret}
writeln('TEmulatedCaret.Show AWidget=',dbghex(PtrUInt(AWidget)));
{$ENDIF}
if FWidget <> AWidget then
begin
{$IFDEF VerboseQtCaret}
writeln('TEmulatedCaret.Show Widget changed IsValid=',IsValid);
{$ENDIF}
Hide;
SetWidget(AWidget);
if (FCaretDirty or (FPixmap=nil))
and (AWidget <> nil) and (FWidget.Context<>0) then
begin
CreateCaret(FWidget, nil, FLastValidWidth, FLastValidHeight);
if (FCaretDirtyPos.X > 0) or (FCaretDirtyPos.Y > 0) then
begin
if (FWidget.LastCaretPos.X >= 0) and (FWidget.LastCaretPos.Y >= 0) then
Pt := FWidget.LastCaretPos
else
Pt := FPos;
SetPos(FCaretDirtyPos);
FCaretDirtyPos := QtPoint(0, 0);
QCoreApplication_processEvents(QEventLoopExcludeUserInputEvents);
SetPos(Pt);
end else
SetPos(FPos);
end;
end;
Result := Assigned(FPixmap);
FVisible := Result;
SetPos(FPos);
//writeln('TEmulatedCaret.Show END Result=',Result,' IsValid=',IsValid,' Handle=',QtWidgetSet.IsValidHandle(HWND(FWidget)),' FPixmap=',FPixmap<>nil,' FWidget.Context=',FWidget.Context<>0);
end;
function TEmulatedCaret.Hide: Boolean;
begin
{$IFDEF VerboseQtCaret}
writeln('TEmulatedCaret.Hide IsValid=',IsValid,' FVisible=',FVisible);
{$ENDIF}
Result := IsValid;
if Result and FVisible then
begin
FVisible := False;
UpdateCaret;
end;
end;
procedure TEmulatedCaret.SetPos(const Value: TQtPoint);
begin
{$IFDEF VerboseQtCaret}
writeln('TEmulatedCaret.SetPos FWidget=',dbghex(PtrUInt(FWidget)),' X=',Value.X,
' Y=',Value.Y,' OldX=',FPos.X,' OldY=',FPos.Y,' Dirty=',FCaretDirty);
{$ENDIF}
if not QtWidgetSet.IsValidHandle(HWND(FWidget)) or (FWidget.Widget = nil) then
begin
// oops, our caret is dirty here.
FCaretDirtyPos := FPos;
FCaretDirty := True;
FPos := Value;
exit;
end;
if ((FPos.x <> Value.x) or (FPos.y <> Value.y)) or FCaretDirty then
begin
FWidget.LastCaretPos := FPos;
FPos := Value;
FTimer.Enabled := False;
FVisibleState := FWidget.Context = 0;
{$note remove complete property RespondToFocus after testing}
// if RespondToFocus and not FCaretDirty then
if not FCaretDirty then
UpdateCaret(True);
if FCaretDirty then
begin
FVisible := True;
if FWidget.Context <> 0 then
DoTimer(FTimer);
FTimer.Enabled := True;
end;
end else
begin
if FWidget.Context = 0 then
begin
FVisibleState := True;
FTimer.Enabled := False;
UpdateCaret;
end else
begin
if not FTimer.Enabled then
FTimer.Enabled := True;
end;
end;
end;
procedure TEmulatedCaret.DoTimer(Sender: TObject);
begin
FVisibleState := not FVisibleState;
if FVisible then
UpdateCaret;
end;
procedure TEmulatedCaret.Lock;
begin
{$note TEmulatedCaret.Lock()
remove this routine later, after
testing on all platforms}
// EnterCriticalSection(FCritSect);
end;
procedure TEmulatedCaret.Unlock;
begin
{$note TEmulatedCaret.UnLock()
remove this routine later, after
testing on all platforms}
// LeaveCriticalSection(FCritSect);
end;
function TEmulatedCaret.CreateColorPixmap(Color: PtrUInt): QPixmapH;
var
QC: TQColor;
AColor: TColor;
begin
{$IFDEF VerboseQtCaret}
writeln('TEmulatedCaret.CreateColorPixmap FWidget=',dbghex(PtrUInt(FWidget)),' Width=',FWidth,
' FHeight=',FHeight);
{$ENDIF}
if (FWidth <= 0) or (FHeight <= 0) then
Result := nil
else
begin
case Color of
0: AColor := clBlack;
1: AColor := clGray;
else
begin
Result := nil;
Exit;
end;
end;
Result := QPixmap_create(FWidth, FHeight);
try
QColor_fromRgb(@QC,Red(AColor),Green(AColor),Blue(AColor));
QPixmap_fill(Result, @QC);
except
QPixmap_destroy(Result);
Result := nil;
end;
end;
end;
function TEmulatedCaret.IsValid: Boolean;
begin
Result := QtWidgetSet.IsValidHandle(HWND(FWidget));
if Result then
Result := (FPixmap <> nil) and (FWidget.Context <> 0);
end;
procedure TEmulatedCaret.SetWidget(AWidget: TQtWidget);
begin
if FWidget <> nil then
FWidget.HasCaret := False;
if AWidget is TQtCustomControl then
AWidget := TQtCustomControl(AWidget).viewport;
FWidget := AWidget;
if FWidget <> nil then
FWidget.HasCaret := True;
end;
procedure TEmulatedCaret.UpdateCaret(const AForceUpdate: Boolean = False);
var
R: TRect;
begin
if not QtWidgetSet.IsValidHandle(HWND(FWidget)) then
exit;
if (FWidget.Widget <> nil) then
begin
if FPos.X < 0 then
FPos.X := 0;
if FPos.Y < 0 then
FPos.Y := 0;
R.Left := FPos.x;
R.Top := FPos.y;
R.Right := R.Left + FWidth + 2;
R.Bottom := R.Top + FHeight + 2;
if (FWidget.Context = 0) then
begin
if not EqualRect(FOldRect, R) then
FWidget.Update(@FOldRect);
FWidget.Update(@R);
FOldRect := R;
end else
begin
if AForceUpdate and not FVisible and not FTimer.Enabled then
FTimer.Enabled := True;
end;
end;
end;
end.
|