Ignore:
Timestamp:
Jan 14, 2017, 10:33:42 PM (7 years ago)
Author:
chronos
Message:
  • Fixed: Implemented platform independent scrollbars usage. Tested on Linux.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/LocalPlayer/PVSB.pas

    r50 r69  
    88  Windows,
    99  {$ENDIF}
    10   Forms, LCLIntf, LCLType, LMessages, Messages, SysUtils;
     10  Classes, Controls, Forms, LCLIntf, LCLType, LMessages, Messages, SysUtils,
     11  StdCtrls, Math;
    1112
    1213type
     
    1415  { TPVScrollbar }
    1516
    16   TPVScrollbar = class
    17     h: integer;
     17  TPVScrollBar = class
     18  private
     19    FOnUpdate: TNotifyEvent;
     20    procedure ScrollBarChanged(Sender: TObject);
     21  public
     22    ScrollBar: TScrollBar;
    1823    si: TScrollInfo;
    19     Form: TForm;
    2024    destructor Destroy; override;
     25    procedure Setup(TopSpacing, RightSpacing, BottomSpacing: integer; Parent: TWinControl);
     26    procedure Init(max, Page: integer);
     27    procedure SetPos(Pos: Integer);
     28    function Process(const m: TMessage): boolean;
     29    function ProcessMouseWheel(Delta: Integer) : boolean;
     30    procedure Show(Visible: boolean);
     31    procedure EndSB;
     32    procedure UpdateScrollBar;
     33    property OnUpdate: TNotifyEvent read FOnUpdate write FOnUpdate;
    2134  end;
    2235
    23   procedure CreatePVSB(var sb: TPVScrollbar; Handle, y0, x1, y1: integer);
    24   procedure InitPVSB(var sb: TPVScrollbar; max, Page: integer);
    25   function ProcessPVSB(var sb: TPVScrollbar; const m: TMessage): boolean;
    26   function ProcessMouseWheel(var sb: TPVScrollbar; const m: TMessage)
    27     : boolean;
    28   procedure ShowPVSB(var sb: TPVScrollbar; Visible: boolean);
    29   procedure EndPVSB(var sb: TPVScrollbar);
    3036
    3137implementation
     
    3440  Count: integer = 0;
    3541
    36 procedure CreatePVSB(var sb: TPVScrollbar; Handle, y0, x1, y1: integer);
     42procedure TPVScrollBar.Setup(TopSpacing, RightSpacing, BottomSpacing: integer;
     43  Parent: TWinControl);
    3744begin
    3845  inc(Count);
    39   {$IFDEF LINUX}
    40   sb.Form := TForm.Create(nil);
    41   sb.Form.SetBounds(x1 - 100, y0, 100, y1 - y0);
    42   sb.Form.Name := 'PVSB' + IntToStr(Count);
    43   sb.h := sb.Form.Handle;
     46  //{$IFDEF LINUX}
     47//  sb.Form := TForm.Create(nil);
     48//  sb.Form.SetBounds(x1 - 100, y0, 100, y1 - y0);
     49//  sb.Form.Name := 'PVSB' + IntToStr(Count);
     50  ScrollBar := TScrollBar.Create(Parent);
     51  ScrollBar.Kind := sbVertical;
     52  ScrollBar.Name := 'PVSB' + IntToStr(Count);
     53  ScrollBar.Parent := Parent;
     54  ScrollBar.BorderSpacing.Top := TopSpacing;
     55  ScrollBar.BorderSpacing.Right := RightSpacing;
     56  ScrollBar.BorderSpacing.Bottom := BottomSpacing;
     57  ScrollBar.Align := alRight;
     58  ScrollBar.OnChange := ScrollBarChanged;
     59  //sb.h := sb.ScrollBar.Handle;
     60  (*
    4461  {$ENDIF}
    4562  {$IFDEF WINDOWS}
     
    4865    Handle, 0, 0, nil);
    4966  {$ENDIF}
    50   sb.si.cbSize := 28;
    51 end;
    52 
    53 procedure InitPVSB(var sb: TPVScrollbar; max, Page: integer);
    54 begin
    55   with sb.si do
    56   begin
     67  *)
     68  si.cbSize := 28;
     69end;
     70
     71procedure TPVScrollBar.Init(max, Page: integer);
     72begin
     73  with si do begin
    5774    nMin := 0;
    5875    nMax := max;
     
    6178    FMask := SIF_PAGE or SIF_POS or SIF_RANGE;
    6279  end;
    63   SetScrollInfo(sb.h, SB_CTL, sb.si, true);
    64   if max < Page then
    65     ShowWindow(sb.h, SW_HIDE)
    66   else
    67     ShowWindow(sb.h, SW_SHOW)
    68 end;
    69 
    70 function ProcessPVSB(var sb: TPVScrollbar; const m: TMessage): Boolean;
     80  UpdateScrollBar;
     81  //SetScrollInfo(sb.ScrollBar.Handle, SB_CTL, sb.si, true);
     82  if max < Page then ScrollBar.Visible := False
     83    else ScrollBar.Visible := True;
     84end;
     85
     86procedure TPVScrollBar.SetPos(Pos: Integer);
     87begin
     88  if Pos <> 0 then begin
     89    si.npos := Pos;
     90    si.FMask := SIF_POS;
     91    //SetScrollInfo(sb.ScrollBar.Handle, SB_CTL, sb.si, true);
     92    UpdateScrollBar;
     93  end;
     94end;
     95
     96function TPVScrollBar.Process(const m: TMessage): boolean;
    7197var
    7298  NewPos: integer;
    7399begin
    74   with sb.si do
     100  with si do
    75101    if nMax < integer(nPage) then
    76102      result := false
     
    105131          npos := NewPos;
    106132          FMask := SIF_POS;
    107           SetScrollInfo(sb.h, SB_CTL, sb.si, true);
     133          UpdateScrollBar;
     134          //SetScrollInfo(sb.ScrollBar.Handle, SB_CTL, sb.si, true);
    108135        end;
    109136      end
     
    111138end;
    112139
    113 function ProcessMouseWheel(var sb: TPVScrollbar; const m: TMessage): Boolean;
     140function TPVScrollBar.ProcessMouseWheel(Delta: Integer
     141  ): boolean;
    114142var
    115143  NewPos: integer;
    116144begin
    117   with sb.si do
     145  with si do
    118146    if nMax < integer(nPage) then
    119147      result := false
    120148    else
    121149    begin
    122       NewPos := npos - m.wParam div (1 shl 16 * 40);
     150      NewPos := npos - Delta div 300;
    123151      if NewPos < 0 then
    124152        NewPos := 0;
     
    130158        npos := NewPos;
    131159        FMask := SIF_POS;
    132         SetScrollInfo(sb.h, SB_CTL, sb.si, true);
     160        UpdateScrollBar;
     161        //SetScrollInfo(sb.ScrollBar.Handle, SB_CTL, sb.si, true);
    133162      end
    134163    end
    135164end;
    136165
    137 procedure ShowPVSB(var sb: TPVScrollbar; Visible: boolean);
    138 begin
    139   if not Visible or (sb.si.nMax < integer(sb.si.nPage)) then
    140     ShowWindow(sb.h, SW_HIDE)
    141   else
    142     ShowWindow(sb.h, SW_SHOW)
    143 end;
    144 
    145 procedure EndPVSB(var sb: TPVScrollbar);
    146 begin
    147   with sb.si do
    148   begin
     166procedure TPVScrollBar.Show(Visible: boolean);
     167begin
     168  if not Visible or (si.nMax < integer(si.nPage)) then
     169    ScrollBar.Visible := False
     170    else ScrollBar.Visible := True;
     171end;
     172
     173procedure TPVScrollBar.EndSB;
     174begin
     175  with si do begin
    149176    if nMax < integer(nPage) then
    150177      npos := 0 // hidden
    151     else
    152     begin
    153       sb.si.npos := nMax - integer(nPage) + 1;
    154       sb.si.FMask := SIF_POS;
    155       SetScrollInfo(sb.h, SB_CTL, sb.si, true);
     178    else begin
     179      si.npos := nMax - integer(nPage) + 1;
     180      si.FMask := SIF_POS;
     181      UpdateScrollBar;
     182      //SetScrollInfo(sb.ScrollBar.Handle, SB_CTL, sb.si, true);
    156183    end
    157184  end
    158185end;
    159186
     187procedure TPVScrollBar.UpdateScrollBar;
     188begin
     189  ScrollBar.Min := si.nMin;
     190  ScrollBar.Max := Max(si.nMax - si.nPage + 1, 0);
     191  ScrollBar.PageSize := si.nPage;
     192  ScrollBar.Position := si.nPos;
     193end;
     194
    160195{ TPVScrollbar }
    161196
    162 destructor TPVScrollbar.Destroy;
    163 begin
    164   h := 0;
     197procedure TPVScrollBar.ScrollBarChanged(Sender: TObject);
     198begin
     199  si.npos := ScrollBar.Position;
     200  if Assigned(FOnUpdate) then FOnUpdate(Self);
     201end;
     202
     203destructor TPVScrollBar.Destroy;
     204begin
     205  //h := 0;
    165206  si.cbSize := 0;
    166   FreeAndNil(Form);
     207  FreeAndNil(ScrollBar);
    167208end;
    168209
Note: See TracChangeset for help on using the changeset viewer.