Browse Source

win: added mouse events

mattias 9 months ago
parent
commit
71f6f238de
1 changed files with 80 additions and 11 deletions
  1. 80 11
      src/winapi/fresnel.winapi.pas

+ 80 - 11
src/winapi/fresnel.winapi.pas

@@ -16,6 +16,7 @@ uses
   System.Skia, Fresnel.SkiaRenderer,
   {$ENDIF}
   // fresnel
+  UTF8Utils,
   Fresnel.Classes, Fresnel.Forms, Fresnel.WidgetSet, Fresnel.DOM,
   Fresnel.Events, FCL.Events;
 
@@ -50,6 +51,8 @@ type
     function GetClientSize: TFresnelPoint; override;
     procedure Invalidate; override;
     procedure InvalidateRect(const aRect: TFresnelRect); override;
+    procedure HandleMouseMsg(XPos,YPos: longint; MouseEventId: TEventID;
+      Button: TMouseButton; Shiftstate: TShiftState); virtual;
     function HandlePaintMsg: LRESULT; virtual;
     procedure HandleSizeMsg; virtual;
   public
@@ -85,14 +88,27 @@ type
   end;
   PWinApiWindowInfo = ^TWinApiWindowInfo;
 
-
 var
   WinApiWidgetSet: TWinApiWidgetSet;
   WindowInfoAtom: ATOM;
   WindowInfoAtomStr: LPCSTR;
 
+function KeyboardStateToShiftState: TShiftState;
+procedure CreateDrawBuffer(const aMemDC: HDC; const aWidth, aHeight: Integer;
+  out aBuffer: HBITMAP; out aData: Pointer; out aStride: Integer);
+
 implementation
 
+function KeyboardStateToShiftState: TShiftState;
+begin
+  Result := [];
+  if GetKeyState(VK_SHIFT) < 0 then Include(Result, ssShift);
+  if GetKeyState(VK_CONTROL) < 0 then Include(Result, ssCtrl);
+  if GetKeyState(VK_MENU) < 0 then Include(Result, ssAlt);
+  if (GetKeyState(VK_LWIN) < 0) or
+     (GetKeyState(VK_RWIN) < 0) then Include(Result, ssMeta);
+end;
+
 procedure CreateDrawBuffer(const aMemDC: HDC; const aWidth, aHeight: Integer;
   out aBuffer: HBITMAP; out aData: Pointer; out aStride: Integer);
 const
@@ -153,9 +169,28 @@ end;
 
 function WindowProc(aHandle: HWND; aMsg: UINT; aWParam: WPARAM; aLParam: LPARAM): LRESULT; stdcall;
 var
-  XPos, YPos: LongInt;
-  aWndInfo: PWinApiWindowInfo;
   aForm: TWinApiWSForm;
+
+  function DoMouseMsg(MouseEvt: TEventID; Button: TMouseButton): LRESULT;
+  var
+    XPos, YPos: LongInt;
+    ShiftState: TShiftState;
+  begin
+    XPos := GET_X_LPARAM(aLParam);
+    YPos := GET_Y_LPARAM(aLParam);
+    ShiftState:=KeyboardStateToShiftState;
+    if MK_LBUTTON and aWParam>0 then
+      Include(ShiftState,ssLeft);
+    if MK_RBUTTON and aWParam>0 then
+      Include(ShiftState,ssRight);
+    if MK_MBUTTON and aWParam>0 then
+      Include(ShiftState,ssMiddle);
+    aForm.HandleMouseMsg(XPos,YPos,MouseEvt,Button,ShiftState);
+    Result:=0;
+  end;
+
+var
+  aWndInfo: PWinApiWindowInfo;
 begin
   aWndInfo:=GetWinApiWindowInfo(aHandle);
   if aWndInfo<>nil then
@@ -185,13 +220,22 @@ begin
         writeln('KeyUp: ',aWParam);
         exit(0);
       end;
-    WM_LBUTTONDOWN:
-      begin
-        XPos := GET_X_LPARAM(aLParam);
-        YPos := GET_Y_LPARAM(aLParam);
-        writeln('LButtonDown ',XPos,',',YPos);
-        exit(0);
-      end;
+
+    //WM_LBUTTONDBLCLK: exit(DoMouseMsg(1, True, True));
+    WM_LBUTTONDOWN:   exit(DoMouseMsg(evtMouseDown,mbLeft));
+    WM_LBUTTONUP:     exit(DoMouseMsg(evtMouseUp,mbLeft));
+    //WM_RBUTTONDBLCLK: exit(DoMouseMsg(2, True, True));
+    WM_RBUTTONDOWN:   exit(DoMouseMsg(evtMouseDown,mbRight));
+    WM_RBUTTONUP:     exit(DoMouseMsg(evtMouseUp,mbRight));
+    //WM_MBUTTONDBLCLK: exit(DoMouseMsg(3, True, True));
+    WM_MBUTTONDOWN:   exit(DoMouseMsg(evtMouseDown,mbMiddle));
+    WM_MBUTTONUP:     exit(DoMouseMsg(evtMouseUp,mbMiddle));
+    //WM_XBUTTONDBLCLK: exit(DoMouseMsg(4, True, True));
+    WM_XBUTTONDOWN:   exit(DoMouseMsg(evtMouseDown,mbFourth));
+    WM_XBUTTONUP:     exit(DoMouseMsg(evtMouseUp,mbFourth));
+
+    WM_MOUSEMOVE:  exit(DoMouseMsg(evtMouseMove,mbLeft));
+
     WM_CLOSE:
       begin
         PostQuitMessage(0);
@@ -388,6 +432,31 @@ begin
   Windows.InvalidateRect(FWindow,aRect.GetRect,true);
 end;
 
+procedure TWinApiWSForm.HandleMouseMsg(XPos, YPos: longint;
+  MouseEventId: TEventID; Button: TMouseButton; Shiftstate: TShiftState);
+var
+  WSData: TFresnelMouseEventInit;
+  p: TPoint;
+begin
+  WSData:=Default(TFresnelMouseEventInit);
+  WSData.Button:=Button;
+  WSData.PagePos.X:=XPos;
+  WSData.PagePos.Y:=YPos;
+  // Note: WSData.ControlPos is set by Form.WSMouseXY
+  Windows.GetCursorPos(p);
+  WSData.ScreenPos.SetLocation(p);
+  WSData.Shiftstate:=ShiftState;
+  if ssLeft in Shiftstate then
+    Include(WSData.Buttons,mbLeft);
+  if ssRight in Shiftstate then
+    Include(WSData.Buttons,mbRight);
+  if ssMiddle in Shiftstate then
+    Include(WSData.Buttons,mbMiddle);
+
+  //writeln('TWinApiWSForm.HandleMouseMsg ',XPos,',',YPos,' ',MouseEventId,' ',Button);
+  Form.WSMouseXY(WSData,MouseEventId);
+end;
+
 procedure TWinApiWSForm.HandleSizeMsg;
 var
   aFormRect, aClientRect: TRect;
@@ -514,7 +583,7 @@ begin
   if ATitle='' then ;
   //if FAppHandle <> 0 then
   //begin
-  //  ws:=ATitle;
+  //  ws:=UTF8ToUTF16(ATitle);
     // todo: TWinApiWidgetSet.AppSetTitle
     //Windows.SetWindowTextW(FAppHandle, PWideChar(ws));
   //end;