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 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997
|
{ $Id: $}
{ --------------------------------------------
cocoascrollers.pas - Cocoa internal classes
--------------------------------------------
This unit contains the private classhierarchy for the Cocoa implemetations
*****************************************************************************
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 CocoaScrollers;
{$mode objfpc}{$H+}
{$modeswitch objectivec1}
{$modeswitch objectivec2}
{$interfaces corba}
{$include cocoadefines.inc}
interface
uses
// rtl+ftl
Types, Classes, SysUtils,
CGGeometry,
// Libs
MacOSAll, CocoaAll, CocoaUtils, CocoaGDIObjects,
cocoa_extra, CocoaPrivate;
type
{ TCocoaScrollView }
TCocoaScrollView = objcclass(NSScrollView)
public
callback: ICommonCallback;
isCustomRange: Boolean;
docrect : NSRect; // have to remember old
holdscroll : Integer; // do not send scroll messages
function initWithFrame(ns: NSRect): id; override;
procedure dealloc; override;
procedure setFrame(aframe: NSRect); override;
function acceptsFirstResponder: LCLObjCBoolean; override;
function lclGetCallback: ICommonCallback; override;
procedure lclClearCallback; override;
procedure resetCursorRects; override;
function lclClientFrame: TRect; override;
function lclContentView: NSView; override;
procedure setDocumentView(aView: NSView); override;
procedure scrollContentViewBoundsChanged(notify: NSNotification); message 'scrollContentViewBoundsChanged:';
procedure resetScrollRect; message 'resetScrollRect';
procedure lclUpdate; override;
procedure lclInvalidateRect(const r: TRect); override;
procedure lclInvalidate; override;
end;
{ TCocoaManualScrollView }
TCocoaManualScrollView = objcclass(NSView)
private
fdocumentView: NSView;
fhscroll : NSScroller;
fvscroll : NSScroller;
public
callback: ICommonCallback;
function lclGetCallback: ICommonCallback; override;
procedure lclClearCallback; override;
function lclContentView: NSView; override;
function lclClientFrame: TRect; override;
function lclIsMouseInAuxArea(event: NSEvent): Boolean; override;
procedure lclUpdate; override;
procedure lclInvalidateRect(const r: TRect); override;
procedure lclInvalidate; override;
procedure setDocumentView(AView: NSView); message 'setDocumentView:';
function documentView: NSView; message 'documentView';
procedure setHasVerticalScroller(doshow: Boolean); message 'setHasVerticalScroller:';
procedure setHasHorizontalScroller(doshow: Boolean); message 'setHasHorizontalScroller:';
function hasVerticalScroller: Boolean; message 'hasVerticalScroller';
function hasHorizontalScroller: Boolean; message 'hasHorizontalScroller';
function horizontalScroller: NSScroller; message 'horizontalScroller';
function verticalScroller: NSScroller; message 'verticalScroller';
function allocHorizontalScroller(avisible: Boolean): NSScroller; message 'allocHorizontalScroller:';
function allocVerticalScroller(avisible: Boolean): NSScroller; message 'allocVerticalScroller:';
// mouse
function acceptsFirstMouse(event: NSEvent): LCLObjCBoolean; override;
end;
{ TCocoaScrollBar }
TCocoaScrollBar = objcclass(NSScroller)
public
callback: ICommonCallback;
preventBlock : Boolean;
// minInt,maxInt are used to calculate position for lclPos and lclSetPos
minInt : Integer;
maxInt : Integer;
pageInt : Integer;
suppressLCLMouse: Boolean;
largeInc: Integer;
smallInc: Integer;
procedure actionScrolling(sender: NSObject); message 'actionScrolling:';
function IsHorizontal: Boolean; message 'IsHorizontal';
function acceptsFirstResponder: LCLObjCBoolean; override;
function lclGetCallback: ICommonCallback; override;
procedure lclClearCallback; override;
procedure resetCursorRects; override;
function lclPos: Integer; message 'lclPos';
procedure lclSetPos(aPos: integer); message 'lclSetPos:';
// mouse
function acceptsFirstMouse(event: NSEvent): LCLObjCBoolean; override;
procedure mouseDown(event: NSEvent); override;
procedure mouseUp(event: NSEvent); override;
procedure rightMouseDown(event: NSEvent); override;
procedure rightMouseUp(event: NSEvent); override;
procedure rightMouseDragged(event: NSEvent); override;
procedure otherMouseDown(event: NSEvent); override;
procedure otherMouseUp(event: NSEvent); override;
procedure otherMouseDragged(event: NSEvent); override;
procedure mouseDragged(event: NSEvent); override;
procedure mouseMoved(event: NSEvent); override;
procedure scrollWheel(event: NSEvent); override;
end;
{ TCocoaManualScrollHost }
TCocoaManualScrollHost = objcclass(TCocoaScrollView)
function lclContentView: NSView; override;
function lclClientFrame: TRect; override;
procedure scrollWheel(theEvent: NSEvent); override;
end;
function isMouseEventInScrollBar(host: TCocoaManualScrollView; event: NSEvent): Boolean;
// These settings are set by a user in "System Preferences"
// One can check the values by running the command line:
// $defaults read
// "AppleShowScrollBars": Automatic, Always, WhenScrolling
function SysPrefScrollShow: string;
// "AppleScrollerPagingBehavior": 0 - adjust by page, 1 - jump to the position
function SysPrefScrollClick: Integer;
function isIncDecPagePart(prt: NSScrollerPart): Boolean; inline;
procedure HandleMouseDown(sc: TCocoaScrollBar; locInWin: NSPoint; prt: NSScrollerPart);
function AdjustScrollerArrow(sc: TCocoaScrollBar; prt: NSScrollerPart): Boolean;
function AdjustScrollerPage(sc: TCocoaScrollBar; prt: NSScrollerPart): Boolean;
implementation
function SysPrefScrollShow: string;
begin
Result := NSStringToString(NSUserDefaults.standardUserDefaults.stringForKey(NSSTR('AppleShowScrollBars')));
end;
function SysPrefScrollClick: Integer; // 0 - adjust by page, 1 - jump to the position
begin
Result := Integer(NSUserDefaults.standardUserDefaults.integerForKey(NSSTR('AppleScrollerPagingBehavior')));
end;
function isIncDecPagePart(prt: NSScrollerPart): Boolean; inline;
begin
Result := (prt = NSScrollerDecrementPage)
or (prt = NSScrollerIncrementPage)
or (prt = NSScrollerDecrementLine)
or (prt = NSScrollerIncrementLine);
end;
function AdjustScrollerPage(sc: TCocoaScrollBar; prt: NSScrollerPart): Boolean;
var
adj : integer;
sz : Integer;
dlt : double;
v : double;
begin
Result := false;
case prt of
NSScrollerDecrementPage: begin
adj := -sc.largeInc;
if adj = 0 then adj := -sc.pageInt;
end;
NSScrollerIncrementPage: begin
adj := sc.largeInc;
if adj = 0 then adj := sc.pageInt;
end;
NSScrollerDecrementLine: begin
adj := -sc.smallInc;
if adj = 0 then adj := -1;
end;
NSScrollerIncrementLine: begin
adj := sc.smallInc;
if adj = 0 then adj := 1;
end;
else
adj := 0;
end;
if adj = 0 then Exit;
sz := sc.maxInt - sc.minInt - sc.pageInt;
if sz = 0 then Exit; // do nothing!
dlt := adj / sz;
v := sc.doubleValue;
v := v + dlt;
if v < 0 then v := 0
else if v > 1 then v := 1;
// todo: call scroll event?
sc.setDoubleValue(v);
{$ifdef BOOLFIX}
sc.setNeedsDisplay__(Ord(true));
{$else}
sc.setNeedsDisplay_(true);
{$endif}
end;
function AdjustScrollerArrow(sc: TCocoaScrollBar; prt: NSScrollerPart): Boolean;
var
adj : Integer;
sz : Integer;
dlt : double;
v : double;
begin
Result := (prt = NSScrollerDecrementLine)
or (prt = NSScrollerIncrementLine);
if not Result then Exit;
sz := sc.maxInt - sc.minInt - sc.pageInt;
if sz = 0 then Exit; // do nothing!
if prt = NSScrollerDecrementLine
then adj := -1
else adj := 1;
dlt := 1 / sz * adj;
v := sc.doubleValue;
v := v + dlt;
if v < 0 then v := 0
else if v > 1 then v := 1;
if v <> sc.doubleValue then
begin
// todo: call scroll event?
sc.setDoubleValue(v);
{$ifdef BOOLFIX}
sc.setNeedsDisplay__(Ord(true));
{$else}
sc.setNeedsDisplay_(true);
{$endif}
end;
end;
procedure HandleMouseDown(sc: TCocoaScrollBar; locInWin: NSPoint; prt: NSScrollerPart);
var
adj : Integer;
sz : Integer;
pt : NSPoint;
ps : double;
newPos: Integer;
begin
if (prt = NSScrollerDecrementLine) or (prt = NSScrollerIncrementLine) then
begin
AdjustScrollerArrow(sc, prt);
Exit;
end;
adj := SysPrefScrollClick;
if adj = 0 then begin
// single page jump
AdjustScrollerPage(sc, prt);
end
else
begin
// direct jump
pt := sc.convertPoint_fromView(locInWin, nil);
if sc.IsHorizontal then begin
if sc.frame.size.width = 0 then Exit; // do nothing
ps := pt.x / sc.frame.size.width;
end
else
begin
if sc.frame.size.height = 0 then Exit; // do nothing
ps := pt.y / sc.frame.size.height;
end;
sz := (sc.maxInt - sc.minInt - sc.pageInt);;
newPos := Round(sc.minInt + sz * ps);
sc.lclSetPos(NewPos);
end;
end;
{ TCocoaManualScrollHost }
function TCocoaManualScrollHost.lclContentView: NSView;
begin
if Assigned(documentView) then
Result := documentView.lclContentView
else
Result := inherited lclContentView;
end;
function TCocoaManualScrollHost.lclClientFrame: TRect;
begin
if Assigned(documentView) then
begin
Result:=documentView.lclClientFrame;
end
else Result:=inherited lclClientFrame;
end;
procedure TCocoaManualScrollHost.scrollWheel(theEvent: NSEvent);
var
nr : NSResponder;
begin
nr := nextResponder;
// do not call inherited scrollWheel, it suppresses the scroll event
if Assigned(nr) then nr.scrollWheel(theEvent)
else inherited scrollWheel(theEvent);
end;
{ TCocoaManualScrollView }
function TCocoaManualScrollView.lclGetCallback: ICommonCallback;
begin
Result := callback;
end;
procedure TCocoaManualScrollView.lclClearCallback;
begin
callback := nil;
end;
function TCocoaManualScrollView.lclContentView: NSView;
begin
Result:=fdocumentView;
end;
function TCocoaManualScrollView.lclClientFrame: TRect;
begin
if Assigned(fdocumentView) then
begin
Result:=fdocumentView.lclClientFrame;
end
else Result:=inherited lclClientFrame;
end;
function TCocoaManualScrollView.lclIsMouseInAuxArea(event: NSEvent): Boolean;
begin
Result := isMouseEventInScrollBar(Self, event);
end;
procedure TCocoaManualScrollView.lclUpdate;
begin
documentView.lclUpdate;
end;
procedure TCocoaManualScrollView.lclInvalidateRect(const r: TRect);
begin
documentView.lclInvalidateRect(r);
end;
procedure TCocoaManualScrollView.lclInvalidate;
begin
documentView.lclInvalidate;
end;
procedure TCocoaManualScrollView.setDocumentView(AView: NSView);
var
f : NSrect;
begin
if fdocumentView=AView then Exit;
if Assigned(fdocumentView) then
fdocumentView.removeFromSuperview;
fdocumentView:=AView;
if Assigned(fdocumentView) then
begin
addSubview(fdocumentView);
f:=fdocumentView.frame;
f.origin.x:=0;
f.origin.y:=0;
fdocumentView.setFrame(f);
fdocumentView.setAutoresizingMask(NSViewWidthSizable or NSViewHeightSizable);
end;
end;
function TCocoaManualScrollView.documentView: NSView;
begin
Result:=fdocumentView;
end;
procedure allocScroller(parent: TCocoaManualScrollView; var sc: NSScroller; dst: NSRect; aVisible: Boolean);
begin
sc:=TCocoaScrollBar(TCocoaScrollBar.alloc).initWithFrame(dst);
parent.addSubview(sc);
{$ifdef BOOLFIX}
sc.setEnabled_(Ord(true));
sc.setHidden_(Ord(not AVisible));
{$else}
sc.setEnabled(true);
sc.setHidden(not AVisible);
{$endif}
TCocoaScrollBar(sc).preventBlock := true;
//Suppress scrollers notifications.
TCocoaScrollBar(sc).callback := parent.callback;
TCocoaScrollBar(sc).suppressLCLMouse := true;
sc.setTarget(sc);
sc.setAction(objcselector('actionScrolling:'));
end;
procedure updateDocSize(parent: NSView; doc: NSView; hrz, vrt: NSScroller);
var
f : NSRect;
hr : NSRect;
vr : NSRect;
hw : CGFLoat;
vw : CGFLoat;
begin
if not Assigned(parent) or not Assigned(doc) then Exit;
f := parent.frame;
f.origin.x := 0;
f.origin.y := 0;
hr := f;
vr := f;
hw := NSScroller.scrollerWidth;
vw := NSScroller.scrollerWidth;
vr.size.width:=vw;
vr.origin.x:=f.size.width-vr.size.width;
hr.size.height:=hw;
if Assigned(hrz) and (not hrz.isHidden) then
begin
f.size.height := f.size.height - hw;
f.origin.y := hw;
vr.origin.y := hw;
vr.size.height := vr.size.height - hw;
if Assigned(vrt) and (not vrt.isHidden) then
hr.size.width:=hr.size.width-vw;
hrz.setFrame(hr);
end;
if Assigned(vrt) and (not vrt.isHidden) then
begin
f.size.width := f.size.width-vw;
vrt.setFrame(vr);
end;
if not NSEqualRects(doc.frame, f) then
begin
doc.setFrame(f);
{$ifdef BOOLFIX}
doc.setNeedsDisplay__(Ord(true));
{$else}
doc.setNeedsDisplay_(true);
{$endif}
end;
end;
procedure TCocoaManualScrollView.setHasVerticalScroller(doshow: Boolean);
var
ch : Boolean;
begin
ch := false;
if doshow then
begin
if not Assigned(fvscroll) then
begin
fvscroll := allocVerticalScroller(true);
ch := true;
end;
if fvscroll.isHidden then
begin
{$ifdef BOOLFIX}
fvscroll.setHidden_(Ord(false));
{$else}
fvscroll.setHidden(false);
{$endif}
ch := true;
end;
end
else if Assigned(fvscroll) and not fvscroll.isHidden then
begin
{$ifdef BOOLFIX}
fvscroll.setHidden_(Ord(true));
{$else}
fvscroll.setHidden(true);
{$endif}
ch := true;
end;
if ch then
updateDocSize(self, fdocumentView, fhscroll, fvscroll);
end;
procedure TCocoaManualScrollView.setHasHorizontalScroller(doshow: Boolean);
var
r : NSRect;
f : NSRect;
ch : Boolean;
begin
f:=frame;
ch:=false;
if doshow then
begin
if not Assigned(fhscroll) then
begin
fhscroll := allocHorizontalScroller(true);
ch := true;
end;
if fhscroll.isHidden then
begin
{$ifdef BOOLFIX}
fhscroll.setHidden_(Ord(false));
{$else}
fhscroll.setHidden(false);
{$endif}
ch := true;
end;
end
else if Assigned(fhscroll) and (not fhscroll.isHidden) then
begin
{$ifdef BOOLFIX}
fhscroll.setHidden_(Ord(true));
{$else}
fhscroll.setHidden(true);
{$endif}
ch := true;
end;
if ch then
updateDocSize(self, fdocumentView, fhscroll, fvscroll);
end;
function TCocoaManualScrollView.hasVerticalScroller: Boolean;
begin
Result:=Assigned(fvscroll) and (not fvscroll.isHidden);
end;
function TCocoaManualScrollView.hasHorizontalScroller: Boolean;
begin
Result:=Assigned(fhscroll) and (not fhscroll.isHidden);
end;
function TCocoaManualScrollView.horizontalScroller: NSScroller;
begin
Result:=fhscroll;
end;
function TCocoaManualScrollView.verticalScroller: NSScroller;
begin
Result:=fvscroll;
end;
function TCocoaManualScrollView.allocHorizontalScroller(avisible: Boolean): NSScroller;
var
r : NSRect;
f : NSRect;
w : CGFloat;
begin
if Assigned(fhscroll) then
Result := fhscroll
else
begin
f := frame;
w := NSScroller.scrollerWidth;
r := NSMakeRect(0, 0, f.size.width, NSScroller.scrollerWidth);
allocScroller( self, fhscroll, r, avisible);
fhscroll.setAutoresizingMask(NSViewWidthSizable);
Result := fhscroll;
end;
end;
function TCocoaManualScrollView.allocVerticalScroller(avisible: Boolean): NSScroller;
var
r : NSRect;
f : NSRect;
w : CGFloat;
begin
if Assigned(fvscroll) then
Result := fvscroll
else
begin
f := frame;
w := NSScroller.scrollerWidth;
r := NSMakeRect(f.size.width-w, 0, w, f.size.height);
allocScroller( self, fvscroll, r, avisible);
fvscroll.setAutoresizingMask(NSViewHeightSizable or NSViewMinXMargin);
Result := fvscroll;
end;
end;
function TCocoaManualScrollView.acceptsFirstMouse(event: NSEvent): LCLObjCBoolean;
begin
Result:=true;
end;
function isMouseEventInScrollBar(host: TCocoaManualScrollView; event: NSEvent): Boolean;
var
pt : NSPoint;
begin
Result := false;
if Assigned(host.fhscroll) and (not host.fhscroll.isHidden) then
begin
pt := host.fhscroll.convertPoint_fromView(event.locationInWindow, nil);
if NSPointInRect(pt, host.fhscroll.bounds) then
begin
Result := true;
Exit;
end;
end;
if Assigned(host.fvscroll) and (not host.fvscroll.isHidden) then
begin
pt := host.fvscroll.convertPoint_fromView(event.locationInWindow, nil);
if NSPointInRect(pt, host.fvscroll.bounds) then
begin
Result := true;
Exit;
end;
end;
end;
{ TCocoaScrollView }
function TCocoaScrollView.lclClientFrame: TRect;
begin
NSToLCLRect(contentView.frame, frame.size.height, Result);
end;
function TCocoaScrollView.lclContentView: NSView;
begin
Result:=documentView;
end;
procedure TCocoaScrollView.setDocumentView(aView: NSView);
begin
inherited setDocumentView(aView);
resetScrollRect;
end;
procedure TCocoaScrollView.scrollContentViewBoundsChanged(notify: NSNotification
);
var
nw : NSRect;
dx,dy : CGFloat;
begin
if not assigned(documentView) then Exit;
nw:=documentVisibleRect;
dx:=nw.origin.x-docrect.origin.x;
dy:=docrect.origin.y-nw.origin.y; // cocoa flipped coordinates
docrect:=nw;
if (dx=0) and (dy=0) then Exit;
if holdscroll>0 then Exit;
inc(holdscroll);
try
// update scrollers (this is required, if scrollWheel was called)
// so processing LM_xSCROLL will not cause any actually scrolling,
// as the current position will match!
self.reflectScrolledClipView(contentView);
if (dx<>0) and assigned(callback) then
callback.scroll(false, round(nw.origin.x));
if (dy<>0) and assigned(callback) then
callback.scroll(true, round(self.documentView.frame.size.height - self.documentVisibleRect.origin.y - self.documentVisibleRect.size.height));
finally
dec(holdscroll);
end;
end;
procedure TCocoaScrollView.resetScrollRect;
begin
docrect:=documentVisibleRect;
end;
procedure TCocoaScrollView.lclUpdate;
begin
documentView.lclUpdate;
end;
procedure TCocoaScrollView.lclInvalidateRect(const r: TRect);
begin
documentView.lclInvalidateRect(r);
end;
procedure TCocoaScrollView.lclInvalidate;
begin
documentView.lclInvalidate;
end;
function TCocoaScrollView.initWithFrame(ns: NSRect): id;
var
sc : TCocoaScrollView;
begin
Result:=inherited initWithFrame(ns);
sc:=TCocoaScrollView(Result);
//sc.contentView.setPostsBoundsChangedNotifications(true);
NSNotificationCenter.defaultCenter
.addObserver_selector_name_object(sc, ObjCSelector('scrollContentViewBoundsChanged:')
,NSViewBoundsDidChangeNotification
,sc.contentView);
end;
procedure TCocoaScrollView.dealloc;
begin
NSNotificationCenter.defaultCenter
.removeObserver(self);
inherited dealloc;
end;
procedure TCocoaScrollView.setFrame(aframe: NSRect);
var
flg : NSUInteger;
iflg : NSUInteger;
docsz : NSSize;
scrlsz : NSSize;
viewRect : NSRect;
dRect : NSRect;
hh : Single;
r : NSRect;
const
NSBothSizable = NSViewWidthSizable or NSViewHeightSizable;
begin
if not isCustomRange then begin
inherited setFrame(aframe);
Exit;
end;
viewRect := documentVisibleRect;
dRect := NSView(documentView).frame;
inherited setFrame(aframe);
flg := documentView.autoresizingMask;
if (flg and NSBothSizable) = NSBothSizable then Exit; // no need to checl
iflg := flg;
docsz := NSView(documentView).frame.size;
scrlsz := frame.size;
if (docsz.width<scrlsz.width) and (flg and NSViewWidthSizable = 0) then
flg := flg or NSViewWidthSizable;
if (docsz.height<scrlsz.height) and (flg and NSViewHeightSizable = 0) then
begin
// force automatic resize for isCustomRange
flg := flg or NSViewHeightSizable;
documentView.setFrameOrigin( NSMakePoint(0, aframe.size.height));
documentView.setFrameSize(aframe.size);
//documentView.setAutoresizingMask(flg);
end;
if iflg <> flg then
documentView.setAutoresizingMask(flg);
// the reason for this code here, is the need to re-allign the position
// if the control Size adjusted after ScrollInfo got changed.
if (documentVisibleRect.size.height < viewRect.size.height)
and (flg and NSViewHeightSizable = 0) then
begin
hh := dRect.size.height - viewRect.origin.y - viewRect.size.height;
r := documentVisibleRect;
r.origin.y := NSView(documentView).frame.size.height - r.size.height - hh;
NSView(documentView).scrollRectToVisible(r);
end;
end;
function TCocoaScrollView.acceptsFirstResponder: LCLObjCBoolean;
begin
Result := false;
end;
function TCocoaScrollView.lclGetCallback: ICommonCallback;
begin
Result := callback;
end;
procedure TCocoaScrollView.lclClearCallback;
begin
callback := nil;
end;
procedure TCocoaScrollView.resetCursorRects;
begin
if not Assigned(callback) or not callback.resetCursorRects then
inherited resetCursorRects;
end;
{ TCocoaScrollBar }
procedure TCocoaScrollBar.actionScrolling(sender: NSObject);
var
event : NSEvent;
prt : NSScrollerPart;
locInWin : NSPoint;
begin
event := NSApplication.sharedApplication.currentEvent;
if not Assigned(event) then Exit;
if not Assigned(event.window) then
begin
locInWin := event.mouseLocation;
if Assigned(window) then
locInWin := window.convertScreenToBase(locInWin);
end else
locInWin := event.locationInWindow;
prt := testPart(locInWin);
if isIncDecPagePart(prt) then
HandleMouseDown(self, locInWin, prt);
if Assigned(callback) then
callback.scroll(not IsHorizontal(), lclPos, prt);
end;
function TCocoaScrollBar.IsHorizontal: Boolean;
begin
Result := frame.size.width > frame.size.height;
end;
function TCocoaScrollBar.lclPos: Integer;
begin
Result:=round( floatValue * (maxint-minInt-pageInt)) + minInt;
end;
procedure TCocoaScrollBar.lclSetPos(aPos: integer);
var
d : integer;
begin
d := maxInt - minInt - pageInt;
if d <= 0 then
setDoubleValue(0)
else
begin
if aPos < minInt then aPos:=minInt
else if aPos > maxInt then aPos:=maxInt;
setDoubleValue( (aPos - minInt) / d );
end;
end;
function TCocoaScrollBar.acceptsFirstMouse(event: NSEvent): LCLObjCBoolean;
begin
Result:=true;
end;
procedure TCocoaScrollBar.mouseDown(event: NSEvent);
begin
if suppressLCLMouse then
begin
inherited mouseDown(event);
end
else
if not Assigned(callback) or not callback.MouseUpDownEvent(event, false, preventBlock) then
begin
inherited mouseDown(event);
if Assigned(callback) then
callback.MouseUpDownEvent(event, true, preventBlock);
end;
end;
procedure TCocoaScrollBar.mouseUp(event: NSEvent);
begin
if suppressLCLMouse then
begin
inherited mouseDown(event)
end
else
if not Assigned(callback) or not callback.MouseUpDownEvent(event, false, preventBlock) then
inherited mouseUp(event);
end;
procedure TCocoaScrollBar.rightMouseDown(event: NSEvent);
begin
if suppressLCLMouse then
inherited rightMouseDown(event)
else
if not Assigned(callback) or not callback.MouseUpDownEvent(event) then
inherited rightMouseDown(event);
end;
procedure TCocoaScrollBar.rightMouseUp(event: NSEvent);
begin
if suppressLCLMouse then
inherited rightMouseUp(event)
else
if not Assigned(callback) or not callback.MouseUpDownEvent(event) then
inherited rightMouseUp(event);
end;
procedure TCocoaScrollBar.rightMouseDragged(event: NSEvent);
begin
if suppressLCLMouse then
inherited rightMouseDragged(event)
else
if not Assigned(callback) or not callback.MouseUpDownEvent(event) then
inherited rightMouseDragged(event);
end;
procedure TCocoaScrollBar.otherMouseDown(event: NSEvent);
begin
if suppressLCLMouse then
inherited otherMouseDown(event)
else
if not Assigned(callback) or not callback.MouseUpDownEvent(event) then
inherited otherMouseDown(event);
end;
procedure TCocoaScrollBar.otherMouseUp(event: NSEvent);
begin
if suppressLCLMouse then
inherited otherMouseUp(event)
else
if not Assigned(callback) or not callback.MouseUpDownEvent(event) then
inherited otherMouseUp(event);
end;
procedure TCocoaScrollBar.otherMouseDragged(event: NSEvent);
begin
if suppressLCLMouse then
inherited otherMouseDragged(event)
else
if not Assigned(callback) or not callback.MouseMove(event) then
inherited otherMouseDragged(event);
end;
procedure TCocoaScrollBar.mouseDragged(event: NSEvent);
begin
if suppressLCLMouse then
inherited mouseDragged(event)
else
if not Assigned(callback) or not callback.MouseMove(event) then
inherited mouseDragged(event);
end;
procedure TCocoaScrollBar.mouseMoved(event: NSEvent);
begin
if suppressLCLMouse then
inherited mouseMoved(event)
else
if (not Assigned(callback) or not callback.MouseMove(event)) then
inherited mouseMoved(event)
end;
procedure TCocoaScrollBar.scrollWheel(event: NSEvent);
begin
if suppressLCLMouse then
inherited scrollWheel(event)
else
if not Assigned(callback) or not callback.scrollWheel(event) then
inherited scrollWheel(event);
end;
function TCocoaScrollBar.acceptsFirstResponder: LCLObjCBoolean;
begin
Result := True;
end;
function TCocoaScrollBar.lclGetCallback: ICommonCallback;
begin
Result := callback;
end;
procedure TCocoaScrollBar.lclClearCallback;
begin
callback := nil;
end;
procedure TCocoaScrollBar.resetCursorRects;
begin
if not Assigned(callback) or not callback.resetCursorRects then
inherited resetCursorRects;
end;
end.
|