wxWidget Beginner Questions

BlitzMax Forums/BlitzMax Programming/wxWidget Beginner Questions

I am just starting to work through the modules.
The below code opens 2 windows:
SuperStrict

Framework wx.wxApp
Import wx.wxFrame

Type MyApp Extends wxApp

        Field ParentFrame:wxFrame
        Field ChildFrame:wxFrame

        Method OnInit:Int()
                ' Method Create:wxFrame(parent:wxWindow = Null, id:Int = -1,
title:String = "", x:Int = -1, y:Int = -1, w:Int = -1, h:Int = -1,
style:Int = wxDEFAULT_FRAME_STYLE)
                ParentFrame = wxFrame.CreateFrame(,,"Parent Frame", 100, 100)
                ParentFrame.show()
                ChildFrame = wxFrame.createframe(ParentFrame,,"Child Frame",150,150)
                ChildFrame.show()

                Return True

        End Method

End Type

New MyApp.run()

' Closing the Child Frame has no effect on the Parent Frame
' Closing the Parent Frame CLOSES the Child Frame too


I now want to ask a question if the parent closes with the code below:
SuperStrict

Framework wx.wxApp
Import wx.wxFrame

Type MyApp Extends wxApp

'       Field ParentFrame:wxFrame
        Field ParentFrame:MyFrame
        Field ChildFrame:wxFrame

        Method OnInit:Int()
                ' Method Create:wxFrame(parent:wxWindow = Null, id:Int = -1,
title:String = "", x:Int = -1, y:Int = -1, w:Int = -1, h:Int = -1,
style:Int = wxDEFAULT_FRAME_STYLE)
'               ParentFrame = wxFrame.CreateFrame(,,"Parent Frame", 100, 100)
                ParentFrame = MyFrame.CreateFrame(,,"Parent Frame", 100, 100)
                ParentFrame.show()
                ChildFrame = wxFrame.createframe(ParentFrame,,"Child Frame",150,150)
                ChildFrame.show()

                Return True

        End Method

End Type

New MyApp.run()

' Closing the Parent Frame ASKS for confirmation before closing

Type MyFrame Extends wxFrame

        Function OnCloseWindow(_event:wxEvent)
                Local Result:Int = Confirm("Are you sure you want to Close BOTH
Windows?")
                If Result = False Then Exit
        End Function

End Type

but it complains about "unable to convert from wxFrame to MyFrame"

What am i doing wrong?

Cheers
Glenn

You'll need something like this :
ParentFrame = MyFrame(New MyFrame.Create(,,"Parent Frame", 100, 100))


"MyFrame.CreateFrame" will create an instance of wxFrame, not MyFrame.
You could override the CreateFrame() function, but using "New MyFrame.Create()" is less work on your part.

What that does, is create a new instance of MyFrame, and then calls the Create() Method - which CreateFrame() also happens to call under-the-hood.

But since Create() is defined as "Method Create:wxFrame()" you need to cast it to MyFrame.
Fun with OOP ;-)

Your next problem is going to be with OnCloseWindow....

OnCloseWindow().. :-)

To receive events for window close, you will need to "Connect" a function to the event - this way of coding lets you choose what you are interested in, rather than having every conceivable event available at a particular time. So, as part of your MyFrame type :

	Method OnInit()
		ConnectAny(wxEVT_CLOSE, OnCloseWindow)
	End Method


Now the important bit. Because you are overriding the Close event, you are now in control of the life of the Window. That means that it is up to you to Destroy the window if you *really* want it to close...

	Function OnCloseWindow(_event:wxEvent)
		Local Result:Int = Confirm("Are you sure you want to Close BOTH Windows?")
		If Result = True Then
			wxWindow(_event.parent).Destroy()
		End If
	End Function


And note that you will also have to add an "Import BRL.System" to get Confirm to work.
You might otherwise use a wxMessageDialog, which works in a similar way to Confirm.

HTH :-)

SuperStrict

Framework wx.wxApp
Import wx.wxFrame
Import BRL.System

Type MyApp Extends wxApp

	Field ParentFrame:MyFrame
	Field ChildFrame:wxFrame
	
	Method OnInit:Int()

		ParentFrame = MyFrame(New MyFrame.Create(,,"Parent Frame", 100, 100))
		ParentFrame.show()
		ChildFrame = wxFrame.createframe(ParentFrame,,"Child Frame",150,150)
		ChildFrame.show()
			
		Return True
	
	End Method

End Type


New MyApp.run()

' Closing the Parent Frame ASKS for confirmation before closing

Type MyFrame Extends wxFrame

	Method OnInit()
		ConnectAny(wxEVT_CLOSE, OnCloseWindow)
	End Method
	
	Function OnCloseWindow(_event:wxEvent)
		' Check if the ChildFrame is still open. If so ASK before closing the ParentFrame
		If wxWindow(ChildFrame) Then
			Local Result:Int = Confirm("Are you sure you want to Close BOTH Windows?")
			If Result = True Then
				wxWindow(_event.parent).Destroy()
			End If
		End If
		
	End Function

End Type


Added what you said and it works perfectly. Many thanks.
The next obvious question is how do I identify if the ChildFrame is still open? No need to ask the question if it is already closed...
I have checked the wxWindow help but i couldn't find anything like "IfChildStillAlive" and the above doesn't work either.

Apologies for the really simple questions but i tried going through the ListCtrl example to get back to basics but it was too much for me. I am building up a small library of step by step examples for myself.

BTW - I have 2 weeks of free time so i plan to get further into the wxWidget stuff.

i tried going through the ListCtrl example

You'll probably find the Minimal and Notebook easier to get your head around, since they don't do very much. The others get a bit more complicated much more quickly.

how do I identify if the ChildFrame is still open?

Perhaps you need to create a new Type for that too, which has a flag on it. Otherwise, you have a basic problem because of this - "Field ChildFrame:wxFrame" - which adds a reference count to the ChildFrame. So, even though when the frame is closed and internally you drop 1 reference, you still have your own reference of it, and therefore ChildFrame will never change to Null.
But that's a design problem ;-)

Maybe your parent window can keep a count of the number of child windows, and when the count is zero you don't need to ask any more...
There is a GetChildren() on wxWindow, but I don't think I've implemented it yet - plus that would return you a list of *all* children : frames, panels etc...

Cheers
Have a Merry Christmas.

Glenn