OpenWindow() in Procedure, what am I missing?

Just starting out? Need help? Post your questions and find answers here.
User avatar
doctorized
Addict
Addict
Posts: 856
Joined: Fri Mar 27, 2009 9:41 am
Location: Athens, Greece

OpenWindow() in Procedure, what am I missing?

Post by doctorized »

As InputRequester window is small and I can't show large texts, I tried to make my own one. The whole code is in a procedure to import it to other projects. When I run the code from its own pb file, it runs as expected and after returning the value entered in the TextGadget, the window is closing. When I call the procedure from another pb file, the window remains on the screen even if the value is returned. What am I missing? I know that OpenWindow() shouldn't be used in threads but I am using Windows, not Linux not MacOS as mentioned in the help file. The numbers used in the enumeration are used nowhere else.

Code: Select all

Enumeration
	#InputWindow = $FFF
	#Input_str
	#Input_txt
	#Input_btn
EndEnumeration
Procedure.s InputRequesterEx(txt$, width.i=, height.i, Window.i=0)
	If OpenWindow(#InputWindow, 0, 0, width, height, "MyWindow", #PB_Window_ScreenCentered|#PB_Window_SystemMenu,Window)
		TextGadget(#Input_txt, 10, 10, width-20, 30, txt$)
		StringGadget(#Input_str, 10, 40, width-20, 20, "")
		ButtonGadget(#Input_btn, 135, 70, 60, 20, "OK")
		SetActiveGadget(#Input_str)
		AddKeyboardShortcut(#InputWindow, #PB_Shortcut_Return, 15)
		Repeat
			Event = WaitWindowEvent()
			If Event = #PB_Event_CloseWindow
				Quit = 1
			ElseIf Event = #PB_Event_Menu
				If EventMenu() = 15
					If GetActiveGadget() = #Input_str
						Quit = 1
						ProcedureReturn GetGadgetText(#Input_str)
					EndIf
				EndIf
			ElseIf Event = #PB_Event_Gadget
				If EventGadget() = #Input_btn
					Quit = 1
					ProcedureReturn GetGadgetText(#Input_str)
				EndIf
			EndIf
		Until Quit = 1
	EndIf
EndProcedure

Debug InputRequesterEx("Some multiline" + #LF$ + "text here.", 330, 100)
User avatar
HeX0R
Addict
Addict
Posts: 992
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

Re: OpenWindow() in Procedure, what am I missing?

Post by HeX0R »

CloseWindow is missing
User avatar
doctorized
Addict
Addict
Posts: 856
Joined: Fri Mar 27, 2009 9:41 am
Location: Athens, Greece

Re: OpenWindow() in Procedure, what am I missing?

Post by doctorized »

HeX0R wrote:CloseWindow is missing
As procedure ends, shouldn't it close?
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4664
Joined: Sun Apr 12, 2009 6:27 am

Re: OpenWindow() in Procedure, what am I missing?

Post by RASHAD »

Typo error

Code: Select all

Procedure.s InputRequesterEx(txt$, width.i=, height.i, Window.i=0)
;Should be
Procedure.s InputRequesterEx(txt$, width.i, height.i, Window.i=0)
Egypt my love
User avatar
HeX0R
Addict
Addict
Posts: 992
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

Re: OpenWindow() in Procedure, what am I missing?

Post by HeX0R »

doctorized wrote:
HeX0R wrote:CloseWindow is missing
As procedure ends, shouldn't it close?
No!
It's Purebasic which closes all windows, when you end your program, that's the reason your snippet works as expected.
User avatar
skywalk
Addict
Addict
Posts: 4004
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: OpenWindow() in Procedure, what am I missing?

Post by skywalk »

doctorized wrote:
HeX0R wrote:CloseWindow is missing
As procedure ends, shouldn't it close?
No. Your code is a simple graphical example of a memory leak. Your code instantiates an object and then leaves without releasing it. Much harder to catch code that creates some array or nested structure and just leaves it open. Your example shows the object(window), but, what if you started with a hidden window or 2?
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
doctorized
Addict
Addict
Posts: 856
Joined: Fri Mar 27, 2009 9:41 am
Location: Athens, Greece

Re: OpenWindow() in Procedure, what am I missing?

Post by doctorized »

So, CloseWindow() is needed. If I add it before ProcedureReturn I will loose the Textgadget value. I will need a variable to hold the value or a different approach. OK. Thank you both for the information!
RASHAD wrote:Typo error
It is just a '=' forgotten when I posted the code here and deleted the default values for width and height.
Post Reply