/usr/lib/lazarus/0.9.30.4/designer/designerprocs.pas is in lazarus-src-0.9.30.4 0.9.30.4-6.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | {/***************************************************************************
DesignerProcs.pas
-----------------
***************************************************************************/
***************************************************************************
* *
* This source is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This code is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* General Public License for more details. *
* *
* A copy of the GNU General Public License is available on the World *
* Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also *
* obtain it by writing to the Free Software Foundation, *
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
* *
***************************************************************************
Author: Mattias Gaertner
}
unit DesignerProcs;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Types, LCLProc, LCLIntf, LCLType, Forms, Controls,
typinfo, Graphics, FormEditingIntf;
type
TDesignerDCFlag = (
ddcDCOriginValid, // please comment
ddcFormOriginValid, //
ddcFormClientOriginValid, //
ddcSizeValid //
);
TDesignerDCFlags = set of TDesignerDCFlag;
TDesignerDeviceContext = class
private
FCanvas: TCanvas;
FDC: HDC;
FDCControl: TWinControl;
FDCOrigin: TPoint; // DC origin on desktop
FFlags: TDesignerDCFlags;
FFormClientOrigin: TPoint; // Form client origin on desktop
FFormOrigin: TPoint; // DC origin relative to designer Form
FDcSize: TPoint;
FForm: TCustomForm;
FSavedDC: HDC;
FPaintCount: integer;
function GetDCOrigin: TPoint;
function GetDCSize: TPoint;
function GetFormClientOrigin: TPoint;
function GetFormOrigin: TPoint;
public
constructor Create;
destructor Destroy; override;
procedure SetDC(AForm: TCustomForm; ADCControl: TWinControl; ADC: HDC);
procedure Clear;
procedure BeginPainting;
procedure EndPainting;
function RectVisible(ALeft, ATop, ARight, ABottom: integer): boolean;
property Canvas: TCanvas read FCanvas;
property DC: HDC read FDC;
property Form: TCustomForm read FForm;
property FormOrigin: TPoint read GetFormOrigin;// DC origin relative to designer Form
property DCOrigin: TPoint read GetDCOrigin; // DC origin on Desktop
property FormClientOrigin: TPoint read GetFormClientOrigin;// Form Client Origin on desktop
property DCSize: TPoint read GetDCSize;
end;
const
NonVisualCompIconWidth = ComponentPaletteImageWidth;
NonVisualCompBorder = 2;
NonVisualCompWidth = NonVisualCompIconWidth + 2 * NonVisualCompBorder;
type
TOnComponentIsInvisible = procedure(AComponent: TComponent;
var Invisible: boolean) of object;
var
OnComponentIsInvisible: TOnComponentIsInvisible;
function GetParentLevel(AControl: TControl): integer;
function ControlIsInDesignerVisible(AControl: TControl): boolean;
function ComponentIsInvisible(AComponent: TComponent): boolean;
function ComponentIsNonVisual(AComponent: TComponent): boolean;
function ComponentBoundsDesignable(AComponent: TComponent): boolean;
function GetParentFormRelativeTopLeft(Component: TComponent): TPoint;
function GetParentFormRelativeBounds(Component: TComponent): TRect;
function GetParentFormRelativeClientOrigin(Component: TComponent): TPoint;
function GetParentFormRelativeParentClientOrigin(Component: TComponent): TPoint;
function GetFormRelativeMousePosition(Form: TCustomForm): TPoint;
procedure GetComponentBounds(AComponent: TComponent;
var Left, Top, Width, Height: integer);
function GetComponentLeft(AComponent: TComponent): integer;
function GetComponentTop(AComponent: TComponent): integer;
function GetComponentWidth(AComponent: TComponent): integer;
function GetComponentHeight(AComponent: TComponent): integer;
procedure InvalidateDesignerRect(aHandle: HWND; ARect: pRect);
implementation
function GetParentFormRelativeTopLeft(Component: TComponent): TPoint;
var
FormOrigin: TPoint;
ParentForm: TCustomForm;
Parent: TWinControl;
p: TPoint;
begin
if Component is TControl then
begin
ParentForm := GetParentForm(TControl(Component));
Parent := TControl(Component).Parent;
if (Parent = nil) or (ParentForm = nil) then
begin
Result := Point(0, 0);
end else
begin
Result := Parent.ClientOrigin;
FormOrigin := ParentForm.ClientOrigin;
//DebugLn(['GetParentFormRelativeTopLeft Component=',dbgsName(Component),' Parent=',dbgsName(Parent),' ',dbgs(Result),' ParentForm=',dbgsName(ParentForm),' ',dbgs(FormOrigin)]);
Result.X := Result.X - FormOrigin.X + TControl(Component).Left;
Result.Y := Result.Y - FormOrigin.Y + TControl(Component).Top;
end;
end else
begin
Result.X := LeftFromDesignInfo(Component.DesignInfo);
Result.Y := TopFromDesignInfo(Component.DesignInfo);
if Component.Owner is TWinControl then
begin
Parent:=TWinControl(Component.Owner);
ParentForm := GetParentForm(Parent);
if (ParentForm<>nil) and (ParentForm<>Parent) then
begin
p:=Parent.ClientOrigin;
FormOrigin := ParentForm.ClientOrigin;
inc(Result.X,p.X-FormOrigin.X);
inc(Result.Y,p.Y-FormOrigin.Y);
end;
end;
end;
end;
function GetParentFormRelativeBounds(Component: TComponent): TRect;
var
CTopLeft: TPoint;
begin
CTopLeft := GetParentFormRelativeTopLeft(Component);
Result.Left := CTopLeft.X;
Result.Top := CTopLeft.Y;
Result.Right := Result.Left + GetComponentWidth(Component);
Result.Bottom := Result.Top + GetComponentHeight(Component);
end;
function GetParentFormRelativeClientOrigin(Component: TComponent): TPoint;
var
FormOrigin: TPoint;
ParentForm: TCustomForm;
begin
if Component is TControl then
begin
ParentForm := GetParentForm(TControl(Component));
if ParentForm = nil then
Result := Point(0, 0)
else
begin
Result := TControl(Component).ClientOrigin;
FormOrigin := ParentForm.ClientOrigin;
Result.X := Result.X - FormOrigin.X;
Result.Y := Result.Y - FormOrigin.Y;
end;
end else
begin
Result.X := LeftFromDesignInfo(Component.DesignInfo);
Result.Y := TopFromDesignInfo(Component.DesignInfo);
end;
end;
function GetParentFormRelativeParentClientOrigin(Component: TComponent): TPoint;
var
FormOrigin, ParentOrigin: TPoint;
ParentForm: TCustomForm;
Parent: TWinControl;
begin
if Component is TControl then
begin
ParentForm := GetParentForm(TControl(Component));
Parent := TControl(Component).Parent;
if (Parent = nil) or (ParentForm = nil) then
Result := Point(0, 0)
else
begin
ParentOrigin := Parent.ClientOrigin;
FormOrigin := ParentForm.ClientOrigin;
Result.X := ParentOrigin.X - FormOrigin.X;
Result.Y := ParentOrigin.Y - FormOrigin.Y;
end;
end
else
begin
Result := Point(0, 0);
ParentForm := GetDesignerForm(Component);
if ParentForm = nil then
Exit;
if (Component <> nil) and (Component.Owner <> ParentForm) then
begin
Component := Component.Owner;
if (csInline in Component.ComponentState) and (Component is TControl) then
begin
with ParentForm.ScreenToClient(TControl(Component).ClientToScreen(Point(0, 0))) do
begin
inc(Result.X, X);
inc(Result.Y, Y);
end;
end;
end;
end;
end;
function GetFormRelativeMousePosition(Form: TCustomForm): TPoint;
var
FormClientOrigin: TPoint;
begin
Result.X:=0;
Result.Y:=0;
GetCursorPos(Result);
FormClientOrigin:=Form.ClientOrigin;
dec(Result.X,FormClientOrigin.X);
dec(Result.Y,FormClientOrigin.Y);
end;
procedure GetComponentBounds(AComponent: TComponent;
var Left, Top, Width, Height: integer);
begin
if AComponent is TControl then
begin
Left := TControl(AComponent).Left;
Top := TControl(AComponent).Top;
Width := TControl(AComponent).Width;
Height := TControl(AComponent).Height;
end else
begin
Left := LeftFromDesignInfo(AComponent.DesignInfo);
Top := TopFromDesignInfo(AComponent.DesignInfo);
Width := NonVisualCompWidth;
Height := Width;
end;
end;
function GetComponentLeft(AComponent: TComponent): integer;
begin
if AComponent is TControl then
Result := TControl(AComponent).Left
else
Result := LeftFromDesignInfo(AComponent.DesignInfo);
end;
function GetComponentTop(AComponent: TComponent): integer;
begin
if AComponent is TControl then
Result := TControl(AComponent).Top
else
Result := TopFromDesignInfo(AComponent.DesignInfo);
end;
function GetComponentWidth(AComponent: TComponent): integer;
begin
if AComponent is TControl then
Result := TControl(AComponent).Width
else
Result := NonVisualCompWidth;
end;
function GetComponentHeight(AComponent: TComponent): integer;
begin
if AComponent is TControl then
Result := TControl(AComponent).Height
else
Result := NonVisualCompWidth;
end;
procedure InvalidateDesignerRect(aHandle: HWND; ARect: pRect);
const
ExtraInvalidateFrame = 3;
var
InvRect: TRect;
begin
InvRect:=ARect^;
dec(InvRect.Left,ExtraInvalidateFrame);
dec(InvRect.Top,ExtraInvalidateFrame);
inc(InvRect.Right,ExtraInvalidateFrame);
inc(InvRect.Bottom,ExtraInvalidateFrame);
InvalidateRect(aHandle,@InvRect,false);
end;
function GetParentLevel(AControl: TControl): integer;
begin
Result:=0;
while AControl<>nil do begin
inc(Result);
AControl:=AControl.Parent;
end;
end;
function ControlIsInDesignerVisible(AControl: TControl): boolean;
begin
Result:=true;
while AControl<>nil do begin
if csNoDesignVisible in AControl.ControlStyle then begin
Result:=false;
exit;
end;
AControl:=AControl.Parent;
end;
end;
function ComponentIsInvisible(AComponent: TComponent): boolean;
begin
Result:=false;
if Assigned(OnComponentIsInvisible) then
OnComponentIsInvisible(AComponent,Result);
end;
function ComponentIsNonVisual(AComponent: TComponent): boolean;
begin
Result:=(AComponent<>nil)
and (not (AComponent is TControl))
and (not ComponentIsInvisible(AComponent));
end;
function ComponentBoundsDesignable(AComponent: TComponent): boolean;
begin
Result:=(not ComponentIsInvisible(AComponent));
if Result and (AComponent is TControl) then begin
if [csDesignFixedBounds,csNoDesignVisible]*TControl(AComponent).ControlStyle
<>[]
then
Result:=false;
end;
end;
{ TDesignerDeviceContext }
function TDesignerDeviceContext.GetDCOrigin: TPoint;
// returns the DC origin in screen coordinates
var
CurFormClientOrigin: TPoint;
CurFormOrigin: TPoint;
begin
if not (ddcDCOriginValid in FFlags) then
begin
CurFormClientOrigin := FormClientOrigin;
CurFormOrigin := FormOrigin;
FDCOrigin.X := CurFormOrigin.X - CurFormClientOrigin.X;
FDCOrigin.Y := CurFormOrigin.Y - CurFormClientOrigin.Y;
Include(FFlags, ddcDCOriginValid);
end;
Result:=FDCOrigin;
end;
function TDesignerDeviceContext.GetDCSize: TPoint;
// returns the DC size
begin
if not (ddcSizeValid in FFlags) then
begin
GetDeviceSize(DC, FDCSize);
Include(FFlags, ddcSizeValid);
end;
Result := FDCSize;
end;
function TDesignerDeviceContext.GetFormClientOrigin: TPoint;
// returns the Form Client Origin on desktop
begin
if not (ddcFormClientOriginValid in FFlags) then
begin
FFormClientOrigin := FForm.ClientOrigin;
Include(FFlags, ddcFormClientOriginValid);
end;
Result := FFormClientOrigin;
end;
function TDesignerDeviceContext.GetFormOrigin: TPoint;
// returns the DC origin relative to the form client origin
// For example: The DC of the client area of the form itself will return 0,0
var
AControlOrigin: TPoint;
begin
if not (ddcFormOriginValid in FFlags) then
begin
if not GetDCOriginRelativeToWindow(DC, FForm.Handle, FFormOrigin) then
begin
// For some reason we cannot retrieve the DC origin. It can happen for example
// when DC is not a control DC but a double buffer DC. Lets use another trick
if FDCControl <> nil then
begin
if FDCControl.Parent <> nil then
AControlOrigin := FDCControl.Parent.ClientToScreen(FDCControl.BoundsRect.TopLeft)
else
AControlOrigin := FDCControl.ClientToScreen(Point(0, 0));
FFormOrigin := FForm.ClientToScreen(Point(0, 0));
FFormOrigin.X := AControlOrigin.X - FFormOrigin.X;
FFormOrigin.Y := AControlOrigin.Y - FFormOrigin.Y;
end
else
FFormOrigin := Point(0, 0);
if GetWindowOrgEx(DC, @AControlOrigin) <> 0 then
begin
Dec(FFormOrigin.X, AControlOrigin.X);
Dec(FFormOrigin.Y, AControlOrigin.Y);
end;
end;
Include(FFlags, ddcFormOriginValid);
// DebugLn(['New origin: ', FFormOrigin.X, ':', FFormOrigin.Y]);
end;
Result := FFormOrigin;
end;
constructor TDesignerDeviceContext.Create;
begin
inherited Create;
FCanvas:=TCanvas.Create;
end;
destructor TDesignerDeviceContext.Destroy;
begin
FCanvas.Free;
inherited Destroy;
end;
procedure TDesignerDeviceContext.SetDC(AForm: TCustomForm; ADCControl: TWinControl; ADC: HDC);
begin
Clear;
FDC := ADC;
FDCControl := ADCControl;
FForm := AForm;
end;
procedure TDesignerDeviceContext.Clear;
begin
if (FSavedDC<>0) or (FPaintCount>0) then
RaiseGDBException('');
FDC := 0;
FFlags := FFlags - [ddcFormOriginValid, ddcFormClientOriginValid, ddcDCOriginValid, ddcSizeValid];
end;
procedure TDesignerDeviceContext.BeginPainting;
begin
if FSavedDC = 0 then
begin
FSavedDC := SaveDC(DC);
FCanvas.Handle := DC;
end;
inc(FPaintCount);
//DebugLn(['TDesignerDeviceContext.BeginPainting ',FPaintCount]);
end;
procedure TDesignerDeviceContext.EndPainting;
begin
//DebugLn(['TDesignerDeviceContext.EndPainting ',FPaintCount]);
dec(FPaintCount);
if (FPaintCount=0) and (FSavedDC <> 0) then
begin
FCanvas.Handle := 0;
RestoreDC(DC, FSavedDC);
FSavedDC := 0;
end;
end;
function TDesignerDeviceContext.RectVisible(ALeft, ATop, ARight,
ABottom: integer): boolean;
// coordinates must be relative to DC origin
var
ASize: TPoint;
begin
if (ARight < 0) or (ABottom < 0) then
Result := False
else begin
ASize := DCSize;
if (ALeft >= ASize.X) or (ATop >= ASize.Y) then
Result := False
else
Result := True;
end;
end;
initialization
OnGetDesignerForm:=nil;
end.
|