MUIClass weird resizing issue when creating an image

MarcusF · 1043

MarcusF

  • Junior Member
  • **
    • Posts: 54
    • Karma: +11/-0
Long title but bear with me.

I'm using FP-IDE on an emulated machine through Amiga Forever, workbench 3.x

I grabbed MUIClass and it's working but when I try the image example, I get some weird results.

It's a simple program

Code: [Select]
program ImageTest;
{$mode objfpc}{$H+}
uses
  MUIClass.Base, MUIClass.Window,  //needed for TMUIWindow
  MUIClass.Group,   //needed for TMUIGroup
  MUIClass.Area,    //needed for TMUIButton and TMUIText
  MUIClass.Image;   //needed for TMUIImage

type
  // My Window (mainly because we need the Event attached to an Object)
  TMyWindow = class(TMUIWindow)
    Text: TMUIText;     // Text field
    Button: TMUIButton; // Button
    Image: TMUIImage;   // Image
    Grp: TMUIGroup;
    procedure ButtonClick(Sender: TObject);

    constructor Create; override;
  end;

//var
  //Win: TMyWindow;     // Window

// Event called, when Button is pressed
procedure TMyWindow.ButtonClick(Sender: TObject);
begin
  // Delete the old image ;)
  Image.Free;
  //
  // create a new image with different picture
  // because the picture you only can change on creation
  Image := TMUIImage.Create;
  Image.FixWidth := 200;
  Image.FixHeight := 123;
  Image.FreeHoriz := True;
  Image.FreeVert := True;
  Image.Spec.SetPicture('test2.png');
  Image.Parent := Grp; // now the element will be created an linked to application
end;

constructor TMyWindow.Create;
var
  Grp2: TMUIGroup;
begin
  inherited Create;

  Title := 'Image Window';
  // a group for holding the image
  Grp := TMUIGroup.Create;
  Grp.Parent := Self;

  // Create a image object
  Image := TMUIImage.Create;
  Image.FixWidth := 174;   // image size, it should be read automatically, but often does not work, so we fix it here
  Image.FixHeight := 129;
  Image.FreeHoriz := True; // do not scale the image
  Image.FreeVert := True;
  Image.Spec.SetPicture('test.png');
  Image.Parent := Grp;

  // separate group or the recreation will change the position of the image
  Grp2 := TMUIGroup.Create;
  Grp2.Parent := Self;

  // Create a Button with some text
  Button := TMUIButton.Create;
  Button.Contents := 'Click Me';
  Button.OnClick := @ButtonClick;       // Connect the Click Event
  Button.Parent := Grp2;                // Insert Button in the Window
end;

begin
  // Create a Window, with a title bar text
  TMyWindow.Create;


  // will actually start everything,
  // Create MUI object, connect them together
  // from the Parent relation we builded with Pascal Classes
  // Open the Window and run the Message Loop
  // destroy everything after the Window is closed again.
  MUIApp.Run;
end.

Create a window, add a button, add an image.
When you click the button, destroy the first image, create a new one.

This works. The weirdness is that when clicking the button, the form resizes, and if I click it again the image is again destroyed and created again but the form resizes properly.
If I click it again, it goes back to the weirdly small size. Rinse and repeat.

I've tried a few things but nothing affects the weird resizing.



magorium

  • Moderator
  • Legendary Member
  • *****
    • Posts: 632
    • Karma: +62/-0
  • Convicted non contributor
Reply #1 on: July 20, 2023, 09:14:17 PM
Are those pictures the actual visual representation of what you see (as per your description) ?

The resizing in itself is correct, as that is how MUI works (if you remove a widget the parent will automatically resize, that is one of the things why it is so difficult to implement LCL as you constantly fight against MUI to have it leave things as is).

What is odd however is the content of your second picture (should be empty, on the first resize) and the fact that the resizing seem to happen on every other sequence. Perhaps things are done to quickly for MUI (e.g. needs a application.processmessages after the deletion and/or creation -> to keep it in Lazarus speak as I do not know the name of the MUI variant) and requires a manual refresh.

I'll have a look but since I'm using my new Linux box, I haven't setup emulation yet so might take some time.

edit: came across method TMUIWindow.InitChange and Exitchange. Seems to be /the/ reason to use those so perhaps you can try that in the meanwhile ?
« Last Edit: July 20, 2023, 10:04:35 PM by magorium »



magorium

  • Moderator
  • Legendary Member
  • *****
    • Posts: 632
    • Karma: +62/-0
  • Convicted non contributor
Reply #2 on: July 21, 2023, 12:23:10 AM
Yups, the init/exit change seem to have done the trick for me.

Code: (pascal) [Select]
procedure TMyWindow.ButtonClick(Sender: TObject);
begin
  InitChange;

  // Delete the old image ;)
  Image.Free;
  //
  // create a new image with different picture
  // because the picture you only can change on creation
  Image := TMUIImage.Create;
  Image.FixWidth := 200;
  Image.FixHeight := 123;
  Image.FreeHoriz := True;
  Image.FreeVert := True;
  Image.Spec.SetPicture('test2.png');
  Image.Parent := Grp; // now the element will be created an linked to application

  ExitChange;
end;


MarcusF

  • Junior Member
  • **
    • Posts: 54
    • Karma: +11/-0
Reply #3 on: July 21, 2023, 08:14:53 AM
You are a wonderful person. That did the trick! No more weird resizing  ;D



AMIGASYSTEM

  • Global Moderator
  • Legendary Member
  • *****
    • Posts: 3740
    • Karma: +69/-2
  • AROS One
    • AROS One
Reply #4 on: July 21, 2023, 05:42:05 PM
Marcus, I tried to compile your source with PortablE on AROS One x86 and I get an error !


MarcusF

  • Junior Member
  • **
    • Posts: 54
    • Karma: +11/-0
Reply #5 on: July 21, 2023, 05:54:52 PM
Marcus, I tried to compile your source with PortablE on AROS One x86 and I get an error !

I'm not surprised, it's FreePascal code ;)



magorium

  • Moderator
  • Legendary Member
  • *****
    • Posts: 632
    • Karma: +62/-0
  • Convicted non contributor
Reply #6 on: July 22, 2023, 07:28:40 AM
mod: I have split the topic so that the "unit not found issue" from user AMIGASYSTEM can now be found here instead as it was unrelated to the issue that TS posted.