Bit flag

Everything else that doesn't fall into one of the other PB categories.
AZJIO
Addict
Addict
Posts: 1355
Joined: Sun May 14, 2017 1:48 am

Bit flag

Post by AZJIO »

A small useful thing

Code: Select all

; AZJIO		18.02.2024
; https://www.purebasic.fr/english/viewtopic.php?p=614208#p614208
; http://forum.ru-board.com/topic.cgi?forum=5&topic=49364&start=2160#7

EnableExplicit

#Window = 0
Enumeration
	#StrField
	#Editor
EndEnumeration

Procedure SizeWindowHandler()
	Protected w, h
	w =  WindowWidth(#Window)
	h =  WindowHeight(#Window)
	ResizeGadget(#StrField, #PB_Ignore, #PB_Ignore, w - 20 , #PB_Ignore)
	ResizeGadget(#Editor, #PB_Ignore, #PB_Ignore, w - 20 , h - 60)
EndProcedure

Procedure InputHandler()
	Protected i, x.q
	Protected flg_bin.q, tmp$
	flg_bin = Val(GetGadgetText(#StrField))
	For i = 0 To 63
		If flg_bin >> i & 1
			x = 1 << i
			tmp$ + Str(x) + #CRLF$
		EndIf
	Next
	SetGadgetText(#Editor, tmp$)
EndProcedure


;- ╔═ GUI ═╗
If OpenWindow(#Window, 0, 0, 320, 360, "Bit flag", #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_SizeGadget | #PB_Window_MaximizeGadget | #PB_Window_MinimizeGadget)
	StringGadget(#StrField, 10 , 10 , 300 , 30 , "", #PB_String_Numeric)
	EditorGadget(#Editor , 10 , 50 , 300 , 300, #PB_Editor_WordWrap)
	BindGadgetEvent(0, @InputHandler(), #PB_EventType_Change)
	BindEvent(#PB_Event_SizeWindow, @SizeWindowHandler())

;- Loop
	Repeat
		Select WaitWindowEvent()
			Case #PB_Event_CloseWindow
				CloseWindow(0)
				End
		EndSelect
	ForEver
EndIf