Skip to content

Commit

Permalink
Eliminate deprecated OnMenuClick usage, move menu methods to TViewMain
Browse files Browse the repository at this point in the history
  • Loading branch information
michaliskambi committed Dec 30, 2024
1 parent b3287d4 commit 9331b2c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 21 deletions.
2 changes: 1 addition & 1 deletion castle_image_viewer.dpr
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ begin
{ We have to set Window.MainMenu before Window is open.
Although this menu only makes sense with ViewMain,
but we cannot change it from view's Start / Stop to nil/non-nil. }
Window.MainMenu := CreateMainMenu;
Window.MainMenu := ViewMain.CreateMainMenu;
Window.Open;

UserConfig.Load;
Expand Down
33 changes: 13 additions & 20 deletions code/gameviewmain.pas
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ TViewMain = class(TCastleView)

procedure SetZoom(var Zoom: Single; const NewZoom: Single);
procedure MultZoom(var Zoom: Single; const Multiplier: Single);
procedure MenuClick(const Item: TMenuItem);
public
{ Window that contains everything, including ViewMain.
Set by main program.
Expand All @@ -78,23 +79,22 @@ TViewMain = class(TCastleView)
var HandleInput: boolean); override;
function Motion(const Event: TInputMotion): Boolean; override;
function Press(const Event: TInputPressRelease): Boolean; override;

{ Create main menu, to be assigned to Window.MainMenu by the caller.
Also initializes Images.Menu. }
function CreateMainMenu: TMenu;
end;

var
ViewMain: TViewMain;

{ Create main menu, to be assigned to Window.MainMenu by the caller.
Also initializes Images.Menu. }
function CreateMainMenu: TMenu;

implementation

uses GameImageList;

{ declare menu routines ------------------------------------------------------ }

procedure DropFiles(Container: TCastleContainer; const FileNames: array of string); forward;
procedure MenuClick(Container: TCastleContainer; Item: TMenuItem); forward;

{ TViewMain ------------------------------------------------------------------ }

Expand Down Expand Up @@ -148,13 +148,13 @@ procedure TViewMain.Start;

CreateUserInterface;

Window.OnMenuClick := {$ifdef FPC}@{$endif} MenuClick;
Window.OnMenuItemClick := {$ifdef FPC}@{$endif} MenuClick;
Window.OnDropFiles := {$ifdef FPC}@{$endif} DropFiles;
end;

procedure TViewMain.Stop;
begin
Window.OnMenuClick := nil;
Window.OnMenuItemClick := nil;
Window.OnDropFiles := nil;
inherited;
end;
Expand Down Expand Up @@ -473,12 +473,7 @@ procedure DropFiles(Container: TCastleContainer; const FileNames: array of strin
end;
end;

procedure MenuClick(Container: TCastleContainer; Item: TMenuItem);

function Window: TCastleWindow;
begin
Result := Application.MainWindow;
end;
procedure TViewMain.MenuClick(const Item: TMenuItem);

procedure ImageSave;
var
Expand Down Expand Up @@ -707,14 +702,12 @@ function InterpolationToStr(const Interpolation: TResizeInterpolation): string;
Result := SEnding(GetEnumName(TypeInfo(TResizeInterpolation), Ord(Interpolation)), 3);
end;

function CreateMainMenu: TMenu;
function TViewMain.CreateMainMenu: TMenu;
var
M: TMenu;
NextRecentMenuItem: TMenuEntry;
Interpolation: TResizeInterpolation;
begin
Assert(ViewMain <> nil);

Result := TMenu.Create('Main menu');
M := TMenu.Create('_File');
M.Append(TMenuItem.Create('_Open ...', 110, CtrlO));
Expand All @@ -733,14 +726,14 @@ function CreateMainMenu: TMenu;
M.Append(TMenuItem.Create('Fit Image Width to Window Width', 220, 'W'));
M.Append(TMenuItem.Create('Fit Image Height to Window Height', 221, 'H'));
M.Append(TMenuSeparator.Create);
M.Append(TMenuItemChecked.Create('Test Is _Tileable', 230, 't', ViewMain.DrawTiled, true));
M.Append(TMenuItemChecked.Create('Test Is _Tileable', 230, 't', DrawTiled, true));
M.Append(TMenuSeparator.Create);
M.Append(TMenuItem.Create('Reset _Zoom and Translation', 240, keyHome));
M.Append(TMenuSeparator.Create);
M.Append(TMenuItemChecked.Create('Use Image Alpha Channel', 270, ViewMain.UseImageAlpha, true));
M.Append(TMenuItemChecked.Create('Use Image Alpha Channel', 270, UseImageAlpha, true));
M.Append(TMenuItem.Create('Background Color ...', 260));
M.Append(TMenuSeparator.Create);
M.Append(TMenuItemToggleFullScreen.Create(ViewMain.Window.FullScreen));
M.Append(TMenuItemToggleFullScreen.Create(Window.FullScreen));
M.Append(TMenuSeparator.Create);
M.Append(TMenuItem.Create('Open In External Application', 2010));
Result.Append(M);
Expand Down Expand Up @@ -780,7 +773,7 @@ function CreateMainMenu: TMenu;
M.Append(TMenuSeparator.Create);
Result.Append(M);
M := TMenu.Create('_Help');
M.Append(TMenuItemChecked.Create('Image Information', 510, keyF1, ViewMain.RectStatus.Exists, true));
M.Append(TMenuItemChecked.Create('Image Information', 510, keyF1, RectStatus.Exists, true));
M.Append(TMenuSeparator.Create);
M.Append(TMenuItem.Create('About castle-image-viewer', 520));
Result.Append(M);
Expand Down

0 comments on commit 9331b2c

Please sign in to comment.