Recent

Author Topic: [CLOSED] Code cleanup in gtk widgetsets  (Read 2338 times)

lagprogramming

  • Sr. Member
  • ****
  • Posts: 407
[CLOSED] Code cleanup in gtk widgetsets
« on: June 30, 2023, 01:32:08 pm »
The gtk2 and gtk3 widgetsets contain some useless result assignments. I have doubts they increase code readability.
Check out the following routines.

Code: Pascal  [Select][+][-]
  1. function TGtk2WidgetSet.FillRgn(DC: HDC; RegionHnd: HRGN; hbr: HBRUSH): Bool;
  2. var
  3.   GtkDC: Integer;
  4.   OldRgn: PGdkRegion;
  5.   DevCtx: TGtkDeviceContext absolute DC;
  6.   ARect: TRect;
  7.   CRect : TGDKRectangle;
  8.   hasClipping: Boolean;
  9. begin
  10.  
  11.   Result := IsValidDC(DC) and IsValidGDIObject(hbr) and IsValidGDIObject(RegionHnd);
  12.   if not Result then Exit;
  13.   GtkDC := SaveDC(DC);
  14.   if (DevCtx.ClipRegion <> nil) and (DevCtx.ClipRegion^.GDIRegionObject <> nil) then
  15.     OldRgn := gdk_region_copy(DevCtx.ClipRegion^.GDIRegionObject)
  16.   else
  17.     OldRgn := nil;
  18.   hasClipping := Assigned(OldRgn);
  19.   try
  20.     if SelectClipRGN(DC, RegionHnd) <> ERROR then
  21.     begin
  22.       gdk_region_get_clipbox({%H-}PGDIObject(RegionHnd)^.GDIRegionObject, @CRect);
  23.       ARect := RectFromGdkRect(CRect);
  24.       DevCtx.FillRect(ARect, hbr, True);
  25.       // revert clip (whatever it is - null or valid region)
  26.       SelectClipRGN(DC, {%H-}HRGN(OldRgn));
  27.       Result := True;
  28.     end;
  29.   finally
  30.     if hasClipping then
  31.       gdk_region_destroy(OldRgn);
  32.     RestoreDC(DC, GtkDC);
  33.   end;
  34. end;

Code: Pascal  [Select][+][-]
  1. function TGtk3WidgetSet.SetForegroundWindow(hWnd: HWND): boolean;
  2. var
  3.   AWindow: TGtk3Window;
  4. begin
  5.   {$IFDEF GTK3DEBUGNOTIMPLEMENTED}
  6.   // DebugLn('WARNING: TGtk3WidgetSet.SetForegroundWindow not implemented ...');
  7.   {$ENDIF}
  8.   if not IsValidHandle(HWnd) then
  9.     exit(False);
  10.   Result := wtWindow in TGtk3Widget(HWND).WidgetType;
  11.   if Result then
  12.   begin
  13.     AWindow := TGtk3Window(HWND);
  14.     if not AWindow.Visible then
  15.       exit(False);
  16.     // DebugLn('TGtk3WidgetSet.SetForegroundWindow ',dbgsName(AWindow.LCLObject));
  17.     AWindow.Activate;
  18.     Result := True;
  19.   end;
  20. end;
  21.  

Code: Pascal  [Select][+][-]
  1. function TGtk3WidgetSet.ShowWindow(hWnd: HWND; nCmdShow: Integer): Boolean;
  2. begin
  3.   {$IFDEF GTK3DEBUGNOTIMPLEMENTED}
  4.   DebugLn('WARNING: TGtk3WidgetSet.ShowWindow not implemented ...');
  5.   {$ENDIF}
  6.   Result := IsValidHandle(Hwnd);
  7.   if not result then exit;
  8.   if TObject(hWnd) is TGtk3Window then
  9.     Result:=TGtk3Window(hWnd).ShowState(nCmdShow)
  10.   else
  11.   begin
  12.     TGtk3Widget(hWnd).Show;
  13.     Result:=true;
  14.   end;
  15. end;

Notice the Result := True; lines. Result is already true when reaching those lines.

In addition, function TGtk3WidgetSet.GetKeyState starts with the following code:
Code: Pascal  [Select][+][-]
  1. function TGtk3WidgetSet.GetKeyState(nVirtKey: Integer): Smallint;
  2. const
  3.   StateDown    = SmallInt($FF80);
  4. var
  5.   AKeyMap: PGdkKeymap;
  6.   AModifiers: TGdkModifierType;
  7. begin
  8.   Result := 0;
  9.  
  10.   Result := 0;
  11.  
  12.   case nVirtKey of
  13.     VK_LSHIFT:   nVirtKey := VK_SHIFT;
  14.     VK_LCONTROL: nVirtKey := VK_CONTROL;
  15.     VK_LMENU:    nVirtKey := VK_MENU;
  16.   end;
  17. ....
   
The following patch removes those three result:=true lines and also removes the duplicate Result:=0 line.
Code: Pascal  [Select][+][-]
  1. diff --git a/lcl/interfaces/gtk2/gtk2winapi.inc b/lcl/interfaces/gtk2/gtk2winapi.inc
  2. index 475afa4cfb..12d3426b61 100644
  3. --- a/lcl/interfaces/gtk2/gtk2winapi.inc
  4. +++ b/lcl/interfaces/gtk2/gtk2winapi.inc
  5. @@ -3998,7 +3998,6 @@ var
  6.    CRect : TGDKRectangle;
  7.    hasClipping: Boolean;
  8.  begin
  9. -
  10.    Result := IsValidDC(DC) and IsValidGDIObject(hbr) and IsValidGDIObject(RegionHnd);
  11.    if not Result then Exit;
  12.    GtkDC := SaveDC(DC);
  13. @@ -4015,7 +4014,6 @@ begin
  14.        DevCtx.FillRect(ARect, hbr, True);
  15.        // revert clip (whatever it is - null or valid region)
  16.        SelectClipRGN(DC, {%H-}HRGN(OldRgn));
  17. -      Result := True;
  18.      end;
  19.    finally
  20.      if hasClipping then
  21. diff --git a/lcl/interfaces/gtk3/gtk3winapi.inc b/lcl/interfaces/gtk3/gtk3winapi.inc
  22. index 54aa27f35e..ac00749898 100644
  23. --- a/lcl/interfaces/gtk3/gtk3winapi.inc
  24. +++ b/lcl/interfaces/gtk3/gtk3winapi.inc
  25. @@ -2197,8 +2197,6 @@ var
  26.  begin
  27.    Result := 0;
  28.  
  29. -  Result := 0;
  30. -
  31.    case nVirtKey of
  32.      VK_LSHIFT:   nVirtKey := VK_SHIFT;
  33.      VK_LCONTROL: nVirtKey := VK_CONTROL;
  34. @@ -3561,7 +3559,6 @@ begin
  35.        exit(False);
  36.      // DebugLn('TGtk3WidgetSet.SetForegroundWindow ',dbgsName(AWindow.LCLObject));
  37.      AWindow.Activate;
  38. -    Result := True;
  39.    end;
  40.  end;
  41.  
  42. @@ -3975,10 +3972,7 @@ begin
  43.    if TObject(hWnd) is TGtk3Window then
  44.      Result:=TGtk3Window(hWnd).ShowState(nCmdShow)
  45.    else
  46. -  begin
  47.      TGtk3Widget(hWnd).Show;
  48. -    Result:=true;
  49. -  end;
  50.  end;
  51.  
  52.  function TGtk3WidgetSet.StretchBlt(DestDC: HDC; X, Y, Width, Height: Integer;
« Last Edit: September 06, 2023, 11:48:46 am by lagprogramming »

AlexTP

  • Hero Member
  • *****
  • Posts: 2478
    • UVviewsoft

 

TinyPortal © 2005-2018