SetGadgetShape for window resize?

Started by markcwm, November 28, 2017, 01:50:50

Previous topic - Next topic

markcwm

I'm hoping someone with more MaxGUI knowledge than me can figure this out.

Press keys 1,2,3 to change window and canvas size. This code works a bit, but it crashes after a while, also the canvas isn't resized properly and if I use RemoveHook and AddHook it crashes but I've used this in another example and it worked fine so I don't know.

Maybe there is already an example for this? Thanks!

Code (blitzmax) Select
' redrawgadget.bmx

' version 3 - fixed to be compatible with virtual resolutions

Import MaxGui.Drivers
'Import Bah.Gtkmaxgui
Import Brl.TimerDefault
Import Brl.EventQueue
Import Brl.FreetypeFont

Strict

Type TApplet

Method OnEvent(Event:TEvent,data:Object) Abstract

Method Run()
AddHook EmitEventHook,eventhook,Self
End Method

Function eventhook:Object(id,data:Object,context:Object)
Local event:TEvent = TEvent(data)
Local app:TApplet = TApplet(context)
app.OnEvent( event,data )
Return data
End Function

End Type

Type TSpinningApplet Extends TApplet

Global image:TImage
Global key:Int[256]

Field timer:TTimer
Field window:TGadget, canvas:TGadget
Field width%, height%, test%

Method Draw()

SetGraphics CanvasGraphics(canvas)
SetVirtualResolution width,height
SetViewport 0,0,width,height

SetBlend( ALPHABLEND )
'SetRotation( MilliSecs()*.1 )
SetClsColor( 255, 0, 0 )

Cls()
DrawText "width="+width+" height="+height,0,0
DrawImage( image, GraphicsWidth()/2, GraphicsHeight()/2 )
Flip()

End Method

Method OnEvent(event:TEvent,data:Object)
If Not event Then Return

Select event.id
Case EVENT_WINDOWCLOSE, EVENT_APPTERMINATE
End
Case EVENT_TIMERTICK
RedrawGadget( canvas )

Case EVENT_KEYUP
Select event.data
Case KEY_1 ; key[KEY_1]=0
Case KEY_2 ; key[KEY_2]=0
Case KEY_3 ; key[KEY_3]=0
End Select

Case EVENT_KEYDOWN
Select event.data
Case KEY_1 ; key[KEY_1]=1
Case KEY_2 ; key[KEY_2]=1
Case KEY_3 ; key[KEY_3]=1
End Select

Case EVENT_GADGETPAINT
If (event.source = canvas)

' testing SetGadgetShape
If key[KEY_1]=1
key[KEY_1]=0
width=512
height=512
test=1
EndIf

If key[KEY_2]=1
key[KEY_2]=0
width=800
height=600
test=1
EndIf

If key[KEY_3]=1
key[KEY_3]=0
width=DesktopWidth()
height=DesktopHeight()
test=1
EndIf

If test=1
test=0
'RemoveHook EmitEventHook, eventhook
DisablePolledInput()
ActivateGadget canvas
EnablePolledInput canvas
'AddHook EmitEventHook, eventhook

SetGadgetShape canvas, 0, 0, width, height
RedrawGadget canvas
SetGadgetShape window, 0, 0, width, height
RedrawGadget window
'SetViewport 0, 0, width, height
EndIf

Draw()
EndIf
End Select
End Method

Method Create:TSpinningApplet(name$)

If Not image Then image = LoadImage( "fltkwindow.png" )

width=512 ; height=512
window = CreateWindow( name, 20, 20, width, height )

Local w = ClientWidth(window)
Local h = ClientHeight(window)

canvas = CreateCanvas( 0, 0, w, h, window )
SetGadgetLayout( canvas, EDGE_ALIGNED, EDGE_ALIGNED, EDGE_ALIGNED, EDGE_ALIGNED )

ActivateGadget canvas
EnablePolledInput canvas

timer = CreateTimer( 100 )
Run()

Return Self

End Method

End Type

AutoMidHandle True

Local spinner:TSpinningApplet = New TSpinningApplet.Create("Spinning Applet")

Repeat
WaitSystem()
Forever

Henri

#1
Hi Mark,

does this fix the sizing issue (regular Bmax):

Code (blitzmax) Select


' version 3 - fixed to be compatible with virtual resolutions

Import MaxGui.Drivers
'Import Bah.Gtkmaxgui
'Import Brl.TimerDefault
'Import Brl.EventQueue
'Import Brl.FreetypeFont

Strict

Type TApplet

        Method OnEvent(Event:TEvent,data:Object) Abstract

        Method Run()
                AddHook EmitEventHook,eventhook,Self
        End Method

        Function eventhook:Object(id,data:Object,context:Object)
                Local event:TEvent = TEvent(data)
                Local app:TApplet = TApplet(context)
                app.OnEvent( event,data )
                Return data
        End Function

End Type

Type TSpinningApplet Extends TApplet
       
        Global image:TImage
        Global key:Int[256]
       
        Field timer:TTimer
        Field window:TGadget, canvas:TGadget
        Field width%, height%, test%
       
        Method Draw()
               
DebugLog "OnDraw"

                SetGraphics CanvasGraphics(canvas)
                SetVirtualResolution width,height
                SetViewport 0,0,width,height

                SetBlend( ALPHABLEND )
                'SetRotation( MilliSecs()*.1 )
                SetClsColor( 255, 0, 0 )
               
                Cls()
                DrawText "width="+width+" height="+height,0,0
                DrawImage( image, GraphicsWidth()/2, GraphicsHeight()/2 )
                Flip()
               
        End Method
       
        Method OnEvent(event:TEvent,data:Object)
                If Not event Then Return
               
                Select event.id
                        Case EVENT_WINDOWCLOSE, EVENT_APPTERMINATE
                                End
                        Case EVENT_TIMERTICK
                                RedrawGadget( canvas )
                               
                        Case EVENT_KEYUP
                        Select event.data
                                Case KEY_1 ; key[KEY_1]=0
                                Case KEY_2 ; key[KEY_2]=0
                                Case KEY_3 ; key[KEY_3]=0
                        End Select
                       
                        Case EVENT_KEYDOWN
                        Select event.data
                                Case KEY_1 ; key[KEY_1]=1
                                Case KEY_2 ; key[KEY_2]=1
                                Case KEY_3 ; key[KEY_3]=1
                        End Select
                       
                        Case EVENT_GADGETPAINT
                                If (event.source = canvas)
                               
                                        ' testing SetGadgetShape
                                        If key[KEY_1]=1
                                                key[KEY_1]=0
                                                width=512
                                                height=512
                                                test=1
                                        EndIf
                                       
                                        If key[KEY_2]=1
                                                key[KEY_2]=0
                                                width=800
                                                height=600
                                                test=1
                                        EndIf
                                       
                                        If key[KEY_3]=1
                                                key[KEY_3]=0
                                                width=DesktopWidth()
                                                height=DesktopHeight()
                                                test=1
                                        EndIf
                                       
                                        If test=1
                                                test=0
                                                'RemoveHook EmitEventHook, eventhook
                                                DisablePolledInput()
                                                ActivateGadget canvas
                                                EnablePolledInput canvas
                                                'AddHook EmitEventHook, eventhook
                                                                       
                                                'SetGadgetShape canvas, 0, 0, width, height
                                                'RedrawGadget canvas
                                                SetGadgetShape window, 0, 0, width, height

SetViewport 0,0,width,height
                                                'RedrawGadget window
                                                'SetViewport 0, 0, width, height       
                                        EndIf
                                       
                                        Draw()
                                EndIf
                End Select
        End Method
       
        Method Create:TSpinningApplet(name$)
               
                If Not image Then image = LoadImage( "fltkwindow.png" )
               
                width=512 ; height=512
                window = CreateWindow( name, 20, 20, width, height )
               
                Local w = ClientWidth(window)
                Local h = ClientHeight(window)
               
                canvas = CreateCanvas( 0, 0, w, h, window )
                SetGadgetLayout( canvas, EDGE_ALIGNED, EDGE_ALIGNED, EDGE_ALIGNED, EDGE_ALIGNED )
               
                ActivateGadget canvas
                EnablePolledInput canvas

                timer = CreateTimer( 100 )
                Run()
               
                Return Self
               
        End Method
       
End Type

AutoMidHandle True

Local spinner:TSpinningApplet = New TSpinningApplet.Create("Spinning Applet")

Repeat
        WaitSystem()
Forever


-Henri
- Got 01100011 problems, but the bit ain't 00000001

markcwm

Hi Henri,

thanks but no, it still crashes and the screen is not resized. I think this may just be an issue on Linux, I'll have to test on Win/Mac and post my findings.

markcwm

#3
Yep! Your code works fine on Mac/Win Henri, so it's a bug in GTK Maxgui, that code should work. I'm happy enough with this for now though.

I confused myself with RemoveHook, it was taken from code with 2 windows, so I think you can't remove then add the hook to the same canvas gadget again (at least in Linux).

Thanks again!

Henri

#4
Eventhook is not directly related to canvas so it can be removed and added at will.

Only thing wrong I see in your eventhook function is that the validity of app variable is not tested before accessing the OnEvent method.

Something like 'If app then app.OnEvent( event, data)'


Edit: Did you try 'Framework bah.gtkmaxgui' ? Without Framework, Bmax imports FLTK by default.

-Henri
- Got 01100011 problems, but the bit ain't 00000001

markcwm

Hi Henri,

no framework didn't work either, so must be a GTKMaxgui bug.

And yes, adding "if app then" stops RemoveHook from crashing. So thanks again!